【HDOJ】3085 Nightmare Ⅱ
双向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 Ⅱ的更多相关文章
- 【HDOJ】1072 Nightmare
bfs,使用ttl进行剪枝. #include <iostream> #include <cstdio> #include <cstring> #include & ...
- 【HDOJ】4729 An Easy Problem for Elfness
其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...
- 【HDOJ】【3506】Monkey Party
DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...
- 【HDOJ】【3516】Tree Construction
DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...
- 【HDOJ】【3480】Division
DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...
- 【HDOJ】【2829】Lawrence
DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...
- 【HDOJ】【3415】Max Sum of Max-K-sub-sequence
DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...
- 【HDOJ】【3530】Subsequence
DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...
- 【HDOJ】【3068】最长回文
Manacher算法 Manacher模板题…… //HDOJ 3068 #include<cstdio> #include<cstring> #include<cstd ...
随机推荐
- agentzh --春哥--调试专家
https://github.com/agentzh/perl-systemtap-toolkit https://github.com/openresty http://openresty.org/ ...
- [转] webpack debug in webstorm
先run build,然后用node server.js来做 WebStorm 11 adds support for debugging client-side apps built with We ...
- Java基础知识强化之集合框架笔记25:Vector的特有功能
1. Vector的特有功能: (1)添加功能 public void addElement(Object obj) -- add() (2)获取功能 pu ...
- TFS 安装与管理
整了几天TFS,把相关的一些配置与安装的要点简单记下,希望对大家有用.本篇主要是安装与配置上的内容,下一篇会介绍如何使用以及使用方面的相关心得体会. 本篇内容简要: 1. 安装部署 1.1. 流 ...
- @ManyToMany中间表附加字段设计
在使用@ManyToMany时,若中间表只有相应的外键字段可以直接建立两个对应的Entity 设置ManyToMany @ManyToMany 两个表多对多关联 但若是中间表有自己的附加字段,这需要为 ...
- 偶遇问题 - - JavaScript 取消链接默认行为问题
今天在测试<JavaScript DOM编程艺术(第2版)>中第69页代码时,遇到了问题.本来预期效果应该是点击链接后不跳转当前页面,而是另外弹出有个窗口.但结果却是页面跳转了.代码如下图 ...
- 学点bootstrap
- JAva Collections类方法详解
http://blog.csdn.net/lskyne/article/details/8961014 Collections则是集合类的一个工具类/帮助类,其中提供了一系列静态方法,用于对集合中元素 ...
- Linux 自动更新时间
1. 从NTP上把时间同步到本地 cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 2. 更新本地时间 ntpdate us.pool.ntp.o ...
- Java封装的与当前时间比较,得到多少年,多少月,多少天前,多少小时前,多小分钟前
public class CalendarCal { /** * 与当前时间比较,得到多少年,多少月,多少天前,多少小时前,多小分钟前 * * @param calendar * ...