一块蒸蛋糕


私信TA

用户名:736015747

访问量:875

签 名:

等  级
排  名 7631
经  验 1244
参赛次数 0
文章发表 10
年  龄 0
在职情况 学生
学  校 北京信息科技大学
专  业 扫地

  自我简介:

TA的其他文章

过程很复杂,没有大佬代码清晰简洁,但是做出来很愉快

解题思路:

注意事项:

参考代码:

import java.util.Scanner;

public class Main {
	static int n;
	static int count;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		sc.nextLine();
		String[] m = new String[n];
		for (int i = 0; i < n; i++) {
			m[i] = sc.nextLine();
		}
		String[][] e = new String[n][n];
		String[][] ew = new String[n][n];
		for (int i = 0; i < n; i++) {
			e[i] = m[i].split("\\s");
			ew[i]=m[i].split("\\s");
		}
		dfswhite(e, ew, 0);
		System.out.println(count);
	}

	private static void dfswhite(String[][] e, String[][] ew, int row) {
		if (row == n) {
		dfsblack(e, ew, 0);
			return;
		}
		for (int col = 0; col < n; col++) {
			if (whitecheck(ew, row, col)) {
				ew[row][col] = "w";
				dfswhite(e, ew, row + 1);
				ew[row][col] = "1";
			}
		}
	}

	private static void dfsblack(String[][] e, String[][] ew, int row) {
		if (row == n) {
			count++;
			
			return;
		}
		for (int col = 0; col < n; col++) {
			if (blackcheck(e, ew, row, col)) {
				e[row][col] = "b";
				dfsblack(e, ew, row + 1);
				e[row][col] = "1";
			}

		}
	}

	private static boolean whitecheck(String[][] e, int row, int col) {
		if(e[row][col].equals("0"))
			return false;
		for (int i = 0; i < e.length; i++) {
			if (e[i][col].equals("w")) {
				return false;
			}
		}
		for (int i = 0; i < e.length; i++) {
			for (int j = 0; j < e.length; j++) {
				if (row + col == i + j && e[i][j].equals("w"))
					return false;
				if (row - col == i - j && e[i][j].equals("w"))
					return false;
			}
		}
		return true;
	}

	private static boolean blackcheck(String[][] e, String[][] ew, int row,
			int col) {
		if(e[row][col].equals("0"))
			return false;
		if (ew[row][col].equals("w")) {
					return false;
				}
		for (int i = 0; i < e.length; i++) {
			if (e[i][col].equals("b")) {
				return false;
			}
		}
		for (int i = 0; i < e.length; i++) {
			for (int j = 0; j < e.length; j++) {
				if (row + col == i + j && e[i][j].equals("b"))
					return false;
				if (row - col == i - j && e[i][j].equals("b"))
					return false;
				
			}
		}
		return true;
	}
}


 

0.0分

0 人评分

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

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区