标签:des style blog http color io os ar java
1 7 SB....T 1 7 SB..#.T 7 11 ########### #T##......# #.#.#..#### #....B....# #.######..# #.....S...# ########### 8 4 .... .##. .#.. .#.. .#.B .##S .... ###T 0 0
Maze #1 EEEEE Maze #2 Impossible. Maze #3 eennwwWWWWeeeeeesswwwwwwwnNN Maze #4 swwwnnnnnneeesssSSS
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 30; 18 struct stats{ 19 int px,py,bx,by; 20 string path; 21 }; 22 struct node{ 23 int x,y; 24 string path; 25 }; 26 char mp[maxn][maxn]; 27 int r,c,box_x,box_y,pson_x,pson_y; 28 string ans; 29 const int dir[4][2] = {0,-1,0,1,-1,0,1,0}; 30 const char dc[4] = {‘W‘,‘E‘,‘N‘,‘S‘}; 31 const char dc2[4] = {‘w‘,‘e‘,‘n‘,‘s‘}; 32 bool check(int x,int y){ 33 return mp[x][y] != ‘#‘; 34 } 35 bool bfs2(int nx,int ny,int tx,int ty,int kx,int ky,string &pans){ 36 queue<node>q; 37 bool vis[maxn][maxn] = {false}; 38 vis[nx][ny] = vis[kx][ky] = true; 39 node now,tmp; 40 now.x = nx; 41 now.y = ny; 42 now.path = ""; 43 q.push(now); 44 while(!q.empty()){ 45 now = q.front(); 46 q.pop(); 47 if(now.x == tx && now.y == ty){ 48 pans = now.path; 49 return true; 50 } 51 for(int i = 0; i < 4; i++){ 52 int zx = now.x + dir[i][0]; 53 int zy = now.y + dir[i][1]; 54 if(check(zx,zy)&&!vis[zx][zy]){ 55 vis[zx][zy] = true; 56 tmp.x = zx; 57 tmp.y = zy; 58 tmp.path = now.path + dc2[i]; 59 q.push(tmp); 60 } 61 } 62 } 63 return false; 64 } 65 bool bfs(){ 66 queue<stats>q; 67 bool vis[maxn][maxn] = {false}; 68 vis[box_x][box_y] = true; 69 stats tmp,now; 70 now.px = pson_x; 71 now.py = pson_y; 72 now.bx = box_x; 73 now.by = box_y; 74 now.path = ""; 75 q.push(now); 76 while(!q.empty()){ 77 now = q.front(); 78 q.pop(); 79 for(int i = 0; i < 4; i++){ 80 int nx = now.bx + dir[i][0]; 81 int ny = now.by + dir[i][1]; 82 int tx = now.bx - dir[i][0]; 83 int ty = now.by - dir[i][1]; 84 string pans = ""; 85 if(check(nx,ny)&&check(tx,ty)&&!vis[nx][ny]){ 86 if(bfs2(now.px,now.py,tx,ty,now.bx,now.by,pans)){ 87 vis[nx][ny] = true; 88 tmp.px = now.bx; 89 tmp.py = now.by; 90 tmp.bx = nx; 91 tmp.by = ny; 92 tmp.path = now.path + pans + dc[i]; 93 if(mp[nx][ny] == ‘T‘){ 94 ans = tmp.path; 95 return true; 96 } 97 q.push(tmp); 98 } 99 } 100 } 101 } 102 return false; 103 } 104 int main(){ 105 int cs = 1; 106 while(~scanf("%d %d",&r,&c) && r + c){ 107 memset(mp,‘#‘,sizeof(mp)); 108 getchar(); 109 for(int i = 1; i <= r; i++){ 110 for(int j = 1; j <= c; j++){ 111 mp[i][j] = getchar(); 112 if(mp[i][j] == ‘B‘){ 113 box_x = i; 114 box_y = j; 115 } 116 if(mp[i][j] == ‘S‘){ 117 pson_x = i; 118 pson_y = j; 119 } 120 } 121 getchar(); 122 } 123 printf("Maze #%d\n", cs++); 124 if(bfs()) cout<<ans<<endl; 125 else puts("Impossible."); 126 puts(""); 127 } 128 return 0; 129 }
标签:des style blog http color io os ar java
原文地址:http://www.cnblogs.com/crackpotisback/p/4003663.html