解题思路:
注意事项:
参考代码:
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语言程序设计教程(第三版)课后习题9.6 (C语言代码)浏览:576 |
C语言程序设计教程(第三版)课后习题9.8 (C语言代码)浏览:1206 |
矩形面积交 (C语言代码)浏览:1514 |
不知道哪里错了浏览:1146 |
C二级辅导-统计字符 (C语言代码)浏览:506 |
C语言训练-求具有abcd=(ab+cd)2性质的四位数 (C语言代码)浏览:567 |
大神老白 (C语言代码)浏览:645 |
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:585 |
【金明的预算方案】 (C++代码)浏览:843 |
C语言程序设计教程(第三版)课后习题6.8 (C++代码)浏览:592 |