Home Problems       Logout
-11:24:01
 
 

A

B

C

D

E

F

G

H

I

J

K

L

M

N

O

O - Bloxorz I

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit 
Status

Description

Little Tom loves playing games. One day he downloads a little computer game called 'Bloxorz' which makes him excited. 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.



The box stands on a single cell



The box lies on two neighbouring cells, horizontally



The box lies on two neighbouring cells, vertically

After Little Tom passes several stages of the game, he finds it much harder than he expected. So he turns to your help.

Input

Input contains multiple test cases. Each test case is one single stage of the game. It starts with two integers R and C(3 ≤ R, C ≤ 500) which stands for number of rows and columns of the plane. That follows the plane, which contains R lines and C characters for each line, with 'O' (Oh) for target cell, 'X' for initial position of the box, '.' for a rigid cell, '#' for a empty cell and 'E' for a easily broken cell. A test cases starts with two zeros ends the input.

It guarantees that

  • There's only one 'O' in a plane.
  • There's either one 'X' or neighbouring two 'X's in a plane.
  • The first(and last) row(and column) must be '#'(empty cell).
  • Cells covered by 'O' and 'X' are all rigid cells.

Output

For each test cases output one line with the minimum number of moves or "Impossible" (without quote) when there's no way to achieve the target cell.

Sample Input

7 7
#######
#..X###
#..##O#
#....E#
#....E#
#.....#
#######
0 0

Sample Output

10
很水的bfs,要注意,走时候箱子,是沿着最底下四个边,向四个方向进行翻转,这样就可以用三个状态来标记,有横树和站着三种,这样开一个三维数组就足以保存,但这样的速度是不今人满意的,下次写个双向bfs
#include <iostream>
#include <stdio.h>
#include<string.h>
#include <queue>
using namespace std ;
int n,m,map[605][605],endx,endy,visit[600][600][3]; struct boxtree{
int step,x[2],y[2],state;
};
int fmin(int a,int b)
{
if(a<b)
return a;
else
return b;
}
boxtree p,temp,start;
int dir[3][4][5]={
{
0,-2,0,-1,1,
1,0,2,0,2,
0,1,0,2,1,
-2,0,-1,0,2
},
{
0,-1,-3,-3,0,
1,0,1,1,1,
0,2,-3,-3,0,
-1,0,-1,1,1
},
{
0,-1,1,-1,2,
2,0,-3,-3,0,
0,1,1,1,2,
-1,0,-3,-3,0,
}
};
void init()
{
char str[200];
int i,j;
char c; start.x[0]=start.x[1]=start.y[0]=start.y[1]=-1;
gets(str);//吸入回车
for( i=0;i<n;i++)
{
for( j=0;j<m;j++)
{
c=getchar();
if(c=='#')
{
map[i][j]=0;//不可放
}
else if(c=='.')
{
map[i][j]=2;//可以完全放
}
else if(c=='E')//只能放一半
{
map[i][j]=1;
}
else if(c=='O')
{
endx=i;
endy=j;
map[i][j]=2;//题目起始和终点都是牢固的
}
else if( c=='X')
{
map[i][j]=2;
if(start.x[0]==-1)
{
start.x[0]=i;
start.y[0]=j;
start.state=0; //站着的壮态
}
else
{
start.x[1]=i;
start.y[1]=j;
start.state=1;
if((start.x[0]==start.x[1])&&(start.y[0]!=start.y[1]))
{
start.state=1;//横着的状态
start.y[0]=fmin(start.y[0],start.y[1]);//横着的状态选左边为参照点
}
else if((start.x[0]!=start.x[1])&&(start.y[0]==start.y[1]))
{
start.state=2;//树着的状态
start.x[0]=fmin(start.x[0],start.x[1]);//树着的选上边为参照点
}
} }
}
gets(str); }
//gets(str);
// printf("%d%d end\n",endx,endy);
}
bool changestate(int i)
{
p.x[0]=temp.x[0]+dir[temp.state][i][0];//都是和相应的参照点进行变换
p.y[0]=temp.y[0]+dir[temp.state][i][1];
p.x[1]=temp.x[0]+dir[temp.state][i][2];
p.y[1]=temp.y[0]+dir[temp.state][i][3];
p.state=dir[temp.state][i][4]; if(p.state==0)//如果是站的状态
{
if((p.x[0]>=0)&&(p.x[0]<n)&&(p.y[0]>=0&&p.y[0]<m)&&(map[p.x[0]][p.y[0]]==2)&&(visit[p.x[0]][p.y[0]][0]==0))
{
visit[p.x[0]][p.y[0]][0]=1;//标记为已访问
return true;
}
else
{
return false ;
}
}
else if(p.state==1)//如果是横着的状态
{
if((p.x[0]>=0)&&(p.x[0]<n)&&(p.y[0]>=0&&p.y[0]<m-1)&&(map[p.x[0]][p.y[0]]!=0)&&(map[p.x[1]][p.y[1]]!=0)&&(visit[p.x[0]][p.y[0]][1]==0))
{
visit[p.x[0]][p.y[0]][1]=1;//标记为已访问
return true;
}
else
{
return false ;
} }
else if( p.state==2)//如果是树的状态
{
if((p.x[0]>=0)&&(p.x[0]<n-1)&&(p.y[0]>=0&&p.y[0]<m)&&(map[p.x[0]][p.y[0]]!=0)&&map[p.x[1]][p.y[1]]!=0&&(visit[p.x[0]][p.y[0]][2]==0))
{
visit[p.x[0]][p.y[0]][2]=1;//标记为已访问
return true;
}
else
{
return false ;
} } return false ;
}
int bfs()
{
int i;
queue<boxtree> q;
memset(visit,0,sizeof(visit)); while(!q.empty())//清空
{
q.pop();
} start.step=0;
q.push(start);
while(!q.empty())
{
temp=q.front();
q.pop(); for( i=0;i<4;i++)//4 个方向进行搜
{ if(changestate(i))
{
p.step=temp.step+1;
q.push(p);
// printf("%d %d %d\n",p.x[0],p.y[0],p.state);
if((p.x[0]==endx)&&(p.y[0]==endy)&&(p.state==0))
{
printf("%d\n",p.step);
return 1;
}
}
}
}
printf("Impossible\n");
return -1;
}
int main()
{ while(scanf("%d%d",&n,&m)!=EOF&&n&&m)
{
init();
bfs();
} return 0;
}


