Time limit: 1 seconds
Space limit: 32768k
Title Description
The little Frog one day accidentally fell into an underground maze, the Little frog hope to use their only physical value p jump out of the underground maze. To make the problem simple, let's say it's a n*m maze. Each location of the maze is 0 or 1. 0 for this position there are obstacles, the small frog can not reach the position; 1 represents the position that the small frog could reach. The small frog is initially in (0,0) position. The exit of the underground labyrinth is in (0,m-1) (ensure that both positions are 1, and that there must be a path from the beginning to the destination). Small frog in the maze horizontal movement of a unit distance needs to consume 1 points of physical value. Climbing a unit distance needs to consume 3 physical value, downward movement does not consume the stamina value. When the small frog's physical strength is equal to 0 times has not reached the exit, the small frog will not escape the maze. Now I need you to help the little frog figure out whether it is possible to jump out of the maze with the physical value of this life (0,m-1)
Input:
Enter n+1 line:
First behavior 3 integers n,m (3<=m,n<=10), P (1<=p<=100)
The next n rows:
M 0 or 1 per line, separated by a space
Output:
If you can escape the maze, output a line of energy consumption of the smallest path, the output format is shown in the sample; If you cannot escape the maze, output "Can not escape!".
Test data guarantees the answer is unique
Input Example:
* 4 4 10
* 1 0 0 1
* 1 1 0 1
* 0 1 1 1
* 0 0 1 1
Output Example:
[0,0],[1,0],[1,1],[2,1],[2,2],[2,3],[1,3],[0,3]
This is a typical dynamic programming (dynamical programming) problem. Now let's analyze how we should answer them.
First, both the exit [0,m-1] and the inlet [0,0] have been given, which means that the program inlet and outlet are fixed. The rest of us need to find a way through and use the least amount of energy. This means that the output value is optimal. Assuming the end from [0,0] to [0,m-1] go K-step, then K-step is the remaining strength of the largest, then the k-1 step is not before reaching K before the largest physical surplus? The answer is yes, and so on, we can exit the state transition formula. Then look at slow not satisfied with no effect: from K to k+1 step does not affect the best route already traversed, so no effect. So now let's take a look at the state transition equation.
=================================== Gorgeous split-line ================================================
We use F[K][I][J] to indicate the remaining strength. Here: K represents the remaining strength of the small frog in step K, I row J.
So obviously: f[0][0][0]=p
F[0][0][m-1]>=0 at the exit, the physical strength is greater than or equal to 0.
And each of the f[k][0][m-1] is to be maximized.
So how do you maximize it? We know that step K is from K-1, there are three ways to go: from k-1 upward, to the right and down, and each of the methods corresponds to a different physical consumption values. So we have:
F[K][I][J] = max (f[k-1][i][j-1]-1, F[k-1][i+1][j], f[k-1][i-1[j]-3)
Here are some students may want to ask, in some border situation is not down, up or right, it doesn't matter, we first write the total state equation, and then at each step of the case to determine the situation can be cut off some unnecessary foliage. The specific procedures are as follows:
//input is second-order Maze matrix (N*M) and initial physical value
1 Public Static voidFindpathforfrog (int[] Maze,intP) {2queue<integer[]> route =NewLinkedlist<integer[]>();3 if(Findpath (Maze, p, 0, 0, route, p) <0)4System.out.println ("Can not escape!");5 }6 7 Private Static intFindpath (int[] Maze,intRestenergy,intNintM, queue<integer[]> route,intOE) {8 //When out of range or frog cannot get through from9 //returnTen if(N < 0 | | m < 0 | | n > Maze.length-1 | | m > MAZE[0].LENGTH-1 | | maze[n][m] = = 0 | | Restenergy < 0) One return-OE; A - //Reach exit with negative energy - if(n = = 0 && m = = maze[0].length-1 && Restenergy < 0) { the return-OE; - } - - //Reach to exit with non-negative Energy + if(n = = 0 && m = = maze[0].length-1 && restenergy >= 0) { -Route.add (NewInteger[] {0, 0 }); + for(integer[] element:route) { A if(Element.length! = 2) atSystem.err.println ("Error ocurred!"); -System.out.println ("[" + element[0] + "," + element[1] + "]"); - } - returnRestenergy; - } - //System.out.println ("N:" + n + "M:" + M + "rest:" + restenergy); in if(Maze[n][m] = = 1) -Route.add (Newinteger[] {n, m}); to intFromup = Findpath (Maze, restEnergy-3, n + 1, M, route, OE); + intFromright = Findpath (Maze, RestEnergy-1, N, m + 1, route, OE); - intFromdown = Findpath (Maze, Restenergy, n-1, M, route, OE); the * intMAM =Math.max (Fromup, Math.max (Fromright, Fromdown)); $ Panax Notoginseng if(Mam = =fromup) -Route.add (NewInteger[] {n + 1, M}); the Else if(Mam = =fromright) +Route.add (NewInteger[] {n, m + 1 }); A Else if(Mam = =Fromdown) theRoute.add (NewInteger[] {n-1, M}); + returnmam; -}
Of course there are several places not detailed, such as how to output path and boundary determination, hope to read the friends of their own brain to understand it.
Drip Travel 2017 autumn recruit Project pen questions-Underground maze