This function seems really bulky and I'm pretty sure that it can be simplified down. Can someone help me do this?
Code:int sd_solve(int x, int y, int n){ //Checks if n is a valid number to input int i,j,k; //Checking the boxes //For this, we will divide the sudoku into //nine sections (top-left, top-mid, top-right, //mid-left, mid-mid, mid-right, bottom-left //bottom-mid, bottom-right) and check if n is //already in the section that we are trying to //solve. if (y<3) { //If n will be in the top section if (x<3) { //If n will be in the top-left for(i=0;i<3;i++){ for(j=0;j<3;j++){ if(n==sd_finish[i][j]) { return 0; } } } } else if(x<6) { //If n will be in the top-mid for(i=3;i<6;i++){ for(j=0;j<3;j++){ if(n==sd_finish[i][j]) { return 0; } } } } else { //If n will be in the top-right for(i=6;i<9;i++){ for(j=0;j<3;j++){ if(n==sd_finish[i][j]) { return 0; } } } } } else if(y<6) { //If n will be in the mid section if(x<3) { //If n will be in the mid-left for(i=0;i<3;i++){ for(j=3;j<6;j++){ if(n==sd_finish[i][j]) { return 0; } } } } else if(x<6) { //If n will be in the mid-mid for(i=3;i<6;i++) { for(j=3;j<6;j++) { if(n==sd_finish[i][j]) { return 0; } } } } else { //If n will be in the mid-right for(i=6;i<9;i++) { for(j=3;j<6;j++) { if(n==sd_finish[i][j]) { return 0; } } } } } else { //If n will be in the bottom section if(x<3) { //If n will be in the bottom-left for(i=0;i<3;i++) { for(j=6;j<9;j++) { if(n==sd_finish[i][j]) { return 0; } } } } else if(x<6) { //If n will be in the bottom-mid for(i=3;i<6;i++) { for(j=6;j<9;j++) { if(n==sd_finish[i][j]) { return 0; } } } } else { //If n will be in the bottom-right for(i=6;i<9;i++) { for(j=6;j<9;j++) { if(n==sd_finish[i][j]) { return 0; } } } } } for (j = 0; j < 9; j++) { //Checks the row and column n is in if (n == sd_finish[j][y] || n == sd_finish[x][j]) { return 0; } } return n; //If everything is fine, it returns n }
It's part of a Sudoku code and another function is basically calling this function with a possible value n when it sees a blank and checks the box the number will be in and the horizontal and vertical possibilities.
I'm almost positive this can be simplified it's just that I can't figure it out :/



LinkBack URL
About LinkBacks


