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. SpringBoot全局异常的捕获设置

    1.新建立一个捕获异常的实体类 如:LeeExceptionHandler package com.leecx.exception; import javax.servlet.http.HttpSer ...

  2. linux系统如何安装vmware Tools(下面以CentOS为例)

    VMwareTools是VMware虚拟机中很重要的一个工具包,有些时候在虚拟机中安装完操作系统会缺少网卡驱动,不能上网,这时只要安装VMwareTools就可以解决问题,下面以CentOS为例,来说 ...

  3. Openssl rsautl命令

    一.简介 rsautl指令能够使用RSA算法签名,验证身份,加密/解密数据 二.语法 openssl rsautl [-in file] [-out file] [-inkey file] [-pas ...

  4. [C++] struct memory allocation

    MAX-byte alignment (最大单位对齐) typedef struct user USER; typedef struct employee E; struct user{ ]; //t ...

  5. 基于CacheManager组件的缓存产品配置

    一.Couchbase 使用CacheManager组件,在配置Couchbase缓存支持时,由于对配置节cache handle命名规则要求不了解,费了点时间查了源码才明白. section配置节 ...

  6. mysql链接错误:2003 can't connect to mysql server on 10038

    出现这个错误原因是端口号不是3306.  打开D:\Program Files\MySQL\MySQL Server 5.5 \my.ini文件,当然还有其他的.ini的文件:   [client] ...

  7. List of HTTP header fields

    https://en.wikipedia.org/wiki/List_of_HTTP_header_fields Content-Type The MIME type of the body of t ...

  8. .NET基础 (06)面向对象的实现

    面向对象的实现1 C#中类可以有多个父类.可以实现多个接口吗2 简述C#中重写.重载和隐藏的概念3 为什么在构造方法中调用虚方法会导致问题4 在C#中如何声明一个类不能被继承 面向对象的实现 1 C# ...

  9. 【图解HTTP】第一章 了解web及网络基础

    [图解HTTP]了解Web及网络基础 Web页面是如何呈现的?根据Web浏览器地址栏中指定的URL,Web浏览器从Web服务器端获取文件资源(resource)等信息,从而显示出Web页面. 这种通过 ...

  10. struts2下面如何同时使用servlet,就是如何实现struts与servlet共存

    转载 原文链接:https://blog.csdn.net/u013358115/article/details/20706607 问题 项目要求struts2和servlet能够共存,就是strut ...