1

indexoutofbounds の問題は解決されましたが、プログラムはコンパイルされず、変更されていないパズルが出力されます..どこが間違っているのかわかりません?? 元のパズル読み取り ROM の std 入力には、数独パズルの「空の」セルの代わりに 0 があります。arraylist 初期化子も含めました。

public ArrayList<Integer> create(){

    ArrayList<Integer> possible = new ArrayList<Integer>(); 

    for(int i=1; i<10; i++){
        possible.add(i);
    }
    return possible;
  }
  public sudoku( int size )
  {
    SIZE = size;
    N = size*size;

    Grid = new int[N][N];
    for( int i = 0; i < N; i++ ) 
        for( int j = 0; j < N; j++ ) 
            Grid[i][j] = 0;
   }

  public void solve()
  { 
    int a, b, c, d, i, j, k, l; 

    int count = 0;
    int value= 0;

    for(i=0; i<N;i++){
        for(j=0; j<N;j++){  
            if(Grid[i][j]==0){

                ArrayList<Integer> possible = create();

                //check row             
                for(a=0; a<N;a++){
                    for(b=0; b<N; b++){  
                        if(Grid[a][0]==possible.get(a)){
                            possible.set(a, 0);
                        }
                    }
                }
                //check column
                for(c=0; c<N;c++){
                    for(d=0; d<N;d++){  
                        if(Grid[0][d]==possible.get(d)){
                            possible.set(d,0);
                        }
                    }
                }
                for(k=0; k<9; k++){
                    if(possible.get(k)!=0){
                        count++;
                    }
                }
                if(count==1){
                    for(l=0; l<9; l++){
                        if(possible.get(l)!=0){
                            value=possible.get(l);
                        }
                    }
                }
                Grid[i][j]=value;
            }
        }
    }
}
4

2 に答える 2

2

ネストされた for ループで i 変数と j 変数をインデックスとして複数回使用しています。

  for (i = 0; i < N; i++) { // **** you use "i" it here
     for (j = 0; j < N; j++) { // **** and "j" here
        if (Grid[i][j] == 0) {

           ArrayList<Integer> possible = create();

           for (i = 0; i < N; i++) { // **** and again here
              for (j = 0; j < N; j++) { // **** and again here
                 if (Grid[i][0] == possible.get(i)) {
                    possible.set(i, 0);
                 }
              }
           }

           for (i = 0; i < N; i++) { // **** and again here
              for (j = 0; j < N; j++) { // **** and again here
                 if (Grid[0][j] == possible.get(i)) {
                    possible.set(i, 0);
                 }
              }
           }

           // ....

           Grid[i][j] = value;
        }
     }
  }

for ループ内からインデックスを進めると、最大インデックスを超える危険性があるため、一番下に到達するまでに、i と j は行と列のサイズを超えて 9 までインクリメントされています。for ループ内から for ループ インデックスを変更することはほとんどありません。このコードを書き直す必要があります。

編集: それよりもさらに簡単です: for ループが終了した後に i をチェックしているため、i は上限の値です。これを実行して、私が何を意味するかを確認してください。

  for (i = 0; i < N; i++) {
     for (i = 0; i < N; i++) {
        System.out.println("C) i = " + i);
     }
     System.out.println("D) i = " + i);
  }
于 2012-04-15T03:58:33.910 に答える
0

possible.get(0) を出力して、空でないことを確認してください。それがエラーをスローする場合は、そこに行きます!

または、次のように、ループの合間に try ステートメントを試して、どの部分がそれをスローするかを判断することもできます。

try{
    ArrayList<Integer> possible = create();
}
catch(ArrayIndexOutOfBoundsException e){
         System.out.println(1);
}
try{
                //check row             
                for(i=0; i<N;i++){
                    for(j=0; j<N;j++){  
                        if(Grid[i][0]==possible.get(i)){
                            possible.set(i, 0);
                        }
                    }
                }
}
catch(ArrayIndexOutOfBoundsException e){
         System.out.println(2);
}
try{
                //check column
                for(i=0; i<N;i++){
                    for(j=0; j<N;j++){  
                        if(Grid[0][j]==possible.get(i)){
                            possible.set(i,0);
                        }
                    }
                }
}
catch(ArrayIndexOutOfBoundsException e){
         System.out.println(3);
}
try{
                for(k=0; k<9; k++){
                    if(possible.get(k)!=0){
                        count++;
                    }
                }
}
catch(ArrayIndexOutOfBoundsException e){
         System.out.println(4);
}
try{
                if(count==1){
                    for(l=0; l<9; l++){
                        if(possible.get(l)!=0){
                            value=possible.get(l);
                        }
                    }
                }
}
catch(ArrayIndexOutOfBoundsException e){
         System.out.println(5);
}
try{
                Grid[i][j]=value;}
catch(ArrayIndexOutOfBoundsException e){
         System.out.println(6);
}
于 2012-04-15T04:07:25.417 に答える