21603_Maze,amazing

2022-5-16 18:19| 发布者: Hocassian| 查看: 24| 评论: 0|原作者: 肇庆学院ACM合集

摘要:
C:\Users\Administrator\Downloads\2019-10-12-10-14-4-89505680376900-Problem List-采集的数据-后羿采集器.html

Pro.ID

21603

Title

Maze, amazing

Title链接

http://10.20.2.8/oj/exercise/problem?problem_id=21603

AC

3

Submit

12

Ratio

25.00%

时间&空间限制

  • Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others)
  • 描述

    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 ≤ sxr1 ) representing the row position of the start position of Max, sy ( 1 ≤ syc ) representing the column position of the start position of Max, ex ( 1 ≤ exr1 ) representing the row position of the exit of the maze, ey ( 1 ≤ eyc ) 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 ≤ sxr1 ) representing the row position of the start position of Max, sy ( 1 ≤ syc ) representing the column position of the start position of Max, ex ( 1 ≤ exr1 ) representing the row position of the exit of the maze, ey ( 1 ≤ eyc ) 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
    1 4 1 1 1 4
    ....
    1 2
    1 4 1 1 1 4
    ..*.
    1 2
    2 4 2 1 2 4
    ....
    ..*.
    1 2
    3 4 2 1 2 4
    *...
    ..*.
    *...
    0 0

    Sample Output

    0
    -1
    4
    4

    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
    1 4 1 1 1 4
    ....
    1 2
    1 4 1 1 1 4
    ..*.
    1 2
    2 4 2 1 2 4
    ....
    ..*.
    1 2
    3 4 2 1 2 4
    *...
    ..*.
    *...
    0 0

    样例输出

    0
    -1
    4
    4

    提示

    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.


    路过

    雷人

    握手

    鲜花

    鸡蛋

    最新评论

    返回顶部