poj dfs之2676 Sudoku
注意啊啊啊啊啊啊啊啊 == 与 = 都写错了。。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<algorithm>
#include<queue>
#define MAXNUM 10020
using namespace std;
bool row[15][15], col[15][15], mark[15][15], flag;
int mp[15][15];
char tmp[20];
void dfs(int x, int y)
{
int i, j, k;
if (flag)
return;
if (x == 10)
{
flag = 1;
for (i = 1; i <= 9; i++)
{
for (j = 1; j <= 9; j++)
printf("%d", mp[i][j]);
printf("\n");
}
return;
}
if (mp[x][y])
{
if (y == 9)
dfs(x + 1, 1);
else
dfs(x, y + 1);
}
else
{
k = ((x - 1) / 3) * 3 + (y - 1) / 3 + 1;
for (i = 1; i <= 9; i++)
{
if (!row[x][i] && !col[y][i] && !mark[k][i])
{
row[x][i] = 1;
col[y][i] = 1;
mark[k][i] = 1;
mp[x][y] = i;
if (y == 9)
dfs(x + 1, 1);
else
dfs(x, y + 1);
row[x][i] = 0;
col[y][i] = 0;
mark[k][i] = 0;
mp[x][y] = 0;
}
}
}
}
int main()
{
freopen("1.txt", "r", stdin);
int i, j, k, t, a, b, cases;
scanf("%d", &cases);
while (cases > 0)
{
memset(mark, 0, sizeof(mark));
memset(col, 0, sizeof(col));
memset(row, 0, sizeof(row));
for (i = 1; i <= 9; i++)
{
scanf("%s", tmp);
for (j = 1; j <= 9; j++)
{
t = int(tmp[j - 1] - '0');
mp[i][j] = t;
if (t)
{
row[i][t] = 1;
col[j][t] = 1;
k = ((i - 1) / 3) * 3 + (j - 1) / 3 + 1;
mark[k][t] = 1;
}
}
}
flag = 0;
dfs(1, 1);
cases--;
}
return 0;
}