标签:
http://acm.hdu.edu.cn/showproblem.php?pid=1728
1 #include <cstdio> 2 #include <cstring> 3 #include <queue> 4 #include <algorithm> 5 using namespace std; 6 #define N 105 7 8 struct node 9 { 10 int x, y, dre, t; 11 node () {} 12 node (int x, int y, int dre, int t) : x(x), y(y), dre(dre), t(t) {} 13 }; 14 char maze[N][N]; 15 bool vis[N][N][15][4]; 16 int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}; 17 int sx, sy, ex, ey, k, n, m; 18 19 bool check(int x, int y) 20 { 21 if(0<=x&&x<m&&0<=y&&y<n&&maze[x][y]!=‘*‘) return true; 22 return false; 23 } 24 //&&!vis[x][y] 25 /* 26 2 27 5 5 28 ..... 29 .*.*. 30 ..... 31 .*.*. 32 ..... 33 1 1 1 3 4 34 */ 35 bool bfs() 36 { 37 queue<node> que; 38 memset(vis ,0 ,sizeof(vis)); 39 que.push(node(sx, sy, -1, 0)); 40 while(!que.empty()) { 41 node u = que.front(); que.pop(); 42 if(u.x == ex && u.y == ey && u.t <= k) return true; 43 if(u.t > k) continue; 44 if(u.dre != -1) { 45 if(vis[u.x][u.y][u.dre][u.t]) continue; 46 vis[u.x][u.y][u.dre][u.t] = 1; 47 } 48 for(int i = 0; i < 4; i++) { 49 int nx = u.x + dx[i], ny = u.y + dy[i]; 50 if(!check(nx, ny)) continue; 51 if(u.dre == -1) { 52 que.push(node(nx, ny, i, u.t)); 53 continue; 54 } 55 que.push(node(nx, ny, i, u.dre == i ? u.t : u.t + 1)); 56 } 57 } 58 return false; 59 } 60 61 int main() 62 { 63 int t; 64 scanf("%d", &t); 65 while(t--) { 66 scanf("%d%d", &m, &n); 67 for(int i = 0; i < m; i++) 68 scanf("%s", maze[i]); 69 scanf("%d%d%d%d%d", &k, &sy, &sx, &ey, &ex); 70 sy--, sx--, ey--, ex--; 71 if(maze[sx][sy] == ‘*‘ || maze[ex][ey] == ‘*‘) { 72 puts("no"); continue; 73 } 74 if(bfs()) puts("yes"); 75 else puts("no"); 76 } 77 return 0; 78 }
标签:
原文地址:http://www.cnblogs.com/fightfordream/p/5764453.html