Skip to content

Commit

Permalink
5-yuyu0830
Browse files Browse the repository at this point in the history
5-yuyu0830
  • Loading branch information
yuyu0830 authored Apr 3, 2024
2 parents 1d7111c + 6b93ce7 commit 2755e58
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions yuyu0830/탐색/2239.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// λ°±νŠΈλž˜ν‚Ή κ³¨λ“œ 4 μŠ€λ„μΏ  https://www.acmicpc.net/problem/2239
#include <iostream>

using namespace std;

int sudoku[9][9] = {0, 0};

bool solve(int pos) {
// λ§ˆμ§€λ§‰ μΉΈ μˆœνšŒμ€‘μ΄λ©΄ true 리턴
if (pos == 81) return true;

// pos κ°’ 톡해 x, y κ°’ μΆ”μΆœ
int x = pos % 9;
int y = pos / 9;

// ν˜„μž¬ 탐색 μœ„μΉ˜μ— 값이 있으면 λ°”λ‘œ λ‹€μŒ 탐색
if (sudoku[y][x]) {
if (solve(pos + 1)) return true;
return false;
}

// 3x3 μΉΈ 탐색을 μœ„ν•œ ν¬μ§€μ…˜ κ°’
int sx = (x / 3) * 3;
int sy = (y / 3) * 3;

// 1~9κΉŒμ§€ κ²ΉμΉ˜λŠ” μˆ˜κ°€ μ—†λŠ”μ§€ 체크
for (int i = 1; i <= 9; i++) {
bool flag = true;

// κ°€λ‘œ, μ„Έλ‘œ, 3x3 μΉΈ 탐색
for (int j = 0; j < 9; j++) {
if (sudoku[j][x] == i || sudoku[y][j] == i || sudoku[sy + (j / 3)][sx + (j % 3)] == i) {
// κ²ΉμΉ˜λŠ”κ²Œ μžˆλŠ” 경우
flag = false;
break;
}
}

// κ°€λ‘œ, μ„Έλ‘œ, 3x3 μΉΈ 탐색이 λ¬΄μ‚¬νžˆ μ’…λ£Œλœ 경우
if (flag) {
sudoku[y][x] = i;
if (solve(pos + 1)) return true;
sudoku[y][x] = 0;
}
}

return false;
}

int main() {
// Input
for (int i = 0; i < 9; i++) {
char str[10]; cin >> str;
for (int j = 0; j < 9; j++) {
sudoku[i][j] = str[j] - 48;
}
}

solve(0);

// Output
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
printf("%d", sudoku[i][j]);
}
printf("\n");
}
}

0 comments on commit 2755e58

Please sign in to comment.