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移动完之后才会改变方向,那么只要统计到达这个点时间奇偶性,就可以知道当前楼梯是 ...
随机推荐
- HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011亚洲北京赛区网络赛)
HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011 亚洲北京赛区网络赛题目) Eliminate Witches! Time Limit: 2000/1000 ...
- Swift - 同步请求获取网络数据
使用NSURLConnection可以实现http通信.它提供了异步请求和同步请求两种通信方式. 注意:同步请求数据会造成主线程阻塞,必须请求结束后用户才能做其他的操作,所有通常在请求大数据或者网络不 ...
- Managing your Actor Systems
今天偶然得机会,找到一篇不错得文章,现在分享给大家. 原文:http://www.kotancode.com/2013/02/15/managing-your-actor-systems/ If yo ...
- jQuery EasyUI API 中文文档 - 分隔按钮(splitbutton)
<html> <head> <script src="jquery-easyui/jquery.min.js"></script> ...
- commondatastorage.googleapis.com訪问失败高速解决
谷歌更新以后非常多sampleproject下载不了. http://commondatastorage.googleapis.com訪问失败高速解决这个问题. 使用在线代理就可以,随便推荐一个htt ...
- hdu 1540 Tunnel Warfare(线段树区间统计)
Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- 使用BigDecimal来进行精确计算
在一些以金融等行业中的计算是需要十分精确的,即使我们使用像double这样的类型,由于浮点数的原因,会使得数据计算变得不精确,例如下面的例子: double a = 0.1; double b = 0 ...
- Transformations 方块转换
题目是中文题,就不做什么解释了,纯模拟题,主要要搞清楚这几种装换方式下标的变化: 第一种:顺时针旋转90度: c[j][n-i+1]=a[i][j]; 第二种:旋转180度: c[n-i+1][n-j ...
- JavaScript闭包(closure)入门: 拿"开发部"和"技术牛"举个例子
虽然只是一小段菜鸟的学习笔记 , 不过还是希望看到的高手看到不足的时候帮忙指点~ 一:代码和执行过程 /** * http://blog.csdn.net/ruantao1989 * ==>Ju ...
- eclipse3.2 汉化 汉化包下载
1.首先去www.eclipse.org下载eclipse3.2 点击下载eclipse3.2 2.再去www.eclipse.org下载它的汉化包 请使用迅雷等下载工具下载汉化包 注意不同版 ...