双向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. Qt 学习之路:自定义事件

    尽管 Qt 已经提供了很多事件,但对于更加千变万化的需求来说,有限的事件都是不够的.例如,我要支持一种新的设备,这个设备提供一种崭新的交互方式,那么,这种事件如何处理呢?所以,允许创建自己的事件 类型 ...

  2. navigation的pushViewController卡顿问题

    问题:在ios中一个viewController中,添加下面代码: <pre name="code" class="objc">UIViewCont ...

  3. Android自定义Notification并没有那么简单

    背景 最近需要实现一个自定义Notification的功能.网上找了找代码,解决方案就是通过RemoteViews来实现.但是在实现过程中遇到不少问题,网上也没有很好的文章描述这些问题,所以在这里做个 ...

  4. Java中获得程序当前路径的4中方法

    Java中获得程序当前路径的4中方法: 在Application中: import java.util.*; public class TestUserDir { public static void ...

  5. Java 8 被动迭代式特性介绍(转自IBM)

    编程语言一般都需要提供一种机制用来遍历软件对象的集合,现代的编程语言支持更为复杂的数据结构,如列表.集合.映射和数组.遍历能力是通过公共方法提供,而内部细节都隐藏在类的私有部分,所以程序员不需要了解其 ...

  6. idea使用笔记

    常用快捷键 ctrl+shift+f12 编辑器全屏 win8下输入法不跟随 使用微软输入法即可 默认设置 之前创建maven工程 每次都要选择自己的版本,原来有个默认全局设置 创建maven模板工程 ...

  7. 调试exynos4412—ARM嵌入式Linux—LEDS/GPIO驱动之一

    /** ****************************************************************************** * @author    暴走的小 ...

  8. C# Activator.CreateInstance()

    C#在类工厂中动态创建类的实例,所使用的方法为: 1. Activator.CreateInstance (Type) 2. Activator.CreateInstance (Type, Objec ...

  9. 转-SecureCRT设置

    原帖地址:http://www.2cto.com/os/201410/341569.html 一.基本设置 1.修改设置 为了SecureCRT用起来更方便,需要做一些设置,需要修改的有如下几处: 1 ...

  10. 第一次用Github desktop(mac)提交代码遇到的问题

    1.新建代码仓库 2.生成密钥 ssh-keygen -C 'your@email.address' -t rsa 3.到根目录下的.ssh文件夹下找到id_rsa.pub文件,将里面的内容复制到下图 ...