博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU2216:Game III(BFS)
阅读量:5157 次
发布时间:2019-06-13

本文共 2606 字,大约阅读时间需要 8 分钟。

Game III

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 31   Accepted Submission(s) : 11

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

Zjt and Sara will take part in a game, named Game III. Zjt and Sara will be in a maze, and Zjt must find Sara. There are some strang rules in this maze. If Zjt move a step, Sara will move a step in opposite direction.
Now give you the map , you shold find out the minimum steps, Zjt have to move. We say Zjt meet Sara, if they are in the same position or they are adjacent . 
Zjt can only move to a empty position int four diraction (up, left, right, down). At the same time, Sara will move to a position in opposite direction, if there is empty. Otherwise , she will not move to any position.
The map is a N*M two-dimensional array. The position Zjt stays now is marked Z, and the position, where Sara stays, is marked E.
>  . : empty position
>  X: the wall
>  Z: the position Zjt now stay
>  S: the position Sara now stay
Your task is to find out the minimum steps they meet each other.

Input

The input contains several test cases. Each test case starts with a line contains three number N ,M (2<= N <= 20, 2 <= M <= 20 ) indicate the size of the map. Then N lines follows, each line contains M character. A Z and a S will be in the map as the discription above.

Output

For each test case, you should print the minimum steps. “Bad Luck!” will be print, if they can't meet each other.

Sample Input

4 4XXXX.Z...XS.XXXX4 4XXXX.Z...X.SXXXX4 4XXXX.ZX..XS.XXXX

Sample Output

11Bad Luck!
#include 
#include
#include
#include
#include
using namespace std;struct node{ int x,y,u,v,ti; node(int a,int b,int c,int d,int e){x=a;y=b;u=c;v=d;ti=e;}};int n,m,i,j,sx,sy,tx,ty,ans;char mp[25][25];int vis[25][25][25][25]; //标记走过否int dr[4][2]={
{
0,1},{
0,-1},{-1,0},{
1,0} };bool check(int x,int y){ if (x>=0 && x
=0 && y
Q; vis[sx][sy][tx][ty]=1; Q.push(node(sx,sy,tx,ty,0)); while(!Q.empty()) { node p=Q.front(); Q.pop(); if (abs(p.x-p.u)+abs(p.y-p.v)<=1) { ans=p.ti; return; } for(i=0;i<4;i++) { int xx=p.x+dr[i][0]; int yy=p.y+dr[i][1]; int uu=p.u-dr[i][0]; int vv=p.v-dr[i][1]; if (check(xx,yy)) { if (!check(uu,vv)) { uu=p.u; vv=p.v; } if (!vis[xx][yy][uu][vv]) { vis[xx][yy][uu][vv]=1; Q.push(node(xx,yy,uu,vv,p.ti+1)); } } } } return;}int main(){ while(~scanf("%d%d",&n,&m)) { for(i=0;i

 

转载于:https://www.cnblogs.com/stepping/p/5669304.html

你可能感兴趣的文章
虚拟机(VM)安装openwrt-koolshare软路由
查看>>
CentOS7下使用Sonatype Nexus3搭建Docker私有仓库
查看>>
Kubernetes-Linux系统初始化
查看>>
170118、快速失败Vs安全失败(Java迭代器附示例)
查看>>
170605、防止sql注入(二)
查看>>
MySQL外键设置中的的 Cascade、Restrict、SET NULL 、NO ACTION
查看>>
ubuntu下mysql的一些命令
查看>>
项目支持
查看>>
这10道javascript笔试题你都会么
查看>>
接口测试工具-Jmeter使用笔记(三:管理请求服务器信息和Headers参数)
查看>>
linux下调整时区和时间的方法
查看>>
【服务器】【tomcat】Tomcat 应用目录重定向
查看>>
【leetcode】Path Sum II
查看>>
集合代码----小练习1
查看>>
iframe自适应高度的多种方法方法
查看>>
Dbzoj#3188. [Coci 2011]Upit
查看>>
SOAP 与 restful service区别
查看>>
centos 6.5 联网
查看>>
Java入门 手把手教你配置环境变量
查看>>
报数游戏
查看>>