[POJ2157]Maze(DFS)
生活随笔
收集整理的這篇文章主要介紹了
[POJ2157]Maze(DFS)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:http://poj.org/problem?id=2157
題意:給一張地圖,地圖里有門和鑰匙,想要開門必須集齊所有鑰匙。給定起點和終點,問從起點出發能否到達終點。
爆搜floodfill,每填一次考慮是否到達終點,并且把門都開開,鑰匙都拿上,再進行下一次,其實可以不重復200次,備份一下上一次的狀態圖,再比對floodfill后的與當前的是否不同即可。
1 #include <algorithm> 2 #include <iostream> 3 #include <iomanip> 4 #include <cstring> 5 #include <climits> 6 #include <complex> 7 #include <cassert> 8 #include <cstdio> 9 #include <bitset> 10 #include <vector> 11 #include <deque> 12 #include <queue> 13 #include <stack> 14 #include <ctime> 15 #include <set> 16 #include <map> 17 #include <cmath> 18 using namespace std; 19 20 const int maxn = 22; 21 const int dx[5] = {1, -1, 0, 0}; 22 const int dy[5] = {0, 0, 1, -1}; 23 int n, m; 24 int sx, sy; 25 int kn[6], ck[6]; 26 char G[maxn][maxn], H[maxn][maxn]; 27 bool vis[maxn][maxn]; 28 bool done; 29 30 bool ok(int x, int y) { 31 return x >= 0 && y >= 0 && x < n && y < m; 32 } 33 34 void dfs(int x, int y) { 35 if(done) return; 36 if(vis[x][y]) return; 37 vis[x][y] = 1; 38 for(int i = 0; i < 4; i++) { 39 int xx = x + dx[i]; 40 int yy = y + dy[i]; 41 if(!ok(xx, yy)) continue; 42 if(G[xx][yy] == 'X') continue; 43 if(vis[xx][yy]) continue; 44 if(G[xx][yy] >= 'A' && G[xx][yy] <= 'E') { 45 if(ck[G[xx][yy]-'A'] == kn[G[xx][yy]-'A']) { 46 G[xx][yy] = '.'; 47 dfs(xx, yy); 48 } 49 } 50 else if(G[xx][yy] >= 'a' && G[xx][yy] <= 'e') { 51 ck[G[xx][yy]-'a']++; 52 G[xx][yy] = '.'; 53 dfs(xx, yy); 54 } 55 else if(G[xx][yy] == 'G') { 56 done = 1; 57 return; 58 } 59 else if(G[xx][yy] == '.') dfs(xx, yy); 60 } 61 } 62 63 int main() { 64 // freopen("in", "r", stdin); 65 while(~scanf("%d%d",&n,&m)&&n+m) { 66 memset(kn, 0, sizeof(kn)); 67 memset(ck, 0, sizeof(ck)); 68 memset(G, 0, sizeof(G)); 69 memset(vis, 0, sizeof(vis)); 70 for(int i = 0; i < n; i++) scanf("%s", G[i]); 71 for(int i = 0; i < n; i++) { 72 for(int j = 0; j < m; j++) { 73 if(G[i][j] >= 'a' && G[i][j] <= 'e') kn[G[i][j]-'a']++; 74 if(G[i][j] == 'S') sx = i, sy = j; 75 } 76 } 77 done = 0; 78 for(int _ = 0; _ < 200; _++) { 79 memset(vis, 0, sizeof(vis)); 80 dfs(sx, sy); 81 if(done) break; 82 } 83 if(done) puts("YES"); 84 else puts("NO"); 85 } 86 return 0; 87 }?
轉載于:https://www.cnblogs.com/kirai/p/6437978.html
總結
以上是生活随笔為你收集整理的[POJ2157]Maze(DFS)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringMVC redirect中文
- 下一篇: 梦到奇怪的动物是什么意思