hdu 1728 bfs **

时间:2022-07-09 18:54:39

简单bfs,记录好状态即可

 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
using namespace std;
#define MOD 1000000007
const int INF=0x3f3f3f3f;
const double eps=1e-;
typedef long long ll;
#define cl(a) memset(a,0,sizeof(a))
#define ts printf("*****\n");
const int MAXN=;
int n,m,tt,k;
int dir[][]={,,-,,,,,-};
char s[MAXN][MAXN];
struct Node
{
int x,y;
int st;
int t;
}node[MAXN],st,ed;
bool vis[MAXN][MAXN][][];
bool bfs()
{
queue<Node> q;
cl(vis);
Node now,next;
st.t=;
st.st=;
vis[st.x][st.y][st.st][st.t]=;
q.push(st);
st.st=;
vis[st.x][st.y][st.st][st.t]=;
q.push(st);
st.st=;
vis[st.x][st.y][st.st][st.t]=;
q.push(st);
st.st=;
vis[st.x][st.y][st.st][st.t]=;
q.push(st);
while(!q.empty())
{
now=q.front();
q.pop();
if(now.x==ed.x&&now.y==ed.y)
{
return ;
}
for(int i=;i<;i++)
{
next.x=now.x+dir[i][];
next.y=now.y+dir[i][];
next.t=now.t;
next.st=i;
if(i!=now.st) next.t+=,next.st=i;
if(next.x>=&&next.x<n&&next.y>=&&next.y<m&&!vis[next.x][next.y][i][next.t]&&s[next.x][next.y]!='*'&&next.t<=k)
{
if(now.st==i)
{
q.push(next);
vis[next.x][next.y][i][next.t]=;
}
else
{
q.push(next);
vis[next.x][next.y][i][next.t]=;
}
}
}
}
return ;
}
int main()
{
int i,j;
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
#endif
scanf("%d",&tt);
while(tt--)
{
scanf("%d%d",&n,&m);
for(i=;i<n;i++)
{
scanf("%s",&s[i]);
}
scanf("%d%d%d%d%d",&k,&st.y,&st.x,&ed.y,&ed.x);
st.x-=;
st.y-=;
ed.x-=;
ed.y-=;
if(bfs())
{
printf("yes\n");
}
else printf("no\n");
}
}