求大神指导,所有测试点都是RE
查看原帖
求大神指导,所有测试点都是RE
911582
chos1npm楼主2023/2/15 14:52
package Luogu;

import java.util.Scanner;

public class Queen {
    static int n;
    static int[] ans=new int [100];//改行在那一列(最终结果)
    static int count=0;
    static int[] cols=new int[100];//列
    static int[] a=new int [100];//正对角线
    static int[] b=new int [100];//逆对角线
   
    public static void main(String[] args) {
      Scanner input=new Scanner(System.in);
      n=input.nextInt();
      dfs(1);
      System.out.print(count);
    }
    public static void dfs(int row) {
    	if(row>n) {
    		count++;
    		if(count<=3) {
    			
    			for(int i=1;i<=n;i++) {
    				System.out.print(ans[i]+" ");
    				
    			}
    			System.out.println();
    		}
    		
    	}
    	else {
    		for(int j=1;j<=n;j++) {
    			if(cols[j]==0&&a[j+row]==0&&b[row-j+n]==0) {
    				cols[j]=1;
    				a[j+row]=1;
    				b[row-j+n]=1;
    				ans[row]=j;
    				dfs(row+1);
    				cols[j]=0;
    				a[j+row]=0;
    				b[row-j+n]=0;
    				
    			}
    		}
    		
    	}
    }

}

2023/2/15 14:52
加载中...