经典的双向BFS, 可以使得世间复杂度大大降低。
因为男生和女生每秒走的步数不一样,所以我们可以利用BFS的特点,以每一层作为一个单位来BFS
细节参见代码:
~~~
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 1000000000;
const int maxn = 1000;
int dx[] = {0,1,0,-1};
int dy[] = {1,0,-1,0};
int T,n,m,step;
bool vis[maxn][maxn][3];
char s[maxn][maxn];
struct node {
int r, c;
node(int rr=0, int cc=0):r(rr), c(cc) {}
}s1,s2,G[2];
bool yougui(int x, int y) {
if((abs(G[0].r-x)+abs(G[0].c-y)) <= 2*step) return true;
if((abs(G[1].r-x)+abs(G[1].c-y)) <= 2*step) return true;
return false;
}
queue<node> q[2];
bool bfs(int id) {
int cnt = q[id].size();
while(cnt--) {
node u = q[id].front(); q[id].pop();
if(yougui(u.r,u.c)) continue;
for(int i=0;i<4;i++){
int x = u.r + dx[i], y = u.c + dy[i];
if(x < 1 || x > n || y < 1 || y > m || s[x][y] == 'X') continue;
if(yougui(x,y)) continue;
if(!vis[x][y][id]) {
if(vis[x][y][(id^1)]) return true;
vis[x][y][id] = true;
q[id].push(node(x,y));
}
}
}
return false;
}
int hehe() {
memset(vis,false,sizeof(vis));
step = 0;
while(!q[0].empty()) q[0].pop();
while(!q[1].empty()) q[1].pop();
q[0].push(node(s2.r,s2.c));
q[1].push(node(s1.r,s1.c));
vis[s2.r][s2.c][0] = vis[s1.r][s1.c][1] = true;
while(!q[0].empty() || !q[1].empty()){
step++;
if(bfs(0)) return step;
if(bfs(0)) return step;
if(bfs(0)) return step;
if(bfs(1)) return step;
}
return -1;
}
int main() {
scanf("%d",&T);
while(T--) {
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%s",s[i]+1);
int c = 0;
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
if(s[i][j] == 'G') s1 = node(i,j);
else if(s[i][j] == 'M') s2 = node(i,j);
else if(s[i][j] == 'Z') G[c++] = node(i,j);
}
}
printf("%d\n",hehe());
}
return 0;
}
~~~
- 前言
- 1608 - Non-boring sequences(折半递归。。暂且这么叫吧)
- 11491 - Erasing and Winning(贪心)
- 1619 - Feel Good(高效算法-利用数据结构优化-优先队列)
- hdu-4127 Flood-it!(IDA*算法)
- UESTC 1132 酱神赏花 (用数据结构优化DP)
- HDU 2874 Connections between cities(LCA离线算法)
- Codeforces Round #317 A. Lengthening Sticks(组合+容斥)
- HDU 3085 Nightmare Ⅱ(双向BFS)
- HDU 5592 ZYB&#39;s Premutation(二分+树状数组)
- Codeforces Round #320 (Div. 1) C. Weakness and Poorness(三分)
- HDU 5212 Code(容斥)
- HDU 5596 GTW likes gt(multiset)
- FZU 2159 WuYou(贪心)
- HDU 3450 Counting Sequences(DP + 树状数组)
- HDU 5493 Queue(二分+树状数组)
- HDU 1166 敌兵布阵(线段树版)
- HDU 1394 Minimum Inversion Number(树状数组||线段树)
- HDU 2795 Billboard(线段树)
- POJ 2828 Buy Tickets(树状数组)
- 《完全版线段树》- NotOnlySuccess
- POJ 2886 Who Gets the Most Candies?(树状数组+二分)
- HDU 1698 Just a Hook(线段树区间修改)
- POJ 3468 A Simple Problem with Integers(线段树|区间加减&amp;&amp;区间求和)
- POJ 2528 Mayor&#39;s posters(线段树区间修改+离散化)
- HDU 5606 tree(并查集)
- POJ 3734 Blocks(矩阵优化+DP)
- POJ 3233 Matrix Power Series(矩阵优化)
- HDU 5607 graph(矩阵优化+概率DP)
- POJ 2777 Count Color(线段树区间修改+位运算)
- POJ 1436 Horizontally Visible Segments(线段树区间修改)
- UVA 1513 - Movie collection(树状数组)
- UVA 1232 - SKYLINE(线段树区间更新)
- 11525 - Permutation(二分+树状数组)
- 11402 - Ahoy, Pirates!(线段树区间更新(标记重叠的处理))
- Educational Codeforces Round 6 E. New Year Tree(DFS序+线段树)