#include<cstring>
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<queue>
#define de system("pause");
using namespace std;
int r,c;
char s[][];
int aim[];
int ans=;
bool bz[][][];
bool check(int way,int x,int y)
{
if(x<||x>r||y<||y>c)return ;
if(way==)
{
if(s[x][y]=='#'||s[x][y+]=='#')return ;
if(bz[x][y][])return ;
}
if(way==)
{
if(s[x][y]=='#'||s[x+][y]=='#')return ;
if(bz[x][y][])return ;
}
if(way==)
{
if(s[x][y]=='#'||s[x][y]=='E')return ;
if(bz[x][y][])return ;
}
return ;
}
struct node
{
int way,x,y,step;
node(int wa,int xx,int yy,int ste)
{
way=wa;
x=xx;
y=yy;
step=ste;
}
};
queue<node> q;
void so(int x,int y,int way,int step)
{
bz[x][y][way]=;
q.push(node(way,x,y,step));
if(way==&&s[x][y]=='O')
{
// cout<<step<<endl;
ans=min(ans,step);
}
}
void bfs()
{
while(!q.empty())
{
node now=q.front();
int x=now.x,y=now.y,way=now.way,step=now.step;
q.pop();
// bz[x][y][way]=0;
if(way==)
{
if(check(,x-,y)) so(x-,y,,step+);
if(check(,x+,y)) so(x+,y,,step+);
if(check(,x,y-)) so(x,y-,,step+);
if(check(,x,y+)) so(x,y+,,step+);
}
if(way==)
{
if(check(,x-,y)) so(x-,y,,step+);
if(check(,x+,y)) so(x+,y,,step+);
if(check(,x,y-)) so(x,y-,,step+);
if(check(,x,y+)) so(x,y+,,step+);
}
if(way==)
{
if(check(,x-,y)) so(x-,y,,step+);
if(check(,x+,y)) so(x+,y,,step+);
if(check(,x,y-)) so(x,y-,,step+);
if(check(,x,y+)) so(x,y+,,step+);
} }
}
int main()
{
while()
{
while(q.size())q.pop();
memset(bz,,sizeof bz);
scanf("%d%d",&r,&c);
if(r==&&c==)break;
int a[][];
int cnt=;
ans=;
for(int i=;i<=r;++i)
{
scanf("%s",s[i]+);
for(int j=;j<=c;j++)
{
if(s[i][j]=='X')
{
++cnt;
a[cnt][]=i,a[cnt][]=j;
}
}
}
if(cnt==) q.push(node(,a[cnt][],a[cnt][],));
else
{
if(a[][]==a[][]) q.push(node(,a[][],a[][],));
else q.push(node(,a[][],a[][],));
}
bfs();
if(ans==)puts("Impossible");
else cout<<ans<<endl;
}
return ;
}

最新文章

  1. C#开机自动启动程序代码
  2. Java通过几种经典的算法来实现数组排序
  3. datanode启动不了
  4. PHP学习笔记:对命名空间(namespace)学习资料的翻译
  5. C#操作求出SQL中某一字段所有行的和方法!
  6. bzoj2719[Violet 4]银河之星
  7. NSTimer你真的会用了吗
  8. ubuntu下安装ssh服务器方法
  9. MySQL执行计划extra中的using index 和 using where using index 的区别
  10. 【FAQ系列】关于SQL_Errno:1677导致主从复制中断的思考和实践
  11. Python获取会议部分的信息内容(不断完善中)
  12. CodeForces 516B Drazil and Tiles 其他
  13. android开发(0):android studio的下载安装与简单使用 | sdk的安装与编译运行
  14. (母函数 Catalan数 大数乘法 大数除法) Train Problem II hdu1023
  15. secFox setting
  16. python中pip
  17. dat.gui.js
  18. 【JavaScript&amp;jQuery】5秒跳转
  19. lintcode 66.67.68 二叉树遍历(前序、中序、后序)
  20. 你们对LinearLayout线性布局中Layout_weight的误解

热门文章

  1. Maven 教程(6)— Maven之pom.xml文件简单说明
  2. vertica审计日志
  3. AKKA 常见异常
  4. MySQL查询指定表的字段名称
  5. Codeforces Round #557 (Div. 1) 简要题解
  6. JDK9对集合添加的优化
  7. java变量的声明和数据类型
  8. ML学习笔记之TF-IDF原理及使用
  9. .NET Core解析DNS域名或主机名的方法
  10. c#中泛型