Task: | Sudoku |
Sender: | |
Submission time: | 2015-08-16 22:07:27 +0300 |
Language: | C++ |
Status: | READY |
Result: | 100 |
group | verdict | score |
---|---|---|
#1 | ACCEPTED | 100 |
test | verdict | time | |
---|---|---|---|
#1 | ACCEPTED | 0.06 s | details |
#2 | ACCEPTED | 0.05 s | details |
#3 | ACCEPTED | 0.06 s | details |
#4 | ACCEPTED | 0.06 s | details |
#5 | ACCEPTED | 0.06 s | details |
Code
#include <iostream> using namespace std; string input; int sudoku[9][9]; bool ok(int number, int row, int col) { for (int i = 0; i < 9; i++) { if (sudoku[row][i] == number) return false; } for (int i = 0; i < 9; i++) { if (sudoku[i][col] == number) return false; } for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (sudoku[i+row-row%3][j+col-col%3] == number) return false; } } return true; } bool solved(int& i, int& j) { for (i = 0; i < 9; i++) { for (j = 0; j < 9; j++) { if (sudoku[i][j] == 0) return false; } } return true; } bool solve() { int row, col; if (solved(row,col)) return true; for (int number = 1; number <= 9; number++) { if (ok(number,row,col)) { sudoku[row][col] = number; if (solve()) return true; sudoku[row][col] = 0; } } return false; } int main() { cin >> input; for (int i = 0; i < 9; i++) { sudoku[0][i] = input[i]-48; } solve(); for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { cout << sudoku[i][j]; } cout << "\n"; } }
Test details
Test 1
Verdict: ACCEPTED
input |
---|
592836471 |
correct output |
---|
592836471 836471592 471592836 928364715 364715928 ... |
user output |
---|
592836471 134257689 678149235 213465798 456798123 ... |
Test 2
Verdict: ACCEPTED
input |
---|
672935418 |
correct output |
---|
672935418 935418672 418672935 729354186 354186729 ... |
user output |
---|
672935418 134268579 589147236 213456897 456789123 ... |
Test 3
Verdict: ACCEPTED
input |
---|
329174658 |
correct output |
---|
329174658 174658329 658329174 291746583 746583291 ... |
user output |
---|
329174658 145268379 678359124 213485796 456791283 ... |
Test 4
Verdict: ACCEPTED
input |
---|
376958421 |
correct output |
---|
376958421 958421376 421376958 769584213 584213769 ... |
user output |
---|
376958421 124367589 589124367 213475698 457689132 ... |
Test 5
Verdict: ACCEPTED
input |
---|
875694321 |
correct output |
---|
875694321 694321875 321875694 756943218 943218756 ... |
user output |
---|
875694321 123578469 469123578 214356897 356789142 ... |