Java算法填充单元格,如“Android - Flow”游戏

时间:2014-07-23 20:03:08

标签: java android algorithm

我有一个问题。

我们有一个表2xN,其链接节点为1,6 -> 1,12,6 -> 2,1就像一个cilinder。

      -----------------------------------------------------
(1)->|  1,1    | 1,2    |  1,3   | 1,4    |  1,5   |  1,6   |  -> (1)
      ------------------------------------------------------
(2)->|   2,1   | 2,2    |  2,3   | 2,4    |  2,5   |  2,6   |  -> (2)
      ------------------------------------------------------

我有一个 StartPoint1 - 是单元格1,1中的一个点,单元格2,6中的 EndPoint1 StartPoint12 - 是单元格2,1中的一个点,单元格2,5中的 EndPoint2

我想在途中找到两个填充所有表格的组合。

例如上面是

  

(P1)=(1,1) - > (1,2) - > (1,3) - > (1,4) - > (1,5) - > (1,6) - > (2,6)
  (P2)=(2,1) - > (2,2) - > (2,3) - > (2,4) - > (2,5)

所以,我走遍所有桌子两个路径(12个牢房 - 12个台阶)

现在我在填充结构后停止了:

private static void buildGrid(int gridResolution) {
    for (int i = 1; i < 3; i++) {
        for (int j = 0; j < gridResolution; j++) {
            Node node = new Node();
            if (startPoint1.x == i && startPoint1.y == j) {
                node.point = new PointM(new Point(i, j), 1);
                startNode1 = node;
            } else if (startPoint2.x == i && startPoint2.y == j) {
                node.point = new PointM(new Point(i, j), 1);
                startNode2 = node;
            } else if (endPoint1.x == i && endPoint1.y == j) {
                node.point = new PointM(new Point(i, j), 2);
                endNode1 = node;
            } else if (endPoint2.x == i && endPoint2.y == j) {
                node.point = new PointM(new Point(i, j), 2);
                endNode2 = node;
            } else {
                node.point = new PointM(new Point(i, j), 0);
            }
            nodes[i][j] = node;

            Node leftNode = getLeftNode(i, j);
            Node topNode = getTopNode(i, j);
            if (leftNode != null) {
                node.left = leftNode;
                leftNode.right = node;
            }
            if (topNode != null) {
                node.top = topNode;
                topNode.bottom = node.top;
            }
        }
    }
}

private static Node getTopNode(int i, int j) {
    return nodes[i - 1][j];
}

private static Node getLeftNode(int i, int j) {
    if (j - 1 > 0)
        return nodes[i][j - 1];
    else return null;
}

private static class Node {
    public PointM point;
    public Node left;
    public Node right;
    public Node top;
    public Node bottom;

    public boolean isChecked;
}

我不知道在那之后我需要做什么。我坚持这一刻。尽力而为,将绕开这张桌子。也许算法是什么?

1 个答案:

答案 0 :(得分:0)

对于您访问的每个单元格,请将其标记为已访问。

从您现在的单元格中,查看可以访问的单元格: 如果没有你可以去的小区,那么检查这是否是游戏的结束。如果不是游戏结束,那就是失败。

如果有可以访问的单元格,请列出要访问的单元格,并为该单元格生成递归。当递归结束时,您有两个选择:游戏完成或递归结束。如果它结束了,那么为下一个可用的单元格产生一个新的递归。没有更多可用的细胞?停止。