————————————————————————————————————————————————————
分析:
暴力, 一个一个去试
————————————————————————————————————————————————————
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int flag[9][9];
int sudo[9][9];
bool judge(int x, int y)
{
int j = 0;
while (j < 9)
{
if ((sudo[x][y] == sudo[x][j]) && (j != y))
{
return false;//low ouhedu!
}
if ((sudo[x][y] == sudo[j][y]) && (j != x))
{
return false;
}
j++;
}
int p = x/3*3;//beauty.
int q = y/3*3;
for (int i=0; i<3; i++)
{
for (j=0; j<3; j++)
{
if ((sudo[x][y] == sudo[i+p][j+q]) && (x != i+p) && (y != j+q))
{
return false;
}
}
}
return true;
}
bool killer(int x, int y)
{
if (x == 9)
{
return true;
}
int X, Y;
if ((y+1<9))
{
X = x;
Y = y+1;
}
else
{
X = x+1;
Y = 0;
}
if (flag[x][y])
{
do
{
sudo[x][y]++;
while ((judge(x, y) == false) && (sudo[x][y] <= 9))
{
sudo[x][y]++;
}
if (sudo[x][y] > 9)
{
sudo[x][y] = 0;
return false;
}
}while(killer(X, Y) == false);
return true; //not only tell it how to walk when it is false, but also tell it when it is true. because there is no default value to return back;
}
else
{
return killer(X, Y);
}
}
int main()
{
int k = 0;
char s[10];
while (cin >> s)
{
memset(flag, 0, sizeof(flag));
if (s[0] == '?')
{
sudo[0][0] = 0;
flag[0][0] = 1;
}
else
{
sudo[0][0] = s[0] - '0';
}
for (int i=0; i<9; i++)
{
for (int j=0; j<9; j++)
{
if ((i == 0) && (j == 0))
{
continue;
}
cin >> s;
if (s[0] == '?')
{
sudo[i][j] = 0;
flag[i][j] = 1;
}
else
{
sudo[i][j] = s[0] - '0';
}
}
}
if (k)
{
cout << endl;
}
k++;
killer(0, 0);
for (int i=0; i<9; i++)
{
for (int j=0; j<9; j++)
{
cout << sudo[i][j];
if (j != 8)
{
cout << " ";
}
}
cout << endl;
}
}
return 0;
}
好久没做题了, 好多低级错误呀, ,,毕竟 是我的第一道递归题, 玩一会儿泪崩。。好感动
(嘛。。。return遇到函数淡然是去执行函数, 执行完后就直接返回函数的结果)
是的, 我又重新写了一遍。。AC代码:
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int flag[9][9];
int sudo[9][9];
int f = 0;
bool killer(int x, int y)
{
if (x == 9)
{
return true;
}
int X, Y;
if (y < 8)
{
Y = y+1;
X = x;
}
else
{
X = x+1;
Y = 0;
}
if (flag[x][y])
{
sudo[x][y] = 1;
while (1)
{
int j=0;
while(j < 9)
{
if (((sudo[x][y] == sudo[x][j]) && (j != y)) || ((sudo[x][y] == sudo[j][y]) && (j != x)))
{
f = 1;
break;
}
j++;
}
int u, v;
for (u=x/3*3; u<x/3*3+3; u++)
{
for( v=y/3*3; v<y/3*3+3; v++)
{
if ((sudo[x][y] == sudo[u][v]) && (u != x) && (v != y))
{
f = 1;
v = -1;
break;
}
}
if (v == -1)
{
break;
}
}
if (f || (killer(X, Y) == false))
{
sudo[x][y]++;
f = 0;
if (sudo[x][y] > 9)
{
sudo[x][y] = 0;
return false;
}
else
{
continue;
}
}
// getchar();
return true;
}
}
return killer(X, Y);
}
int main()
{
char s[10];
int k = 0;
while (cin >> s)
{
memset(flag, 0, sizeof(flag));
sudo[0][0] = (s[0] == '?'?0:(s[0]-'0'));
flag[0][0] = !(sudo[0][0]);
for (int i=0; i<9; i++)
{
for (int j=0; j<9; j++)
{
if ((i == 0) && (j == 0))
{
continue;
}
cin >> s;
sudo[i][j] = (s[0] == '?' ? 0 : (s[0]-'0'));
flag[i][j] = !(sudo[i][j]);
}
}
killer(0, 0);
if (k)
{
cout << endl;
}
k++;
for (int i=0; i<9; i++)
{
for (int j=0; j<9; j++)
{
cout << sudo[i][j];
cout << (j<8 ? " " : "\n");
}
}
}
return 0;
}
加油吧