welcome: please sign in
location: "解のある配置を作る"の差分
1と2のリビジョン間の差分
2009-12-04 06:16:15時点のリビジョン1
サイズ: 689
編集者: masahiko
コメント:
2009-12-07 01:04:49時点のリビジョン2
サイズ: 459
編集者: masahiko
コメント:
削除された箇所はこのように表示されます。 追加された箇所はこのように表示されます。
行 4: 行 4:
 void mazeru()
 {
  int i, j, x1, y1, x2, y2, w;
  
  ban = new int[nx][ny];
  for(i = 0; i < nx; i++) // 元の位置
   for(j = 0; j < ny; j++)
    ban[i][j] =i + j * nx;
  spx = nx-1; // 空白の位置
  spy = ny-1;
  for(i = 0; i < (nx*ny*2); i++) // 偶数回置換
 int spx, spy;
}}}

{{{
  g.setColor(Color.lightGray);
  g.fillRect(spx*haba, spy*haba, haba, haba);
}}}

{{{
  spx = yoko-1;
  spy = tate-1;
  for (cnt = 0; cnt < (tate*yoko*2); ) // 偶数回置換
行 16: 行 17:
   do    x = (int)(Math.random() * yoko);
   y = (int)(Math.random() * tate);
   if(x != spx || y != spy)
行 18: 行 21:
    x1 = (int)(Math.random() * nx);
    y1 = (int)(Math.random() * ny);
    x2 = (int)(Math.random() * nx);
    y2 = (int)(Math.random() * ny);
   } while ( (x1 == x2 && y1 == y2) ||
    (x1 == spx && y1 == spy) || (x2 == spx && y2 == spy) );
   w = ban[x1][y1];
   ban[x1][y1] = ban[x2][y2];
   ban[x2][y2] = w;
    ban[spx][spy] = ban[x][y];
    spx = x;
    spy = y;
    cnt++;
   }
行 28: 行 27:
 }

ばらばらにする

        int spx, spy;

                g.setColor(Color.lightGray);
                g.fillRect(spx*haba, spy*haba, haba, haba);

                spx = yoko-1;
                spy = tate-1;
                for (cnt = 0; cnt < (tate*yoko*2); )    // 偶数回置換
                {
                        x = (int)(Math.random() * yoko);
                        y = (int)(Math.random() * tate);
                        if(x != spx || y != spy)
                        {
                                ban[spx][spy] = ban[x][y];
                                spx = x;
                                spy = y;
                                cnt++;
                        }
                }

解のある配置を作る (最終更新日時 2012-01-25 00:55:25 更新者 masahiko)