用Graph实现数独puzzel 9x9

时间:2017-04-22 19:05:49

标签: java

我在文本文件中有一个数独的puzzel 9x9,我想知道如何从数独的puzzel创建一个图。苏木puzzel是一个int [] []示例puzzel

0 0 0 0 9 8 0 4 5 
0 0 4 3 2 0 7 0 0 
7 9 0 5 0 0 0 3 0 
0 0 0 9 0 0 4 0 0 
0 4 5 0 0 2 8 0 0 
8 7 9 6 0 4 0 1 0 
0 3 0 0 7 9 0 6 4 
4 5 0 2 1 3 9 0 8 
0 8 7 4 6 5 0 0 0 

和类Graph

    class Graph
    {
        private int V; 
        private LinkedList<Integer> adj[]; 
        Graph(int v)
        {
            V = v;

            adj = new LinkedList[v];
            for (int i=0; i<v; ++i)
                adj[i] = new LinkedList();


        }

        void addEdge(int v,int w)
        {
            adj[v].add(w);
            adj[w].add(v); 
        }
        public int getV()
        {
            return V;
        }
 public LinkedList<Integer> getListAdj(int u)
    {
        return adj[u];
    }

我编写了一个函数来从文本文件中读取puzzel并将其实现为图形

public boolean readGraph(String input_name, int result[])
    {

          return true;
    }

但我坚持这一步。

1 个答案:

答案 0 :(得分:-1)

这里是:

首先,我已经知道拼图是9x9,所以文本文件中的换行符对我来说毫无意义。我可以忽略它们。此外,我知道拼图的每个元素只会是一个单个字符,所以(在将文本文件放入内存后如此:Reading a plain text file in Java):

我将String puzzle这个文本文件存入内存。现在我想像这样遍历那个字符串:

Graph graph = new Graph(81); //9x9 graph with 81 verticies
int whichVertextAreWeOn = 0;

for (int i = 0; i < puzzle.length(); i++){
    char c = puzzle.charAt(i);
    if(c>'0' && c > '9'){
       int integer = Integer.parseInt(c);
       //now I need to add this to my Graph... Saving my work now, comments are appreciated
       //Turns out you simply add edges to each vertex in a graph, the vertex itself has no value...
       //so I'm going to keep track of which vertex I'm on, this is starting to seem like a bad data structure for this purpose, but I shall carry on. -Adding an iterator to keep track of which vertex I'm on.
       for(int w = 0; w < graph.V(); w++;){
         if(vertextIsLinked(whichVertextAreWeOn, w))
           graph.addEdge(whichVertextAreWeOn, w);
       }
       //hum... from here I need to add an edge to this vertex for each connected vertex...
     }
}

private boolean vertextIsLinked(int vertexWeAreOn, int possibleVertext){
  //use rules of Sukdoku to figure out if these two verticies should be linked. This would return true for any vertex in horizontal or vertical alignment to the vertexWeAreOn.
}