Transcribing ASCII maze into graph

I have a maze such as the below:

XXXOOOOX
OXXXOXOE
OXXOOXXO
OSXOXXXO
XOOOXOOO

//X = wall
//O = path
//S = startpoint
//E = endpoint

I want to transcribe this to a graph (adjacency matrix) but am unsure in my attempt to do so. An adjacency matrix is a list of lists of booleans, with true representing a takeable path and false representing an impossible path (eg. (0,-1) in the maze cannot connect to (7,-1) (0,0 being the top-left most node)). I'm not sure how to transcribe this, my best guess would be to take each element in a list and then a sublist with connecting nodes. It's difficult to process given the size of this maze, so here's a smaller one:

X E || A B
S O || C D

Would an adjacency matrix be below for the above maze (1 = true, 0 = false)?

  A B C D
A 0 0 0 0
B 0 0 0 1
C 0 0 0 1
D 0 1 1 0

//C connects to D (start)
//D connects to B (end)

It's just really confusing, I'm not sure how to map an x,y (sometimes z, too) co-ordinate to a graph.


尝试这个

 static void set(boolean[][] paths, int cols, int row0, int col0, int row1, int col1) {
     int index0 = row0 * cols + col0;
     int index1 = row1 * cols + col1;
     paths[index0][index1] = paths[index1][index0]= true;
 }

 static boolean[][] paths(String[] maze) {
     int rows = maze.length;
     int cols = maze[0].length();
     boolean[][] paths = new boolean[rows * cols][rows * cols];
     for (int i = 0; i < rows; ++i) {
         for (int j = 0; j < cols; ++j) {
             if (maze[i].charAt(j) == 'X')
                 continue;
             if (i + 1 < rows && maze[i + 1].charAt(j) != 'X')
                 set(paths, cols, i, j, i + 1, j);
             if (j + 1 < cols && maze[i].charAt(j + 1) != 'X')
                 set(paths, cols, i, j, i, j + 1);
         }
     }
     return paths;
 }

 public static void main(String[] args) {
     String[] maze = {
         "XXXOOOOX",
         "OXXXOXOE",
         "OXXOOXXO",
         "OSXOXXXO",
         "XOOOXOOO",
     };
     boolean[][] paths = paths(maze);
     for (boolean[] row : paths) {
         for (boolean cell : row)
             System.out.print(cell ? "1" : "0");
         System.out.println();
     }
 }
链接地址: http://www.djcxy.com/p/79626.html

上一篇: OpenCV将白色像素分组

下一篇: 将ASCII迷宫转录成图形