【题目描述】

	 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,大佬的代码为什么就这么清晰明了。。。。

【题目链接】

Bloxorz I

【代码】

#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;
}

最新文章

  1. mysql中更新或者删除语句中子语句不能操作同一个表You can&#39;t specify target table &#39;test&#39; for update in FROM clause
  2. vue 2.0版本----》常用代码说明
  3. Java对象表示方式2:XStream实现对对象的XML化
  4. HDU4758 Walk Through Squares AC自动机&amp;&amp;dp
  5. Hadoop 中 Eclipse 的配置
  6. JS Math.sin() 与 Math.cos() 用法
  7. Music Studio项目心得--JNI实现C++调用JAVA
  8. scrollView顶部空白
  9. Doing Homework
  10. 使用Angular CLI进行Build (构建) 和 Serve
  11. 低成本制作基于OpenWRT的渗透工具
  12. Linux &quot;ls -l&quot;文件列表权限详解 【转】
  13. react-01
  14. SQL Server 跨服务器查询
  15. MySql cmd下的学习笔记 —— 有关建立数据库的操作(连接Mysql,建立数据库,删除数据库等等)
  16. vue中鼠标移入字体下面显示颜色并改变字体颜色的问题
  17. 在delphi XE5 里面编译kbmmw4.3
  18. 微信小程度腾讯地图SDK使用方法
  19. mpvue基本使用
  20. php中各种操作字符串和时间戳的代码关键词

热门文章

  1. pandas、matplotlib、Numpy模块的简单学习
  2. [CF1004E] Sonya and Ice-cream
  3. centos搭建lamp环境参考(根据腾讯云实验室)
  4. django model 操作总结
  5. SpringCloud 教程 (五) 断路器监控(Hystrix Dashboard)
  6. iOS9 3DTouch 之 Home Screen Quick Actions
  7. CTO爆料:2019程序员最需要了解的行业前沿技术是什么?
  8. 十三、python列表方法汇总
  9. 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_08 Map集合_3_Map接口中的常用方法
  10. Java各类型占字节数