题面描述
一个游戏,为一个NNN行MMM列第二矩阵,位置可能是硬地("...")、易碎地面(“EEE”)、禁地("#")、起点(“XXX”)或终点(“OOO”).你的任务是操作一个1∗1∗21*1*21∗1∗2的长方体。这个长方体在地面上有两种放置形式.“立”(占1∗11*11∗1),“躺”(占1∗21*21∗2)。每一步操作中,可以按上下左右四个键之一。按下任意键,可以使长方体向对应方向沿着棱滚动 90°90°90°
有以下禁忌

  1. 不能有任何部位接触“#”
  2. 不能有立在“EEE”的情况

图中可能有两个“XXX”的情况,目标:求出滚动到“OOO”的步数,如果不能则输出“ImpossibleImpossibleImpossible”
数据范围:3≤N,M≤5003≤N,M≤5003≤N,M≤500.

思路
其实挺好想的,就是看自己愿不愿意下功夫
本文代码用了三元组(x,y,lie)(x,y,lie)(x,y,lie),
来表示当
lie=0lie=0lie=0时,即为立起来时,位置为(x,y)(x,y)(x,y).
lie=1lie=1lie=1时,即为横躺着,左半部分的位置为(x,y)(x,y)(x,y).
lie=2lie=2lie=2时,即为竖躺着,上半部分的位置为(x,y)(x,y)(x,y)
手动模拟一下
得出

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_lie[3][4]={{1,1,2,2},{0,0,1,1},{2,2,0,0}};

0~3分别表示左、右、上、下方向
解释一下nextx[i][j]next_x[i][j]nextx​[i][j]即处于lie=i方向为j滚动lie=i~~方向为j滚动lie=i  方向为j滚动的x增减状态,
以此类推。

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstdlib>
using namespace std;
const int dx[4]={0,0,1,-1},dy[4]={1,-1,0,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_lie[3][4]={{1,1,2,2},{0,0,1,1},{2,2,0,0}};
struct node
{int x,y,lie;//lie==0立着,lie==1横着躺,lie==2竖着躺 node(){};node(int x,int y,int lie):x(x),y(y),lie(lie){}
};
char s[510][510];
node st,ed;
int n,m,d[510][510][3];
queue<node>q;
bool valid(int x,int y)
{return x>=1&&x<=n&&y>=1&&y<=m;
}
bool valid(node next)
{if(!valid(next.x,next.y))return 0;int x=next.x,y=next.y,lie=next.lie;if(s[x][y]=='#')return 0;if(s[x][y]!='.'&&!lie)return 0;if(lie==1&&s[x][y+1]=='#')return 0;if(lie==2&&s[x+1][y]=='#')return 0;return 1;
}
void pre()
{for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){if(s[i][j]=='O')ed.x=i,ed.y=j,ed.lie=0,s[i][j]='.';else if(s[i][j]=='X'){for(int k=0;k<4;k++){int x=dx[k]+i,y=dy[k]+j;if(s[x][y]=='X'&&valid(x,y)){st.x=min(i,x),st.y=min(j,y);st.lie=k<2?1:2;s[i][j]=s[x][y]='.';break;}if(s[i][j]=='X')st.x=i,st.y=j,st.lie=0,s[i][j]='.';}}}
}
int bfs()
{for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)for(int k=0;k<3;k++)d[i][j][k]=-1;while(q.size())q.pop();d[st.x][st.y][st.lie]=0;q.push(st);while(!q.empty()){node t=q.front();q.pop();for(int i=0;i<4;i++){node next=t;next.x+=next_x[next.lie][i];next.y+=next_y[next.lie][i];next.lie=next_lie[next.lie][i];if(!valid(next))continue;if(d[next.x][next.y][next.lie]==-1){d[next.x][next.y][next.lie]=d[t.x][t.y][t.lie]+1;q.push(next);if(next.x==ed.x&&next.y==ed.y&&next.lie==ed.lie)return d[next.x][next.y][next.lie];}if(d[ed.x][ed.y][ed.lie]!=-1)return d[ed.x][ed.y][ed.lie];}}return -1;
}
int main()
{while(scanf("%d%d",&n,&m)&&n){for(int i=1;i<=n;i++)scanf("%s",s[i]+1);pre();int ans=bfs();if(ans==-1)puts("Impossible");else printf("%d\n",ans);}return 0;
}

