CSUST选拔赛题解之-Problem H: 逃出监狱
生活随笔
收集整理的這篇文章主要介紹了
CSUST选拔赛题解之-Problem H: 逃出监狱
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Problem H: 逃出監獄
Time Limit: 1 Sec??Memory Limit: 128 MBSubmit: 116??Solved: 42
[Submit][Status][Web Board]
Description
給你一個n*m的圖,地圖上'.'代表可以走的地方,而'#'代表障礙物不能走,'A'代表小偷,'B'代表警察,'E'代表出口。每個位置可以向(上,下,左,
右)四個方向走一格,花費一個單位時間,現在給出小偷,警察和出口所在
位置,警察為了捉住小偷會先到出口的位置守株待兔,如果警察在小偷之前
或同時到達出口,或者小偷到達不了出口,輸出"No",否則輸出"Yes"。
Input
第一行一個整數T(T<=50),代表數據的組數接下來一行n,m(n<=500,m<=500),代表圖的行和列
接下來n行,每行為長度為m的字符串,組成一張圖
Output
每行輸出"Yes"或"No"Sample Input
2 5 5 ....A ##... E#..B ##... ..... 5 5 A.... ..... B.... ...E. .....Sample Output
No NoHINT
解題思路:bfs模板題,直接上代碼吧。 AC代碼: #include<stdio.h> #include<string.h> #include<iostream> #include<string> #include<algorithm> #include<set> #include<queue> #define N 50000using namespace std;int _map[510][510], vis[510][510]; int n, m; int dirx[4] = {0,-1,0,1}; int diry[4] = {1,0,-1,0}; int xx, xy, jx, jy, endx, endy, x, j; char str[510];struct node {int x;int y;int lenth; };bool can_go(int x, int y) {if(x < 0 || y < 0 || x >= n || y >= m)return false;if(_map[x][y])return false;return true; }int bfs(int x, int y) {memset(vis, 0, sizeof(vis));queue<node> q;node start;start.x = x;start.y = y;start.lenth = 0;q.push(start);while(!q.empty()){node cur;cur = q.front();if(cur.x == endx && cur.y == endy){return cur.lenth;}q.pop();for(int i = 0; i < 4; i ++){node next;next.x = cur.x + dirx[i];next.y = cur.y + diry[i];next.lenth = cur.lenth + 1;if(can_go(next.x, next.y)){if(vis[next.x][next.y]) continue;vis[next.x][next.y] = 1;q.push(next);}}}return -1; }int main() {int T;scanf("%d", &T);while(T--){scanf("%d%d", &n, &m);memset(_map, 0, sizeof(_map));for(int i = 0; i < n; i ++){scanf("%s", str);for(int j = 0; j < m; j ++){if(str[j] == '#'){_map[i][j] = 1;}if(str[j] == 'A'){xx = i;xy = j;}if(str[j] == 'B'){jx = i;jy = j;}if(str[j] == 'E'){endx = i;endy = j;}}}x = bfs(xx, xy);j = bfs(jx, jy);if(x == -1){printf("No\n");continue;}if(j <= x) printf("No\n");else printf("Yes\n");}return 0; }總結
以上是生活随笔為你收集整理的CSUST选拔赛题解之-Problem H: 逃出监狱的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【bzoj 1340】 Escap
- 下一篇: 【BOI2007】逃跑问题 (BSOI2