1 条题解
-
0
#include<bits/stdc++.h> using namespace std; int a[25]; //存放编号 int ans=0; //多少种排列方法 bool b1[5][5]; //此行是否已经有1-4中的某个数字 bool b2[5][5]; //此列是否已经有1-4中的某个数字 bool b3[9][9]; //此四小块是否已经有1-4中的某个数字 int r[17]={0,1,1,1,1,//横行编号 2,2,2,2,//竖行编号 3,3,3,3,//小块编号 4,4,4,4}; int c[17]={0,1,2,3,4, 1,2,3,4, 1,2,3,4, 1,2,3,4}; int b[17]={0,1,1,2,2, 1,1,2,2, 3,3,4,4, 3,3,4,4}; void dfs(int x){//第x个空填什么,范围1-16 if(x>16){ //如果16个空全部填满 ans++; //增加结果数量 /*for(int i=1;i<=16;i++){ cout<<a[i]<<" "; if(i%4==0) cout<<endl; } cout<<endl;*/ return ; } int row=r[x];//横行编号 int col=c[x]; //竖行编号 int block=b[x]; //小块编号 //cout<<"\nblock2:"<<block2<<endl; for(int i=1;i<=4;i++) //如果此行此列此小块都没有填过i,则把i填上去 if(b1[row][i]==0 and b2[col][i]==0 and b3[block][i]==0){ a[x]=i; //记录放置的位置 b1[row][i]=true; b2[col][i]=true; b3[block][i]=true; dfs(x+1); //下一层递归 //只有x==17时下面代码才会被执行 b1[row][i]=false; //取消占位,重新排位 b2[col][i]=false; b3[block][i]=false; } } int main(){ //四阶数独 dfs(1); cout<<ans; return 0; }
- 1
信息
- ID
- 1184
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 9
- 标签
- (无)
- 递交数
- 8
- 已通过
- 6
- 上传者