开始8个谜题

Starting the 8 puzzles

给定起始节点的任何 x 和 y 坐标,我将如何着手创建板。例如,如果 x=3 和 y=2,板应该像:

1 2 3
4 5 x
6 7 8

java 中的示例或伪代码将非常有用。

希望这对您有所帮助。如果您有任何问题,请告诉我。

int x_lim = 2;
int y_lim = 3;

int count=1;
for(int x=1;x<3+1;x++)
{
    for(int y=1;y<3+1;y++)
    {
        if(x_lim==x && y_lim==y)  //skip case (blank tile)
        {
            System.out.println("x"+" ");
        }
        else  //other numbers
        {
            System.out.println(count+" ");
            count++;            
        }

    }
}

在发布代码之前,我只推荐一件事。在编程中,您必须开始考虑从零开始的索引。此外,如果您发布的格式在索引中没有任何拼写错误(因为在打印 'x' 之后合理地您必须打印 7 而不是 6,以便 3x3 板最终在索引 9 中)可能是下面的代码会帮你的。

    int coord_x = 3;
    int coord_y = 2;

    int rows = 3;
    int columns = 3;

    int counter = 1;
    for (int i = 1; i <= rows; i++){
        for (int j = 1; j <= columns; j++){
            if (i == coord_y && j == coord_x){
                System.out.print("x ");
                continue;
            }
            System.out.print(counter + " ");
            counter++;
        }
        System.out.println();
    }