Pro.ID21603 TitleMaze, amazing Title链接http://10.20.2.8/oj/exercise/problem?problem_id=21603 AC3 Submit12 Ratio25.00% 时间&空间限制描述Let's play maze game again! Maze problem is an old problem but here comes a challenge one. Max, an intelligent robot, is trapped in an N*M maze grid. He is located at a start position initially and aiming to find the exit of the maze. In the maze there are numbers of pillars which are set as obstacles. Max is energetic and not afraid of strolling in the maze at all. But he dislikes turning round in the maze all through. "Turning left or right several times keeps me uncomfortable and confused! It makes me feel sick and unconfident to run out of this maze!" Max said. Given the cost of turning left and right of Max, the description of the maze grid, the start position and destination, you are going to give your hand to Max by calculating the minimum cost of turning round (no matter left or right) to get to the exit from start position. Note: Max can just perform three operations at one time: go front, turn left, and turn right Initially Max stands at the start point and you should decide in which direction he starts the first step in order to minimize the total dislike of Max. 输入Input may consist of several test data sets. For each data set, it can be format as below: First comes two integers in one line separating with one space: l ( 1 ≤ l ≤ 100 ) representing the cost of turning left of Max, r ( 1 ≤ r ≤ 100 ) representing the cost of turning right of Max. Then six integers follows in next coming line separating with one space: r1 ( 1 ≤ r1 ≤ 100 ) representing the number of rows of the maze grid, c ( 1 ≤ c ≤ 100 ) representing the number of columns of the maze grid, sx (1 ≤ sx ≤ r1 ) representing the row position of the start position of Max, sy ( 1 ≤ sy ≤ c ) representing the column position of the start position of Max, ex ( 1 ≤ ex ≤ r1 ) representing the row position of the exit of the maze, ey ( 1 ≤ ey ≤ c ) representing the column position of the exit of the maze. Finally comes r1 row(s) with c character(s) in each row. This part represents the maze data part and all character(s) can only be two types: '*' representing a pillar of the maze and '.' representing an empty grid cell that Max can stand on. Position of cell in the upper-left comer of the grid is (1, 1). Input is ended with l = r = 0. Note: Max can't go outside the range of the row and column of the maze, either go pass the pillar. You can assume the input is legal that means there is no pillar in the start position and exit of the maze. 输出Description Let's play maze game again! Maze problem is an old problem but here comes a challenge one. Max, an intelligent robot, is trapped in an N*M maze grid. He is located at a start position initially and aiming to find the exit of the maze. In the maze there are numbers of pillars which are set as obstacles. Max is energetic and not afraid of strolling in the maze at all. But he dislikes turning round in the maze all through. "Turning left or right several times keeps me uncomfortable and confused! It makes me feel sick and unconfident to run out of this maze!" Max said. Given the cost of turning left and right of Max, the description of the maze grid, the start position and destination, you are going to give your hand to Max by calculating the minimum cost of turning round (no matter left or right) to get to the exit from start position. Note: Max can just perform three operations at one time: go front, turn left, and turn right Initially Max stands at the start point and you should decide in which direction he starts the first step in order to minimize the total dislike of Max. Input Input may consist of several test data sets. For each data set, it can be format as below: First comes two integers in one line separating with one space: l ( 1 ≤ l ≤ 100 ) representing the cost of turning left of Max, r ( 1 ≤ r ≤ 100 ) representing the cost of turning right of Max. Then six integers follows in next coming line separating with one space: r1 ( 1 ≤ r1 ≤ 100 ) representing the number of rows of the maze grid, c ( 1 ≤ c ≤ 100 ) representing the number of columns of the maze grid, sx (1 ≤ sx ≤ r1 ) representing the row position of the start position of Max, sy ( 1 ≤ sy ≤ c ) representing the column position of the start position of Max, ex ( 1 ≤ ex ≤ r1 ) representing the row position of the exit of the maze, ey ( 1 ≤ ey ≤ c ) representing the column position of the exit of the maze. Finally comes r1 row(s) with c character(s) in each row. This part represents the maze data part and all character(s) can only be two types: '*' representing a pillar of the maze and '.' representing an empty grid cell that Max can stand on. Position of cell in the upper-left comer of the grid is (1, 1). Input is ended with l = r = 0. Note: Max can't go outside the range of the row and column of the maze, either go pass the pillar. You can assume the input is legal that means there is no pillar in the start position and exit of the maze. Output Output one integer in one line representing the minimum cost of turning to get to the exit if there is one way to get there, or output -1 in one line if it is impossible for Max to get to the destination. Sample Input 1 2 Sample Output 0 Hint Case 1: In this case there is no need to turn round for Max to go to (1, 4) from (1, 1), he just chooses the right direction at start and then goes straight all through. Case 2: In this case there is no way for Max to go to (1, 4) from (1, 1). Case 3: In this case Max can turn right twice to achieve the goal costing 4 in total Case 4: The minimum cost is 4, below is the optimal solution In this route, Max need to turn right once (at (2, 2)), then turn left twice (at (3, 2) and (3, 4)), so the total cost is 2 + 1 + 1 = 4, which is the least cost that can find. Source 样例输入1 2 样例输出0 提示Case 1: In this case there is no need to turn round for Max to go to (1, 4) from (1, 1), he just chooses the right direction at start and then goes straight all through. Case 2: In this case there is no way for Max to go to (1, 4) from (1, 1). Case 3: In this case Max can turn right twice to achieve the goal costing 4 in total Case 4: The minimum cost is 4, below is the optimal solution In this route, Max need to turn right once (at (2, 2)), then turn left twice (at (3, 2) and (3, 4)), so the total cost is 2 + 1 + 1 = 4, which is the least cost that can find. |