标签:des c style class blog code
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 #include <queue> 6 using namespace std; 7 8 const int MAXN = 110; 9 10 struct Node { 11 int f, x, y; 12 Node() {} 13 Node(int f, int x, int y): 14 f(f), x(x), y(y) {} 15 }; 16 17 int fx[] = {-1, 0, 1, 0}; 18 int fy[] = {0, 1, 0, -1}; 19 20 int dis[4][MAXN][MAXN]; 21 char mat[MAXN][MAXN]; 22 int T, n, m; 23 int k, x1, y1, x2, y2; 24 25 bool solve() { 26 memset(dis, 0x3f, sizeof(dis)); 27 queue<Node>* pre = new queue<Node>(); 28 queue<Node>* cur = new queue<Node>(); 29 int step = 0; 30 for(int i = 0; i < 4; ++i) pre->push(Node(i, x1, y1)); 31 for(int i = 0; i < 4; ++i) dis[i][x1][y1] = 0; 32 while(step <= k) { 33 Node t = pre->front(); pre->pop(); 34 if(dis[t.f][t.x][t.y] != step) continue; 35 if(t.x == x2 && t.y == y2) return true; 36 for(int i = 0; i < 4; ++i) { 37 if((t.f + 2) % 4 == i) continue; 38 int x = t.x + fx[i], y = t.y + fy[i], d = dis[t.f][t.x][t.y] + (t.f != i); 39 if(mat[x][y] == ‘.‘ && d < dis[i][x][y]) { 40 dis[i][x][y] = d; 41 if(t.f == i) pre->push(Node(i, x, y)); 42 else cur->push(Node(i, x, y)); 43 } 44 } 45 if(pre->empty()) { 46 step++; 47 if(cur->empty()) break; 48 swap(pre, cur); 49 } 50 } 51 return false; 52 } 53 54 int main() { 55 scanf("%d", &T); 56 while(T--) { 57 scanf("%d%d", &n, &m); 58 memset(mat, 0, sizeof(mat)); 59 for(int i = 1; i <= n; ++i) scanf("%s", &mat[i][1]); 60 scanf("%d%d%d%d%d", &k, &y1, &x1, &y2, &x2); 61 puts(solve() ? "yes" : "no"); 62 } 63 }
HDU 1728 逃离迷宫(BFS),布布扣,bubuko.com
标签:des c style class blog code
原文地址:http://www.cnblogs.com/oyking/p/3756208.html