过程很复杂,没有大佬代码清晰简洁,但是做出来很愉快
解题思路:
注意事项:
参考代码:
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 人评分
【绝对值排序】 (C语言代码)浏览:821 |
Minesweeper (C语言描述,蓝桥杯)浏览:1126 |
C语言程序设计教程(第三版)课后习题11.5 (C语言代码)浏览:1479 |
C语言程序设计教程(第三版)课后习题9.8 (C语言代码)浏览:527 |
1250题解浏览:561 |
P1002 (C++代码)浏览:717 |
C语言程序设计教程(第三版)课后习题6.10 (C语言代码)简单循环浏览:550 |
分解质因数 (C语言代码)浏览:846 |
剪格子 (C++代码)浏览:929 |
矩阵乘法 (C语言代码)浏览:1810 |