POJ3322 Bloxorz I(BFS)
来源:互联网 发布:淘宝化妆品店铺介绍 编辑:程序博客网 时间:2024/05/22 14:13
很有意思的一个益智小游戏。题目我就不描述了,玩过的人都知道规则。
题目传送门:http://poj.org/problem?id=3322
空格子不能走(可以看成墙),标号为E表示脆弱的格子,箱子在上面不能竖放,只能横放。终点只能竖着放进去。
很明显是搜索题,状态表示也不用太费力去想。需要保存箱子一个角的位置,和摆放的状态f;如果箱子竖放,则位置就是它本身的位置,f=0;如果左右横放,则保存左端的位置,f=1,判断的时候要考虑右端格子;如果上下横放,则保存上端的位置,f=2,判断的时候要考虑下端格子。
然后按不同方向移动箱子的时候,f的值也需要作出相应的更改。
#include<cstdio>#include<cstring>#include<queue>using namespace std;struct Node{ int x,y,f,stp; Node(){} Node(int a,int b,int c,int d) {x = a; y = b; f = c; stp = d;}}st;int n,m,vis[501][501][3],ed[2],cas;int dir1[4][2] = {{-2,0},{1,0},{0,-2},{0,1}};//竖放移动的方向int dir2[4][2] = {{-1,0},{1,0},{0,-1},{0,2}};//左右横放移动的方向int dir3[4][2] = {{-1,0},{2,0},{0,-1},{0,1}};//上下横放移动的方向char mp[501][501];bool inarea(int x,int y){ if(x < 1||x > n||y < 1||y > m) return false; else return true;}int bfs(){ queue<Node> Q; Q.push(st); vis[st.x][st.y][st.f] = cas; while(!Q.empty()) { Node u = Q.front(); Q.pop(); int tx,ty,tf; if(u.f == 0) for(int i = 0; i < 4; i++) { tx = u.x+dir1[i][0],ty = u.y+dir1[i][1]; if(i == 0||i == 1) tf = 2; else tf = 1; if((i == 0||i == 1)&&(mp[tx][ty] == '#'||mp[tx+1][ty] == '#'||!inarea(tx,ty)||!inarea(tx+1,ty))) continue; if((i == 2||i == 3)&&(mp[tx][ty] == '#'||mp[tx][ty+1] == '#'||!inarea(tx,ty)||!inarea(tx,ty+1))) continue; if(vis[tx][ty][tf] == cas) continue; vis[tx][ty][tf] = cas; Q.push(Node(tx,ty,tf,u.stp+1)); } if(u.f == 1) for(int i = 0; i < 4; i++) { tx = u.x+dir2[i][0],ty = u.y+dir2[i][1]; if(i == 0||i == 1) tf = 1; else tf = 0; if(tx == ed[0]&&ty == ed[1]&&tf == 0) return u.stp+1; if((i == 0||i == 1)&&(mp[tx][ty] == '#'||mp[tx][ty+1] == '#'||!inarea(tx,ty)||!inarea(tx,ty+1))) continue; if((i == 2||i == 3)&&(mp[tx][ty] == '#'||mp[tx][ty] == 'E'||!inarea(tx,ty))) continue;//不能竖着放在脆弱的格子上面 if(vis[tx][ty][tf] == cas) continue; vis[tx][ty][tf] = cas; Q.push(Node(tx,ty,tf,u.stp+1)); } if(u.f == 2) for(int i = 0; i < 4; i++) { tx = u.x+dir3[i][0],ty = u.y+dir3[i][1]; if(i == 0||i == 1) tf = 0; else tf = 2; if(tx == ed[0]&&ty == ed[1]&&tf == 0) return u.stp+1; if((i == 0||i == 1)&&(mp[tx][ty] == '#'||mp[tx][ty] == 'E'||!inarea(tx,ty))) continue; if((i == 2||i == 3)&&(mp[tx][ty] == '#'||mp[tx+1][ty] == '#'||!inarea(tx,ty)||!inarea(tx+1,ty))) continue; if(vis[tx][ty][tf] == cas) continue; vis[tx][ty][tf] = cas; Q.push(Node(tx,ty,tf,u.stp+1)); } } return -1;}void Init(){ st.x = st.y = st.f = 0; cas++; memset(mp,0,sizeof mp);}int main(){ while(scanf("%d%d",&n,&m) != EOF&&n+m) { Init(); for(int i = 1; i <= n; i++) { scanf("%s",mp[i]+1); } for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) { if(mp[i][j] == 'X') { mp[i][j] = '.'; if(!st.y) { st.x = i,st.y = j,st.f = 0,st.stp = 0; } else if(st.y+1 == j) st.f = 1; else st.f = 2; } if(mp[i][j] == 'O') ed[0] = i,ed[1] = j,mp[i][j] = '#';//终点格子也视作空格子,但是好像并没有什么影响 } int ans = bfs(); if(ans == -1) printf("Impossible\n"); else printf("%d\n",ans); }}
0 0
- poj3322 Bloxorz I (bfs)
- POJ3322 Bloxorz I(BFS)
- poj3322——Bloxorz I//bfs
- poj3322 Bloxorz I
- poj3322 Bloxorz I
- poj3322 Bloxorz I
- poj3322 Bloxorz I(滚箱子游戏) 状压BFS
- bfs-poj-Bloxorz I
- POJ 3322 Bloxorz I BFS
- poj 3322 Bloxorz I (BFS)
- poj 3322 Bloxorz I (bfs)
- POJ 3322 Bloxorz I 三维BFS
- Bloxorz I (poj 3322 水bfs)
- Bloxorz很有意思的一个游戏(POJ3322)
- poj 3322 Bloxorz I ( bfs+三维判重 )
- POJ 3322 Bloxorz I(BFS:求迷宫最短路径)
- PKU3322 bloxorz I
- poj 3322 Bloxorz I
- ubuntu下安装maven
- SpatialHadoop实例:面向空间数据的高效MapReduce框架
- 网络协议系列之四:IGMP、ICMP和ARP
- 在StoryBoard已画TabbarItem的情况下 代码手动添加TabBarItem
- Android 5.1 以太网服务启动过程
- POJ3322 Bloxorz I(BFS)
- OpenCV 2.4.12 GPU编译及使用
- tar error:"Error exit delayed from previous errors"的几种解决办法
- qrcode生成二维码,canvas转化为图片
- Redis-3.0.7主从配置
- SAP基本计量单位更改
- Oracle 之 SQL 面试题 录
- linux进程管理与调度
- LaTeX 加粗