单测正确。
但测试数 ≥10 时,后面的测试点会直接输出。
用样例复制很多次来测试未见 RE。
#include<bits/stdc++.h>
using namespace std;
int sd[12][12];
int h[12],l[12],area[12];
bool flag;
int cnt[1<<12];
int num[1<<12];
inline int getarea(int x,int y) {
return (x-1)/3*3+(y-1)/3+1;
}
inline void flip(int x,int y,int z) {
h[x]^=1<<z;
l[y]^=1<<z;
area[getarea(x,y)]^=1<<z;
}
bool dfs(int tot) {
if(tot==0) {
return true;
}
int tmp=10,x,y;
for(int i=1; i<=9; i++) {
for(int j=1; j<=9; j++) {
if(sd[i][j])continue;
int tmp1=h[i]&l[j]&area[getarea(i,j)];
if(!tmp1)return false;
if(tmp>cnt[tmp1]) {
tmp=cnt[tmp1];
x=i,y=j;
}
}
}
int tmp1=h[x]&l[y]&area[getarea(x,y)];
for(; tmp1; tmp1-=tmp1&(-tmp1)) {
int tmp2=num[tmp1&(-tmp1)];
sd[x][y]=tmp2+1;
flip(x,y,tmp2);
if(dfs(tot-1))return true;
sd[x][y]=0;
flip(x,y,tmp2);
}
return false;
}
int main() {
int a;
cin>>a;
for(int __=1; __<=a; __++) {
memset(sd,0,sizeof(sd));
for(int i=0; i<(1<<9); ++i) {
for(int j=i; j; j-=j&(-j)) {
++cnt[i];
}
}
for(int i=0; i<9; ++i) {
num[1<<i]=i;
h[i+1]=l[i+1]=area[i+1]=(1<<9)-1;
}
int tot=81;
for(int i=1; i<=9; i++) {
for(int j=1; j<=9; j++) {
char t;
scanf(" %c",&t);
t-='0';
if(t!=0) {
flip(i,j,t-1);
tot--;
}
sd[i][j]=t;
}
}
dfs(tot);
for(int i=1; i<=9; i++) {
for(int j=1; j<=9; j++) {
cout<<sd[i][j];
}
cout<<'\n';
}
}
return 0;
}