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移动完之后才会改变方向,那么只要统计到达这个点时间奇偶性,就可以知道当前楼梯是 ...
随机推荐
- android studio 报错,google后无果
你可能在环境变量中配置了adk的环境变量,同时eclipse可studio公用一个avd,在两个之间切换时过出错
- hdu5338 ZZX and Permutations
hdu5338 ZZX and Permutations 非原创,来自多校题解 不是自己写的,惭愧ing…… 留着以后自己参考…… lower_bound {1,2,4,5} 询问 2,返回的是 2 ...
- Android漫游记(4)---.so文件动态调试一例
Android平台的动态调试一直以来是个困扰我等Coder的头疼问题,特别是对于本地的动态调试支持.能够说是"弱智"级别的,不知道Google的新版NDK和新出的Android S ...
- IOS UItableView得到group如何摆脱的剪裁线条样式问题
在他们的定义UItableView什么时候,选择当style至Group时间,后常透明切割线依然,去除.只有再次刷新了BackgroundView它可以覆盖原来的 //取消切割线 UIView *vi ...
- uva 10127 - Ones(数论)
题目链接:uva 10127 - Ones 题目大意:给出n,问说者少要多少为1才干够整除n. 解题思路:等于是高精度取模,直到余数为0为止. #include <cstdio> #inc ...
- perl 循环类选择器 ,爬取内容
jrhmpt01:/root/lwp/0526# cat 0526.txt <div class="TXD_sy_title"><span class=" ...
- 前端javascript框架之BackboneJS学习笔记
<!DOCTYPE html><html><head><meta charset="utf-8"><script src=&q ...
- HTTP协议结构
HTTP报文=从客户机到服务器的请求+从服务器到客户机的响应 1.请求报文的格式如下: 请求头 通用信息头 请求头 实体头 报文主体 请求行的格式为: Method[分隔符]Re ...
- hive udaf 用maven打包运行create temporary function 时报错
用maven打包写好的jar,在放到hive中作暂时函数时报错. 错误信息例如以下: hive> create temporary function maxvalue as "com. ...
- Eclipse设置Android Logcat输出字体大小
Window -> Preferences -> Android -> Logcat -> Display Font:点击"Change"button 如图 ...