Bloxorz I [POJ3322]相关推荐

  1. Bloxorz I (poj3322) (BFS)

    [题目描述] It's a game about rolling a box to a specific position on a special plane. Precisely, the pla ...

  2. Bloxorz POJ3322

    Bloxorz POJ3322 思路 题干在这:POJ3322 学习广搜,抄的算法竞赛进阶指南的示例代码 ac代码 #include<cstdio> #include<iostrea ...

  3. POJ-3322 Bloxorz I

    题目描述 Little Tom loves playing games. One day he downloads a little computer game called 'Bloxorz' wh ...

  4. poj3322 Bloxorz I

    Description Little Tom loves playing games. One day he downloads a little computer game called 'Blox ...

  5. POJ3322 Bloxorz I BFS

    题目链接 http://poj.org/problem?id=3322 分析 每次操作,地图不会改变,变化的坐标及放置方式可以设为状态,进行BFS. 预处理出各种放置方式下,向各个方向移动后状态的变化 ...

  6. POJ3322 Bloxorz “迷宫”类经典例题

    题目大意 游戏地图是一个N行M列的矩阵,每个位置可能是硬地(用"."表示).易碎地面(用"E"表示).禁地(用"#"表示).起点(用&quo ...

  7. POJ3322 Bloxorz I(BFS)

    很有意思的一个益智小游戏.题目我就不描述了,玩过的人都知道规则. 题目传送门:http://poj.org/problem?id=3322 空格子不能走(可以看成墙),标号为E表示脆弱的格子,箱子在上 ...

  8. 【POJ3322】Bloxorz I

    一道非常棒的搜索题,做完之后感觉神清气爽-- 可以肯定的是,本题属于走地图一类的搜索题,适合用bfs求解,然而本题细节极多,有很多需要注意的地方. 我们定义状态三元组(x,y,z)表示当前移动到位置( ...

  9. POJ - 3322 Bloxorz I(bfs+状态设计)

    题目链接:点击查看 题目大意:模拟Bloxorz小游戏找出最优解,简单说一下规则,给出一个n*m的矩阵,其中,"#"代表墙,"X"代表起点,"O&qu ...

最新文章

  1. Tomcat网页加载速度过慢的解决方法
  2. php中条件查询语句,thinkphp3.2框架中where条件查询用法总结
  3. Linux 上安装 appium
  4. find之exec和args
  5. 三相同步电机怎么接线图_三相电度表怎么看倍数
  6. 预处理器sass_Sass — Web的预处理器装饰
  7. 面试常备题---JVM加载class文件的原理机制
  8. Eclipse如何不使用alt+/来实现自动提示
  9. python自动执行脚本
  10. 【论文笔记】A Convolutional Neural Network Cascade for Face Detection
  11. 深度学习-浅层神经网络
  12. evernote 有道_如何备份Evernote笔记本(以防万一)
  13. 013:Django商城项目规划与环境搭建
  14. linux多网卡的路由模式和桥接模式设置方法
  15. js中元素样式设置的六种方法
  16. Mysql全站最全笔记,从入门到资深!
  17. Probability Through Experiments
  18. linux抢占式调度
  19. JS获取字符串占用多少空间
  20. jpg格式电脑怎么弄_电脑上怎么压缩JPG图片?两种方法帮你轻松解决

热门文章

  1. [书]x86汇编语言:从实模式到保护模式 -- 第六、七章 编写主引导扇区代码
  2. 自定义控件 - 收藏集 - 掘金
  3. Mysql sleep线程过多解决方案
  4. mac u盘文件过大 拷贝不进去_Mac-文件太大不能放入U盘中
  5. sonoff开关改装件控制(3) ------flutter导入其他安卓项目的依赖包
  6. 各种问答机器人平台调研
  7. 【课程论文】严肃游戏设计与运用
  8. 关于 Linux中systemd的一些笔记
  9. 大学学计算机专业好吗,就读清华大学计算机专业好处竟然这么多?
  10. 量化交易系统 python源码_零起点TensorFlow与量化交易[PDF+源码][238.77MB]