-->
当前位置:首页 > 题库

PROGRAMMING:Solving the shortest path of maze from entrance to exit

Luz5年前 (2021-05-10)题库445
Solve the shortest path of maze from entrance to exit. Input a maze to find the shortest path from the entrance to the exit. In order to simplify the problem, the maze uses two-dimensional array int maze [10] [10] to store the distribution of obstacles, assuming that the size of the maze's transverse and longitudinal dimensions are the same, and read by the program. If the value of the read maze size is n (3 < n < = 10), then the maze's transverse and longitudinal dimensions are n, and the outermost circle of the maze is an obstacle, and the maze's entrance is maze [1] [1], The exit is maze [n-2] [n-2]. If maze [i] [J] = 1, it means that the position is an obstacle. If maze [i] [J] = 0, it means that the position is a walkable vacancy (0 < = I < = n-1, 0 < = J < = n-1). Find the path from the entrance maze [1] [1] to the exit maze [n-2] [n-2]. The maze is only allowed to walk in the horizontal or up and down four directions, and the position can not be repeated. It is required to search forward in the order of right, down, left and up, and output the shortest path to the exit first.
This is a maze
![ dddd.png](~/c7031624-69f6-4956-bd81-9ebc2c7219e9.png)
The corresponding two-dimensional array represents:
int maze[10][10]={
{1,1,1,1,1,1,1,1,1,1},
{1,0,0,1,0,0,0,1,0,1},
{1,0,0,1,0,0,0,1,0,1},
{1,0,0,0,0,1,1,0,0,1},
{1,0,1,1,1,0,0,0,1,1},
{1,0,0,0,1,0,0,0,1,1},
{1,0,1,0,0,0,1,0,0,1},
{1,1,1,1,0,1,1,0,1,1},
{1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}};
###Input format:
Enter the integer n of maze size, and a two-dimensional array of N rows and N columns (array element 1 represents obstacles and 0 represents vacancies).
###Output format:
Output the first shortest path to the exit according to the specified search trial sequence, output the row and column subscripts (I, J) of each position of the shortest feasible path from the entrance to the exit in turn, and separate each position with ",". If there is no channel, output: No.
###Input sample 1:
```in
four
1 1 1 1
1 0 1 1
1 0 0 1
1 1 1 1
```
###Output sample 1:
```out
(1,1)(2,1)(2,2)
```
###Input sample 2:
```in
ten
1 1 1 1 1 1 1 1 1 1
1 0 0 1 0 0 0 1 0 1
1 0 0 1 0 0 0 1 0 1
1 0 0 0 0 1 1 0 0 1
1 0 1 1 1 0 0 0 0 1
1 0 0 0 1 0 0 0 0 1
1 0 1 0 0 0 1 0 0 1
1 0 1 1 1 0 1 1 0 1
1 1 0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1 1
```
###Output sample 2:
```out
(1,1)(2,1)(3,1)(4,1)(5,1)(5,2)(5,3)(6,3)(6,4)(6,5)(7,5)(8,5)(8,6)(8,7)(8,8)
```







answer:If there is no answer, please comment