HDU 1728 逃离迷宫(BFS)

来源:互联网 发布:小照域名授权系统 编辑:程序博客网 时间:2024/06/05 02:41

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1728

跟上一篇文章一样,但是这次转弯次数是给出的,问是否可以到达终点。

坑点1:

5 5......*.*.......*.*......1 1 1 4 35 5......*.*.......*.*......1 1 1 3 4
坑点2:

终点和起点坐标一样所以直接输出yes???


代码如下:

#include<bits/stdc++.h>using namespace std;const int maxn = 105;char G[maxn][maxn];int dir[][2] = {-1, 0, 0, 1, 1, 0, 0, -1};int sx, sy, ex, ey, n, m, k, cnt[maxn][maxn];bool vst[maxn][maxn];struct node{int step;int x, y;};queue <node> q;bool Check(int x, int y, int step){if(x > 0 && x <= m && y > 0 && y <= n && G[x][y] != '*' && ((!vst[x][y]) || (cnt[x][y] >= step)))return 1;return 0;}void add(int x, int y, int d, int step){node tmp;x += dir[d][0];y += dir[d][1];while(Check(x, y, step)){vst[x][y] = 1;tmp.x = x;tmp.y = y;tmp.step = step;cnt[x][y] = step; q.push(tmp);x += dir[d][0];y += dir[d][1];}}bool bfs(){while(!q.empty())q.pop();node now, next;vst[sx][sy] = 1;for(int i = 0; i < 4; i++)add(sx, sy, i, 0);while(!q.empty()){now = q.front();q.pop();if(now.step > k)return 0;//cout << now.x << " " << now.y << " " << now.step << endl;if(now.x == ex && now.y == ey)return 1;next.step = now.step + 1;for(int i = 0; i < 4; i++){next.x = now.x + dir[i][0];next.y = now.y + dir[i][1];if(Check(next.x, next.y, next.step))add(now.x, now.y, i, next.step);}}return 0;}int main(){//ios::sync_with_stdio(false); int T;cin >> T;while(T--){memset(vst, 0, sizeof(vst));memset(cnt, 0, sizeof(cnt));scanf("%d%d", &m, &n);for(int i = 1; i <= m; i++)scanf("%s", &G[i][1]);//for(int i = 1; i <= m; i++)//{//for(int j = 1; j <= n; j++)//cout << G[i][j];//cout << endl;//}scanf("%d%d%d%d%d", &k, &sy, &sx, &ey, &ex);if(sx == ex && sy == ey)printf("yes\n");else if(bfs())printf("yes\n");elseprintf("no\n");}return 0;} 



0 0
原创粉丝点击