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移动完之后才会改变方向,那么只要统计到达这个点时间奇偶性,就可以知道当前楼梯是 ...
随机推荐
- C++能在三个地方创造对象,而Delphi只有一个地方
C++能在堆栈.堆.资料区创造对象. 但是Delphi只能在堆上创造对象
- SLF4J warning or error messages and their meanings(转)
The method o.a.commons.logging.impl.SLF4FLogFactory#release was invoked. Given the structure of the ...
- 基于visual Studio2013解决面试题之0905子串数量
题目
- HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛)
HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛) Panda Time Limit: 10000/4000 MS (Java/Others) Memory Limit: ...
- [ACM] n划分数m部分,它要求每一个部分,并采取了最大的产品(间隔DP)
A - 爱管闲事 春希很爱管闲事,他每天都会抽出时间帮助一些同学,因为春希很死板,出于公平性,春希不会先帮助后来找他的同学. 如今有n个同学须要他的帮助,尽管他非常想一天之类帮助全部人,但毕竟精力有限 ...
- Swift - 同步请求获取网络数据
使用NSURLConnection可以实现http通信.它提供了异步请求和同步请求两种通信方式. 注意:同步请求数据会造成主线程阻塞,必须请求结束后用户才能做其他的操作,所有通常在请求大数据或者网络不 ...
- iOS技术开发-人机交互指南之UI设计基础:iOS App Anatomy
第二篇更多的是从技术的角度对iOS界面组成原理进行了简单的解析,篇幅很短,可稍作了解:更多关于iOS开发入门的内容可参考“设计师应该了解的iOS应用开发基础知识”一文.另外,非常感谢各位朋友在微博上的 ...
- Android中View绘制优化之一---- 优化布局层次
本文原创, 转载请注明出处:http://blog.csdn.net/qinjuning 前言,竟然是翻译,当然得弄的有板有眼. 照着大作家格式来咯 , - - . 译序 最近一直在做锁屏界面,之前也 ...
- Servlet过滤器——过滤器分析流量
1.概述 Servlet过滤器可以对用户提交的数据或服务器返回的数据进行更改.任何到达服务器的请求都会首先经过过滤器的处理.本实例应用过滤器的这个特点,编写了一个在过滤器中统计网站流量的实例. 本实例 ...
- OpenStack使用Bosh部署CloudFoundry(一)—准备OpenStack环境
版本说明: CloudFoundry:V2版本 OpenStack:Folsom或者Grizzly版本 本篇文章采用OpenStack Folsom+nova-network的OpenStack环境, ...