开始8个谜题

时间:2016-11-28 21:08:36

标签: java 8-puzzle

如果给定起始节点的x和y坐标,我将如何创建板。例如,如果x = 3且y = 2,则董事会应该:

1 2 3
4 5 x
6 7 8

java或伪代码中的一个例子非常有用。

2 个答案:

答案 0 :(得分:0)

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

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++;            
        }

    }
}

答案 1 :(得分:-1)

在发布代码之前,我只推荐一件事。在编程中,你必须开始考虑从零开始的索引。此外,如果您发布的格式在索引编制中没有任何拼写错误(因为在打印&#39; x&#39;之后必须合理地打印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();
    }