import java.util.Scanner; public class Main { private static int m, n, ans = Integer.MAX_VALUE; private static int nums; private static int[][] dis, Grid; private static int[] dx = { 1, 0, -1, 0 }, dy = { 0, -1, 0, 1 }; public static void main(String[] args) { Scanner sc = new Scanner(System.in); m = sc.nextInt(); n = sc.nextInt(); Grid = new int[n][m]; nums = 0; dis = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { Grid[i][j] = sc.nextInt(); nums += Grid[i][j]; } } CutGrid(0, 0, 0, 0); System.out.println(ans); } public static void CutGrid(int x, int y, int cut, int steap) { if (cut == nums / 2) { ans = Math.min(ans, steap); return; } if (cut > nums / 2) { return; } dis[x][y] = -1; //标记已访问 cut += Grid[x][y]; //累加 for (int i = 0; i < dx.length; i++) { int tx = x + dx[i]; int ty = y + dy[i]; if (tx >= 0 && tx < n && ty >= 0 && ty < m && dis[tx][ty] == 0) { CutGrid(tx, ty, cut, steap + 1); dis[tx][ty] = 0; } } } }
解题思路:
注意事项:
参考代码:
0.0分
2 人评分
分糖果 (C++代码)浏览:1438 |
不会做的浏览:870 |
WU-字符串比较 (C++代码)浏览:754 |
C语言程序设计教程(第三版)课后习题8.8 (C语言代码)浏览:853 |
三角形 (C语言代码)浏览:903 |
C语言程序设计教程(第三版)课后习题5.4 (C语言代码)浏览:780 |
大家好,我是验题君浏览:576 |
1035 题解浏览:778 |
局部变量作函数返回值的问题浏览:963 |
C二级辅导-计负均正 (C语言代码)浏览:480 |