题目大意:两个东西朝相同方向移动
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. Linux信息搜集

    ## 1.取证工具 - LiME 内存获取工具 - volatility 内存分析工具 ## 2.机器信息收集 #sysinfo 16 # # 查看当前登录用户 who > who.txt # ...

  2. Python标准库笔记(6) — struct模块

    该模块作用是完成Python数值和C语言结构体的Python字符串形式间的转换.这可以用于处理存储在文件中或从网络连接中存储的二进制数据,以及其他数据源. 用途: 在Python基本数据类型和二进制数 ...

  3. C# 调用WSDL接口及方法

    1.首先需要清楚WSDL的引用地址 如:http://XX.XX.4.146:8089/axis/services/getfileno?wsdl 上述地址的构造为 类名getfileno. 2.在.N ...

  4. html5新增表单元素

    1.验证 <form> <input type="email"></input>    验证邮箱 <input type="ur ...

  5. vue总结 06组件

    组件基础 基本示例 这里有一个 Vue 组件的示例: // 定义一个名为 button-counter 的新组件Vue.component('button-counter', { data: func ...

  6. Flask:使用Eclipse+PyDev插件编辑基于package的项目

    Windows 10家庭中文版,Python 3.6.4,Flask 1.0.2,Eclipse Oxygen.1a Release (4.7.1a),PyDev 6.3.2 本文记录了 使用Ecli ...

  7. Scala中的"null" 和“_”来初始化对象

    Alternatives Use null as a last resort. As already mentioned, Option replaces most usages of null. I ...

  8. 原生JS实现AJAX、JSONP及DOM加载完成事件

    一.JS原生Ajax ajax:一种请求数据的方式,不需要刷新整个页面:ajax的技术核心是 XMLHttpRequest 对象:ajax 请求过程:创建 XMLHttpRequest 对象.连接服务 ...

  9. 在Eclipse中建立Maven工程

  10. thinkphp5 url传参

    url('index/blog/read',['id'=>5,'name'=>'thinkphp']); 手册https://www.kancloud.cn/manual/thinkphp ...