-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2580_스도쿠.cpp
66 lines (60 loc) · 1.32 KB
/
2580_스도쿠.cpp
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
#include <iostream>
using namespace std;
bool row[9][9];
bool col[9][9];
bool square[9][9];
int arr[9][9];
void search(int cnt)
{
if (cnt == 81)
{
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
cout << arr[i][j]<<' ';
cout << endl;
}
exit(0);
}
int x = cnt / 9;
int y = cnt % 9;
if (arr[x][y] == 0)
{
for (int i = 1; i <= 9; i++)
{
if (!row[x][i] && !col[y][i] && !square[(x / 3) * 3 + (y / 3)][i])
{
row[x][i] = true;
col[y][i] = true;
arr[x][y] = i;
square[(x / 3) * 3 + (y / 3)][i] = true;
search(cnt + 1);
row[x][i] = false;
col[y][i] = false;
square[(x / 3) * 3 + (y / 3)][i] = false;
arr[x][y] = 0;
}
}
}
else
{
search(cnt + 1);
}
}
int main()
{
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
cin >> arr[i][j];
if (arr[i][j] != 0)
{
row[i][arr[i][j]] = true;
col[j][arr[i][j]] = true;
square[(i / 3) * 3 + (j / 3)][arr[i][j]] = true;
}
}
}
search(0);
}