#include // [row][column][changeable] int puz[9][9][2]; int row = 0; int col = 0; // Some global variables for commandline arguments int pretty = 0; // print the result in a grid instead standard notation; int load = 0; // load input written like '6 4 0 0 3' instead of '64..3' // Moving row and column void goNext() { // Sets row and col to the next square that can be changed // THIS FUNCTION DOES NOT CHECK BOUNDS while(1) { row++; if(row>8) { row = 0; col++; } if(puz[row][col][1] == 1) { return; } } } void goPrevious() { // Sets row and col to the previous square that can be changed // THIS FUNCTION DOES NOT CHECK BOUNDS while(1) { row--; if(row<0) { row = 8; col--; } if(puz[row][col][1] == 1) { return; } } } //------------------------------------ // Checking properties of puzzle int rowContains(int checkrow, int num) { // Check if a row contains a number for(int i=0; i<9; i++) { if(puz[checkrow][i][0] == num) { return 1; } } return 0; } int colContains(int checkcol, int num) { // Check if a column contains a number for(int i=0; i<9; i++) { if(puz[i][checkcol][0] == num) { return 1; } } return 0; } int blockContains(int checkrow, int checkcol, int num) { // Check if a 3x3 block contains a number // The blocks are ordered as following: // 1 | 2 | 3 // ----------- // 4 | 5 | 6 // ----------- // 7 | 8 | 9 // To avoid making a translation between row/col and block, we input a row and column number instead of block number int blockrow = checkrow / 3; int startrow = blockrow * 3; int endrow = startrow + 3; int blockcol = checkcol / 3; int startcol = blockcol * 3; int endcol = startcol + 3; for(int i = startrow; i < endrow; i++) { for(int j = startcol; j < endcol; j++) { if(puz[i][j][0] == num) { return 1; } } } return 0; } int changeable(int checkrow, int checkcol) { return puz[checkrow][checkcol][1]; } //----------------------------------------- // Loading and printing puzzle void loadSpecial() { for(int i=0; i<9; i++) { for(int j=0; j<9; j++) { scanf("%d",&puz[i][j][0]); if(puz[i][j][0] == 0) { puz[i][j][1] = 1; } else puz[i][j][1] = 0; } } } void loadStandard() { for(int i=0; i<9; i++) { for(int j=0; j<9; j++) { char c; scanf("%c",&c); if(c == '.') { puz[i][j][0] = 0; puz[i][j][1] = 1; } else { puz[i][j][0] = c - 48; puz[i][j][1] = 0; } } } } void loadPuzzle() { if(load) { loadSpecial(); } else { loadStandard(); } } void printPretty() { for(int i=0; i<9; i++) { for(int j=0; j<9; j++) { int num = puz[i][j][0]; if(num != 0) { printf("%d",puz[i][j][0]); } else { if(i == row && j == col) { printf("*"); } else printf(" "); } if(j == 2 || j == 5) printf("|"); } printf("\n"); if(i == 2 || i == 5) printf("-----------\n"); } printf("\n"); } void printStandard() { for(int i=0; i<9; i++) { for(int j=0; j<9; j++) { if(puz[i][j][0] == 0) { printf("."); } else { printf("%d",puz[i][j][0]); } } } printf("\n"); } void printPuzzle() { if(pretty) { printPretty(); } else { printStandard(); } } //------------------------------ void solve() { // The algotihm: // 1. Go to the first changeable spot // 2. Increase it's number by 1 // 3. Check: // If this number is possible here, go to the next spot and repeat // If this number is not possible here and can be increased, increase and check again // If this number is not possible here and cannot be increased, reset the number, go back a spot and repeat if(!changeable(row,col)) { goNext(); } while(row < 9 && col < 9) { int cur = puz[row][col][0]; int new = cur + 1; if(new > 9) { puz[row][col][0] = 0; goPrevious(); } else { if(!rowContains(row,new) && !colContains(col,new) && !blockContains(row,col,new)) { puz[row][col][0] = new; goNext(); } else { puz[row][col][0] = new; } } } } void parseArguments(int count, char *args[]) { if(count > 1) { for(int i=1; i