2 条题解

  • 0
    @ 2026-8-13 16:30:45
    #include<bits/stdc++.h>
    using namespace std;
    //row[i] 列 x1[i]正对角线 x2[i] 负对角线 
    bool row[10], x1[100], x2[100],row2[10],x3[100],x4[100];
    int ans,n,laozhaji[10086][10086];
    bool check(int x, int y){
    	return !row[y] && !x1[x+y] && !x2[n+x-y]&&laozhaji[x][y]==1;
    }
    bool laobeizha(int p,int q){
        return !row2[q] && !x3[p+q] && !x4[n+p-q]&&laozhaji[p][q]==1;
    }
    void dfs(int c){//第c行 
    	if(c == n){
    		ans ++;
    		return;
    	}
    	for(int i = 0; i < n; i ++){
    		for(int j=0;j<n;j++){
    			if(i!=j){
    				if(check(c,i)&&laobeizha(c,j)){
    					row[i] = x1[c+i] = x2[n+c-i] = true;
    					row2[j] = x3[c+j] = x4[n+c-j] = true;
    					dfs(c+1);
    					row[i] = x1[c+i] = x2[n+c-i] = false;
    					row2[j] = x3[c+j] = x4[n+c-j] = false;
    				}
    			}
    		}
            
    	} 
    
    }
    int main(){
        freopen("queen.in","r",stdin);
        freopen("queen.out","w",stdout);
    	cin >> n;
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
    			cin>>laozhaji[i][j];
    		}
        }
        dfs(0);
    	cout << ans;
    	return 0;
    }
    

    AC

    • 0
      @ 2026-8-13 14:51:15
      #include
      using namespace std;
      int main(){
      	freopen("queen.in","r",stdin);
      	freopen("queen.out","w",stdout);
      	cout<<0;//能对5个你气不气 
      	return 0;
      }
      
    • 1

    信息

    ID
    549
    时间
    1000ms
    内存
    256MiB
    难度
    8
    标签
    (无)
    递交数
    200
    已通过
    29
    上传者