【题目链接】

http://acm.hdu.edu.cn/showproblem.php?pid=3085

【算法】

双向BFS

【代码】

#include<bits/stdc++.h>
using namespace std;
#define MAXN 810 const int dx[] = {,,-,};
const int dy[] = {-,,,}; int i,n,m,px,py,qx,qy,T;
char mp[MAXN][MAXN];
bool visited1[MAXN][MAXN];
bool visited2[MAXN][MAXN]; inline bool valid(int x,int y,int k)
{
if (x <= || x > n || y <= || y > m) return false;
if (abs(x - px) + abs(y - py) <= * k) return false;
if (abs(x - qx) + abs(y - qy) <= * k) return false;
if (mp[x][y] == 'X') return false;
return true;
}
inline int bfs()
{
int i,j,step,bx,by,gx,gy,tx,ty,s;
queue< pair<int,int> > q1,q2;
pair<int,int> cur;
px = py = qx = qy = ;
for (i = ; i <= n; i++)
{
for (j = ; j <= m; j++)
{
if (mp[i][j] == 'M')
{
bx = i;
by = j;
}
if (mp[i][j] == 'G')
{
gx = i;
gy = j;
}
if (mp[i][j] == 'Z')
{
if (!px)
{
px = i;
py = j;
} else
{
qx = i;
qy = j;
}
}
}
}
step = ;
while (!q1.empty()) q1.pop();
while (!q2.empty()) q2.pop();
memset(visited1,false,sizeof(visited1));
memset(visited2,false,sizeof(visited2));
visited1[bx][by] = true;
visited2[gx][gy] = true;
q1.push(make_pair(bx,by));
q2.push(make_pair(gx,gy));
while ((!q1.empty()) || (!q2.empty()))
{
step++;
s = q1.size();
for (i = ; i <= s; i++)
{
cur = q1.front();
q1.pop();
if (!valid(cur.first,cur.second,step)) continue;
for (j = ; j < ; j++)
{
tx = cur.first + dx[j];
ty = cur.second + dy[j];
if (valid(tx,ty,step) && !visited1[tx][ty])
{
if (visited2[tx][ty]) return step;
visited1[tx][ty] = true;
q1.push(make_pair(tx,ty));
}
}
}
s = q1.size();
for (i = ; i <= s; i++)
{
cur = q1.front();
q1.pop();
if (!valid(cur.first,cur.second,step)) continue;
for (j = ; j < ; j++)
{
tx = cur.first + dx[j];
ty = cur.second + dy[j];
if (valid(tx,ty,step) && !visited1[tx][ty])
{
if (visited2[tx][ty]) return step;
visited1[tx][ty] = true;
q1.push(make_pair(tx,ty));
}
}
}
s = q1.size();
for (i = ; i <= s; i++)
{
cur = q1.front();
q1.pop();
if (!valid(cur.first,cur.second,step)) continue;
for (j = ; j < ; j++)
{
tx = cur.first + dx[j];
ty = cur.second + dy[j];
if (valid(tx,ty,step) && !visited1[tx][ty])
{
if (visited2[tx][ty]) return step;
visited1[tx][ty] = true;
q1.push(make_pair(tx,ty));
}
}
}
s = q2.size();
for (i = ; i <= s; i++)
{
cur = q2.front();
q2.pop();
if (!valid(cur.first,cur.second,step)) continue;
for (j = ; j < ; j++)
{
tx = cur.first + dx[j];
ty = cur.second + dy[j];
if (valid(tx,ty,step) && !visited2[tx][ty])
{
if (visited1[tx][ty]) return step;
visited2[tx][ty] = true;
q2.push(make_pair(tx,ty));
}
}
}
}
return -;
} int main()
{ scanf("%d",&T);
while (T--)
{
scanf("%d%d",&n,&m);
for (i = ; i <= n; i++) scanf("%s",mp[i]+);
printf("%d\n",bfs());
} return ; }

【HDU 3085】 Nightmare Ⅱ的更多相关文章

  1. 【HDU - 3085】Nightmare Ⅱ(bfs)

    -->Nightmare Ⅱ 原题太复杂,直接简单的讲中文吧 Descriptions: X表示墙 .表示路 M,G表示两个人 Z表示鬼 M要去找G但是有两个鬼(Z)会阻碍他们,每一轮都是M和G ...

  2. 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题

    [HDU  3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...

  3. 【HDU 5647】DZY Loves Connecting(树DP)

    pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...

  4. -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】

    [把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...

  5. 【HDU 2196】 Computer(树的直径)

    [HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...

  6. 【HDU 2196】 Computer (树形DP)

    [HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...

  7. 【HDU 5145】 NPY and girls(组合+莫队)

    pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Other ...

  8. 【hdu 1043】Eight

    [题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=1043 [题意] 会给你很多组数据; 让你输出这组数据到目标状态的具体步骤; [题解] 从12345 ...

  9. 【HDU 3068】 最长回文

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3068 [算法] Manacher算法求最长回文子串 [代码] #include<bits/s ...

随机推荐

  1. JS高级——instanceof语法

    基本语法 对象 instanceof 构造函数 基本使用 <script> function Person() { } //p--->Person.prototype--->O ...

  2. 【译】x86程序员手册07 - 2.5操作数选择

    2.5 Operand Selection 操作数选择 An instruction can act on zero or more operands, which are the data mani ...

  3. Java_Web三大框架之Hibernate操作数据库(三)

    使用Hibernate操作数据库需要七个步骤: (1)读取并解析配置文件 Configuration conf = newConfiguration().configure(); (2)读取并解析映射 ...

  4. Centos 安装 Moosefs文件系统

    一.环境介绍Moosefs master:192.168.55.148Moosefs Metalogger:192.168.55.149Moosefs Chunk-01:192.168.55.150M ...

  5. (转)postgis常用函数介绍(一)

    http://blog.csdn.net/gisshixisheng/article/details/47701237 概述: 在进行地理信息系统开发的过程中,常用的空间数据库有esri的sde,po ...

  6. C# 返回值为 list<T>

    public List<T> test<T>(List<T> EntityList) where T : class { return EntityList; }

  7. 通过offset值的设置使html元素对齐

    今天是我第一次写这个随笔,为了记录我发现的一个jquery的offset的值的问题. 这个offset的值会因为页面标签是否处于隐藏状态而表现出不同的值,隐藏状态时,offset的值是相对于直接父亲的 ...

  8. PHP 之ftp客户端类封装实现

    <?php /** * Class FtpClient */ class FtpClient { private $host = '';//远程服务器地址 private $user = ''; ...

  9. ThinkPHP框架表单验证AJAX

    验证有两种方式:静态验证与动态验证. 一.静态验证 在模型类里面预先定义好该模型的自动验证规则,我们称为静态定义. 验证时要在test表的Model里面加验证条件:新建testModel.class. ...

  10. discourse论坛迁移

    在源设备的操作备份数据文件tar -czvf discoursefile716.tar.gz /var/discourse然后把此discoursefile716.tar.gz文件传到需要迁移的设备上 ...