【题目链接】

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高级——原型链

    构造函数 构造函数是特殊的函数,里面没有returen返回值 new的过程中,会自动将对象返回,不需要return new的过程中,会执行函数中的代码,会将创建的对象赋值给构造函数中的this 基本概 ...

  2. html5——盒子模式

    box-sizing属性 box-sizing: border-box;/*内减模式*/ box-sizing: content-box;/*外加模式(默认值)*/ box-sizing: paddi ...

  3. Qt 窗体间传值(代码备份)

    刚开始看的时候看的云里雾里的,现在稍微明白一点了.现在假设有一个form,一个MainWindow,如图所示: 实现点击PushButton,将文本框中的内容传输到MainWindow中,显示为Lab ...

  4. Address space layout randomization

    Address space layout randomization (ASLR) is a computer security technique involved in preventing ex ...

  5. appium 使用send_keys方法时报错: driver.find_element_by_id("com.hmkx.zgjkj:id/layout_search_bar_input").send_keys("123")

    新手 使用send_keys方法时一直报错,上网查这个方法的用法,看着大家都是这么写的啊,后来直接搜索 报错信息,搜索结果的针对性就清楚多了. 原来是seleium版本太高导致的问题. 可以先在cmd ...

  6. Map的两种遍历方式

    ********************************************************************************* ****************** ...

  7. 第五节:numpy之数组维度处理

  8. 【郑轻邀请赛 D】hipercijevi

    [题目链接]:https://acm.zzuli.edu.cn/zzuliacm/problem.php?id=2130 [题意] [题解] 把那个管泛化成一个点; 然后把每一个在管里面的点都和它相连 ...

  9. socket状态

    SYN_SEND Indicates active open. SYN_RECEIVED Server just received SYN from the client. ESTABLISHED C ...

  10. zoj 3693

    #include<stdio.h> #include<string.h>//进位问题如3.985    应该进位3.99 int main() {     int n,k,i; ...