</pre><pre name="code" class="html">
</pre><pre name="code" class="html">
在這個問題中涉及到人走後轉彎的問題,搜索的難度並不是很大,但是還是要好好理解。主要的優點在於用一個vis【】【】去記錄到達每個點後的已經走過的彎道
數,這個在裏面尤其要注意
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <iostream>
using namespace std;
#define maxn 110
char map[maxn][maxn];
int vis[maxn][maxn];
struct node{
int x;
int y;
int count;
int dir;
};
int di[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int x1,y1,x2,y2;
int n,m,k;
int BFS()
{
node st,en;
queue<node>q;
st.x=y1-1;st.y=x1-1;st.count=0;st.dir=-1;
vis[y1-1][x1-1]=0;
q.push(st);
while(!q.empty())
{
st=q.front();
q.pop();
// printf("%d %d\n",st.x,st.y);
if(st.x==y2-1&&st.y==x2-1)
return 1;
for(int i=0;i<=3;i++)
{
int y3=st.x+di[i][0];
int x3=st.y+di[i][1];
// printf("x3=%d x3=%d\n",x3,y3);
int count=st.count;
int dir=st.dir;
if(x3>=0&&x3<n&&y3>=0&&y3<m&&map[y3][x3]=='.')
{
if(dir!=i&&dir!=-1)
{
dir=i;
count++;
}
if(dir==-1)
dir=i;
// printf("%d\n",count);
if(count>k)
continue;
if(count<=vis[y3][x3])//當我們的廣度搜索已經超過了他他以前走過的彎道數的時候就沒有必要再把點放到隊列中
{
vis[y3][x3]=count;
en.x=y3;en.y=x3;en.count=count;en.dir=dir;
q.push(en);
}
}
}
}
return 0;
}
int main()
{
int T;
// freopen("in.txt","r",stdin);
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&m,&n);
for(int i=0;i<m;i++)
{
scanf("%s",&map[i]);
}
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
{
vis[i][j]=1000000;
}
scanf("%d%d%d%d%d",&k,&x1,&y1,&x2,&y2);
if( BFS()==1)
printf("yes\n");
else
printf("no\n");
}
return 0;
}