welcome: please sign in

2009-12-04 06:16:15時点のリビジョン1

メッセージを消す
location: 解のある配置を作る

ばらばらにする

        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++)  // 偶数回置換
                {
                        do
                        {
                                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;
                }
        }