- C++
八皇后
- @ 2026-8-7 9:01:28
#include<bits/stdc++.h> using namespace std; #define ll long long #define str string int a[9][9],v1[105],v2[105],v3[105],cnt; void dfs(int x){ if(x>8){ cnt++; cout<<"No. "<<cnt<<endl; for(int i=1;i<=8;i++){ for(int j=1;j<=8;j++){ cout<<a[j][i]<<" "; } cout<<endl; } } for(int j=1;j<=8;j++){ if(v1[j]==0&&v2[x-j+8]==0&&v3[x+j]==0){ v1[j]=v2[x-j+8]=v3[x+j]=1; a[x][j]=1; dfs(x+1); v1[j]=v2[x-j+8]=v3[x+j]=0; a[x][j]=0; } } } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); dfs(1); return 0; }
0 条评论
目前还没有评论...