[JS][dfs] Problem solving | #Maze problem#

Problem solving | #Maze problem#


topic link

maze problem

Topic description

Define a two-dimensional array N*M as shown below for a 5 × 5 array:

int maze[5][5] = {
0, 1, 0, 0, 0,
0, 1, 1, 1, 0,
0, 0, 0, 0, 0,
0, 1, 1, 1, 0,
0, 0, 0, 1, 0,
};

It represents a maze, in which 1 represents a wall, and 0 represents a path that can be walked. You can only walk horizontally or vertically, and you cannot walk diagonally. It requires programming to find the route from the upper left corner to the lower right corner. The entry point is [0,0], that is, the first grid is the way to go.

This question contains multiple sets of data.

Data range: 2 \le n,m \le 10 \2≤n,m≤10 , the input content only contains 0 \le val \le 1 \0≤val≤1

Enter description:

Enter two integers, representing the number of rows and columns of the two-dimensional array, respectively. Then enter the corresponding array, where 1 represents the wall and 0 represents the road that can be walked. The data is guaranteed to have a unique solution, regardless of the case of multiple solutions, that is, the maze has only one passage.

Output description:

The shortest path from the upper left corner to the lower right corner, in the format shown in the example.

Example 1

enter:

5 5
0 1 0 0 0
0 1 1 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0

output:

(0,0)
(1,0)
(2,0)
(2,1)
(2,2)
(2,3)
(2,4)
(3,4)
(4,4)

Example 2

enter:

5 5
0 1 0 0 0
0 1 0 1 0
0 0 0 0 1
0 1 1 1 0
0 0 0 0 0

output:

(0,0)
(1,0)
(2,0)
(3,0)
(4,0)
(4,1)
(4,2)
(4,3)
(4,4)

Problem solving ideas

Simple wayfinding, pay attention to multiple sets of inputs. The method used is dfs, and it is OK to judge whether it is a shorter circuit and refresh every time it reaches the end point.


answer

while (line = readline()) {
    
    
    let nums = line.split(' ');
    let col = +nums[0];
    let row = +nums[1];
    let map = [];
    for (let i = 0; i < col; i++) {
    
    
        map.push([]);
        let mapline = readline().split(' ');
        for (let j = 0; j < row; j++) {
    
    
            map[i][j] = +mapline[j];
        }
    }
    let minLine = [];
    let nowLine = [];
    let dir = [[0, 1], [0, -1], [1, 0], [-1, 0]];
    let book = [];
    for (let i = 0; i < col; i++) {
    
    
        book.push([]);
        for (let j = 0; j < row; j++) {
    
    
            book[i][j] = 0;
        }
    }
    let dfs = (x, y) => {
    
    
        nowLine.push(`(${
      
      x},${
      
      y})`);
        book[x][y] = 1;
        if (x == col - 1 && y == row - 1) {
    
    
            if (minLine.length == 0 || nowLine.length < minLine.length) {
    
    
                minLine = [];
                for (let i = 0; i < nowLine.length; i++) {
    
    
                    minLine[i] = nowLine[i];
                }
            }
            book[x][y] = 0;
            nowLine.pop();
            return;
        }
        for (let i = 0; i < 4; i++) {
    
    
            let tox = x + dir[i][0];
            let toy = y + dir[i][1];
            if (tox >= 0 && tox < col && toy >= 0 && toy < row && map[tox][toy] == 0 && book[tox][toy] == 0) {
    
    
                dfs(tox, toy);
            }
        }
        book[x][y] = 0;
        nowLine.pop();
        return;
    }
    dfs(0, 0);
    for (let i = 0; i < minLine.length; i++) {
    
    
        console.log(minLine[i]);
    }
}

Guess you like

Origin blog.csdn.net/qq_36286039/article/details/123331254
Recommended