hdu1728

来源:互联网 发布:java获取浏览器地址 编辑:程序博客网 时间:2024/06/03 18:52

bfs遍历:


#include<stdio.h>#include<queue>using namespace std;const int N = 102;struct p {int x, y;int tnum;}; int dir[4][2] ={{-1,0} ,{1,0} ,{0 ,-1} , {0 ,1}};int m, n;int sx ,sy ,ex,ey;int k = 0;char map[N][N];bool bfs() {queue<p> s;p p1;p1.x = sx; p1.y = sy;p1.tnum = -1;s.push(p1);while(s.size()) {p temp;temp = s.front();   s.pop();if (temp.tnum >= k)   continue;for(int i = 0; i < 4; ++i) {int nx = temp.x + dir[i][0];intny = temp.y + dir[i][1];p1.tnum = temp.tnum + 1;while(1) {if (nx == ex && ny == ey)   return true;if (nx < 0 || nx >= m || ny < 0 || ny >= n || map[nx][ny] == '*') break;p1.x = nx; p1.y = ny;map[temp.x][temp.y] = '*';s.push(p1);nx += dir[i][0];   ny += dir[i][1];} }} return false;} int main() {int t ;scanf("%d", &t);for (int i = 0; i < t; i++) {scanf("%d %d", &m, &n);for (int j = 0; j < m; j++) {getchar();for (int k = 0; k < n; k++) {scanf("%c", &map[j][k]);}}scanf("%d%d%d%d%d", &k, &sy, &sx, &ey, &ex);sx--; sy--;ex--;   ey--;if(bfs()) printf("yes\n");else   printf("no\n"); } return 0;}


0 0
原创粉丝点击