-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
5-yuyu0830
- Loading branch information
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |