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移动完之后才会改变方向,那么只要统计到达这个点时间奇偶性,就可以知道当前楼梯是 ...
随机推荐
- leetcode:Reverse Nodes in k-Group(以k为循环节反转链表)【面试算法题】
题目: Given a linked list, reverse the nodes of a linked list k at a time and return its modified list ...
- HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011亚洲北京赛区网络赛)
HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011 亚洲北京赛区网络赛题目) Eliminate Witches! Time Limit: 2000/1000 ...
- 一个必用的javascript框架:underscore.js - wine的思考 - ITeye技术网站
AngularJS+JqueryMobile+PhoneGap 打造APP « Dogeek AngularJS+JqueryMobile+PhoneGap 打造APP
- iOS 获取当前时间以及计算年龄(时间差)
获取当前时间 NSDate *now = [NSDate date]; NSLog(@"now date is: %@", now); NSCalendar *calendar = ...
- CodeForces 446B DZY Loves Modification
题意: k次操作 每次选择一行或一列 得到所选数字的和 并将所选数字同一时候减去p 问最多得到多少 思路: 重点在消除行列间的相互影响 因为每选一行全部列所相应的和都会-p 那么假设选了i次 ...
- IOS UItableView得到group如何摆脱的剪裁线条样式问题
在他们的定义UItableView什么时候,选择当style至Group时间,后常透明切割线依然,去除.只有再次刷新了BackgroundView它可以覆盖原来的 //取消切割线 UIView *vi ...
- Swift - 使用位运算提取颜色,合并颜色
通常我们可以使用16进制的格式表示RGB颜色,比如0x2f88c0.通过位操作运算,能很方便的将其中的R,G,B颜色各部分分别提取出来.反之,也可以将R,G,B颜色值组合成一个完整的颜色. 1,提取颜 ...
- ASP.NET - 在线编辑器(FreeTextBox)
1.首先下载FreeTextBox程序集,再次使用3.3.1 · 官网:http://freetextbox.com/ · 百度云: 2.在程序中引入程序集 3.在工具箱中添加控件. 4.之后拖动控件 ...
- MSSQL - 存储过程OutPut返回值
1.存储过程中不使用外部参数. 存储过程: SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ========================== ...
- Servlet的学习之ServletContext(2)
本篇接上篇<Servlet的学习(五)>,继续从ServletContext对象中的方法进行学习,在这一篇中,我们重点关注的是ServletContext对象中对于在web工程中的资源文件 ...