/* 一开始我以为是简单的bfs,可是后来百度后发现要bfs + dp 好奇怪,我用hash标记了已经走过的点,就是wrong,还是纠结的 但是后来感觉hash是错的,以为从一个点到另一个点有很多走法, 结果是要去到达该点最小步数的点,标记了就不可以走了 */ #include<iostream> //2417041 2010-05-04 21:16:19 Accepted 1728 78MS 412K 2278 B C++ 悔惜晟 #include<queue> using namespace std;
int x1, y1, x2, y2; int n, m; int k; char map[105][105]; //bool hash[105][105]; int hush[105][105];//标记最小转弯的次数 int dir[4][2] ={ {0, 1}, {0, -1}, {1, 0}, {-1, 0} };//上 下 右 左 struct node { int x; int y; int dir;//标记方向 int st; //标记转弯的次数 };
int bfs(int si, int sj) { node P, N; int i; queue<node> q; N.x = si; N.y = sj; if(si == y2 && sj == x2) { cout<<"yes"<<endl; return 1; } q.push(N); N = q.front(); q.pop(); hush[N.x][N.y] = 0; //hash[si][sj] = true; for(i = 0; i < 4; i++) { P.x = N.x + dir[i][0]; P.y = N.y + dir[i][1]; P.dir = i; P.st = 0; if(P.x >= 1 && P.x <= n && P.y >= 1 && P.y <= m && map[P.x][P.y] != '*') { q.push(P); //hash[P.x][P.y] = true; hush[P.x][P.y] = 0; } } while(!q.empty()) { N = q.front(); /* if(N.x == y2 && N.y == x2) if(hush[N.x][N.y] <= k) { cout<<"yes"<<endl; return 1; } else { cout<<"no"<<endl; return 1; } */ q.pop(); for(i = 0; i < 4; i++) { P.x = N.x + dir[i][0]; P.y = N.y + dir[i][1]; P.dir = i; if(P.x >= 1 && P.x <= n && P.y >= 1 && P.y <= m && map[P.x][P.y] != '*')// && hush[N.x][N.y] <= k) { if(N.dir != P.dir ) { P.st = N.st + 1; if(P.st <= k && P.st <= hush[P.x][P.y]) { hush[P.x][P.y] = P.st; q.push(P); //hash[P.x][P.y] = true; } //hush[P.x][P.y] = hush[N.x][N.y] + 1; } else { P.st = N.st; if(P.st <= k && P.st <= hush[P.x][P.y]) { hush[P.x][P.y] = P.st; q.push(P); //hash[P.x][P.y] = true; } //hush[P.x][P.y] = hush[N.x][N.y]; } //q.push(P); //hash[P.x][P.y] = true;
} } } if(hush[y2][x2] <= k) { cout<<"yes"<<endl; return 1; } else { cout<<"no"<<endl; return 1; } //return 1; } int main() { int t; int i, j; cin>>t; while(t--) { cin>>n>>m; for(i = 1; i <= n; i++) for(j = 1; j <= m; j++) { cin>>map[i][j]; hush[i][j] = 12; } cin>>k>>x1>>y1>>x2>>y2; //memset(hash, false, sizeof(hash)); //memset(hush, 0, sizeof(hush)); bfs(y1, x1); } }