## 4245 Su-Su-Sudoku WA

Do you want to discuss about these problems? Go now!
Users are shared (no need to re-register).

Moderator: Board moderators

Obaida
A great helper
Posts: 380
Joined: Wed Jan 16, 2008 6:51 am

### 4245 Su-Su-Sudoku WA

Some one please post some test case for me. I got wa in this problem. I need to figure the bug.

Code: Select all

``````#include<stdio.h>

char st[10][10];

int compare(int a_low,int b_low,int a,int b)
{
bool repeat = 0,check[10] = {0};
int i,j,num,count=0;
for(i=a_low;i<a;i++)
{
for(j=b_low;j<b;j++)
{
if(st[i][j]!='0')
{
if(!check[st[i][j]-48])
check[st[i][j]-48] = 1;
else {repeat = 1;break;}
}
}
if(repeat)break;
}
for(i=1;i<9;i++)
{
if(check[i]==0)
{
num = i;
count++;
}
}
if(count==1)
return num;
else return 0;
}

int check_box(int a,int b)
{
int a_low,b_low;
if(a<3&&b<3){a_low = 0;b_low = 0;a = 3;b = 3;return compare(a_low,b_low,a,b);}
else if(a<3&&b<6){a_low = 0;b_low = 3;a = 3;b = 6;return compare(a_low,b_low,a,b);}
else if(a<3&&b<9){a_low = 0;b_low = 6;a = 3;b = 9;return compare(a_low,b_low,a,b);}
else if(a<6&&b<3){a_low = 3;b_low = 0;a = 6;b = 3;return compare(a_low,b_low,a,b);}
else if(a<6&&b<6){a_low = 3;b_low = 3;a = 6;b = 6;return compare(a_low,b_low,a,b);}
else if(a<6&&b<9){a_low = 3;b_low = 6;a = 6;b = 9;return compare(a_low,b_low,a,b);}
else if(a<9&&b<3){a_low = 6;b_low = 0;a = 9;b = 3;return compare(a_low,b_low,a,b);}
else if(a<9&&b<6){a_low = 6;b_low = 3;a = 9;b = 6;return compare(a_low,b_low,a,b);}
else if(a<9&&b<9){a_low = 6;b_low = 6;a = 9;b = 9;return compare(a_low,b_low,a,b);}
}

int check_row(int a,int b)
{
bool check[10]={0},repeat=0;
int i,num,count=0;
for(i=0;i<9;i++)
{
if(st[a][i]!='0')
{
if(!check[st[a][i]-48])
check[st[a][i]-48] = 1;
else {repeat = 1;break;}
}
}
if(repeat)return 0;
for(i=1;i<9;i++)
{
if(check[i]==0&&i!=b)
{
num = i;
count++;
}
}
if(count==1)
return num;
else return 0;
}

int check_col(int a,int b)
{
bool check[10]={0},repeat = 0;
int i,num,count=0;
for(i=0;i<9;i++)
{
if(st[i][a]!='0')
{
if(!check[st[i][a]-48])
check[st[i][a]-48] = 1;
else {repeat = 1;break;}
}
}
if(repeat)return 0;
for(i=1;i<9;i++)
{
if(check[i]==0&&i!=a)
{
num = i;
count++;
}
}
if(count==1)
return num;
else return 0;
}

int main()
{
bool found;
int i,j,n,blank_x[6],blank_y[6],a,x,y,z,num;
scanf("%d",&n);
getchar();
while(n--)
{
a=0;
for(i=0;i<9;i++)
{
for(j=0;j<9;j++)
{
scanf("%c",&st[i][j]);
if(st[i][j]=='0')
{
blank_x[a] = i;
blank_y[a] = j;
a++;
}
}
getchar();
}
found = 1;
for(i=0;i<a;i++)
{
x = check_box(blank_x[i],blank_y[i]);
y = check_row(blank_x[i],blank_y[i]);
z = check_col(blank_y[i],blank_y[i]);
num = 0;
if(x)num = x;
else if(y)if((num&&num==y)||(!num&&y))num = y;
else if(z)if((num&&num==z)||(!num&&z))num = z;
if(!num)
{
puts("Could not complete this grid.");
found = 0;
break;
}
else
st[blank_x[i]][blank_y[i]] = num+48;
}
if(found)
{
for(i=0;i<9;i++)
{
for(j=0;j<9;j++)
{
printf("%c",st[i][j]);
}
puts("");
}
}
}
return 0;
}``````
try_try_try_try_&&&_try@try.com
This may be the address of success.