解题思路:
注意事项:
参考代码:
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++代码)浏览:1453 |
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:688 |
WU-输入输出格式练习 (C++代码)浏览:1083 |
2006年春浙江省计算机等级考试二级C 编程题(2) (C语言代码)浏览:473 |
WU-C语言程序设计教程(第三版)课后习题12.1 (C++代码)浏览:943 |
字符逆序 (C语言代码)浏览:460 |
C语言程序设计教程(第三版)课后习题5.6 (C语言代码)浏览:515 |
C二级辅导-等差数列 (C语言代码)浏览:837 |
小O的数字 (C语言代码)浏览:1418 |
A+B for Input-Output Practice (III) (C语言代码)浏览:427 |