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

时间:2023-03-09 06:13:25
(2016弱校联盟十一专场10.3) 	B.Help the Princess!

题目链接

宽搜一下就行。

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