FAQ | About Virtual Judge |  Forum |  Discuss |  Open Source Project
All Copyright Reserved ©2010-2012 
HUST ACM/ICPC TEAM 

Anything about the OJ, please ask in the 
forum, or contact author:
Isun

Server Time: 

poj3322 Bloxorz I的更多相关文章

  1. POJ3322 Bloxorz I 无脑广搜(我死了。。。)

    多测不清空,爆零两行泪....我死了QWQ 每个节点3个状态:横坐标,纵坐标,和方向 说一下方向:0:立着,1:竖着躺着,上半部分在(x,y),2:横着躺着,左半部分在(x,y) 然后就有了常量数组: ...

  2. POJ3322Bloxorz I

    POJ3322 Bloxorz I 暴搜,next数组与处理一下(小技巧) #include <cstdio> #include <iostream> #include < ...

  3. Bloxorz I (poj3322) (BFS)

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

  4. Bloxorz I POJ - 3322 (bfs)

    Little Tom loves playing games. One day he downloads a little computer game called 'Bloxorz' which m ...

  5. 寒假训练——搜索 E - Bloxorz I

    Little Tom loves playing games. One day he downloads a little computer game called 'Bloxorz' which m ...

  6. POJ 3322 Bloxorz I

    首先呢 这个题目的名字好啊 ORZ啊 如果看不懂题意的话 请戳这里 玩儿几盘就懂了[微笑] http://www.albinoblacksheep.com/games/bloxorz 就是这个神奇的木 ...

  7. Bloxorz I (poj 3322 水bfs)

    Language: Default Bloxorz I Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5443   Acce ...

  8. POJ 3322 Bloxorz(算竞进阶习题)

    bfs 标准广搜题,主要是把每一步可能的坐标都先预处理出来,会好写很多 每个状态对应三个限制条件,x坐标.y坐标.lie=0表示直立在(x,y),lie=1表示横着躺,左半边在(x,y),lie=2表 ...

  9. 【POJ 3322】 Bloxorz I

    [题目链接] http://poj.org/problem?id=3322 [算法] 广度优先搜索 [代码] #include <algorithm> #include <bitse ...

随机推荐

  1. MyEclipse自带且常用的快捷键和自己定义的快捷键方法步骤

    1.MyEclipse自带且常用的快捷键 内容提示(补全): Alt+/    导包快捷键: Ctrl+Shift+o    格式化代码: Ctrl+Shift+f    行代码位置上下调换: Alt ...

  2. POJ 3280 Cheapest Palindrome(区间DP求改成回文串的最小花费)

    题目链接:http://poj.org/problem?id=3280 题目大意:给你一个字符串,你可以删除或者增加任意字符,对应有相应的花费,让你通过这些操作使得字符串变为回文串,求最小花费.解题思 ...

  3. celery在Django中的集成使用

    继上回安装和使用Redis之后,看看如何在Django中使用Celery.Celery是Python开发分布式任务列队的处理库.可以异步分布式地异步处理任务,也可定时执行任务等等.通常我们可以在Dja ...

  4. 20155225 2016-2017-2 《Java程序设计》第2周学习总结

    20155225 2016-2017-2 <Java程序设计>第2周学习总结 教材学习内容总结 比较java和C语言的不同点: java除了基本类型还有类类型 基本类型中还有字节和布尔 对 ...

  5. MEF实现设计上的“松耦合”(二)

    介绍了下MEF的基础用法,让我们对MEF有了一个抽象的认识.当然MEF的用法可能不限于此,比如MEF的目录服务.目录筛选.重组部件等高级应用在这里就不做过多讲解,因为博主觉得这些用法只有在某些特定的环 ...

  6. 21 包含min函数的栈

    定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数. C++: class Solution { private: stack<int> dataStack ; stac ...

  7. 一个带bash,带glibc,中国时区,非root用户可运行crond命令的基于alpine镜像的Dockerfile

    这个镜像现在说起来简单, 带bash(增加执行脚本的兼容性,带GLIBC,中国时区,非root用户可运行crond命令-安全) 但让我开始陷入时,真的让我有段时间有点爆了. 比如,将filebeat文 ...

  8. 004 Hadoop2.x基础知识

    一:大数据应用 1.Cloudera cloudera公司是Hadoop三大发行商之一,其版本为CDH版本,现在最新的版本是CDH5. 网站:http://archive.cloudera.com/c ...

  9. 02:实现Singleton模式

    Java实现单例模式有很多种实现方法,其中我们应根据需要选择线程安全的与非线程安全的两种方式,根据对象实现的方式又分为饱汉与饿汉方式. 这里使用java中的volatile关键字与synchroniz ...

  10. HP电脑的增霸卡功能操作详解

    机房管理中HP电脑的增霸卡功能操作详解 一.软件去除保护 1).电脑开机后等待进入增霸卡选择系统界面: 2).按F1帮助,F10进入增霸卡BIOS界面: 3).光标切换到>>>系统还 ...