[Two mice run fast] The Mouse Maze problem

Source: Internet
Author: User

[Description]

MouseAAnd miceBIn oneM × nIn the maze ,. Each square represents a room in the maze. ThisM × nSome rooms are closed and cannot be accessed by anyone. Any position in the Maze can be up, down, left, right4To enter the unclosed room. MouseALocated in the maze(P,Q)In the square, it must find a line leading to the mouseBLocation(R,S)Square path. Please help MiceAFind all leading MiceB.

For the given Mouse Maze, programmed to calculate the mouseALeading to miceBAll the shortest roads.

[Input format]

the input data is provided by the input.txt file. The first line contains 3 positive integers n , m , K indicates the number of rows, columns, and number of closed rooms in the maze. In the next K line, each row 2 positive integers, indicating the row number and column number of the closed room. The last 2 rows also have 2 positive integers, indicating A is located in the (P , q) and mouse B Square (r , S) .

[Output format]

The calculated mouseALeading to miceBThe maximum short-circuit length of and how many different short-circuit outputs to the fileOutput.txt. The first line of the file is the shortest length. The2Rows are different shortest paths.

If the mouseAUnable to access MiceBThe output"Nosolution !".

[Example input]

8 8 3

3 3

4 5

6 6

2 1

7

[Sample output]

11

96

[Analysis]

F [I] [J]. Dis indicates the shortest path to the vertex I and j. F [I] [J]. Count indicates the number of entries in the shortest path. Then spfa is enough.

 

# Include <stdio. h> # define maxn 1001 # define maxint 1000000000 bool have [maxn] [maxn]; struct TT {int DIS, count;} f [maxn] [maxn]; struct SS {int X, Y;} Q [maxn * 100]; int L, R; int Sx, Sy, ex, ey; int n, m; bool V [maxn] [maxn]; int Tx, Ty, NX, NY, K; int XX [4] = {1,-1,0, 0 }; int YY [4] = {0, 0, 1,-1}; bool inmap (int x, int y) {return (x> 0) & (x <= N) & (Y> 0) & (Y <= m);} int main () {freopen ("maze. in "," r ", stdin); freopen (" maze. out "," W ", stdou T); scanf ("% d", & N, & M, & K); For (INT I = 1; I <= K; ++ I) {scanf ("% d", & Tx, & ty); Have [TX] [ty] = 1 ;} scanf ("% d", & Sx, & Sy, & Ex, & ey); Q [0]. X = SX; Q [0]. y = sy; V [SX] [sy] = 1; for (INT I = 1; I <= N; ++ I) for (Int J = 1; j <= m; ++ J) f [I] [J]. dis = maxint; F [SX] [sy]. dis = 0; F [SX] [sy]. count = 1; while (L <= r) {Tx = Q [l]. x; ty = Q [l]. y; For (INT I = 0; I <4; ++ I) {Nx = Tx + XX [I]; ny = ty + YY [I]; If (! Inmap (NX, NY) continue; If (have [NX] [NY]) continue; If (F [TX] [ty]. DIS + 1 <F [NX] [NY]. dis) {f [NX] [NY]. dis = f [TX] [ty]. DIS + 1; F [NX] [NY]. count = f [TX] [ty]. count; If (! V [NX] [NY]) {q [++ R]. X = NX; Q [R]. y = NY; V [NX] [NY] = 1 ;}} else if (F [TX] [ty]. DIS + 1 = f [NX] [NY]. dis) {f [NX] [NY]. count + = f [TX] [ty]. count; If (! V [NX] [NY]) {q [++ R]. X = NX; Q [R]. y = NY; V [NX] [NY] = 1 ;}} V [TX] [ty] = 0; ++ L ;} if (F [Ex] [ey]. dis <maxint) printf ("% d \ n", F [Ex] [ey]. dis, F [Ex] [ey]. count); else printf ("no solution! \ N "); Return 0 ;}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.