#include<stdio.h> int map[11][11], dp[11][11];int main(){int n, i, j, num=0, res=0;scanf("%d", &n);for (i = 1; i <= n; i++)for (j = 1; j <= n; j++){map[i][j] = 0;dp[i][j] = 0;}while (map[i][j] || i || j){scanf("%d%d", &i, &j);scanf("%d", &map[i][j]);dp[i][j] = map[i][j];}for (int i = 2; i <= n; i++){dp[1][i] += dp[1][i - 1];dp[i][1] += dp[i - 1][1];}for (int i = 2; i <= n; i++)for (int j = 2; j <= n; j++)if (dp[i - 1][j] != 0 || dp[i][j - 1] != 0)if (dp[i - 1][j] > dp[i][j - 1])dp[i][j] += dp[i - 1][j];else dp[i][j] += dp[i][j - 1];num += dp[n][n];res = num;for (int i = 1; i <= n; i++){for (int j = 1; j <= n; j++){if (dp[i][j] == num){if (map[i][j] + dp[i - 1][j] == num || map[i][j] + dp[i][j - 1] == num){num -= map[i][j];map[i][j] = 0;i = 1;j = 0;if (num == 0)break;}}}if (num == 0)break;}for (int i = 2; i <= n; i++)for (int j = 2; j <= n; j++)if (map[i - 1][j] != 0 || map[i][j - 1] != 0)if (map[i - 1][j] > map[i][j - 1])map[i][j] += map[i - 1][j];else map[i][j] += map[i][j - 1];res += map[n][n];printf("%d", res);return 0;}格式化这段代码
时间: 2023-10-09 17:15:00 浏览: 93
#include<stdio.h>
int map[11][11], dp[11][11];
int main(){
int n, i, j, num=0, res=0;
scanf("%d", &n);
for (i = 1; i <= n; i++){
for (j = 1; j <= n; j++){
map[i][j] = 0;
dp[i][j] = 0;
}
}
while (map[i][j] || i || j){
scanf("%d%d", &i, &j);
scanf("%d", &map[i][j]);
dp[i][j] = map[i][j];
}
for (int i = 2; i <= n; i++){
dp[1][i] += dp[1][i - 1];
dp[i][1] += dp[i - 1][1];
}
for (int i = 2; i <= n; i++){
for (int j = 2; j <= n; j++){
if (dp[i - 1][j] != 0 || dp[i][j - 1] != 0){
if (dp[i - 1][j] > dp[i][j - 1]){
dp[i][j] += dp[i - 1][j];
}
else{
dp[i][j] += dp[i][j - 1];
}
}
}
}
num += dp[n][n];
res = num;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++){
if (dp[i][j] == num){
if (map[i][j] + dp[i - 1][j] == num || map[i][j] + dp[i][j - 1] == num){
num -= map[i][j];
map[i][j] = 0;
i = 1;
j = 0;
if (num == 0){
break;
}
}
}
}
if (num == 0){
break;
}
}
for (int i = 2; i <= n; i++){
for (int j = 2; j <= n; j++){
if (map[i - 1][j] != 0 || map[i][j - 1] != 0){
if (map[i - 1][j] > map[i][j - 1]){
map[i][j] += map[i - 1][j];
}
else{
map[i][j] += map[i][j - 1];
}
}
}
}
res += map[n][n];
printf("%d", res);
return 0;
}
阅读全文