nyoj 284坦克大戰(優先隊列+BFS)

題目鏈接http://acm.nyist.net/JudgeOnline/problem.php?pid=284

坦克大戰

時間限制:1000 ms  |  內存限制:65535 KB
難度:3
描述
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 .

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?
輸入
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.
輸出
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.
樣例輸入
3 4
YBEB
EERE
SSTE
0 0
樣例輸出
8
來源
POJ
題目大意:
在地圖中有幾種障礙物,“S” 金屬塊(你不能打破),“B”磚塊,可以打破它(需要一分鐘,打破之後就相當於沒有障礙物了,即穿過一個磚塊要兩分鐘),“R”河流(不能通過),“E”沒有障礙物(走一步要一分鐘)。現在你處於“Y”,從“Y”走到“T”,輸出需要的最短時間。
解題思路:
剛開始,我直接是用的BFS,結果WA,後來在討論區發現一個測試數據,結果發現自己的程序確實過不了這個數據。之後才知道這個題目要用優先隊列+BFS,要好好理解這些數據結構才能快速切題,加油
關於優先隊列的使用方法其實和隊列差不多,可以在這裏看看用法:http://blog.csdn.net/yin_zongming/article/details/21559981
下面是AC代碼
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<stack>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
#ifdef __int64
typedef __int64 LL;
#else
typedef long long LL;
#endif
#define maxn 300+10
int vis[maxn][maxn];
char v[maxn][maxn];
int dir[4][2]={-1,0,1,0,0,1,0,-1};
int m,n;
int bx,by;
int cnt;
struct point
{
    int x,y,step;
    friend bool operator < (const point &s1,const point &s2)//改變優先級,由於優先隊列默認是大的數字優先級高
    {                                                       //現在改爲step小的優先級高,符合題意
        return s1.step>s2.step;
    }
};
void bfs(int x,int y)
{
    priority_queue<point>p;
    point t,q;
    t.x=x;t.y=y;t.step=0;
    p.push(t);
    while(!p.empty())
    {
        t=p.top();//優先隊列是用top獲取隊列頭部的元素,而隊列是用front,注意
        p.pop();
        if(v[t.x][t.y]=='T')
        {
            cnt=t.step;
            return ;
        }
        for(int i=0;i<4;i++)
        {
            q.x=t.x+dir[i][0];
            q.y=t.y+dir[i][1];
            if(q.x>=0&&q.x<m&&q.y>=0&&q.y<n&&!vis[q.x][q.y]&&v[q.x][q.y]!='S'&&v[q.x][q.y]!='R')
            {
                vis[q.x][q.y]=1;
                if(v[q.x][q.y]=='B')
                {
                    q.step=t.step+2;
                    p.push(q);
                }
                else
                {
                    q.step=t.step+1;
                    p.push(q);
                }
            }
        }
    }
}
int main()
{
    while(scanf("%d%d",&m,&n)&&m&&n)
    {
        for(int i=0;i<m;i++)
        {
            scanf("%s",v[i]);
            for(int j=0;j<n;j++)
            {
                if(v[i][j]=='Y')
                {
                    bx=i;
                    by=j;
                }
            }
        }
        memset(vis,0,sizeof(vis));
        vis[bx][by]=1;
        cnt=0;
        bfs(bx,by);
        if(cnt)
            printf("%d\n",cnt);
        else
            printf("-1\n");
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章