解题思路:
dfs
注意事项:
参考代码:import java.util.Scanner;
public class nKingM {
static int res = 0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] rec = new int[n];//记录数组
dfsK(rec,0);
System.out.println(res);
}
private static void dfsK(int[] rec, int row) {
if (row == rec.length) {
res++;
if(res<4){
for (int i :rec){
System.out.print(i+1+" ");
}
System.out.println();
}
return;
}
for (int i = 0; i < rec.length; i++) {//列
if(check(rec,i,row)){
rec[row] = i;
dfsK(rec,row+1);
rec[row] = 0;
}
}
}
private static boolean check( int[] rec, int col,int row) {
for (int i = 0; i < row; i++) {
if (rec[i] == col||i+rec[i] == row+col || rec[i]-i == col-row) return false;
}
return true;
}
}
0.0分
1 人评分
C语言程序设计教程(第三版)课后习题8.9 (C++代码)浏览:844 |
程序员的表白 (C语言代码)浏览:1319 |
妹子杀手的故事 (C语言代码)浏览:1218 |
字符串的输入输出处理 (C语言代码)浏览:925 |
C语言程序设计教程(第三版)课后习题7.1 (C语言代码)浏览:512 |
WU-printf基础练习2 (C++代码)浏览:2001 |
C语言程序设计教程(第三版)课后习题10.4 (C语言代码)浏览:879 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:667 |
1157题解浏览:711 |
Cylinder (C语言描述+详细分析)浏览:3263 |