bzoj3109: [cqoi2013]新数独

www.lydsy.com/JudgeOnline/problem.php?id=3109

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char map[15][15];
char flag[9][9][4];
int a[9][9];
void dfs(int x, int y)
{
if (y == 9)
++x, y = 0;
if (x == 9)
{
int i, j;
for (i = 0; i < 9; ++i)
{
for (j = 0; j < 8; ++j)
printf("%d ", a[i][j]);
printf("%d\n", a[i][8]);
}
exit(0);
}
int i, j, k;
for (i = 1; i <= 9; ++i)
{
for (j = 0; j < y; ++j)
if (a[x][j] == i)
break;
if (j != y)
continue;
for (j = 0; j < x; ++j)
if (a[j][y] == i)
break;
if (j != x)
continue;
int tmp = x / 3 * 3 + y / 3;
int sx = tmp / 3 * 3, sy = tmp % 3 * 3;
for (j = 0; j < 3; ++j)
{
for (k = 0; k < 3; ++k)
if (a[sx + j][sy + k] == i)
break;
if (k != 3)
break;
}
if (j != 3)
continue;
if ((flag[x][y][0] == 'v' && i > a[x - 1][y]) || (flag[x][y][0] == '^' && i < a[x - 1][y]))
continue;
if ((flag[x][y][2] == '>' && i > a[x][y - 1]) || (flag[x][y][2] == '<' && i < a[x][y - 1]))
continue;
a[x][y] = i;
dfs(x, y + 1);
a[x][y] = 0;
}
}
int main()
{
memset(a, 0, sizeof(a));
int i;
for (i = 0; i < 15; ++i)
{
if (i % 5 == 0 || i % 5 == 2 || i % 5 == 4)
{
map[i][0] = ' ';
scanf(" %c", &map[i][1]);
scanf(" %c", &map[i][2]);
map[i][3] = ' ';
scanf(" %c", &map[i][4]);
scanf(" %c", &map[i][5]);
map[i][6] = ' ';
scanf(" %c", &map[i][7]);
scanf(" %c", &map[i][8]);
map[i][9] = ' ';
int j = i % 5;
if (j == 2)
j = 1;
else if (j == 4)
j = 2;
j += i / 5 * 3;
int k;
for (k = 0; k < 9; ++k)
{
flag[j][k][2] = map[i][k];
flag[j][k][3] = map[i][k + 1];
}
}
else
{
int j, k;
for (j = 0; j < 9; ++j)
scanf(" %c", &map[i][j]);
j = i % 5;
if (j == 1)
j = 0;
else
j = 1;
j += i / 5 * 3;
for (k = 0; k < 9; ++k)
{
flag[j][k][1] = map[i][k];
flag[j + 1][k][0] = map[i][k];
}
}
}
dfs(0, 0);
return 0;
}


• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120