过程很复杂,没有大佬代码清晰简洁,但是做出来很愉快
解题思路:
注意事项:
参考代码:
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语言代码)浏览:664 |
程序员的表白 (C语言代码)浏览:1462 |
C语言程序设计教程(第三版)课后习题7.2 (C语言代码)浏览:570 |
Cylinder (C语言描述,蓝桥杯)浏览:1279 |
Minesweeper (C语言描述,蓝桥杯)浏览:1176 |
C语言程序设计教程(第三版)课后习题3.7 (C语言代码)浏览:561 |
简单的a+b (C语言代码)浏览:618 |
C语言程序设计教程(第三版)课后习题10.2 (C语言代码)浏览:755 |
陈教主的三角形 (C语言代码)浏览:1196 |
前10名 (C语言代码)浏览:773 |