代码说明
Posted bit1120161927
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了代码说明相关的知识,希望对你有一定的参考价值。
1、生成数独终局
void swap(int i, int begin);/*交换seed数组中的两个数*/ { int temp; temp = seed[i]; seed[i] = seed[begin]; seed[begin] = temp; } void put();/*将生成的数独和求解出来的数独写回文件sudoku.txt*/ { int i,j,k; for(i = 0; i < 9; i++){ k = 0; for(j = 0; j < 17; j++){ if(j % 2 == 0) y[i][j] = x[i][k++] + ‘0‘; else if(j % 2 == 1) y[i][j] = ‘ ‘; } y[i][j] = ‘\n‘; } if(count == 0) fp = fopen(".\\sudoku.txt", "w+"); else fp = fopen(".\\sudoku.txt", "a"); for(j = 0; j < 9; j++) fprintf(fp, y[j]); fprintf(fp, "\n"); fclose(fp); } void sudoku();/*x数组第一行形成一个全排列数后,根据第一行生成一个数独终局并进行行变换生成更多数独终局*/ { int i,j; int vary[9] = {0,3,6,1,4,7,2,5,8}; for(i = 1; i < 9; i++) { for(j = 0; j < 9; j++) { x[i][ (j + vary[i]) % 9 ] = x[0][j]; } } for(i = 0; i < 6; i++) { switch(i) { case 0: swap_x(0,0);break; case 1: swap_x(4,5);break; case 2: swap_x(3,4);break; case 3: swap_x(3,4);swap_x(4,5);break; case 4: swap_x(3,5);swap_x(4,5);break; case 5: swap_x(3,5);break; } for(j = 0; j < 6; j++) { switch(j) { case 0: swap_x(0,0);break; case 1: swap_x(7,8);break; case 2: swap_x(6,7);break; case 3: swap_x(6,7);swap_x(7,8);break; case 4: swap_x(6,8);swap_x(7,8);break; case 5: swap_x(6,8);break; } if(count < sum) { put(); count++; } else break; switch(j) { case 0: swap_x(0,0);break; case 1: swap_x(7,8);break; case 2: swap_x(6,7);break; case 3: swap_x(7,8);swap_x(6,7);break; case 4: swap_x(7,8);swap_x(6,8);break; case 5: swap_x(6,8);break; } } switch(i) { case 0: swap_x(0,0);break; case 1: swap_x(4,5);break; case 2: swap_x(3,4);break; case 3: swap_x(4,5);swap_x(3,4);break; case 4: swap_x(4,5);swap_x(3,5);break; case 5: swap_x(3,5);break; } if(count >= sum) break; } }
2、解数独
void input();/*命令行输入-s时从指定文件中读取待求解数独,嵌套put(),dfs()函数*/ { int i,j,k; char ch; count = 0; while(!feof(fps)){ for(i = 0; i < 9; i++) fgets(y[i], 30, (FILE*)fps); ch = fgetc(fps); for(i = 0; i < 9; i++) y[i][17] = 0; for(i = 0; i < 9; i++) { k = 0; for(j = 0; j < 17; j++) { if(y[i][j] != ‘ ‘) x[i][k++] = y[i][j] - ‘0‘; } } dfs(0, 0); count ++; } fclose(fps); } void dfs(int row, int col);/*回溯求解数独*/ { int i,j,k; int count = 0; int d[50]; for(i = 0; i < 9; i++) d[i] = 0; if (row == 9 && col == 9) { put(); return; } if(x[row][col] == 0) { for (i = 0; i < 9; i++) if (x[row][i] != 0) d[x[row][i] - 1] = 1; for(i = 0; i < 9; i++) if(x[i][col] != 0) d[ x[i][col] - 1 ] = 1; for (i = int(row / 3) * 3; i < int(row / 3) * 3 + 3; i++) { for (j = int(col / 3) * 3; j < int(col / 3) * 3 + 3; j++) { if (x[i][j] != 0) d[x[i][j] - 1] = 1; } } for (i = 0; i < 9; i++) if (d[i] == 0) count++; if (count == 0) return; else{ for (i = 0; i < 9; i++) { if (d[i] == 0){ x[row][col] = i + 1; d[i] = 1; if (col == 8) { if(row == 8) { col++; row++; } if(row < 8){ col = 0; row++; } } else if(col < 8 && row <= 8) col++; dfs(row, col); if (col == 0){ col = 8; row--; } else col--; x[row][col] = 0; d[i] = 0; } } } } else{ if (col == 8){ if(row == 8){ col++; row++; } if(row < 8){ col = 0; row++; } } else if(col < 8 && row <= 8) col++; dfs(row, col); } }
以上是关于代码说明的主要内容,如果未能解决你的问题,请参考以下文章
[AndroidStudio]_[初级]_[配置自动完成的代码片段]