-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path2580_스도쿠.cpp
More file actions
88 lines (75 loc) · 1.52 KB
/
Copy path2580_스도쿠.cpp
File metadata and controls
88 lines (75 loc) · 1.52 KB
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
82
83
84
85
86
87
88
#include <iostream>
using namespace std;
int sudoku_size = 9;
int sudoku[10][10];
bool chk_row[10][10];
bool chk_col[10][10];
bool chk_square[10][10];
// (x,y)가 몇 번째 정사각형(0~8)에 해당하는지 return.
int square(int x, int y)
{
return (x / 3) * 3 + (y / 3);
}
void print_Sudoku()
{
for (int i = 0; i < sudoku_size; i++)
{
for (int j = 0; j < sudoku_size; j++)
{
cout << sudoku[i][j] << " ";
}
cout << "\n";
}
}
void make_sudoku(int z)
{
if (z == 81)
{
print_Sudoku();
exit(0);
}
int x = z / sudoku_size, y = z % sudoku_size;
if (sudoku[x][y] != 0)
{
make_sudoku(z + 1);
}
// 0이 들어가있는 곳이면
else
{
// 들어갈 숫자 찾기
for (int i = 1; i <= 9; i++)
{
// 해당 열, 행, 정사각형 안에 i가 없으면 i를 사용한다
if (chk_row[x][i] == 0 && chk_col[y][i] == 0 && chk_square[square(x, y)][i] == 0)
{
chk_row[x][i] = chk_col[y][i] = chk_square[square(x, y)][i] = true;
sudoku[x][y] = i;
make_sudoku(z + 1);
// 숫자 i가 아니면 백트래킹
sudoku[x][y] = 0;
chk_row[x][i] = chk_col[y][i] = chk_square[square(x, y)][i] = false;
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
for (int i = 0; i < sudoku_size; i++)
{
for (int j = 0; j < sudoku_size; j++)
{
cin >> sudoku[i][j];
if (sudoku[i][j] != 0)
{
// 각 열, 행, 정사각형에 입력된 숫자가 있다고 저장한다.
chk_row[i][sudoku[i][j]] = true;
chk_col[j][sudoku[i][j]] = true;
chk_square[square(i, j)][sudoku[i][j]] = true;
}
}
}
make_sudoku(0);
return 0;
}