PotDs


私信TA

用户名:PotDs

访问量:506

签 名:

等  级
排  名 13119
经  验 885
参赛次数 0
文章发表 3
年  龄 0
在职情况 学生
学  校 湖南科技职业学院
专  业 软件技术

  自我简介:

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 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区