题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1728
解题思路: 一个方向搜到底,那么每次搜过的点肯定都是用转弯次数最少的路径走到的
///2014.7.10
///hdu1728
//2014-07-10 20:03:49 Accepted 1728 46MS 372K 2360 B G++
/*
*bfs
*一个方向搜到底,
*那么每次搜过的点肯定都是用转弯次数最少的路径走到的
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int t,m,n;
bool maze[110][110];
bool used[110][110];
int kk,x1,y1,x2,y2;
struct step{
int x,y;
int t;
};
int dir[4][2] = { {0,1},{1,0},{0,-1},{-1,0} };
int init(){
scanf("%d %d\n",&m,&n);
char temp;
for(int i=0 ; i<m ; i++){
for(int j=0 ; j<n ; j++){
scanf("%c",&temp);
if( temp == '.' )
maze[i][j] = true;
else
maze[i][j] = false;
}
scanf("%c",&temp);
}
scanf("%d %d %d %d %d",&kk,&y1,&x1,&y2,&x2);
x1--,y1--,x2--,y2--;
for(int i=0 ; i<m ; i++)
for(int j=0 ; j<n ; j++)
used[i][j] = false;
}
bool check(step a){
if( !maze[a.x][a.y] ) return false;
if( a.x>=0 && a.x<m && a.y>=0 && a.y<n ) return true;
return false;
}
bool bfs(){
queue<step> que;
step p;
p.x = x1,p.y = y1,p.t = -1;
used[p.x][p.y] = true;
que.push(p);
while( !que.empty() ){
p = que.front();
que.pop();
if( p.x==x2 && p.y==y2 && p.t<=kk ) return true;
if( p.t > kk) continue;
for(int i=0 ; i<4 ; i++){
step q;
q.x = p.x + dir[i][0];
q.y = p.y + dir[i][1];
q.t = p.t + 1;
while( check(q) ){
if( !used[q.x][q.y] ){
used[q.x][q.y] = true;
que.push(q);
}
q.x += dir[i][0];
q.y += dir[i][1];
}
}
}
return false;
}
///测试 init() 函数
// void out(){
// cout<<m<<" "<<n<<endl;
// for(int i=0 ; i<m ; i++){
// for(int j=0 ; j<n ; j++){
// if( maze[i][j] )
// cout<<'.';
// else
// cout<<'*';
// }
// cout<<endl;
// }
// cout<<kk<<" "<<x1<<" "<<y1<<" "<<x2<<" "<<y2<<endl;
// }
int main(){
// freopen("in","r",stdin);
// freopen("out","w",stdout);
scanf("%d",&t);
while( t-- ){
init();
// out();
if( bfs() )
printf("yes\n");
else
printf("no\n");
}
return 0;
}