WA求调
查看原帖
WA求调
970168
Wight_sl楼主2024/5/30 16:08
#include<bits/stdc++.h>
#include<queue>
#define IOS ios::sync_with_stdio(false)
#define CT cin.tie(NULL)
#define Ct cout.tie(NULL) 
#define f first
#define s second
#define l_queue priority_queue <int,vector<int>,greater<int> >
#define p_queue priority_queue <int>
#define int int
#define N 10
inline void read(int& a){int s=0,w=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-') w=-1;ch=getchar();}while(ch>='0'&&ch<='9'){s=s*10+ch-'0';ch=getchar();}a=s*w;}
void write(int x){if(x<0) putchar('-'),x=-x;if(x>9) write(x/10);putchar(x%10+'0');}
using namespace std;
bool pd(int x,int y,int n,int m){if(x>=1&&x<=n&&y>=1&&y<=m/*&&!v[x][y]*/) return 1;else return 0;}
int a[N][N],b[N][N];
bool pd(int dep){
	int x=dep/9,y=dep%9;
	for(int i=0;i<9;i++){
		if(a[x][i]==a[x][y]&&i!=y){
			return 0;
		}
	}
	for(int i=0;i<9;i++){
		if(a[i][y]==a[x][y]&&i!=x){
			return 0;
		}
	}
	int xx=x/3*3,yy=y/3*3;
	for(int i=xx;i<xx+3;i++){
		for(int j=yy;j<yy+3;j++){
			if(a[i][j]==a[x][y]&&i!=x&&j!=y){
				return 0;
			}
		}
	}
	return 1;
}
void dfs(int dep){
	if(dep==81){
		for(int i=0;i<9;i++){
			for(int j=0;j<9;j++){
				b[i][j]=a[i][j];
			}
		}
		return ;
	}
	int x=dep/9,y=dep%9;
	if(a[x][y]==0){
		for(int i=1;i<=9;i++){
			a[x][y]=i;
			if(pd(dep)) dfs(dep+1);
		}
		a[x][y]=0;
	}
	else dfs(dep+1);
}
signed main(){
	IOS;CT;Ct;
	int t;
	read(t);
	while(t--){
		for(int i=0;i<9;i++){
			string s;
			cin>>s;
			for(int j=0;j<9;j++){
				a[i][j]=s[j]-'0';
			}
		}
		dfs(0);
		for(int i=0;i<9;i++){
			for(int j=0;j<9;j++){
				write(b[i][j]);
			}
			puts("");
		}
	}
    return 0;
}
2024/5/30 16:08
加载中...