From 370130ccb03ca7e5596231c2c1e6fa42de9044bb Mon Sep 17 00:00:00 2001 From: MCC Date: Wed, 26 Jun 2019 12:07:10 +0900 Subject: [PATCH] =?UTF-8?q?[Jake]=20=EB=B0=B1=EC=A4=80=202580=EB=B2=88=20?= =?UTF-8?q?=EC=8A=A4=EB=8F=84=EC=BF=A0=20=ED=92=80=EC=9D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 190624/jake/2580.cpp | 75 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 190624/jake/2580.cpp diff --git a/190624/jake/2580.cpp b/190624/jake/2580.cpp new file mode 100644 index 0000000..f177661 --- /dev/null +++ b/190624/jake/2580.cpp @@ -0,0 +1,75 @@ +#include +using namespace std; + +bool solved = false; +int board[9][9], answer[9][9]; +bool row[9][10]{0,}, col[9][10]{0,}, block[9][10]{0,}; + +void get_next(int& y, int& x) { + for(int i=0; i<9; ++i){ + for(int j=0; j<9; ++j){ + if(!board[i][j]){ + y = i, x = j; + return; + } + } + } +} + +bool check_possible(int y, int x, int v){ + return !col[y][v] && !row[x][v] && !block[(y/3)*3+(x/3)][v]; +} + +void set_boolean(int y, int x, int v, bool set_or_unset) { + col[y][v] = row[x][v] = block[(y/3)*3+(x/3)][v] = set_or_unset; +} + +void solve() { + if(solved) return; + int y = -1, x = -1; + get_next(y, x); + + if(y == -1){ + for(int i=0; i<9; ++i){ + for(int j=0; j<9; ++j){ + answer[i][j] = board[i][j]; + } + } + solved = true; + return; + } + + for(int v=1; v<10; ++v) { + if(check_possible(y, x, v)){ + set_boolean(y, x, v, true); + board[y][x] = v; + solve(); + board[y][x] = 0; + set_boolean(y, x, v, false); + } + } +} + +int main() +{ + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + for(int i=0; i<9; ++i){ + for(int j=0; j<9; ++j){ + cin >> board[i][j]; + set_boolean(i, j, board[i][j], true); + } + } + + solve(); + + for(int i=0; i<9; ++i){ + for(int j=0; j<9; ++j){ + cout << answer[i][j] << " "; + } + cout << "\n"; + } + + return 0; +}