- ベストアンサー
数独解答判定プログラム
数独の解答判定プログラムを作成しているのですが、完成しません。下に僕の作ったプログラムを載せておくので間違い等の指摘をお願いします。 #include <stdio.h> #define row 9 #define column 9 int question[row][column]={ 915284376, 273916584, 468753912, 396178245, 742365891, 851492637, 629847153, 587631429, 134529768, }; int num; int singlenumber(void); int saferow(int, int); int safecolumn(int, int); int safebox(int, int, int); int main(void){ int judge; for(num = 1; num <= 9; num++){ int singlenumber(void); } if(judge == 0){ printf("おめでとう!! 正解です。"); }else{ printf("残念!! 不正解です。"); } return 0; } int singlenumber(void){ int i, j; int row_judge; int column_judge; int box_judge; int judge; for(i = 0; i < row; i++){ int saferow(int i, int num); } for(j = 0; j < column; j++){ int safecolumn(int j, int num); } for(i = 0; i < row; i++){ for(j = 0; j < column; j++){ int safebox(int i, int j, int num); } } if(row_judge == 0 && column_judge == 0 && box_judge == 0){ judge = 0; }else{ judge = 1; } return judge; } int saferow(int i, int num){ int k; int row_judge; for(k = 0; k < column; k++){ if(num == question[i][k]){ row_judge = 0; }else{ row_judge = 1; } } return row_judge; } int safecolumn(int j, int num){ int l; int column_judge; for(l = 0; l < row; l++){ if(num == question[l][j]){ column_judge = 0; }else{ column_judge = 1; } } return column_judge; } int safebox(int i, int j, int num){ int m, n; int box_judge01, box_judge02, box_judge03; int box_judge11, box_judge12, box_judge13; int box_judge21, box_judge22, box_judge23; int box_judge0, box_judge1, box_judge2, box_judge; for(m = 0; m < row ; m++){ if(m / 3 == 0){ for(n = 0; n < 3; n++){ if(num == question[m][n]){ box_judge01 = 0; }else{ box_judge01 = 1; } } for(n = 3; n < 6; n++){ if(num == question[m][n]){ box_judge02 = 0; }else{ box_judge02 = 1; } } for(n = 6; n < 9; n++){ if(num == question[m][n]){ box_judge03 = 0; }else{ box_judge03 = 1; } } } if(box_judge01 == 0 && box_judge02 == 0 && box_judge03 == 0){ box_judge0 = 0; }else{ box_judge0 = 1; } if(m / 3 == 1){ for(n = 0; n < 3; n++){ if(num == question[m][n]){ box_judge11 = 0; }else{ box_judge11 = 1; } } for(n = 3; n < 6; n++){ if(num == question[m][n]){ box_judge12 = 0; }else{ box_judge12 = 1; } } for(n = 6; n < 9; n++){ if(num == question[m][n]){ box_judge13 = 0; }else{ box_judge13 = 1; } } } if(box_judge11 == 0 && box_judge12 == 0 && box_judge13 == 0){ box_judge1 = 0; }else{ box_judge1 = 1; } if(m / 3 == 2){ for(n = 0; n < 3; n++){ if(num == question[m][n]){ box_judge21 = 0; }else{ box_judge21 = 1; } } for(n = 3; n < 6; n++){ if(num == question[m][n]){ box_judge22 = 0; }else{ box_judge22 = 1; } } for(n = 6; n < 9; n++){ if(num == question[m][n]){ box_judge23 = 0; }else{ box_judge23 = 1; } } } if(box_judge21 == 0 && box_judge22 == 0 && box_judge23 == 0){ box_judge2 = 0; }else{ box_judge2 = 1; } if(box_judge0 == 0 && box_judge1 == 0 && box_judge2 == 0){ box_judge = 0; }else{ box_judge = 1; } } return box_judge; }
- みんなの回答 (3)
- 専門家の回答
質問者が選んだベストアンサー
int question[row][column]={ 915284376, 273916584, 468753912, 396178245, 742365891, 851492637, 629847153, 587631429, 134529768, }; だと要素数が9個しかないです int question[ROW][COLUMN]={ 9,1,5,2,8,4,3,7,6, 2,7,3,9,1,6,5,8,4, 4,6,8,7,5,3,9,1,2, 3,9,6,1,7,8,2,4,5, 7,4,2,3,6,5,8,9,1, 8,5,1,4,9,2,6,3,7, 6,2,9,8,4,7,1,5,3, 5,8,7,6,3,1,4,2,9, 1,3,4,5,2,9,7,6,8, }; だと9*9の81個になります。 for(num = 1; num <= 9; num++){ int singlenumber(void); } 関数呼び出しの時は型名は不要です for(num = 1; num <= 9; num++){ singlenumber(); } でいいですが多分やりたいことは for(num = 1; num <= 9; num++){ judge = singlenumber(); } みたいな感じでしょうか ただこれでもループの最後の値しか判定できませんが judge = 0; for(num = 1; num <= 9; num++){ judge += singlenumber(); } にすると9回全部0の時のみ正解になります。 int saferow(int i, int num){ int k; int row_judge; for(k = 0; k < column; k++){ if(num == question[i][k]){ row_judge = 0; }else{ row_judge = 1; } } return row_judge; } これはi番目の行に数字numがあるかどうかの判定のようですがこれもループの最後だけの判定になっちゃいますね。私なら int saferow(int i, int num){ int k; int row_judge = 1; for(k = 0; k < column; k++){ if(num == question[i][k]){ row_judge = 0; break; } } return row_judge; } のようにして一致したら0を代入してループから抜けます。 columnもboxも同じバグがあります。 あと int safebox(int i, int j, int num){ } の i, j は使われてないので引数をなくしても良さそうですが。 それと間違いではないのですがマクロと変数を区別しやすいようにマクロは大文字で書くのが一般的なようです。
その他の回答 (2)
- chie65536
- ベストアンサー率41% (2512/6032)
#include <stdio.h> #define row 9 #define column 9 int question[row][column]={ {9,1,5,2,8,4,3,7,6}, {2,7,3,9,1,6,5,8,4}, {4,6,8,7,5,3,9,1,2}, {3,9,6,1,7,8,2,4,5}, {7,4,2,3,6,5,8,9,1}, {8,5,1,4,9,2,6,3,7}, {6,2,9,8,4,7,1,5,3}, {5,8,7,6,3,1,4,2,9}, {1,3,4,5,2,9,7,6,8} }; int answercheck(void) { int n,x,y,xx,yy; for (n = 1;n <= 9;n++) { for (x = 0;x < column;x++) { int f=1; for (y = 0;y < row;y++) { if (question[y][x] == n) { f=0; break; } } if (f) return 1; } for (y = 0;y < row;y++) { int f=1; for (x = 0;x < column;x++) { if (question[y][x] == n) { f=0; break; } } if (f) return 1; } for (x = 0;x < column;x+=3) { for (y = 0;y < row;y+=3) { int f=1; for (xx = 0;xx < 3;xx++) { for (yy = 0;yy < 3;yy++) { if (question[y+yy][x+xx] == n) { f=0; break; } } } if (f) return 1; } } } return 0; } int main(void){ int judge; judge=answercheck(); if(judge == 0){ printf("おめでとう!! 正解です。"); }else{ printf("残念!! 不正解です。"); } return 0; }
- D-Matsu
- ベストアンサー率45% (1080/2394)
コードだけ書いて「あとはヨロシク」ではなく、「何が、どう」うまくいっていないのかも書いてください。 だいたい、このコードではコンパイルが通ると思えないんですけど。