Language:
Default
Bloxorz I
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 5443   Accepted: 1811

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

Source

题意就不详细说了,去这里玩一下就知道了戳我玩游戏。还是非常好玩的~

思路:Move函数写得非常蛋疼,我是硬来的,一定要细心。

代码:

#include <iostream>
#include <functional>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v) memset ((t) , v, sizeof(t))
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf printf
#define DBG pf("Hi\n")
typedef long long ll;
using namespace std; #define INF 0x3f3f3f3f
#define mod 1000000009
const int maxn = 550;
const int MAXN = 2005;
const int MAXM = 200010;
const int N = 1005; struct Node
{
int state; //0表示立着 1表示横着 2表示竖着
int x1,y1,x2,y2;
int step;
}; int n,m;
int ex,ey;
char mp[maxn][maxn];
int dir[4][2]={0,-1,0,1,-1,0,1,0};
bool vis[3][maxn][maxn];
int pos[5]; bool isok(int x,int y)
{
if (x>=0&&x<n&&y>=0&&y<m&&mp[x][y]!='#') return true;
return false;
} bool Move(int x,int y,int d,Node &now)
{
if (now.state==0)
{
now.x1=x+dir[d][0]; now.y1=y+dir[d][1];
now.x2=x+2*dir[d][0]; now.y2=y+2*dir[d][1];
if (d==0||d==2) { //始终保持横着的左边一个为主块。竖着的上面一个为主块
swap(now.x1,now.x2);
swap(now.y1,now.y2);
}
if (d<2) now.state=1;
else now.state=2;
if (isok(now.x1,now.y1)&&isok(now.x2,now.y2)&&!vis[now.state][now.x1][now.y1])
return true;
}
else if (now.state==1)
{
if (d<2){
now.x1=x+dir[d][0]; now.y1=y+dir[d][1];
if (d==1) now.x1=now.x1+dir[d][0] , now.y1=now.y1+dir[d][1];
now.state=0;
if (isok(now.x1,now.y1)&&mp[now.x1][now.y1]!='E'&&!vis[now.state][now.x1][now.y1])
return true;
}
else
{
now.x1=x+dir[d][0]; now.y1=y+dir[d][1];
now.x2=now.x1; now.y2=now.y1+1;
if (isok(now.x1,now.y1)&&isok(now.x2,now.y2)&&!vis[now.state][now.x1][now.y1])
return true;
}
}
else
{
if (d>1){
now.x1=x+dir[d][0]; now.y1=y+dir[d][1];
if (d==3) now.x1=now.x1+dir[d][0] , now.y1=now.y1+dir[d][1];
now.state=0;
if (isok(now.x1,now.y1)&&mp[now.x1][now.y1]!='E'&&!vis[now.state][now.x1][now.y1])
return true;
}
else
{
now.x1=x+dir[d][0]; now.y1=y+dir[d][1];
now.x2=now.x1+1; now.y2=now.y1;
if (isok(now.x1,now.y1)&&isok(now.x2,now.y2)&&!vis[now.state][now.x1][now.y1])
return true;
}
}
return false;
} int bfs(int nn)
{
Node st,now;
memset(vis,false,sizeof(vis));
if (nn<3)
{
st.x1=pos[0];
st.y1=pos[1];
st.state=0;
}
else
{
st.x1=pos[0]; st.y1=pos[1];
st.x2=pos[2]; st.y2=pos[3];
if (st.x1==st.x2) st.state=1;
else st.state=2;
}
st.step=0;
vis[st.state][st.x1][st.y1]=true;
queue<Node>Q;
Q.push(st);
while (!Q.empty())
{
st=Q.front();Q.pop();
if (st.state==0&&st.x1==ex&&st.y1==ey)
return st.step;
// printf("\n");
// printf("=====================\n");
for (int i=0;i<4;i++)
{
now.state=st.state;
if (Move(st.x1,st.y1,i,now))
{
// printf("%d %d %d \n",now.state,now.x1,now.y1);
vis[now.state][now.x1][now.y1]=true;
now.step=st.step+1;
Q.push(now);
}
}
}
return -1;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("C:/Users/lyf/Desktop/IN.txt","r",stdin);
#endif
int i,j,num;
while (scanf("%d%d",&n,&m))
{
if (n==0&&m==0) break;
num=0;
for (i=0;i<n;i++)
{
scanf("%s",mp[i]);
for (j=0;j<m;j++)
{
if (mp[i][j]=='X')
{
pos[num++]=i;
pos[num++]=j;
}
if (mp[i][j]=='O')
{
ex=i;ey=j;
}
}
}
int ans=bfs(num);
if (ans==-1) printf("Impossible\n");
else printf("%d\n",ans);
}
return 0;
}

