HDU 1242 && ZOJ 1649( BFS (隊列 || 優先隊列)).

~~~~

突然發現一篇搜索的題目都有寫。昨天發現道bfs題目,HDU上AC, ZOJ上WA。不得不說HDU上的數據之水。。

今天早起刷題有了思路,並用隊列和單調隊列都寫了一遍,0MS飄過~~

~~~~

題目鏈接:

http://acm.hdu.edu.cn/showproblem.php?pid=1242

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=649

~~~~

首先有坑的地方是friends,對嘛,朋友有很多,angel只有一個。所以更好的處理辦法就是以angel爲起點。

其次是殺死guard並走到其位置上需要2mins,這樣就違背了BFS等距離向外擴展的原理(應該是這樣說吧,大牛請賣萌)。

所以用隊列的話,可以把殺死guard和走到其位置上看成兩個步驟,入隊兩次。

優先隊列就簡單了,直接以時間從小到大建立優先級就好了。

~~~~

隊列:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#define N 222
using namespace std;

int n,m;
char g[N][N];
int dir[4][2]={1,0,-1,0,0,-1,0,1};
struct node
{
    int x,y;
    int t;
};
int bfs(int x,int y)
{
    queue<node> q;
    node cur,next;
    cur.x=x,cur.y=y,cur.t=0;
    q.push(cur);
    g[x][y]='#';    //相當於vis數組記錄是否訪問過。
    while(!q.empty())
    {
        cur=q.front();
        q.pop();
        if(g[cur.x][cur.y]=='y')
        {
            g[cur.x][cur.y]='#'; //~~
            cur.t=cur.t+1;
            q.push(cur);    //再入隊一次
            continue;
        }
        for(int i=0;i<4;i++)
        {
            next.x=cur.x+dir[i][0],next.y=cur.y+dir[i][1];
            if(next.x>=0 && next.x<n && next.y>=0 && next.y<m && g[next.x][next.y]!='#')
            {
                if(g[next.x][next.y]=='.')
                {
                    g[next.x][next.y]='#';
                    next.t=cur.t+1;
                    q.push(next);
                }
                else if(g[next.x][next.y]=='x') //~~
                {
                    g[next.x][next.y]='y';  //~~
                    next.t=cur.t+1;
                    q.push(next);
                }
                else if(g[next.x][next.y]=='r')
                    return cur.t+1;
            }
        }
    }
    return -1;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        int sx,sy;
        for(int i=0;i<n;i++)
        {
            scanf("%s",g[i]);
            for(int j=0;j<m;j++)
                if(g[i][j]=='a') sx=i,sy=j;
        }
        int t=bfs(sx,sy);
        if(t<0) puts("Poor ANGEL has to stay in the prison all his life.");
        else printf("%d\n",t);
    }
    return 0;
}

優先隊列:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#define N 222
using namespace std;

int n,m;
char g[N][N];
int dir[4][2]={1,0,-1,0,0,-1,0,1};
struct node
{
    int x,y;
    int t;
};
bool operator < (node a,node b) 
{
    return a.t>b.t;  //~~
}
int bfs(int x,int y)
{
    priority_queue<node> q;
    node cur,next;
    cur.x=x,cur.y=y,cur.t=0;
    q.push(cur);
    g[x][y]='#';
    while(!q.empty())
    {
        cur=q.top();
        q.pop();
        for(int i=0;i<4;i++)
        {
            next.x=cur.x+dir[i][0],next.y=cur.y+dir[i][1];
            if(next.x>=0 && next.x<n && next.y>=0 && next.y<m && g[next.x][next.y]!='#')
            {
                if(g[next.x][next.y]=='.')
                {
                    g[next.x][next.y]='#';
                    next.t=cur.t+1;
                    q.push(next);
                }
                else if(g[next.x][next.y]=='x')
                {
                    g[next.x][next.y]='#';
                    next.t=cur.t+2;
                    q.push(next);
                }
                else if(g[next.x][next.y]=='r')
                    return cur.t+1;
            }
        }
    }
    return -1;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        int sx,sy;
        for(int i=0;i<n;i++)
        {
            scanf("%s",g[i]);
            for(int j=0;j<m;j++)
                if(g[i][j]=='a') sx=i,sy=j;
        }
        int t=bfs(sx,sy);
        if(t<0) puts("Poor ANGEL has to stay in the prison all his life.");
        else printf("%d\n",t);
    }
    return 0;
}


發佈了76 篇原創文章 · 獲贊 8 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章