题目大意:两个东西朝相同方向移动
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. oracle同义词是什么意思?

    相当于alias,比如把user1.table1 在user2中建一个同义词table1create synonym table1 for user1.table1;这样当我们在user2中查sele ...

  2. python3之pymysql模块

    1.python3 MySQL数据库链接模块 PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中则使用mysqldb. PyMySQL 遵循 Pyt ...

  3. 众签demo

    众签demo using System; using System.Collections.Generic; using System.Linq; using System.Text; using S ...

  4. C#抓取网络图片保存到本地

    C#抓取网络图片保存到本地 System.Net.WebClient myWebClient = new System.Net.WebClient(); //将头像保存到服务器 string virP ...

  5. Python中使用LMDB

    在python中使用lmdb linux中,可以使用指令pip install lmdb安装lmdb包. 生成一个空的lmdb数据库文件 # -*- coding: utf-8 -*- import ...

  6. MySQL字符集编码相关

    Windows 10家庭中文版,MySQL  5.7.20,2018-05-07 Part.1 查找数据库的字符集编码 查看MySQL字符集编码:status命令 使用命令行登录MySQL服务器,然后 ...

  7. python基础--subprocess模块

    可以执行shell命令的相关模块和函数有: os.system os.spawn* os.popen*          --废弃 popen2.*           --废弃 commands.* ...

  8. 原生js封装dom操作库

    var utils = (function(window) { var flag = "getComputedStyle" in window; function win(attr ...

  9. 16/11/22_plsql

    1.数据类型: char 固定长度,varchar 字符长度按照实际长度, varchar2 字符均存储2个字节, nvarchar 按照Unicode存储.number(m,n)总长度m,小数 n. ...

  10. SQL2000数据库修改sa密码

    开始——程序——Microsoft SQL Server——企业管理器 2 展开数据库Microsoft SQL Server—— SQL Server组——安全性——登录——双击sa 3 在常规内有 ...