题目大意:两个东西朝相同方向移动
Sample Input
4 4
XXXX
.Z..
.XS.
XXXX
4 4
XXXX
.Z..
.X.S
XXXX
4 4
XXXX
.ZX.
.XS.
XXXX
Sample Output
1
1
Bad Luck!

由于两个棋子必然有一个移动。所以假设其中一个一直移动即可,比较水了

 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
int n,m,t;
int d1[][]={,,,,-,,,-};
int d2[][]={-,,,-,,,,};
char s[][];
int vis[][][][];
struct node
{
int x1,y1,s,x2,y2;
node(){}
node(int xx,int yy,int xxx,int yyy,int ss)
{
x1=xx;y1=yy;x2=xxx;y2=yyy;s=ss;
}
}st,ed;
void bfs()
{
queue<node> q;
node now,next;
while(!q.empty()) q.pop();
q.push(node(st.x1,st.y1,st.x2,st.y2,));
while(!q.empty())
{
now=q.front();
q.pop();
//printf("%d %d %d %d %d\n",now.x1,now.y1,now.x2,now.y2,now.s);
if(fabs(now.x1-now.x2)+fabs(now.y1-now.y2)<)
{
printf("%d\n",now.s);
return;
}
for(int i=;i<;i++) //肯定有一个棋子是移动的,以这个棋子作为判断依据
{
next.x1=now.x1+d1[i][];
next.y1=now.y1+d1[i][];
next.x2=now.x2+d2[i][];
next.y2=now.y2+d2[i][];
if(next.x1<||next.x1>=n||next.y1<||next.y1>=m||s[next.x1][next.y1]=='X') continue; //这棋子必须移动
if(next.x2<||next.x2>=n||next.y2<||next.y2>=m||s[next.x2][next.y2]=='X')
{
next.x2=now.x2,next.y2=now.y2;
}
if(vis[next.x1][next.y1][next.x2][next.y2]) continue;
vis[next.x1][next.y1][next.x2][next.y2]=;
next.s=now.s+;
q.push(next);
}
}
printf("Bad Luck!\n");
}
int main()
{
int i,j,k;
freopen("1.in","r",stdin);
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=;i<n;i++)
{
scanf("%s",s[i]);
for(j=;j<m;j++)
{
if(s[i][j]=='Z') st.x1=i,st.y1=j;
if(s[i][j]=='S') st.x2=i,st.y2=j;
}
}
memset(vis,,sizeof(vis));
bfs();
}
return ;
}

hdu 2216 bfs的更多相关文章

  1. hdu 4531 bfs(略难)

    题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...

  2. hdu - 2216 Game III && xtu 1187 Double Maze (两个点的普通bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=2216 zjt和sara在同一个地图里,zjt要去寻找sara,zjt每移动一步sara就要往相反方向移动,如果他 ...

  3. HDU 2216 Game III(BFS)

    Game III Problem Description Zjt and Sara will take part in a game, named Game III. Zjt and Sara wil ...

  4. HDU 2822 (BFS+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同 ...

  5. HDU 1180 (BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1180 题目大意:迷宫中有一堆楼梯,楼梯横竖变化.这些楼梯在奇数时间会变成相反状态,通过楼梯会顺便到达 ...

  6. HDU 2531 (BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2531 题目大意: 你的身体占据多个点.每次移动全部的点,不能撞到障碍点,问撞到目标点块(多个点)的最 ...

  7. HDU 5025 (BFS+记忆化状压搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5025 题目大意: 迷宫中孙悟空救唐僧,可以走回头路.必须收集完钥匙,且必须按顺序收集.迷宫中还有蛇, ...

  8. HDU 1429 (BFS+记忆化状压搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1429 题目大意:最短时间内出迷宫,可以走回头路,迷宫内有不同的门,对应不同的钥匙. 解题思路: 要是 ...

  9. HDU 1026 (BFS搜索+优先队列+记录方案)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 题目大意:最短时间内出迷宫.迷宫里要杀怪,每个怪有一定HP,也就是说要耗一定时.输出方案. 解 ...

随机推荐

  1. IE安全系列之——RES Protocol

    IE安全系列之--RES Protocol res Protocol用于从一个文件里面提取指定资源.语法为:res://sFile[/sType]/sID 各Token含义: sfile:百分号编码. ...

  2. plsql链接数据库配置

    一. 目录结构 D:\install\PLSQL        |-- instantclient_11_2            |-- tnsnames.ora        |-- PLSQL ...

  3. mysql取字段名注意事项!!!!千万不能和关键字同名

    今天就碰到一个恶心的问题,更新时update sql语句报错,查了半天感觉没问题啊,后来一行一行定位,终于找到原因了, 原来是有个字段是show,和mysql关键字冲突了,坑爹! 改了个名字就好了,或 ...

  4. java创建并配置多module的maven项目

    1 使用idea创建(推荐) 这篇博客写的特别好,很详细: https://blog.csdn.net/sinat_30160727/article/details/78109769 2 使用ecli ...

  5. Nginx中worker_connections的问题

    查看日志,有一个[warn]: 3660#0: 20000 worker_connections are more than open file resource limit: 1024 !! 原来安 ...

  6. Linux的bg和fg命令 ---让程序在前台后台之间切换

    Linux的bg和fg命令 我们都知道,在 Windows 上面,我们要么让一个程序作为服务在后台一直运行,要么停止这个服务.而不能让程序在前台后台之间切换.而 Linux 提供了 fg 和 bg 命 ...

  7. Python爬取微信好友

    前言 今天看到一篇好玩的文章,可以实现微信的内容爬取和聊天机器人的制作,所以尝试着实现一遍,本文记录了实现过程和一些探索的内容 来源: 痴海 链接: https://mp.weixin.qq.com/ ...

  8. HTML 多张图片无缝连接

    <table border="0" cellspacing="0" cellpadding="0" style="heigh ...

  9. 【Android开发日记】之入门篇(六)——Android四大组件之Broadcast Receiver

    广播接受者是作为系统的监听者存在着的,它可以监听系统或系统中其他应用发生的事件来做出响应.如设备开机时,应用要检查数据的变化状况,此时就可以通过广播来把消息通知给用户.又如网络状态改变时,电量变化时都 ...

  10. 漂亮的SVG时钟

    漂亮的SVG时钟 效果图: 代码如下,复制即可使用: <!DOCTYPE html> <html lang="en"> <head> <m ...