Maze, amazing

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Wastematerial/article/details/45543779
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 anN*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) withc 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.

这道是要用BFS,用个优先队列储存,因为每个点都有4个方向过该点,不一定是某个方向最小的值就是通终点最小,我是开了个三维数组储存每个点4个方向不同的值,最后用优先队列进行广搜。

#include <stdio.h>
#include <string.h>
#include <queue>
#define minn 0x3f3f3f3f
using namespace std;
int l,r,sx,sy,ex,ey,n,m;
char a[110][110];
int vist[110][110][4];//某个点4个方向的值
int fx[4][2]={{1,0},{0,-1},{-1,0},{0,1}};//4个前进方向
struct node
{
    int x;
    int y;
    int dir;
    int step;
    node(int xx,int yy,int dd,int st)
    {
        x=xx;
        y=yy;
        dir=dd;
        step=st;
    }
    friend bool operator < (const node ss,const node qq)//优先队列
    {
        return ss.step>qq.step;
    }
};
priority_queue <node> q;
int bfs()
{
    while(!q.empty())
        q.pop();
    for(int j=0;j<4;j++)
        q.push(node(sx,sy,j,0));//入队
    while(!q.empty())
    {
        node s=q.top();
        q.pop();//出队
        if(s.x<0||s.x>=n||s.y<0||s.y>=m)
            continue;
        if(a[s.x][s.y]=='*')
            continue;
        if(s.step>=vist[s.x][s.y][s.dir])
            continue;
        if(s.x==ex&&s.y==ey)
            return s.step;
        vist[s.x][s.y][s.dir]=s.step;
        q.push(node(s.x+fx[(s.dir+1)%4][0],s.y+fx[(s.dir+1)%4][1],(s.dir+1)%4,s.step+r));//右边的点入队
        q.push(node(s.x+fx[(s.dir+3)%4][0],s.y+fx[(s.dir+3)%4][1],(s.dir+3)%4,s.step+l));//左边的点入队
        q.push(node(s.x+fx[s.dir][0],s.y+fx[s.dir][1],s.dir,s.step));//前面的点入队

    }
    return -1;
}
int main()
{
    while(scanf("%d%d",&l,&r)&&(l+r))
    {
        scanf("%d%d%d%d%d%d",&n,&m,&sx,&sy,&ex,&ey);
        for(int i=0;i<n;i++)
            scanf("%s",a[i]);
        sx--;
        sy--;
        ex--;
        ey--;
        memset(vist,minn,sizeof(vist));
        printf("%d\n",bfs());
    }
}

新手博客,望大神指教 微笑

猜你喜欢

转载自blog.csdn.net/Wastematerial/article/details/45543779