双向BFS。注意,任何一个点出队后,首先需要考虑ghost。

 /* 3085 */
#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
using namespace std; #define MAXN 805 typedef struct node_t {
int x, y;
node_t() {}
node_t(int xx, int yy) {
x = xx; y = yy;
}
} node_t; int n, m;
node_t z[], beg0, beg1;
char map[MAXN][MAXN];
bool visit0[MAXN][MAXN];
bool visit1[MAXN][MAXN];
int dir[][] = {
-,,,,,-,,
}; inline bool check(int x, int y) {
return x< || x>=n || y< || y>=m;
} inline bool meetZ(int x, int y, int t) {
return abs(x-z[].x)+abs(y-z[].y)<=t+t || abs(x-z[].x)+abs(y-z[].y)<=t+t;
} int bfs() {
int size;
int x, y, t = ;
int i, j, k, r;
node_t nd;
queue<node_t> Q0;
queue<node_t> Q1; memset(visit0, false, sizeof(visit0));
memset(visit1, false, sizeof(visit1));
visit0[beg0.x][beg0.y] = true;
visit1[beg1.x][beg1.y] = true;
Q0.push(beg0);
Q1.push(beg1); while (!Q0.empty() || !Q1.empty()) {
++t;
// Q0
r = ;
while (r--) {
size = Q0.size();
while (size--) {
nd = Q0.front();
Q0.pop();
if (meetZ(nd.x, nd.y, t))
continue;
for (i=; i<; ++i) {
x = nd.x + dir[i][];
y = nd.y + dir[i][];
if (check(x, y) || visit0[x][y])
continue;
if (map[x][y]=='X' || meetZ(x, y, t))
continue;
if (visit1[x][y])
return t;
visit0[x][y] = true;
Q0.push(node_t(x, y));
}
}
}
// Q1
size = Q1.size();
while (size--) {
nd = Q1.front();
Q1.pop();
if (meetZ(nd.x, nd.y, t))
continue;
for (i=; i<; ++i) {
x = nd.x + dir[i][];
y = nd.y + dir[i][];
if (check(x, y) || visit1[x][y])
continue;
if (map[x][y]=='X' || meetZ(x, y, t))
continue;
if (visit0[x][y])
return t;
visit1[x][y] = true;
Q1.push(node_t(x, y));
}
}
} return -;
} int main() {
int t;
int i, j, k; #ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif scanf("%d", &t);
while (t--) {
scanf("%d %d", &n, &m);
k = ;
for (i=; i<n; ++i) {
scanf("%s", map[i]);
for (j=; j<m; ++j) {
if (map[i][j] == 'Z') {
z[k].x = i;
z[k].y = j;
++k;
} else if (map[i][j] == 'M') {
beg0.x = i;
beg0.y = j;
} else if (map[i][j] == 'G') {
beg1.x = i;
beg1.y = j;
}
}
}
k = bfs();
printf("%d\n", k);
} return ;
}

【HDOJ】3085 Nightmare Ⅱ的更多相关文章

  1. 【HDOJ】1072 Nightmare

    bfs,使用ttl进行剪枝. #include <iostream> #include <cstdio> #include <cstring> #include & ...

  2. 【HDOJ】4729 An Easy Problem for Elfness

    其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...

  3. 【HDOJ】【3506】Monkey Party

    DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...

  4. 【HDOJ】【3516】Tree Construction

    DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...

  5. 【HDOJ】【3480】Division

    DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...

  6. 【HDOJ】【2829】Lawrence

    DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...

  7. 【HDOJ】【3415】Max Sum of Max-K-sub-sequence

    DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...

  8. 【HDOJ】【3530】Subsequence

    DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...

  9. 【HDOJ】【3068】最长回文

    Manacher算法 Manacher模板题…… //HDOJ 3068 #include<cstdio> #include<cstring> #include<cstd ...

随机推荐

  1. GDB踪函数的完整调用过程 及原理

    http://www.lenky.info/archives/2013/02/2202 Breakpoint , .so. (gdb) bt # .so. # .so. # .so. # .so. # ...

  2. VMware虚拟机中调整Linux分区大小——使用gparted

    虚拟机分配了50G大小的空间,最近发现不够用,于是将扩展一下分区的大小,查了几种方法都不是很好,后来借助了gparted分区空间完成了,这个工具简单,方便,下面就简单的介绍一下.扩展分区主要要分为两步 ...

  3. java文件处理工具类

    import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedRead ...

  4. Java基础知识强化之集合框架笔记36:List练习之键盘录入多个数据在控制台输出最大值

    1. 键盘录入多个数据,以0结束,要求在控制台输出这多个数据中的最大值 分析: •  创建键盘录入数据对象 •  键盘录入多个数据,我们不知道多少个,所以用集合存储 •  以0结束,这个简单,只要键盘 ...

  5. Quartz Features

    Runtime Environments Quartz can run embedded within another free standing application Quartz can be ...

  6. 【转】纯 CSS 实现高度与宽度成比例的效果

    先来演示页面:Demo; 转的内容: 最近在做一个产品列表页面,布局如右图所示.页面中有若干个 item,其中每个 item 都向左浮动,并包含在自适应浏览器窗口宽度的父元素中. item 元素的 C ...

  7. HTML5 文件域+FileReader 分段读取文件并上传(八)-WebSocket

    一.同时上传多个文件处理 HTML: <div class="container"> <div class="panel panel-default&q ...

  8. iOS9 application:application openURL: sourceApplication: annotation: 方法不执行

    - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url NS_DEPRECATED_IOS(2_0, 9 ...

  9. SGU 148.B-Station

    时间限制:0.25s 空间限制:4M  题目 在离著名的国家Berland不远的地方,有一个水下工作站.这个工作站有N层.已知:是第层装有Wi的水,最多可以容纳Li的水,恐怖分子炸毁第i的代价是Pi. ...

  10. 在CentOS 7中轻松安装Atomic应用(atomicapp)

    sudo yum install docker atomic etcd kubernetes sudo systemctl enable docker.service sudo systemctl s ...