Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void wpisywanieSudokuDoRozwiazania(){
- int a;
- for (int j=0;j<21;j++){
- for (int i=0;i<9;i++){
- cin >> a;
- sudoku[j][i]=a;
- }
- }
- }
- bool solve(int x, int y);
- int sudoku[9][9] = { /* tu zadane sudoku... */ };
- int curr[9][9];
- bool can_insert(int x, int y, int value) {
- for(int i = 0; i < 9; i++) {
- if (value == curr[x][i] || value == curr[i][y] ||
- value == curr[x/3*3+i%3][y/3*3+i/3]) return false;
- } return true;
- }
- bool next(int x, int y) {
- if (x == 8 && y == 8) return true;
- else if (x == 8) return solve(0, y + 1);
- else return solve(x + 1, y);
- }
- bool solve(int x, int y) {
- if (sudoku[x][y] == 0) {
- for(int i = 1; i <= 9; i++) {
- if (can_insert(x, y, i)) {
- curr[x][y] = i;
- if (next(x, y)) return true;
- }
- } curr[x][y] = 0; return false;
- } return next(x, y);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement