poj2312(BFS加优先队列)

题目:
Description

Many of us had played the game “Battle city” in our childhood, and some people (like me) even often play it on computer now.

What we are discussing is a simple edition of this game. Given a map that consists of empty spaces, rivers, steel walls and brick walls only. Your task is to get a bonus as soon as possible suppose that no enemies will disturb you (See the following picture).

Your tank can’t move through rivers or walls, but it can destroy brick walls by shooting. A brick wall will be turned into empty spaces when you hit it, however, if your shot hit a steel wall, there will be no damage to the wall. In each of your turns, you can choose to move to a neighboring (4 directions, not 8) empty space, or shoot in one of the four directions without a move. The shot will go ahead in that direction, until it go out of the map or hit a wall. If the shot hits a brick wall, the wall will disappear (i.e., in this turn). Well, given the description of a map, the positions of your tank and the target, how many turns will you take at least to arrive there?
Input

The input consists of several test cases. The first line of each test case contains two integers M and N (2 <= M, N <= 300). Each of the following M lines contains N uppercase letters, each of which is one of ‘Y’ (you), ‘T’ (target), ‘S’ (steel wall), ‘B’ (brick wall), ‘R’ (river) and ‘E’ (empty space). Both ‘Y’ and ‘T’ appear only once. A test case of M = N = 0 indicates the end of input, and should not be processed.
Output

For each test case, please output the turns you take at least in a separate line. If you can’t arrive at the target, output “-1” instead.
Sample Input

3 4
YBEB
EERE
SSTE
0 0
Sample Output

8
Source

题目大意:从开始的Y到目标T求最短路,但是这里有个限制,如果通过砖的话,算两次。这就使得题目比较难解决。但是如果我们用了优先级队列,这题目就相当于BFS模板题。。。
优先队列,听着名字就知道和普通的FIFO队列不同。这里优先的意思是取队首元素时具有一定的选择性。我也是刚刚接触优先队列,正在学习中。大家可以去网上查阅一下相关资料,我们先了解基本的语法规则即可
下面放上代码

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
typedef struct point
{
    int x;
    int y;
    int step;
} Node;
struct cmp
{
    bool operator () (Node a,Node b)
    {
        return a.step > b.step;   //最小值优先
    }
};
int dirx[4]= {-1, 1, 0, 0};
int diry[4]= {0, 0, -1, 1};
char Map[350][350];
int  Vis[350][350];
char str[1000];
int tmp[4], cnt;
int row, col;
int xru, yru, xchu, ychu;

void bfs();
int main()
{
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int i, j;
    while(scanf("%d%d", &row, &col)!=EOF&&row&&col)
    {
        cnt=0;
        memset(Map, 0, sizeof(Map));
        memset(Vis, 0, sizeof(Vis));
        memset(str, 0, sizeof(str));

        for(i=1; i<=row; i++)
        {
            scanf("%s", str);
            for(j=1; j<=col; j++)
            {
                Map[i][j]=str[j-1];

                if(Map[i][j]=='Y')
                {
                    xru=j;
                    yru=i;
                }
                if(Map[i][j]=='T')
                {
                    xchu=j;
                    ychu=i;
                }
            }
        }
        bfs();
    }
    return 0;
}

void bfs()
{
    priority_queue<Node,vector<Node>,cmp> Q;//定义了一个最小优先队列
    Node a, b;
    a.x=xru;
    a.y=yru;
    a.step=0;
    Vis[yru][xru]=1;
    Q.push(a);
    while(!Q.empty())
    {
        a=Q.top();
        Q.pop();
        if(a.x==xchu&&a.y==ychu)
        {
            printf("%d\n", a.step);
            return;
        }
        for(int i=0; i<4; i++)
        {
            b.x=a.x+dirx[i];
            b.y=a.y+diry[i];
            if(Map[b.y][b.x]!='B')
                b.step=a.step+1;
            else
                b.step=a.step+2;

            if(b.x<1||b.x>col||b.y<1||b.y>row||Map[b.y][b.x]=='R'||Map[b.y][b.x]=='S'||Vis[b.y][b.x])
                continue;
            else
            {
                Vis[b.y][b.x]=1;
                Q.push(b);
            }
        }
    }
    printf("%d\n", -1);
}

你可能感兴趣的:(搜索,优先队列,bfs)