解题思路:
注意事项:
参考代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int m = scanner.nextInt();
int n = scanner.nextInt();
int g[][] = new int[n][m];
int dx[] = {0,0,-1,1};
int dy[] = {1,-1,0,0};
int ans = 0, res= 0, sum = 0;
for(int i = 0 ; i < n ; i ++){
for(int j = 0; j < m; j ++){
g[i][j] =scanner.nextInt();
sum += g[i][j];
}
}
sum /= 2;
int v[][] = new int[n][m];
int x = 0, y = 0;
while(true){
v[x][y] = 1;
res += g[x][y];
ans ++;
if(res == sum){
System.out.println(ans);
return;
}
int maxNum = 0, mx = x, my = y;
for(int i = 0 ; i < 4; i ++){
int tx = x + dx[i];
int ty = y + dy[i];
if(tx >= 0 && tx < n && ty >= 0 && ty < m && v[tx][ty] == 0 && res + g[tx][ty] > maxNum && res + g[tx][ty] <= sum){
maxNum = res + g[tx][ty];
mx = tx;
my = ty;
}
}
x = mx;
y = my;
}
}
}
0.0分
0 人评分
C语言程序设计教程(第三版)课后习题6.4 (C语言代码)浏览:1073 |
C语言程序设计教程(第三版)课后习题8.3 (C语言代码)浏览:624 |
C语言训练-求1+2!+3!+...+N!的和 (C语言代码)浏览:824 |
1113题解浏览:825 |
sizeof的大作用 (C语言代码)浏览:1139 |
剪刀石头布 (C++代码)浏览:1814 |
C语言程序设计教程(第三版)课后习题9.3 (C语言代码)浏览:650 |
1005答案错误为什么浏览:1989 |
C二级辅导-阶乘数列 (C语言代码)浏览:671 |
C语言程序设计教程(第三版)课后习题6.9 (C语言代码)简单版浏览:801 |