1 条题解

  • 2
    @ 2025-7-23 20:02:20
    #include<bits/stdc++.h>
    #define int long long
    using namespace std;
    const int N = 101;
    const int inf = 0x3f3f3f3f;
    int a[N][N];
    bool hqt[N][N];
    int sum;
    int n;
    int ans;
    struct abc{
    	int l;
    	int zh;
    	int fu;
    };
    abc black[N];
    abc white[N];
    bool check1(int x , int y){
    	return !black[y].l && !black[x + y].zh && !black[x - y + 10].fu && a[x][y] == 1;
    }
    bool check2(int x , int y){
    	return !white[y].l && !white[x + y].zh && !white[x - y + 10].fu && a[x][y] == 1;
    }
    void dfs(int h){//固定行 
    	//cout << h << " " << ans << endl;
    	if(h == n + 1){
    		ans ++;	
    		return;
    	}
    	for(int i = 1 ; i <= n ; i ++){//列 
    		for(int j = 1 ; j <= n ; j ++){
    			if(i != j && check1(h , i) && check2(h , j)){//符合条件 
    				black[i].l = black[h + i].zh = black[h - i + 10].fu = true;
    				white[j].l = white[h + j].zh = white[h - j + 10].fu = true;
    				dfs(h + 1);
    				black[i].l = black[h + i].zh = black[h - i + 10].fu = false;
    				white[j].l = white[h + j].zh = white[h - j + 10].fu = false;
    			}
    		}
    		
    	}
    
    }
    signed main(){
    	freopen("queen.in" , "r" , stdin);
    	freopen("queen.out" , "w" , stdout);
    	cin >> n;
    	for(int i = 1 ; i <= n ; i ++){
    		for(int j = 1 ; j <= n ; j ++){
    			cin >> a[i][j];
    		}
    	}
    	dfs(1);
    	cout << ans;
    	
    	return 0;
    }
    

    信息

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