Skip to content

Commit c65d4c5

Browse files
authored
[BOJ] 2239 스도쿠 (G4)
1 parent 6146c4a commit c65d4c5

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include <iostream>
2+
#include <vector>
3+
using namespace std;
4+
5+
struct Node {
6+
int x, y;
7+
};
8+
9+
char arr[10][10];
10+
bool rows[10][10];
11+
bool cols[10][10];
12+
bool board[10][10];
13+
vector<Node> zeros;
14+
15+
int getBoardIdx(int r, int c) {
16+
return (r / 3) * 3 + (c / 3);
17+
}
18+
19+
void dfs(int idx){
20+
if (idx == zeros.size()) {
21+
for (int i = 0; i < 9; i++) {
22+
for (int j = 0; j < 9; j++) {
23+
cout << arr[i][j];
24+
}
25+
cout << '\n';
26+
}
27+
exit(0);
28+
}
29+
30+
int r = zeros[idx].x;
31+
int c = zeros[idx].y;
32+
int b = getBoardIdx(r, c);
33+
34+
for(int i = 1; i <= 9; i++){
35+
// 행, 열, 9칸 다 검색
36+
if (!rows[r][i] && !cols[c][i] && !board[b][i]) {
37+
rows[r][i] = true;
38+
cols[c][i] = true;
39+
board[b][i] = true;
40+
arr[r][c] = i + '0';
41+
dfs(idx + 1);
42+
arr[r][c] = '0';
43+
rows[r][i] = false;
44+
cols[c][i] = false;
45+
board[b][i] = false;
46+
}
47+
}
48+
}
49+
50+
int main() {
51+
ios::sync_with_stdio(false);
52+
cin.tie(0); cout.tie(0);
53+
54+
for(int i = 0; i < 9; i++){
55+
for(int j = 0; j < 9; j++){
56+
cin >> arr[i][j];
57+
int num = arr[i][j] - '0';
58+
if (num == 0) zeros.push_back({i, j});
59+
else {
60+
rows[i][num] = true;
61+
cols[j][num] = true;
62+
board[getBoardIdx(i, j)][num] = true;
63+
}
64+
}
65+
}
66+
dfs(0);
67+
68+
return 0;
69+
}

0 commit comments

Comments
 (0)