HDU3085 Nightmare Ⅱ
Posted veasky
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU3085 Nightmare Ⅱ相关的知识,希望对你有一定的参考价值。
题目:
Last night, little erriyue had a horrible nightmare. He dreamed that he and his girl friend were trapped in a big maze separately. More terribly, there are two ghosts in the maze. They will kill the people. Now little erriyue wants to know if he could find his girl friend before the ghosts find them.
You may suppose that little erriyue and his girl friend can move in 4 directions. In each second, little erriyue can move 3 steps and his girl friend can move 1 step. The ghosts are evil, every second they will divide into several parts to occupy the grids within 2 steps to them until they occupy the whole maze. You can suppose that at every second the ghosts divide firstly then the little erriyue and his girl friend start to move, and if little erriyue or his girl friend arrive at a grid with a ghost, they will die.
Note: the new ghosts also can devide as the original ghost.
输入:
The input starts with an integer T, means the number of test cases.
Each test case starts with a line contains two integers n and m, means the size of the maze. (1<n, m<800)
The next n lines describe the maze. Each line contains m characters. The characters may be:
‘.’ denotes an empty place, all can walk on.
‘X’ denotes a wall, only people can’t walk on.
‘M’ denotes little erriyue
‘G’ denotes the girl friend.
‘Z’ denotes the ghosts.
It is guaranteed that will contain exactly one letter M, one letter G and two letters Z.
输出:
Output a single integer S in one line, denotes erriyue and his girlfriend will meet in the minimum time S if they can meet successfully, or output -1 denotes they failed to meet.
样例:
分析:cin cout加std::ios::sync_with_stdio(false);都过不去(/‵Д′)/~ ╧╧,换scanf就ac了?!
双向BFS,预处理鬼什么时候覆盖该位置(貌似大多数人都是用曼哈顿距离?互相抄的估计。。。明明预处理更明显想到啊)
记住鬼在人前行动,对照样例3理解这句话
1 #include<iostream> 2 #include<sstream> 3 #include<cstdio> 4 #include<cstdlib> 5 #include<string> 6 #include<cstring> 7 #include<algorithm> 8 #include<functional> 9 #include<iomanip> 10 #include<numeric> 11 #include<cmath> 12 #include<queue> 13 #include<vector> 14 #include<set> 15 #include<cctype> 16 #define PI acos(-1.0) 17 const int INF = 0x3f3f3f3f; 18 const int NINF = -INF - 1; 19 typedef long long ll; 20 using namespace std; 21 typedef pair<int, int> P; 22 char maze[805][805]; 23 int n, m; 24 int mx, my, gx, gy; 25 P z[2]; 26 int timz[805][805]; 27 int visz[805][805]; 28 int dzx[12] = 1, 2, 0, 0, -1, -2, 0, 0, 1, 1, -1, -1, dzy[12] = 0, 0, 1, 2, 0, 0, -1, -2, 1, -1, 1, -1; 29 void ini() 30 31 for (int i = 0; i < n; ++i) 32 33 for (int j = 0; j < m; ++j) 34 timz[i][j] = INF; 35 36 for (int i = 0; i < n; ++i) 37 38 for (int j = 0; j < m; ++j) 39 visz[i][j] = 0; 40 41 queue<P> p; 42 timz[z[0].first][z[0].second] = 0; 43 timz[z[1].first][z[1].second] = 0; 44 visz[z[0].first][z[0].second] = 1; 45 visz[z[1].first][z[1].second] = 1; 46 p.push(z[0]); 47 p.push(z[1]); 48 while (p.size()) 49 50 P tmp = p.front(); 51 p.pop(); 52 for (int i = 0; i < 12; ++i) 53 54 int nx = tmp.first + dzx[i], ny = tmp.second + dzy[i]; 55 if (nx >= 0 && nx < n && ny >= 0 && ny < m && !visz[nx][ny]) 56 57 visz[nx][ny] = 1; 58 timz[nx][ny] = timz[tmp.first][tmp.second] + 1; 59 p.push(P(nx, ny)); 60 61 62 63 64 int dx[4] = 1, 0, -1, 0, dy[4] = 0, 1, 0, -1; 65 int vis[2][805][805]; 66 queue<P> q[2]; 67 int step; 68 int bfs(int flag) 69 70 int num = q[flag].size(); 71 while (num--) 72 73 P tmp = q[flag].front(); 74 q[flag].pop(); 75 if (step >= timz[tmp.first][tmp.second]) continue; 76 for (int i = 0; i < 4; ++i) 77 78 int nx = tmp.first + dx[i], ny = tmp.second + dy[i]; 79 if (nx < 0 || nx >= n || ny < 0 || ny >= m || vis[flag][nx][ny] || maze[nx][ny] == ‘X‘ || step >= timz[nx][ny]) 80 continue; 81 if (vis[1 - flag][nx][ny]) 82 83 printf("%d\\n", step); 84 return 1; 85 86 vis[flag][nx][ny] = 1; 87 q[flag].push(P(nx, ny)); 88 89 90 return 0; 91 92 void solve() 93 94 for (int i = 0; i < 2; ++i) 95 96 while (q[i].size()) q[i].pop(); 97 98 memset(vis[0], 0, sizeof(vis[0])); 99 memset(vis[1], 0, sizeof(vis[1])); 100 vis[0][mx][my] = 1; 101 vis[1][gx][gy] = 1; 102 q[0].push(P(mx, my)); 103 q[1].push(P(gx, gy)); 104 step = 0; 105 while (q[0].size() || q[1].size()) 106 107 step++; 108 for (int i = 0; i < 3; ++i) 109 if (bfs(0)) return; 110 if (bfs(1)) return; 111 112 printf("-1\\n"); 113 114 int main() 115 116 int T; 117 scanf("%d", &T); 118 while (T--) 119 120 scanf("%d%d",&n,&m); 121 int num = 0; 122 for (int i = 0; i < n; ++i) 123 124 scanf("%s",maze[i]); 125 for (int j = 0; j < m; ++j) 126 127 if (maze[i][j] == ‘M‘) mx = i, my = j; 128 if (maze[i][j] == ‘G‘) gx = i, gy = j; 129 if (maze[i][j] == ‘Z‘) z[num].first = i, z[num++].second = j; 130 131 132 ini(); 133 solve(); 134 135 return 0; 136
以上是关于HDU3085 Nightmare Ⅱ的主要内容,如果未能解决你的问题,请参考以下文章