联赛前该练什么?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)的更多相关文章

  1. HDU3085 Nightmare Ⅱ —— 双向BFS + 曼哈顿距离

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Other ...

  2. HDU 3085 Nightmare Ⅱ (双向BFS)

    Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  3. HDU3085(双向BFS+曼哈顿距离)题解

    Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  4. HDU 3085 Nightmare Ⅱ 双向BFS

    题意:很好理解,然后注意几点,男的可以一秒走三步,也就是三步以内的都可以,鬼可以穿墙,但是人不可以,鬼是一次走两步 分析:我刚开始男女,鬼BFS三遍,然后最后处理答案,严重超时,然后上网看题解,发现是 ...

  5. HDU3085(KB2-G 双向bfs)

    Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  6. Nightmare Ⅱ(双向BFS)

    Problem Description Last night, little erriyue had a horrible nightmare. He dreamed that he and his ...

  7. HDU 3085 Nightmare II 双向bfs 难度:2

    http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...

  8. 【HDU3085】nightmare2 双向BFS

    对于搜索树分支很多且有明确起点和终点的情况时,可以采用双向搜索来减小搜索树的大小. 对于双向BFS来说,与单向最大的不同是双向BFS需要按层扩展,表示可能到达的区域.而单向BFS则是按照单个节点进行扩 ...

  9. HDU 3085 Nightmare Ⅱ(双向BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 题目大意:给你一张n*m地图上,上面有有 ‘. ’:路 ‘X':墙 ’Z':鬼,每秒移动2步,可 ...

随机推荐

  1. 竟然还有人说ArrayList是2倍扩容,今天带你手撕ArrayList源码

    ArrayList是我们开发中最常用到的集合,但是很多人对它的源码并不了解,导致面试时,面试官问的稍微深入的问题,就无法作答,今天我们一起来探究一下ArrayList源码. 1. 简介 ArrayLi ...

  2. 开源流程引擎osworkflow、jbpm、activiti、flowable、camunda哪个好?

    市场上比较有名的开源流程引擎有osworkflow.jbpm.activiti.flowable.camunda.其中:Jbpm4.Activiti.Flowable.camunda四个框架同宗同源, ...

  3. 【转载】k8s入坑之路(2)kubernetes架构详解

    每个微服务通过 Docker 进行发布,随着业务的发展,系统中遍布着各种各样的容器.于是,容器的资源调度,部署运行,扩容缩容就是我们要面临的问题. 基于 Kubernetes 作为容器集群的管理平台被 ...

  4. 论文解读(MGAE)《MGAE: Masked Autoencoders for Self-Supervised Learning on Graphs》

    论文信息 论文标题:MGAE: Masked Autoencoders for Self-Supervised Learning on Graphs论文作者:Qiaoyu Tan, Ninghao L ...

  5. 1.为什么要从古典概率入门概率学《zobol的考研概率论教程》

    在入门概率论与数理统计这门课中,刚开始我们都会从古典概率开始学习,为什么要选择它呢?这是因为古典概率作为一种将生活中的事情简化为有限种情况,并假设它们的发生可能差不多的手段,十分的好用且简洁. 这里我 ...

  6. C语言学习之我见-strncpy()字符串复制函数(可控制范围)

    strncpy()函数,用于两个字符串值的复制. (1)函数原型 char *strncpy(char * _Dest,const char * _Source,size_t _Count); (2) ...

  7. 使用阿里云RDS for SQL Server性能洞察优化数据库负载-初识性能洞察

    简介 数据库性能调优通常需要较高数据库水平,并伴随较多的前期准备工作,比如收集各种性能基线.不同种类的性能指标.慢SQL日志等,这通常费时费力且效果一般,当面对多个数据库时总体拥有成本会大幅增加.今天 ...

  8. 一条 SQL 语句是如何执行的

    一条 SQL 语句是如何执行的 SQL查询语句 select * from user where ID=10; MySQL 的基本架构可以分为 Server 层和存储引擎两部分.Server 层又包含 ...

  9. C#中将字符串转换成数值

    Convert.ToInt32("999");

  10. java。多态

    package Demo.oop.APP.Demo05; public class application { public static void main(String[] args) { //一 ...