Bloxorz I (poj3322) (BFS)
【题目描述】
It's a game about rolling a box to a specific position on a special plane. Precisely, the plane, which is composed of several unit cells, is a rectangle shaped area. And the box, consisting of two perfectly aligned unit cube, may either lies down and occupies two neighbouring cells or stands up and occupies one single cell. One may move the box by picking one of the four edges of the box on the ground and rolling the box 90 degrees around that edge, which is counted as one move. There are three kinds of cells, rigid cells, easily broken cells and empty cells. A rigid cell can support full weight of the box, so it can be either one of the two cells that the box lies on or the cell that the box fully stands on. A easily broken cells can only support half the weight of the box, so it cannot be the only cell that the box stands on. An empty cell cannot support anything, so there cannot be any part of the box on that cell. The target of the game is to roll the box standing onto the only target cell on the plane with minimum moves.
【算法】
貌似没啥算法,就是BFS,但是写起来很烦,%lyd,大佬的代码为什么就这么清晰明了。。。。
【题目链接】
【代码】
#include <stdio.h>
#include <queue>
using namespace std;
struct rec{ int x,y,state; }st,ed;
char s[510][510];
int m,n,d[510][510][3];
queue<rec> q;
const int dx[]={0,0,-1,1},dy[]={-1,1,0,0};
bool valid(int x,int y) {
return x>=1&&x<=m&&y>=1&&y<=n;
}
void parse_st_ed() {
for(int i=1;i<=m;i++) {
for(int j=1;j<=n;j++) {
if(s[i][j]=='O') {
ed.x=i,ed.y=j,s[i][j]='.';
}else if(s[i][j]=='X') {
for(int k=0;k<4;k++) {
int x=i+dx[k],y=j+dy[k];
if(valid(x,y)&&s[x][y]=='X') {
st.x=min(x,i),st.y=min(y,j);
st.state=x==i?1:2;
s[i][j]=s[x][y]='.';
break;
}
}
if(s[i][j]=='X') st.x=i,st.y=j,st.state=0;
}
}
}
}
const int next_x[3][4]={ {0,0,-2,1},{0,0,-1,1},{0,0,-1,2} };
const int next_y[3][4]={ {-2,1,0,0},{-1,2,0,0},{-1,1,0,0} };
const int next_state[3][4]={ {1,1,2,2},{0,0,1,1},{2,2,0,0} };
bool valid(rec k) {
if(!valid(k.x,k.y)) return 0;
if(s[k.x][k.y]=='#') return 0;
if(k.state==0&&s[k.x][k.y]=='E') return 0;
if(k.state==1&&s[k.x][k.y+1]=='#') return 0;
if(k.state==2&&s[k.x+1][k.y]=='#') return 0;
return 1;
}
int bfs() {
while(q.size()) q.pop();
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
d[i][j][0]=d[i][j][1]=d[i][j][2]=-1;
d[st.x][st.y][st.state]=0;
q.push(st);
while(q.size()) {
rec now=q.front(); q.pop();
for(int i=0;i<4;i++) {
rec next;
next.x=now.x+next_x[now.state][i];
next.y=now.y+next_y[now.state][i];
next.state=next_state[now.state][i];
if(!valid(next)) continue;
if(d[next.x][next.y][next.state]==-1) {
d[next.x][next.y][next.state]
=d[now.x][now.y][now.state]+1;
if(next.x==ed.x&&next.y==ed.y&&!next.state) return d[next.x][next.y][0];
q.push(next);
}
}
}
return -1;
}
int main() {
while(~scanf("%d%d",&m,&n)&&m) {
for(int i=1;i<=m;i++) scanf("%s",s[i]+1);
parse_st_ed();
int ans=bfs();
if(ans==-1) puts("Impossible");
else printf("%d\n",ans);
}
return 0;
}
Bloxorz I (poj3322) (BFS)的更多相关文章
- Bloxorz I POJ - 3322 (bfs)
Little Tom loves playing games. One day he downloads a little computer game called 'Bloxorz' which m ...
- poj3322 Bloxorz I
Home Problems Logout -11:24:01 Overview Problem Status Rank A B C D E F G H I J K L M N O ...
- Bloxorz I (poj 3322 水bfs)
Language: Default Bloxorz I Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5443 Acce ...
- POJ3322 Bloxorz I 无脑广搜(我死了。。。)
多测不清空,爆零两行泪....我死了QWQ 每个节点3个状态:横坐标,纵坐标,和方向 说一下方向:0:立着,1:竖着躺着,上半部分在(x,y),2:横着躺着,左半部分在(x,y) 然后就有了常量数组: ...
- 寒假训练——搜索 E - Bloxorz I
Little Tom loves playing games. One day he downloads a little computer game called 'Bloxorz' which m ...
- POJ 3322 Bloxorz I
首先呢 这个题目的名字好啊 ORZ啊 如果看不懂题意的话 请戳这里 玩儿几盘就懂了[微笑] http://www.albinoblacksheep.com/games/bloxorz 就是这个神奇的木 ...
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- 【BZOJ-1656】The Grove 树木 BFS + 射线法
1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 186 Solved: 118[Su ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
随机推荐
- [CF1051F] Shortest Statement
问题描述 You are given a weighed undirected connected graph, consisting of n vertices and m edges. You s ...
- 使用IDEA连接mysql数据库
1.IDEA配置数据库连接 2.添加数据库: 3.填写数据库信息: database输入框里面填写要连接的数据库名称 然后点击test connection,会弹出一个框,提示下载驱动,点击进行下载即 ...
- 多线程之同时更改数据问题--启用lock
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- kohana orm巧用字段备注支持扩展
1.SELECT * FROM `bota_language` WHERE `type` = 'order_type'; id key value type ---- ...
- #1127-JSP表单处理
JSP 表单处理 我们在浏览网页的时候,经常需要向服务器提交信息,并让后台程序处理.浏览器中使用 GET 和 POST 方法向服务器提交数据. GET 方法GET方法将请求的编码信息添加在网址后面,网 ...
- Oracle Select语句
Oracle Select语句 作者:初生不惑 Oracle基础 评论:0 条 Oracle技术QQ群:175248146 在本教程中,将学习如何使用Oracle SELECT语句从单个表中查询数据. ...
- MySQL Password Expired
好久没有登录MySQL,登录的时候遇到如下提示: 尝试使用命令行登录,发现是可以使用之前的密码登录的,如下: 只是登录之后,发现做不了什么操作.并且使用,alter 语句重设密码也没有成功,如下: 试 ...
- 背包&数位dp(8.7)
背包 0/1背包 设dp[i][j]为前i个物品选了j体积的物品的最大价值/方案数 dp[i][j]=max(dp[i-1][j-w[i]]+v[i],dp[i-1][j])(最大价值) dp[i][ ...
- Bash is an sh-compatible command language interpreter that executes commands read from the standard input or from a file.
w https://linux.die.net/man/1/bash bash - GNU Bourne-Again SHell Description Bash is an sh-compatibl ...
- IK词库扩展
先写个标题,慢慢更新 默认的词库就算最小细粒度分词,很多名词也不会单字分词 比如:阿迪达斯,在IK是一个词,搜索每个字的单字关键词是无结果的,必须搜索阿迪达斯才有结果 所以我们需要扩展词库 IK官方教 ...