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

  1. 7 7
  2. #######
  3. #..X###
  4. #..##O#
  5. #....E#
  6. #....E#
  7. #.....#
  8. #######
  9. 0 0

Sample Output

  1. 10
  2.  
  3. 题目:Bloxorz I
    大意:(非博主翻译)
šBloxorz是一个风靡世界的小游戏。Bloxorz的地图是一个N行M列的矩阵,每个位置可能是硬地(用.表示)、易碎地面(用E表示)、禁地(用#表示)、起点(用X表示)或终点(用O表示)。你的任务是操作一个1*1*2的长方体。这个长方体在地面上有两种放置形式,“立”在地面上(1*1的面接触地面)或者“躺”在地面上(1*2的面接触地面)。在每一步操作中,可以按上下左右四个键之一。按下之后,长方体向对应的方向沿着棱滚动90度。任意时刻,长方体不能有任何部位接触禁地(否则就会掉下去),并且不能立在易碎地面上(否则会因为压强太大掉下去)。X标识长方体的起始位置,地图上可能有一个X或者两个相邻的X。地图上唯一的一个O标识目标位置。求把长方体移动到目标位置(即立在O上)所需要的最少步数。如果无解,输出Impossible。在移动过程中,X和O标识的位置都可以看作是硬地被利用,3<=N,M<=500。
  1. 有两个相邻的X,理解为横躺。
    思路:
    就是一个bfs,不过不太一样,可以这么理解一下,如果X是正方体和平常迷宫问题有区别吗?这个题目只是X是长方体,
    所以在id不同的位置,上下左右移动也不同。
    X,若只有一个说明是竖放,两个则说明是延x轴或者y轴放。
    X会上下左右移动,这时候就麻烦一点了,因为以前的vis数组是一个二维的直接存放位置即可,但是这个时候若是横放,则会占据
    两个位置,不过不用急,很简单就可以想到的,用三维数组,以左上角为标准,和id一起构成判断标准,这样就不会重判了。
    具体代码:
    main函数里面,先读取这个图像,然后根据X判断出横躺还是竖放,其次读取O的位置,这个为目标位置
    bfs函数里面,先将这个X的位置存放进去,然后进去搜索。
    有一个check函数,就是把这个搜索之后的数读进去,判断,有没有到禁格里,有没有超出去,简单来说,就是是否合法。
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <queue>
  5. #include <iostream>
  6. #include <algorithm>
  7. using namespace std;
  8. int n,m;
  9. bool vis[5][505][505];
  10. char a[505][505];
  11. struct node
  12. {
  13. int id;
  14. int r1,c1,r2,c2;
  15. int step;
  16. node(int id,int r1,int c1,int r2,int c2,int step):id(id),r1(r1),c1(c1),r2(r2),c2(c2),step(step){}
  17. };
  18. node ed=node(0,0,0,0,0,0),exa=node(0,0,0,0,0,0);
  19. int to[3][4][4]={
  20. {{-2,0,-1,0},{1,0,2,0},{0,1,0,2},{0,-2,0,-1}},
  21. {{-1,0,-1,0},{1,0,1,0},{0,2,0,1},{0,-1,0,-2}},
  22. {{-1,0,-2,0},{2,0,1,0},{0,1,0,1},{0,-1,0,-1}}
  23. };
  24. queue<node>que;
  25. int cmp(int &x1,int &y1,int &x2,int &y2)//确定是竖放还是横躺
  26. {
  27. if(x1==x2&&y1==y2) return 1;
  28. else if(x1==x2)
  29. {
  30. if(y1>y2) swap(y1,y2);
  31. return 2;
  32. }
  33. else
  34. {
  35. if(x1>x2) swap(x1,x2);
  36. return 3;
  37. }
  38. }
  39. void check()//检查是否合法
  40. {
  41. if(a[exa.r1][exa.c1]=='#') return ;
  42. if(a[exa.r2][exa.c2]=='#') return;
  43. if(exa.id==1&&(a[exa.r1][exa.c1]=='E'||a[exa.r2][exa.c2]=='E')) return ;
  44. if(vis[exa.id][exa.r1][exa.c1]) return ;
  45. if(exa.r1<1||exa.r2<1||exa.r1>n||exa.r2>n||exa.c1<1||exa.c2<1||exa.c1>m||exa.c2>m) return ;
  46. vis[exa.id][exa.r1][exa.c1]=1;
  47. que.push(exa);
  48. }
  49. int bfs()//搜索,和普通一样的写法
  50. {
  51. while(!que.empty()) que.pop();
  52. memset(vis,0,sizeof(vis));
  53. vis[ed.id][ed.r1][ed.c1]=1;
  54. que.push(ed);
  55. while(!que.empty())
  56. {
  57. ed=que.front();
  58. //printf("%d (%d,%d),(%d,%d)\n",ed.id,ed.r1,ed.c1,ed.r2,ed.c2);
  59. que.pop();
  60. if(ed.id==1&&a[ed.r1][ed.c2]=='O') return ed.step;
  61. for(int i=0;i<4;i++)
  62. {
  63. exa.r1=ed.r1+to[ed.id-1][i][0];
  64. exa.c1=ed.c1+to[ed.id-1][i][1];
  65. exa.r2=ed.r2+to[ed.id-1][i][2];
  66. exa.c2=ed.c2+to[ed.id-1][i][3];
  67. exa.step=ed.step+1;
  68. exa.id=cmp(exa.r1,exa.c1,exa.r2,exa.c2);
  69.  
  70. check();
  71. // printf("id=%d (%d,%d),(%d,%d) i=%d\n",exa.id,exa.r1,exa.c1,exa.r2,exa.c2,i);
  72. }
  73. }
  74. return -1;
  75. }
  76.  
  77. int main()
  78. {
  79. while(scanf("%d%d",&n,&m)!=EOF&&n+m)
  80. {
  81. int cnt=0,r1,c1,r2,c2;
  82. for(int i=1;i<=n;i++)
  83. {
  84. scanf("%s",a[i]+1);
  85. for(int j=1;j<=m;j++)
  86. {
  87. if(a[i][j]=='X')
  88. {
  89. cnt++;
  90. if(cnt==1)
  91. {
  92. r1=r2=i;
  93. c1=c2=j;
  94. }
  95. else
  96. {
  97. r2=i;
  98. c2=j;
  99. }
  100. }
  101. }
  102. }
  103. int t=cmp(r1,c1,r2,c2);
  104. ed=node(t,r1,c1,r2,c2,0);
  105. int exe=bfs();
  106. if(exe==-1) printf("Impossible\n");
  107. else printf("%d\n",exe);
  108. }
  109. return 0;
  110. }

  

  1.  

寒假训练——搜索 E - Bloxorz I的更多相关文章

  1. 寒假训练——搜索 K - Cycle

    A tournament is a directed graph without self-loops in which every pair of vertexes is connected by ...

  2. 寒假训练——搜索——C - Robot

    The Robot Moving Institute is using a robot in their local store to transport different items. Of co ...

  3. 寒假训练——搜索 G - Xor-Paths

    There is a rectangular grid of size n×mn×m . Each cell has a number written on it; the number on the ...

  4. J - Abbott's Revenge 搜索 寒假训练

    题目 题目大意:这个题目就是大小不超过9*9的迷宫,给你起点终点和起点的方向,让你进行移动移动特别之处是不一定上下左右都可以,只有根据方向确定可以走的方向.思路:需要写一个读入函数,这个需要读入起点, ...

  5. 寒假训练 A - A Knight's Journey 搜索

    Background The knight is getting bored of seeing the same black and white squares again and again an ...

  6. 算法专题训练 搜索a-T3 Ni骑士(ni)

    搞了半天八数码弄不出来就只好来打题解  这道题是在搜索a碰到的(链接: http://pan.baidu.com/s/1jG9rQsQ ) 感觉题目最大亮点就是这英文简写"ni", ...

  7. HRBUST - 2347 - 递归画图 - vj大一上寒假训练2.11

    其他题可由本题变形得到. 思路:利用坐标dfs搜索. 注意:1,初始化.2,坐标实时更新(x,y) 代码: #include<iostream> #include<cstdio> ...

  8. 寒假集训——搜索 B - Sudoku

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream&g ...

  9. 寒假集训——搜索 D - Cubes for Masha

    #include <stdio.h> #include <stdlib.h> #include <iostream> #include <string.h&g ...

随机推荐

  1. Asp.net mvc 5 CRUD代码自动生成工具- vs.net 2013 Saffolding功能扩展

    Asp.net mvc 5 CRUD代码自动生成工具 -Visual Studio.net2013 Saffolding功能扩展 上次做过一个<Asp.net webform scaffoldi ...

  2. Java基础之循环语句、条件语句、switch case 语句

    Java 循环结构 - for, while 及 do...while 顺序结构的程序语句只能被执行一次.如果您想要同样的操作执行多次,,就需要使用循环结构. Java中有三种主要的循环结构: whi ...

  3. ssh采用expect实现自动输入密码登录、拷贝

    1. 引言 最近做了一个项目,需要频繁与另一台主机进行文件的传输:中间想到了很多方式:FTP.samba.curl等,但是还是感觉scp最好用. SCP使用教程可参阅:http://www.jb51. ...

  4. [转]TFS2010 Team Project Collections

    本文转自:https://www.cnblogs.com/shanyou/archive/2010/04/14/1712252.html Team Foundation Server 2010有一个改 ...

  5. C#实现微信AES-128-CBC加密数据的解密

    小程序登录时,获得用户的信息,只是昵称,无法用作ID.而有用的数据,都加密着,腾讯给出了解密的方法: 加密数据解密算法 接口如果涉及敏感数据(如wx.getUserInfo当中的 openId 和un ...

  6. 【Core】创建简单的Core MVC项目

    创建项目: 首先:打开vs选中新建项目- >选中.NET Core - >ASP.NET Core Web应用程序: 然后:在选择web应用程序,注意上面要选中.net Core 别选错了 ...

  7. JavaSE 异常抛光解析

    异常 异常指的是程序中的不正常现象,一般异常都是由第三方数据的使用造成的.java中每种异常现象都会有一个对应的异常类.java对异常的处理方式就是终止程序.异常机制其实是为了帮助我们找到程序中的问题 ...

  8. APP如何进行通信的

    什么是B/S架构(Browser/server):浏览器和服务器架构

  9. csharp:SMO run sql script

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  10. Power BI 与 Azure Analysis Services 的数据关联:4、Power BI 连接到Azure Analysis Services 并展示

    Power BI 与 Azure  Analysis Services 的数据关联:4.Power BI 连接到Azure  Analysis Services 过使用服务器名称别名,用户可以使用较短 ...