POJ3322 Bloxorz I

暴搜,next数组与处理一下(小技巧)

 #include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std; #define res register int
const int N=;
char s[N][N];
int n,m,d[N][N][];
struct node{int x,y,lie;};//lie=0 立 lie=1 横向躺 lie=2 纵向躺
node st,ed;
queue<node> q;
const int dx[]={,,-,},dy[]={-,,,}; inline bool valid(int x,int y){
return x>&&x<=n&&y>&&y<=m;
} inline void read()
{
for(res i= ; i<=n ; i++)
for(res j= ; j<=m ; j++) cin>>s[i][j];
for(res i= ; i<=n ; i++)
for(res j= ; j<=m ; j++)
{
if(s[i][j]=='O') ed.x=i,ed.y=j,ed.lie=,s[i][j]='.';
else if(s[i][j]=='X')
{
for(res k= ; k< ; k++)
{
int x=i+dx[k],y=j+dy[k];
if(valid(x,y) && s[x][y] == 'X') {
st.x=min(i,x),st.y=min(j,y),st.lie=k<?:;
s[i][j]=s[x][y]='.';
break;
}
if(s[i][j]=='X') st.x=i,st.y=j,st.lie=,s[i][j]='.';
}
}
}
} const int next_x[][]={{,,-,},{,,-,},{,,-,}};
const int next_y[][]={{-,,,},{-,,,},{-,,,}};
const int next_lie[][]={{,,,},{,,,},{,,,}}; inline bool valid(node t)
{
if(!valid(t.x,t.y)) return false;
int nx=t.x,ny=t.y;
if(s[nx][ny]=='#') return false;
if(t.lie== && s[nx][ny]!='.') return false;
if(t.lie== && s[nx][ny+]=='#') return false;
if(t.lie== && s[nx+][ny]=='#') return false;
return true;
} inline int bfs()
{
for(res i= ; i<=n ; i++)
for(res j= ; j<=m ; j++)
for(res k= ; k< ; k++) d[i][j][k]=-;
while(q.size()) q.pop();
d[st.x][st.y][st.lie]=; q.push(st);
while(q.size())
{
node now=q.front(); q.pop();
for(res i= ; i< ; i++)
{
node t;
t.x=now.x+next_x[now.lie][i];
t.y=now.y+next_y[now.lie][i];
t.lie=next_lie[now.lie][i];
if(!valid(t)) continue;
if(d[t.x][t.y][t.lie]==-)
{
d[t.x][t.y][t.lie]=d[now.x][now.y][now.lie]+;
q.push(t);
if(t.x==ed.x && t.y==ed.y && t.lie==ed.lie)
return d[t.x][t.y][t.lie];
}
}
}
return -;
} int main()
{
while(scanf("%d %d",&n,&m)== && n && m)
{
read();
int ans=bfs();
if(ans==-) puts("Impossible");
else printf("%d\n",ans);
} return ;
}

POJ3322Bloxorz I的更多相关文章

随机推荐

  1. Cw210开发板

    达内培训嵌入式开发板 qt + kernel uboot toolchain

  2. 我的ubuntu14.04配置

    完全卸载java(移除所有 Java相关包 (Sun, Oracle, OpenJDK, IcedTea plugins, GIJ)) 转载修改, 原文见:http://blog.csdn.net/s ...

  3. Python - excel 详解

    安装 pip install xlrd        # 读xlspip install xlwt     # 写xlspip install xlutils     # 改写xls 读取 Excel ...

  4. [GO]有缓冲通道

    有缓冲通道就是在有能力保留数据的通道,那么通道在满的时候或者通道是空的时候,存数据和取数据就会发生阻塞 package main import ( "fmt" "time ...

  5. kcp结构体字段含义

    conv 会话IDmtu 最大传输单元mss 最大分片大小state 连接状态(0xFFFFFFFF表示断开连接)snd_una 第一个未确认的包snd_nxt 下一个待分配的包的序号rcv_nxt ...

  6. 编写高质量代码改善C#程序的157个建议——建议152:最少,甚至是不要注释

    建议152:最少,甚至是不要注释 以往,我们在代码中不写上几行注释,就会被认为是钟不负责任的态度.现在,这种观点正在改变.试想,如果我们所有的命名全部采用有意义的单词或词组,注释还有多少存在的价值. ...

  7. 长时间停留在calculating requirements and dependencies

    如果安装插件的时候,Eclipse花费了很长的时间calculating requirements and dependencies(计算需求和依赖性 )这个问题通常就是在点击安装之后显示“Calcu ...

  8. 深入理解java虚拟机(八)类加载过程详解

    类从被加载到虚拟机内存开始,到卸载出内存为止,它的整个生命周期包括:加载(Loading).验证(Verification).准备(Preparation).解析(Resolution).初始化(In ...

  9. 【modelsim常见问题集锦】Can't launch the ModelSim-Altera software

    一.Can't launch the ModelSim-Altera software Error: Can't launch the ModelSim-Altera software -- the ...

  10. 前端技术俗语js

    注:原文是英文,本文是我翻译的.有人把我翻译的内容原文照抄,放到他自己的专栏,搞得有人问我是不是我抄袭了……请支持我的劳动成果,花了两个小时翻译的,谢谢.转载请注明译者为方应杭. 嘿,我最近接到一个 ...