hdu 2216 bfs
题目大意:两个东西朝相同方向移动
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的更多相关文章
- hdu 4531 bfs(略难)
题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...
- hdu - 2216 Game III && xtu 1187 Double Maze (两个点的普通bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=2216 zjt和sara在同一个地图里,zjt要去寻找sara,zjt每移动一步sara就要往相反方向移动,如果他 ...
- 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 ...
- HDU 2822 (BFS+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同 ...
- HDU 1180 (BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1180 题目大意:迷宫中有一堆楼梯,楼梯横竖变化.这些楼梯在奇数时间会变成相反状态,通过楼梯会顺便到达 ...
- HDU 2531 (BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2531 题目大意: 你的身体占据多个点.每次移动全部的点,不能撞到障碍点,问撞到目标点块(多个点)的最 ...
- HDU 5025 (BFS+记忆化状压搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5025 题目大意: 迷宫中孙悟空救唐僧,可以走回头路.必须收集完钥匙,且必须按顺序收集.迷宫中还有蛇, ...
- HDU 1429 (BFS+记忆化状压搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1429 题目大意:最短时间内出迷宫,可以走回头路,迷宫内有不同的门,对应不同的钥匙. 解题思路: 要是 ...
- HDU 1026 (BFS搜索+优先队列+记录方案)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 题目大意:最短时间内出迷宫.迷宫里要杀怪,每个怪有一定HP,也就是说要耗一定时.输出方案. 解 ...
随机推荐
- 【codeforces】【比赛题解】#937 CF Round #467 (Div. 2)
没有参加,但是之后几天打了哦,第三场AK的CF比赛. CF大扫荡计划正在稳步进行. [A]Olympiad 题意: 给\(n\)个人颁奖,要满足: 至少有一个人拿奖. 如果得分为\(x\)的有奖,那么 ...
- aarch64_g4
golang-github-inconshreveable-muxado-devel-0-0.7.gitf693c7e.fc26.noarch.rpm 2017-02-11 16:47 30K fed ...
- Flask:初次使用Flask-SQLAlchemy读取SQLite3
Windows 10家庭中文版,Python 3.6.4,Flask 1.0.2,Eclipse Oxygen.1a Release (4.7.1a),PyDev 6.3.2 SQLAlchemy是一 ...
- JAVA复习笔记分布式篇:kafka
前言:第一次使用消息队列是在实在前年的时候,那时候还不了解kafka,用的是阿里的rocket_mq,当时觉得挺好用的,后来听原阿里的同事说rocket_mq是他们看来kafka的源码后自己开发了一套 ...
- Ubuntu 16.04 使用docker资料汇总与应用docker安装caffe并使用Classifier(ros kinetic+usb_cam+caffe)
Docker是开源的应用容器引擎.若想简单了解一下,可以参考百度百科词条Docker.好像只支持64位系统. Docker官网:https://www.docker.com/ Docker - 从入门 ...
- python中round(四舍五入)的坑
python中的round函数不能直接拿来四舍五入,一种替代方式是使用Decimal.quantize()函数. 具体内容待补. >>> round(2.675, 2) 2.67 可 ...
- 安装VM虚拟机提示 尝试创建目录 C:\Public\documents\SharedVirtual Machines 时发生错误解决方法
把Windows Defender安全中心的“受控制文件夹的访问”给关闭了,然后就可以顺利安装上了. 作者:耑新新,发布于 博客园 转载请注明出处,欢迎邮件交流:zhuanxinxin@foxmai ...
- ElasticSearch实战概要
最近中美关系越来越紧张,国内经济下滑,股市一片惨淡,互联网行业越来越不景气,动不动都是跌掉几千亿市值,来写一些文档来抚慰这颗受伤的心吧... 随着互联网的发展,数据越来越重要,每个公司保存的数据也是越 ...
- python学习day4之路
装饰器(http://egon09.blog.51cto.com/9161406/1836763) 1.装饰器:本质是函数: 装饰器(装饰其他函数),就是为其他函数添加附加功能: 原则:1.不能修改被 ...
- Smart Pointer 智能指针
P76 参考:http://www.cnblogs.com/lanxuezaipiao/p/4132096.html http://blog.csdn.net/hackbuteer1/article/ ...