welcome: please sign in
location: "解のある配置を作る"の差分
1と5のリビジョン間の差分 (その間の編集: 4回)
2009-12-04 06:16:15時点のリビジョン1
サイズ: 689
編集者: masahiko
コメント:
2009-12-15 12:40:03時点のリビジョン5
サイズ: 712
編集者: masahiko
コメント:
削除された箇所はこのように表示されます。 追加された箇所はこのように表示されます。
行 3: 行 3:
ピースを1つ取り除いてできた空白部分を使って
移動するための処理を記述します。
行 4: 行 7:
 void mazeru()  int spx, spy;
}}}

{{{
 public void paintComponent(Graphics g)
行 6: 行 13:
  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++) // 偶数回置換
  for(...)
   for(...)
   {
    ...
   }
  g.setColor(Color.lightGray);
  g.fillRect(spx*haba, spy*haba, haba, haba);
 }
}}}

{{{#!java
 void shokika()
 {
  ...
  spx = yoko-1;
  spy = tate-1;
  for (cnt = 0; cnt < (tate*yoko*2); ) // 偶数回置換
行 16: 行 31:
   do    x = (int)(Math.random() * yoko);
   y = (int)(Math.random() * tate);
   if(x != spx || y != spy)
行 18: 行 35:
    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++;
   }

ばらばらにする

ピースを1つ取り除いてできた空白部分を使って 移動するための処理を記述します。

        int spx, spy;

        public void paintComponent(Graphics g)
        {
                for(...)
                        for(...)
                        {
                                ...
                        }
                g.setColor(Color.lightGray);
                g.fillRect(spx*haba, spy*haba, haba, haba);
        }

   1         void shokika()
   2         {
   3                 ...
   4                 spx = yoko-1;
   5                 spy = tate-1;
   6                 for (cnt = 0; cnt < (tate*yoko*2); )    // 偶数回置換
   7                 {
   8                         x = (int)(Math.random() * yoko);
   9                         y = (int)(Math.random() * tate);
  10                         if(x != spx || y != spy)
  11                         {
  12                                 ban[spx][spy] = ban[x][y];
  13                                 spx = x;
  14                                 spy = y;
  15                                 cnt++;
  16                         }
  17                 }
  18         }

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