A


私信TA

用户名:uq_52713468251

访问量:153

签 名:

等  级
排  名 41007
经  验 325
参赛次数 0
文章发表 2
年  龄 20
在职情况 学生
学  校 hbjm
专  业 计科

  自我简介:

小白求带.....z

TA的其他文章

解题思路:
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 人评分

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

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

代码解释器

代码纠错

SQL生成与解释

  评论区