(2016弱校联盟十一专场10.3) B.Help the Princess!

题目链接

宽搜一下就行。

 

#include <iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int n,m;
char str[209][209];
int to[4][2]={1,0,-1,0,0,1,0,-1};
bool mp[209][209];
bool check(int x,int y)
{
    if(x<0||x>=n||y<0||y>=m)return 1;
    if(str[x][y]=='#'||mp[x][y]==1)return 1;
    return 0;
}
int prx,pry;
struct node
{
    int x,y,time;
};

bool bfs(int posx,int posy)
{
    int pt=-1,st=-1;
    queue<node>Q;
    node a,next;
    mp[posx][posy]=1;
    a.x=posx,a.y=posy,a.time=0;
    Q.push(a);
    while(!Q.empty())
    {
        a=Q.front();
        Q.pop();
        if(st==-1&&str[a.x][a.y]=='$') {
            st = a.time+1;
        }
        if(str[a.x][a.y]=='@'&&pt==-1) {
            pt = a.time+1;
        }
        if(pt!=-1&&st!=-1) {
            if(pt>=st) return 0;
            else return 1;
        }
        for(int i=0;i<4;i++)
        {
            next=a;
            next.x+=to[i][0];
            next.y+=to[i][1];
            if(check(next.x,next.y))continue;
            next.time=a.time+1;
            mp[next.x][next.y]=1;
            Q.push(next);
        }
    }
    if(pt!=-1)
        return 1;
    else return 0;
}
int main()
{
   // freopen("cin.txt","r",stdin);
    while(~scanf("%d%d",&n,&m))
    {
        int posx,posy;
        memset(mp,0,sizeof(mp));
        for(int i=0;i<n;i++)
        {
            scanf("%s",str[i]);
            for(int j=0;j<m;j++)
                if(str[i][j]=='%')
            {
                posx=i;
                posy=j;
            }
        }
        if(bfs(posx,posy))puts("Yes");
        else puts("No");
    }
    return 0;
}

 

posted @ 2016-12-21 22:14  Ritchie丶  阅读(243)  评论(0编辑  收藏  举报