Bloxorz I (poj 3322 水bfs)的更多相关文章

  1. Bloxorz I POJ - 3322 (bfs)

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

  2. 【POJ 3322】 Bloxorz I

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

  3. POJ 2252 Dungeon Master 三维水bfs

    题目: http://poj.org/problem?id=2251 #include <stdio.h> #include <string.h> #include <q ...

  4. POJ 3322 Bloxorz I

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

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

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

  6. POJ 3322 Bloxorz

    #include<cstring> #include<algorithm> #include<iostream> #include<cstdio> #i ...

  7. POJ 3322(广搜)

    ---恢复内容开始--- http://poj.org/problem?id=3322 题意:http://jandan.net/2008/01/24/bloxorz.html就是这个鬼游戏 我也是郁 ...

  8. Pots(POJ - 3414)【BFS 寻找最短路+路径输出】

    Pots(POJ - 3414) 题目链接 算法 BFS 1.这道题问的是给你两个体积分别为A和B的容器,你对它们有三种操作,一种是装满其中一个瓶子,另一种是把其中一个瓶子的水都倒掉,还有一种就是把其 ...

  9. POJ 3026(BFS+prim)

    http://poj.org/problem?id=3026 题意:任意两个字母可以连线,求把所有字母串联起来和最小. 很明显这就是一个最小生成树,不过这个题有毒.他的输入有问题.在输入m和N后面,可 ...

随机推荐

  1. Remember the Word UVALive - 3942 DP_字典树

    每个小单词的长度都是小于等于100的,这是个重要的突破口. Code: #include <cstdio> #include <algorithm> #include < ...

  2. 树上倍增求LCA

    大概思想就是,节点$i$的第$2^{j}$个父节点是他第$2^{j-1}$个父亲的第$2^{j-1}$个父亲 然后可以$O(nlogn)$时间内解决…… 没了? //fa[i][j]表示i的第2^j个 ...

  3. 用TamperMonkey去掉cdsn中的广告

    最近CSDN需要登录后才能查看更多内容,有点影响心情 解决方案 添加一段书签 javascript:(function(){document.getElementById('article_conte ...

  4. IE模式下EasyUI Combobox无效问题

    近期开发过程中遇到IE浏览器Combobox无法正常加载问题. 经过一番百度说IE渲染过快导致页面渲染完了easyUI Combobox还没有加载.设置延迟加载后依旧无效. 后将input标签的Cla ...

  5. BZOJ 2555 SubString(LCT+后缀树)

    喜闻乐见的LCT+SAM 此题要求动态插入,直接上后缀树.然后询问其实就是求一个节点的子树后缀结束节点的个数. 因为建立后缀树需要插入和删除,就直接上LCT.每次加入一个点,把它到根的路径加一 (现在 ...

  6. Linux学习02--Linux一切皆文件

    Linux学习第二部 Linux一切皆对象 啊啊啊啊啊,今天被学妹说太直了,嘤嘤嘤. 学习linux两三天了,前期感觉并不难,只是命令多,多记记多敲一敲就能都记住了.希望自己能够坚持下去吧! 下面是根 ...

  7. python基础8(装饰器)

    1.装饰器本质 装饰器的本质:一个闭包函数 装饰器的功能:在不修改原函数及其调用方式的情况下对原函数功能进行扩展 2.装饰器函数 假设要写一个输出函数执行时间的装饰器 def timer(func): ...

  8. 紫书 例题 11-5 UVa 10048 (Floyd求最大权值最小的路径)

    这道题是Floyd的变形 改成d[i][j] = min(d[i][j], max(d[i][k], d[k][j]))就好了. #include<cstdio> #include< ...

  9. 紫书 习题8-3 UVa 12545 (构造法)

    参考了https://blog.csdn.net/catglory/article/details/47188949 最后推出来操作的个数为问号的个数 加上 同一位置上S串为0而T串为1的位置数量 与 ...

  10. MySQL数据类型及后面小括号的意义

    1,数值类型 1.1数值类型的种类 标准 SQL 中的数值类型,包括严格数值类型(INTEGER.SMALLINT.DECIMAL.NUMERIC),以及近似数值数据类型(FLOAT.REAL.DOU ...