HDU 1242 Rescue

题意:迷宫搜索,一个起点可能有多个终点,遇到门卫要额外花费一个单位时间;

思路:bfs;

结构体表示队列:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,s1,s2,e1,e2;
char mm[505][505];
int vis[505][505];
int shortest[505][505];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct node
{
    int x,y;
    node(){}
    node(int x,int y)
    {
        this->x=x;
        this->y=y;
    }
}q[500010];
int bfs()
{
    int b=0,e=0;
    q[e++]=node(s1,s2);
    vis[s1][s2]=1;
    shortest[s1][s2]=1;
    while(b<e)
    {
        node now=q[b++];
        for(int i=0;i<4;i++)
        {
            int xx=now.x+dir[i][0];
            int yy=now.y+dir[i][1];
            if(xx<0||xx>=n||yy<0||yy>=m||vis[xx][yy]||mm[xx][yy]=='#') continue;
            if(mm[xx][yy]=='r')
            {
                return shortest[now.x][now.y];
            }        
            vis[xx][yy]=1;
            shortest[xx][yy]=shortest[now.x][now.y]+1;    
            if(mm[xx][yy]=='x')
            {
                shortest[xx][yy]++;
            }
            q[e++]=node(xx,yy);
        }
    }
    return -1;
}
int main()
{
    int i,j,k,ans;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(mm,0,sizeof(mm));
        memset(vis,0,sizeof(vis));
        memset(shortest,0,sizeof(shortest));
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                scanf(" %c",&mm[i][j]);
                if(mm[i][j]=='a')
                {
                    s1=i;s2=j;
                }
            }
        }
        ans=bfs();
        if(ans==-1) printf("Poor ANGEL has to stay in the prison all his life.\n");
        else printf("%d\n",ans);
    }
}

stl普通队列:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
int n,m,s1,s2,e1,e2;
char mm[505][505];
int vis[505][505];
int shortest[505][505];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct node
{
    int x,y;
};
queue<node> q;
int bfs()
{
    int b=0,e=0;
    while(!q.empty()) q.pop();
    node s;
    s.x=s1,s.y=s2;
    q.push(s);
    vis[s1][s2]=1;
    shortest[s1][s2]=1;
    while(!q.empty())
    {
        node now=q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            int xx=now.x+dir[i][0];
            int yy=now.y+dir[i][1];
            if(xx<0||xx>=n||yy<0||yy>=m||vis[xx][yy]||mm[xx][yy]=='#') continue;
            if(mm[xx][yy]=='r')
            {
                return shortest[now.x][now.y];
            }        
            vis[xx][yy]=1;
            shortest[xx][yy]=shortest[now.x][now.y]+1;    
            if(mm[xx][yy]=='x')
            {
                shortest[xx][yy]++;
            }
            node d;
            d.x=xx;d.y=yy;
            q.push(d);
        }
    }
    return -1;
}
int main()
{
    int i,j,k,ans;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(mm,0,sizeof(mm));
        memset(vis,0,sizeof(vis));
        memset(shortest,0,sizeof(shortest));
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                scanf(" %c",&mm[i][j]);
                if(mm[i][j]=='a')
                {
                    s1=i;s2=j;
                }
            }
        }
        ans=bfs();
        if(ans==-1) printf("Poor ANGEL has to stay in the prison all his life.\n");
        else printf("%d\n",ans);
    }
}

 优先队列:

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
int n,m,s1,s2;
char mm[505][505];
int vis[505][505];
int  dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct node
{
    int x,y,t;
    bool operator<(const node&xx)const
    {
        return t>xx.t;
    }
};
int bfs()
{
    priority_queue<node>q;
    vis[s1][s2]=1;
    node cur,now;
    node s;
    s.x=s1;s.y=s2;s.t=0;
    q.push(s);
    while(!q.empty())
    {
        cur=q.top();
        q.pop();
        for(int i=0;i<4;i++)
        {
            now.x=cur.x+dir[i][0];
            now.y=cur.y+dir[i][1];
            now.t=cur.t+1;
            if(now.x<0||now.x>=n||now.y<0||now.y>=m||vis[now.x][now.y]||mm[now.x][now.y]=='#') continue;
            if(mm[now.x][now.y]=='r') return now.t;
            if(mm[now.x][now.y]=='x')
            {
                now.t++;
            }
            vis[now.x][now.y]=1;
            q.push(now);
        }
    }
    return -1;
}
int main()
{
    int i,j,k,ans;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(mm,0,sizeof(mm));
        memset(vis,0,sizeof(vis));
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                scanf(" %c",&mm[i][j]);
                if(mm[i][j]=='a')
                {
                    s1=i;s2=j;
                }
            }
        }
        ans=bfs();
        if(ans==-1) printf("Poor ANGEL has to stay in the prison all his life.\n");
        else printf("%d\n",ans);
    }
}

 

posted on 2015-05-01 13:35  大树置林  阅读(193)  评论(0编辑  收藏  举报

导航