#P1238. 走迷宫

走迷宫

Description

There is an m×nm \times n grid maze (meaning mm rows and nn columns), with some cells passable and others not. Use 11 for passable and 00 for impassable. The input provides these m×nm \times n values and the start and end points (each point is described by two numbers: its row index and column index). Your task is to write a program to find all feasible paths such that no cell is visited more than once, and movement is allowed only in the four directions: up, down, left, and right. If no path is feasible, output the corresponding information (1-1 means no path).

Priority order: left, up, right, down. The testdata are guaranteed to be randomly generated.

Input Format

The first line contains two integers m,nm, n (1<m,n<151 < m, n < 15). Then follow mm rows and nn columns of data consisting of 11 and 00. The last two lines are the start point and the end point.

Output Format

Output all feasible paths. When describing a point, use the form (x,y)(x, y). Except for the starting point, connect consecutive points using -> to indicate direction.

If there is no feasible path, output 1-1.

5 6
1 0 0 1 0 1
1 1 1 1 1 1
0 0 1 1 1 0
1 1 1 1 1 0
1 1 1 0 1 1
1 1
5 6
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(2,5)->(3,5)->(3,4)->(3,3)->(4,3)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(2,5)->(3,5)->(3,4)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(2,5)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(3,4)->(3,3)->(4,3)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(3,4)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(3,4)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,4)->(2,4)->(2,5)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,4)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,4)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(4,3)->(4,4)->(3,4)->(2,4)->(2,5)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(4,3)->(4,4)->(3,4)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(4,3)->(4,4)->(4,5)->(5,5)->(5,6)

Hint

The testdata are guaranteed to be randomly generated. In fact, if n=m=14n = m = 14 and every cell is 11, there are 6945066476152136166427470154890735899648869450664761521361664274701548907358996488 paths.

Translated by ChatGPT 5