- 数字游戏(sudoku)
- 2024-07-25 16:59:54 @
#include <bits/stdc++.h>
using namespace std;
int n,a[105][105],need;
map<int,bool>row[105],col[105];
void dfs(int dep,int x,int y){
if(a[x][y]){
if(y==n)y=0,x++;
dfs(dep,x,y+1);
return ;
}
for(int i=1;i<=n;i++){
if(!row[x][i]&&!col[y][i]){
a[x][y]=i;
row[x][i]=1;
col[y][i]=1;
int xx=x,yy=y;
if(y==n)y=0,x++;
dfs(dep+1,x,y+1);
x=xx,y=yy;
row[x][i]=0;
col[y][i]=0;
}
}
if(dep==need){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++)cout<<a[i][j]<<" ";
cout<<endl;
}
exit(0);
}
}
int main(){
ios::sync_with_stdio(false);
cin>>n;
int t=1;
for(int i=1;i<=n;i++)
{
row[i][t]=0;
col[i][t]=0;
t++;
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
cin>>a[i][j];
if(a[i][j]){
row[i][a[i][j]]=1;
col[j][a[i][j]]=1;
}else need++;
}
dfs(1,1,1);
return 0;
}
1 条评论
-
240803gj徐嘉昊 (2212224徐嘉昊) LV 10 @ 2024-07-25 17:38:31
初步推断是你的dfs函数参数中的x或y越界,建议你加一个判断或者把所有需要填数的点的坐标单独存一下,用tot表示数量,然后用dep进行遍历每一个需要填数的点。
- 1
信息
- ID
- 1521
- 难度
- 6
- 分类
- (无)
- 标签
- 递交数
- 41
- 已通过
- 13
- 通过率
- 32%
- 被复制
- 5
- 上传者