hdu 3309 Roll The Cube ( bfs )
Roll The Cube
'B' -- ball
'H' -- hole
'.' -- land
'*' -- wall
Remember when a ball rolls into a hole, they(the ball and the hole) disappeared, that is , 'H' + 'B' = '.'.
Now you are controlling two balls at the same time.Up, down , left , right --- once one of these keys is pressed, balls exist roll to that direction, for example , you pressed up , two balls both roll up.
A ball will stay where it is if its next point is a wall, and balls can't be overlap.
Your code should give the minimun times you press the keys to achieve the goal.
Then T blocks , each block has two integers n , m (n , m <= 22) indicating size of the map.
Then n lines each with m characters.
There'll always be two balls(B) and two holes(H) in a map.
The boundary of the map is always walls(*).
Tell me "Sorry , sir , my poor program fails to get an answer." if you can never achieve the goal.
6 3
***
*B*
*B*
*H*
*H*
***
4 4
****
*BB*
*HH*
****
4 4
****
*BH*
*HB*
****
5 6
******
*.BB**
*.H*H*
*..*.*
******
1
2
Sorry , sir , my poor program fails to get an answer.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define maxn 25
#define INF 0x3f3f3f3f
using namespace std; int n,m,ans;
int sx[2],sy[2];
int dx[]= {-1,1,0,0};
int dy[]= {0,0,-1,1};
bool vis[maxn][maxn][maxn][maxn];
char mp[maxn][maxn];
char s[maxn];
struct Node
{
int x[2],y[2],step;
int b[2],h[2]; // 球 洞 球是否进洞和洞是否被求填满分开看
} cur,now;
queue<Node>q; bool bfs()
{
int i,j,t,flag;
memset(vis,0,sizeof(vis));
while(!q.empty()) q.pop();
cur.x[0]=sx[0],cur.y[0]=sy[0];
cur.x[1]=sx[1],cur.y[1]=sy[1];
cur.h[0]=cur.h[1]=0;
cur.b[0]=cur.b[1]=0;
cur.step=0;
vis[sx[0]][s[0]][sx[1]][sy[1]]=1;
q.push(cur);
while(!q.empty())
{
now=q.front();
q.pop();
for(i=0; i<4; i++)
{
cur=now;
for(j=0; j<2; j++)
{
if(cur.b[j]) continue ;
cur.x[j]+=dx[i];
cur.y[j]+=dy[i];
if(mp[cur.x[j]][cur.y[j]]=='*')
{
cur.x[j]-=dx[i];
cur.y[j]-=dy[i];
}
}
if(vis[cur.x[0]][cur.y[0]][cur.x[1]][cur.y[1]]||cur.x[0]==cur.x[1]&&cur.y[0]==cur.y[1]&&cur.b[0]+cur.b[1]==0) continue ;
cur.step++;
vis[cur.x[0]][cur.y[0]][cur.x[1]][cur.y[1]]=1;
flag=1;
for(j=0; j<2; j++)
{
t=mp[cur.x[j]][cur.y[j]];
if(t<2&&!cur.h[t]) cur.b[j]=1,cur.h[t]=1;
if(!cur.b[j]) flag=0;
}
if(flag)
{
ans=cur.step;
return true ;
}
q.push(cur);
}
}
return false ;
}
int main()
{
int i,j,t,cnt1,cnt2;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
cnt1=cnt2=0;
for(i=1; i<=n; i++)
{
scanf("%s",s);
for(j=1; j<=m; j++)
{
mp[i][j]=s[j-1];
if(mp[i][j]=='H') mp[i][j]=cnt1++;
else if(mp[i][j]=='B') sx[cnt2]=i,sy[cnt2]=j,cnt2++;
}
}
if(bfs()) printf("%d\n",ans);
else printf("Sorry , sir , my poor program fails to get an answer.\n");
}
return 0;
}
hdu 3309 Roll The Cube ( bfs )的更多相关文章
- HDU 5836 Rubik's Cube BFS
Rubik's Cube 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5836 Description As we all know, Zhu is ...
- 【HDOJ】3309 Roll The Cube
BFS,考虑一球进洞仅一球滚动以及两球重叠的情况即可. /* 3309 */ #include <iostream> #include <queue> #include < ...
- HDU 2717 Catch That Cow --- BFS
HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先 ...
- HDU 5876 关于补图的bfs
1.HDU 5876 Sparse Graph 2.总结:好题,把STL都过了一遍 题意:n个点组成的完全图,删去m条边,求点s到其余n-1个点的最短距离. 思路:把点分为两个集合,A为所有没有到达 ...
- hdu 1240:Asteroids!(三维BFS搜索)
Asteroids! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- HDU(1175),连连看,BFS
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1175 越学越不会,BFS还是很高级的. 连连看 Time Limit: 20000/100 ...
- HDU 3085 Nightmare II 双向bfs 难度:2
http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...
- hdu - 1240 Nightmare && hdu - 1253 胜利大逃亡(bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1240 开始没仔细看题,看懂了发现就是一个裸的bfs,注意坐标是三维的,然后每次可以扩展出6个方向. 第一维代表在 ...
- hdu - 1180 诡异的楼梯 (bfs+优先队列)
http://acm.hdu.edu.cn/showproblem.php?pid=1180 注意点就是楼梯是在harry移动完之后才会改变方向,那么只要统计到达这个点时间奇偶性,就可以知道当前楼梯是 ...
随机推荐
- 云计算被视为继大型计算机、个人计算机、互联网之后的第4次IT产业革命,顺应了当前各行业整合计算资源和服务能力的要求(转)
云计算被视为继大型计算机.个人计算机.互联网之后的第4次IT产业革命,顺应了当前各行业整合计算资源和服务能力的要求,成为引领当今世界信息技术变革的主力军.越来越多的金融企业认识到只有与云计算结合,才能 ...
- lua中打印所以类型功能实现table嵌套table
lua中打印所以类型功能实现 本人測试 number.string.bool.nil.table嵌套table.userdata没问题 共享一下有什么问题请拍砖 代码例如以下 cclog = func ...
- UVA 10718 Bit Mask 贪心+位运算
题意:给出一个数N,下限L上限U,在[L,U]里面找一个整数,使得N|M最大,且让M最小. 很明显用贪心,用位运算搞了半天,样例过了后还是WA,没考虑清楚... 然后网上翻到了一个人家位运算一句话解决 ...
- ANTLR4权威參考手冊(一)
写在前面的话: 此文档是对伟大的Terence Parr的著作<the definitive antlr4 reference>的翻译本.致敬!欢迎转载,请注明原地址,请尊重劳动成果.翻译 ...
- 【开发手记一】老生常谈:简简单单配置ZED板开发环境
说明:整理之前项目博客,此系列之前发表于与非网 http://www.openhw.org/module/forum/thread-552476-1-1.html 在拿到开发板和配套教材之前,我们小组 ...
- Eclipse设置Android Logcat输出字体大小
Window -> Preferences -> Android -> Logcat -> Display Font:点击"Change"button 如图 ...
- ASP.NET - 多文件上传,纯代码,不使用插件
解决方案: 前段代码: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Mu ...
- 慎用MonthsBetweent和MonthSpan
●function MonthsBetween(const ANow, AThen: TDateTime): Integer; 描述: 根据两个给定的TDateTime类型的参数ANow和AThen, ...
- asp.net ajax检查用户名是否存在代码
原文 asp.net ajax检查用户名是否存在代码 用户注册时,我们经常需要检查用户名是否存在,本文就是实现无刷新验证用户名 打开开发环境VS 2005,新建项目(或打开现有项目),新建一个Web ...
- java 控制表项删除、编辑、添加(实现接口)
package com.platformda.optimize; import java.awt.BorderLayout; import java.awt.Point; import java.aw ...