-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2580.cpp
More file actions
108 lines (95 loc) · 1.67 KB
/
2580.cpp
File metadata and controls
108 lines (95 loc) · 1.67 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// 2580. 스도쿠
// 2019.05.20
// 백트래킹
#include<iostream>
#include<vector>
using namespace std;
int map[10][10]; // 스도쿠 판
vector<pair<int, int>> zeros; // 0의 위치 저장
bool flag;
vector<int> Check(int x, int y)
{
vector<int> possible;
int num[10];
fill(num, num + 10, 0);
// 가로 검사
for (int i = 0; i < 9; i++)
{
num[map[x][i]]++;
}
// 세로 검사
for (int i = 0; i < 9; i++)
{
num[map[i][y]]++;
}
// 주위 9칸 검사
int xx = (x / 3) * 3;
int yy = (y / 3) * 3;
for (int i = xx; i < xx + 3; i++)
{
for (int j = yy; j < yy + 3; j++)
{
num[map[i][j]]++;
}
}
// 1부터 9까지 가로,세로,주위칸에 없는것은 넣을수 있는 숫자
for (int i = 1; i < 10; i++)
{
if (num[i] == 0)
{
possible.push_back(i);
}
}
return possible;
}
// cnt : 현재까지 채운 빈칸의 개수
void go(int cnt)
{
// 한번 출력했으면 탈출
if (flag)
{
return;
}
// 모든 빈칸이 다 채워지면 출력 zeros.size() : 기존 0의 개수
if (cnt == zeros.size())
{
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
cout << map[i][j] << " ";
}
cout << "\n";
}
flag = true;
return;
}
int x = zeros[cnt].first;
int y = zeros[cnt].second;
// 빈칸에서 가능한 숫자 받아오기
vector<int> possible = Check(x, y);
// 가능한 숫자들로 탐색
for (int i = 0; i < possible.size(); i++)
{
int num = possible[i];
map[x][y] = num;
go(cnt + 1);
map[x][y] = 0;
}
}
int main()
{
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
cin >> map[i][j];
if (map[i][j] == 0)
{
zeros.push_back({ i,j });
}
}
}
go(0);
return 0;
}