【每日一题25】AcWing 703. 数独检查

Day25 AcWing 703. 数独检查

思路

  1. 模拟

拓展

  1. dancing links

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 40;

int n, m;
int w[N][N];
bool st[N];

bool check_row()
{
for (int i = 0; i < m; i ++ )
{
memset(st, 0, sizeof st);
for (int j = 0; j < m; j ++ )
{
int t = w[i][j];
if (t < 1 || t > m) return false;
if (st[t]) return false;
st[t] = true;
}
}
return true;
}

bool check_col()
{
for (int i = 0; i < m; i ++ )
{
memset(st, 0, sizeof st);
for (int j = 0; j < m; j ++ )
{
int t = w[j][i];
if (t < 1 || t > m) return false;
if (st[t]) return false;
st[t] = true;
}
}
return true;
}

bool check_cell()
{
for (int x = 0; x < m; x += n)
for (int y = 0; y < m; y += n)
{
memset(st, 0, sizeof st);
for (int dx = 0; dx < n; dx ++ )
for (int dy = 0; dy < n; dy ++ )
{
int t = w[x + dx][y + dy];
if (t < 1 || t > m) return false;
if (st[t]) return false;
st[t] = true;
}
}
return true;
}

int main()
{
int T;
scanf("%d", &T);
for (int C = 1; C <= T; C ++ )
{
scanf("%d", &n);
m = n * n;
for (int i = 0; i < m; i ++ )
for (int j = 0; j < m; j ++ )
scanf("%d", &w[i][j]);

if (check_row() && check_col() && check_cell())
printf("Case #%d: Yes\n", C);
else
printf("Case #%d: No\n", C);
}
return 0;
}