双向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. WebLogic: The Definitive Guide examined WebLogic's security mechanisms--reference

    reference from: http://www.onjava.com/pub/a/onjava/excerpt/weblogic_chap17/index1.html?page=1 ...... ...

  2. iOS-CGContextRef画各种图形例子

    iOS-CGContextRef画各种图形例子 绘制效果图 绘制代码 - (void)drawRect:(CGRect)rect { //一个不透明类型的Quartz 2D绘画环境,相当于一个画布,你 ...

  3. 亲测PHpnow 安装环境

    出现问题1: "C:\Windows\system32\7z.exe"' 不是 或批处理文件. 找不到 C:\Windows\system32\7z.exe------------ ...

  4. [转] 在React Native中使用ART

    http://bbs.reactnative.cn/topic/306/%E5%9C%A8react-native%E4%B8%AD%E4%BD%BF%E7%94%A8art 前半个月捣腾了一下Rea ...

  5. 常用的 css 命名规则

    头:header 内容:content/container 尾:footer 导航:nav 侧栏:sidebar 栏目:column 页面外围控制整体布局宽度:wrapper 左右中:left rig ...

  6. (转载)记录函数 getStyle() 获取元素 CSS 样式

    设置元素(element)的css属性值可以用element的style属性,例如要将element的背景色设置为黑色,可以这么做: element.style.backgroundColor = ' ...

  7. C#日期转换类

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Te ...

  8. oracle - 创建数据库

    在服务器端的oracle,用户有点差异,当 我通过 'sqlplus / as sysdba' 命令登陆后,并不能创建数据库,sysdba拥有最高的系统权限,登陆后是 sys,以as sysdba登录 ...

  9. JS中escape 方法和C#中的对应

    在项目中遇到js中escape过的json字符串,需要在C#中对应模拟编码,记得原来遇到过这个问题,但是当时没记录下来方案, 于是又搜索了一番,发现别人说的都是HttpUtility.UrlEncod ...

  10. Missing iOS Distribution signing identity问题解决

    问题描述 打包上传APPStore  Xcode报以下错误:Missing iOS Distribution signing identity for XXXXXX 查看证书后发现,Develop证书 ...