HDU3085 Nightmare Ⅱ (双向BFS)
联赛前该练什么?DP,树型,状压当然是爆搜啦
双向BFS就是两个普通BFS通过一拼接函数联系,多多判断啦
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++a)
#define nR(a,b,c) for(register int a = (b); a >= (c); --a)
#define Fill(a,b) memset(a, b, sizeof(a))
#define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b))
#define ON_DEBUGG
#ifdef ON_DEBUGG
#define D_e_Line printf("\n-----------\n")
#define D_e(x) std::cout << (#x) << " : " <<x << "\n"
#define FileOpen() freopen("in.txt", "r", stdin)
#define FileSave() freopen("out.txt", "w", stdout)
#define Pause() system("pause")
#include <ctime>
#define TIME() fprintf(stderr, "\nTIME : %.3lfms\n", clock() * 1000.0 / CLOCKS_PER_SEC)
#else
#define D_e_Line ;
#define D_e(x) ;
#define FileOpen() ;
#define FilSave ;
#define Pause() ;
#define TIME() ;
#endif
struct ios {
template<typename ATP> ios& operator >> (ATP &x) {
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x *= f;
return *this;
}
}io;
using namespace std;
template<typename ATP> inline ATP Min(ATP a, ATP b) {
return a < b ? a : b;
}
template<typename ATP> inline ATP Max(ATP a, ATP b) {
return a > b ? a : b;
}
#include <queue>
const int N = 1007;
int n, m;
struct Nod {
int x, y;
};
queue<Nod> q[2];
int step, mx, my, gx, gy, zx[5], zy[5], vis[2][N][N];
int dx[] = {0, 0, -1, 1}, dy[] = {1, -1, 0, 0};
char mp[N][N];
inline bool Check(int x, int y) {
return (abs(x - zx[1]) + abs(y - zy[1]) > 2 * step) && (abs(x - zx[2]) + abs(y - zy[2]) > 2 * step);
}
inline bool Valid(int x, int y) {
return x >= 1 && x <= n && y >= 1 && y <= m && mp[x][y] != 'X';
}
inline bool BFS(int t) { // t = 1 -> man, t = 0 -> woman
int siz = q[t].size();
while(siz--){
int x = q[t].front().x, y = q[t].front().y;
q[t].pop();
if(!Check(x, y)) continue;
R(k,0,3){
int fx = x + dx[k], fy = y + dy[k];
if(!Valid(fx, fy)) continue;
if(vis[t][fx][fy]) continue;
if(!Check(fx, fy)) continue;
vis[t][fx][fy] = true;
if(vis[0][fx][fy] && vis[1][fx][fy]) return true;
q[t].push((Nod){ fx, fy});
}
}
return false;
}
inline int Solve() {
q[0].push((Nod){ mx, my});
q[1].push((Nod){ gx, gy});
step = 0;
while(!q[0].empty() || !q[1].empty()){
++step;
R(i,1,3){
if(BFS(0)) return step; // man move three times
}
if(BFS(1)) return step; // woman move
}
return -1;
}
int main() {
//FileOpen();
int Tasks;
io >> Tasks;
while(Tasks--){
io >> n >> m;
R(i,1,n){
scanf("%s", mp[i] + 1);
}
int nmIdx = 0; // night mare
while(!q[0].empty()) q[0].pop();
while(!q[1].empty()) q[1].pop();
Fill(vis, 0);
R(i,1,n){
R(j,1,m){
if(mp[i][j] == 'M'){
mx = i, my = j;
vis[0][i][j] = true;
}
else if(mp[i][j] == 'G'){
gx = i, gy = j;
vis[1][i][j] = true;
}
else if(mp[i][j] == 'Z'){
zx[++nmIdx] = i, zy[nmIdx] = j;
}
}
}
printf("%d\n", Solve());
}
return 0;
}
HDU3085 Nightmare Ⅱ (双向BFS)的更多相关文章
- HDU3085 Nightmare Ⅱ —— 双向BFS + 曼哈顿距离
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Other ...
- HDU 3085 Nightmare Ⅱ (双向BFS)
Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU3085(双向BFS+曼哈顿距离)题解
Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 3085 Nightmare Ⅱ 双向BFS
题意:很好理解,然后注意几点,男的可以一秒走三步,也就是三步以内的都可以,鬼可以穿墙,但是人不可以,鬼是一次走两步 分析:我刚开始男女,鬼BFS三遍,然后最后处理答案,严重超时,然后上网看题解,发现是 ...
- HDU3085(KB2-G 双向bfs)
Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- Nightmare Ⅱ(双向BFS)
Problem Description Last night, little erriyue had a horrible nightmare. He dreamed that he and his ...
- HDU 3085 Nightmare II 双向bfs 难度:2
http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...
- 【HDU3085】nightmare2 双向BFS
对于搜索树分支很多且有明确起点和终点的情况时,可以采用双向搜索来减小搜索树的大小. 对于双向BFS来说,与单向最大的不同是双向BFS需要按层扩展,表示可能到达的区域.而单向BFS则是按照单个节点进行扩 ...
- HDU 3085 Nightmare Ⅱ(双向BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 题目大意:给你一张n*m地图上,上面有有 ‘. ’:路 ‘X':墙 ’Z':鬼,每秒移动2步,可 ...
随机推荐
- 【Windbg】记一次线程卡主的问题
测试告诉我们定时任务没有执行了,排查相关日志,只有开始执行,没有执行结束.估计是什么地方卡主了. 所以dump分析日志 先检查一下加载情况 !eeversion 线程卡主了,先看线程 !runaway ...
- 关于TornadoFx和Android的全局配置工具类封装实现及思路解析
原文地址: 关于TornadoFx和Android的全局配置工具类封装实现及思路解析 - Stars-One的杂货小窝 目前个人开发软件存在设置页面,可以让用户自定义些设置,但我发现,存储数据的代码逻 ...
- 1个程序员单干之:怎样给我的升讯威在线客服系统编写堪比 MSDN 的用户手册
本系列文章详细介绍使用 .net core 和 WPF 开发 升讯威在线客服与营销系统 的过程. 免费在线使用 & 免费私有化部署:https://kf.shengxunwei.com 视频实 ...
- docker引起服务器磁盘爆满
服务器异常 又是开开心心打开我心爱的服务器一天: 吔!这是嘛啊?我的服务器域名访问不了了,一直转圈圈超时了,好,打开ssh远程看看,吔!!!还是访问不了,宕机了?怀着一颗憋大便的心情打开了阿里云控制面 ...
- c++ 超大整数除法 高精度除法
c++ 超大整数除法 高精度除法 解题思路 计算a/b,其中a为大整数,b为普通整数,商为c,余数为r. 根据手算除法的规则,上一步的余数记为r,则本次计算的被除数为t=r*10+被除数的本位数值a[ ...
- ExtJS 布局-Accordion布局(Accordion layout)
更新记录: 2022年6月2日 开始. 2022年6月3日 发布. 1.说明 accordion(手风琴)布局一次仅显示一个子组件,内置支持 折叠 和 展开.当需要堆叠多个子组件,并每次只显示一次时, ...
- Visual Studio Installer下载速度为0处理办法
DNS改为:223.5.5.5即可. 223.5.5.5 下载完成后记得改回来.
- 29.MySQL高级SQL语句
MySQL高级SQL语句 目录 MySQL高级SQL语句 创建两个表 SELECT DISTINCT WHERE AND OR IN BETWEEN 通配符 LIKE ORDER BY 函数 数学函数 ...
- Vue-简单安装和运行
安装Vue CLI 安装nodejs 下载: https://nodejs.org/en/download/ 安装Vue CLI 文档: https://cli.vuejs.org/guide/ins ...
- 使用Vite2+TypeScript4+Vue3技术栈,如何入手开发项目
前言 今天,我们使用Vite2.0+Vue3+TS来试玩一下,开发一个demo项目.实战 我们,打开Vite官方网站(https://cn.vitejs.dev/). Vite (法语意为 " ...