hdu 1242 &&zoj 1649 Rescue

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

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



现在hdu上看到这道题,直接BFS,没想到HDU的数据太弱,居然BFS过了,但在zoj上就不停地wa.

后来看别人的解题报告,才知道原来问题在guard上。

第一次的BFS直接在guard处加了2,而BFS队列中的元素的距离不超过1.

所以先把guard当成路,弹出队列时再加一,然后再入队,等于两次入队.


#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
int n,m,vis[210][210],count1;
char map[210][210];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
struct node
{
    int x,y,time;
};
void bfs(node vs)
{
    node vw,vn;
    int flag=0,ans,flag2=0;
    queue<node>q;
    vn.x=vs.x;
    vn.y=vs.y;
    vn.time=0;
    vis[vn.x][vn.y]=1;
    q.push(vn);
    while(!q.empty())
    {
        vn=q.front();
        q.pop();
        if(map[vn.x][vn.y]=='x')
        {
            map[vn.x][vn.y]='.';
            vn.time++;
            q.push(vn);
            continue;
        }
        if(map[vn.x][vn.y]=='r')
        {
            flag=1;
            break;
        }
        else
        {
                int i;
                for(i=0;i<4;i++)
                {
                    vw.x=vn.x+dx[i];
                    vw.y=vn.y+dy[i];
                    if(vw.x>=0&&vw.x<n&&vw.y>=0&&vw.y<m&&!vis[vw.x][vw.y]&&map[vw.x][vw.y]!='#')
                    {
                        vw.time=vn.time+1;
                        vis[vw.x][vw.y]=1;
                        q.push(vw);
                    }
                }
        }
    }
    if(flag)
    printf("%d\n",vn.time);
    else
    printf("Poor ANGEL has to stay in the prison all his life.\n");
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int i,j;
        node vs;
        for(i=0;i<n;i++)
        {
            scanf("%s",map[i]);
            for(j=0;j<m;j++)
            {
                if(map[i][j]=='a')
                 {
                     vs.x=i;vs.y=j;
                 }
            }
        }
        memset(vis,0,sizeof(vis));
          bfs(vs);
   
    }
    return 0;
}

法二:

/*
priority_queue
*/
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
int n,m,vis[210][210],count1;
char map[210][210];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
struct node
{
    int x,y,time;
};
bool operator < (node a,node b)
{
    return a.time>b.time;
}
void bfs(node vs)
{
    node vw,vn;
    int flag=0,ans,flag2=0;
    priority_queue<node>q;
    vn.x=vs.x;
    vn.y=vs.y;
    vn.time=0;
    vis[vn.x][vn.y]=1;
    q.push(vn);
    while(!q.empty())
    {
        vn=q.top();
        q.pop();
        if(map[vn.x][vn.y]=='r')
        {
            flag=1;
            break;
        }
        else
        {
                int i;
                for(i=0;i<4;i++)
                {
                    vw.x=vn.x+dx[i];
                    vw.y=vn.y+dy[i];
                    if(vw.x>=0&&vw.x<n&&vw.y>=0&&vw.y<m&&!vis[vw.x][vw.y]&&map[vw.x][vw.y]!='#')
                    {
                        vw.time=vn.time+1;
                        if(map[vw.x][vw.y]=='x')
                        vw.time++;
                        vis[vw.x][vw.y]=1;
                        q.push(vw);
                    }
                }
        }
    }
    if(flag)
    printf("%d\n",vn.time);
    else
    printf("Poor ANGEL has to stay in the prison all his life.\n");
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int i,j;
        node vs;
        for(i=0;i<n;i++)
        {
            scanf("%s",map[i]);
            for(j=0;j<m;j++)
            {
                if(map[i][j]=='a')
                 {
                     vs.x=i;vs.y=j;
                 }
            }
        }
        {
            memset(vis,0,sizeof(vis));
            bfs(vs);
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值