2014-07-23 71 views
0

我遇到問題。Java算法填充像「Android - Flow」遊戲的單元格

我們有一個表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端點1細胞2,6
點和StartPoint12 - 在細胞2,1端點2細胞點2,5

我想找到填充所有表格的兩種方法的組合。

例如以上是

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

所以,我走周圍的所有表上的兩個pathes(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; 
} 

而我不知道我需要做什麼之後。我堅持在這一刻。最好,並會繞過這張桌子。也許這是算法?

回答

0

對於您訪問的每個單元格,將其標記爲已訪問。

從你現在的細胞中,看看哪些是可能的細胞,你可以去: 如果沒有細胞,你可以去,然後檢查這是否是遊戲的結束。如果不是比賽結束,那麼這是一場失敗。

如果有可以訪問的單元格,請列出要訪問的單元格併爲該單元格產生遞歸。當遞歸完成時,你有兩種選擇:遊戲已經完成或者遞歸失敗。如果結果不好,則爲下一個可用單元產生新的遞歸。沒有更多可用的細胞?停止。

+0

你能提供一些代碼給更詳細的視覺嗎? –

+0

哪部分你不明白? –

+0

我需要遞歸查找路徑?所以如果我位於中心單元格中,我需要檢查所有這兩個定位的單元格? –