HDU2216:Game III(BFS)
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
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
Output
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 <iostream> #include <cstdio> #include<cmath> #include<queue> #include<cstring> 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<n && y>=0 && y<m && mp[x][y]!='X') return 1;return 0; } void bfs() {queue<node>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<n;i++){scanf("%s",&mp[i]);for(j=0;j<m;j++){if (mp[i][j]=='Z') sx=i,sy=j;if (mp[i][j]=='S') tx=i,ty=j;}}ans=-1;memset(vis,0,sizeof(vis));bfs();if (ans==-1) printf("Bad Luck!\n");else printf("%d\n",ans);}return 0; }
转载于:https://www.cnblogs.com/stepping/p/5669304.html
HDU2216:Game III(BFS)相关推荐
- leetcode 1306. Jump Game III | 1306. 跳跃游戏 III(BFS)
题目 https://leetcode.com/problems/jump-game-iii/ 题解 正如 hint 所说: 用 BFS,跳过的就不跳了,直到全部跳过,即 count == arr.l ...
- leetcode1306. 跳跃游戏 III(bfs)
这里有一个非负整数数组 arr,你最开始位于该数组的起始下标 start 处.当你位于下标 i 处时,你可以跳到 i + arr[i] 或者 i - arr[i]. 请你判断自己是否能够跳到对应元素值 ...
- LeetCode 248. 中心对称数 III(DFS/BFS)
文章目录 1. 题目 2. 解题 2.1 DFS 2.2 BFS 1. 题目 中心对称数是指一个数字在旋转了 180 度之后看起来依旧相同的数字(或者上下颠倒地看). 写一个函数来计算范围在 [low ...
- LeetCode 1306. 跳跃游戏 III(广度优先搜索BFS)
1. 题目 这里有一个非负整数数组 arr,你最开始位于该数组的起始下标 start 处. 当你位于下标 i 处时,你可以跳到 i + arr[i] 或者 i - arr[i]. 请你判断自己是否能够 ...
- bzoj4997 [Usaco2017 Feb]Why Did the Cow Cross the Road III(bfs)
枚举每一个起点,bfs能访问到几头牛即可.复杂度O(n3)O(n^3) #include <cstdio> #include <cstring> #include <al ...
- 牛客编程巅峰赛:Tree III(BFS or DFS)
链接:https://ac.nowcoder.com/acm/contest/9557/C 来源:牛客网 题目描述 给出一棵有n个节点的节点标号为1~n的有根树(根为第一个节点,并给出从第2个节点到第 ...
- 剑指Offer - 面试题32 - III. 从上到下打印二叉树 III(BFS,queue+stack)
1. 题目 请实现一个函数按照之字形顺序打印二叉树, 即第一行按照从左到右的顺序打印, 第二层按照从右到左的顺序打印, 第三行再按照从左到右的顺序打印,其他行以此类推. 例如: 给定二叉树: [3,9 ...
- 剑指offer面试题32 - III. 从上到下打印二叉树 III(二叉树)(BFS)
题目描述 请实现一个函数按照之字形顺序打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右到左的顺序打印,第三行再按照从左到右的顺序打印,其他行以此类推. 思路 详见链接 代码 class So ...
- 【学时总结】 ◆学时·III◆ 二分图
[学时·III] 二分图 ■基本策略■ 其实本质是图论中的网络流 二分图是两个由多个点组成的集合(上部和下部,且没有重叠),两个集合中的点不与该集合内其他的点连通,但和另一个集合内的点连通.我们称这两 ...
最新文章
- img引Linux的绝对路径,什么是绝对路径和相对路径
- 网站SEO优化可通过哪些数据分析来解决问题?
- 17.抢购(秒杀)业务的技术要点
- api 原生hbase_Hbase常用api
- Dapr 已在塔架就位 将发射新一代微服务
- nssl1304-最大正方形【二分答案】
- mysql与串口通信_虚拟机串口与主机串口通信·小程序(下)
- hadoop eclipse plugin windows下载集合
- everything 中文免安装_GTA5中文免安装版
- 苹果系统被曝漏洞, 大麦网再遭撞库攻击 | 宅客周刊
- 一个程序员父亲的呼吁:不要教你的孩子从小学编程!
- 广西大学计算机学院录取名单,西南大学计算机与信息科学学院关于2021年硕士研究生拟录取名单的公示(不含推免生)...
- 计算机专用英语词汇1695个词汇表(传)
- map拼接URL参数
- 住得越高越安静? 中间楼层噪音最大
- 中国历代大大小小的皇帝(2)
- 那些年啊,那些事——一个程序员的奋斗史 ——83
- Ubuntu 18.04 安装 onedrive
- open stack——Nove计算服务
- 【STM32学习 自制STM32游戏机】
热门文章
- 小程序开发好学吗?需要掌握哪些知识技能?
- python列表大于60_Python使用filter如何对给定列表中的数字进行过滤,保留大于等于60的数字?...
- latch.await java有什么作用,Android系统。 Countdownlatch.await不起作用
- c语言编程新思路知道答案,C语言编程新思路_知道答案公众号免费
- HDU-1233-还是畅通工程(最小生成树)
- Python风格总结:Python3 标准库概览
- DCMTK: DcmSCP, error:QueryRetrieveLevel larger remaining bytes
- BitNami-Redmine1.1.0安装和VisualSVN-Server配合使用
- 学习template算法(template matching)以及改进(二)
- Android Shape工具 Duck