【题目链接】

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. win32动态库

    先讲一个基本的动态库,功能为自定义一个动态库,里面有一个函数MyMessage实现弹出MessageBox. 1. 先在头文件中定义: #ifdef __cplusplus #define EXPOR ...

  2. HTML CSS, JavaScript 计算器

    效果图: 代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...

  3. html5——拖拽

    基本情况 在HTML5的规范中,我们可以通过为元素增加draggable="true"来设置此元素是否可以进行拖拽操作,其中图片.链接默认是开启的. 拖拽元素 页面中设置了drag ...

  4. CSS——盒子居中显示

    嵌套中个的子盒子使用了绝对定位,父盒子使用了相对定位.那么子盒子如何居中显示: 1.距离左偏离50% 2.margin-right子盒子宽度的一半 <!DOCTYPE html> < ...

  5. 【译】x86程序员手册03 - 2.1内存组织和分段

    2.1 Memory Organization and Segmentation 内存组织和分段 The physical memory of an 80386 system is organized ...

  6. 03--QT教程(转自:豆子)

    http://blog.51cto.com/zt/20

  7. unable to get system library for the project

    当向eclipse导入项目实例后,项目上出现红叉的错误提示,在项目属性里的Java Build Path里发现了错误提示复选选项: unable to get system library for t ...

  8. Lua练习题集嚢

    --1.table.sort() am = {"cc","nn","ll","dd"} arr = function ( ...

  9. c# 异步执行

    c#异步执行 Action logAction = () => {////执行的操作 }; logAction.BeginInvoke(null, null);

  10. BZOJ2730 矿场搭建 解题报告 点双联通分量

    题意概述: 一张有向图,在其中设置一些关键点(即题目中的逃生出口),使得删除任意一个点之后其余点都可以到达至少一个关键点. 问至少需要设置多少中关键点,有多少种设置方法. 解析: 首先,这道题要求删掉 ...