Poj 3322 bloxorz I (BFS + 3D weighting)

Source: Internet
Author: User
Bloxorz I
Time limit:2000 ms   Memory limit:65536 K
Total submissions:4808   Accepted:1592

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 integersRAndC(3 ≤ r, c ≤ 500) which stands for number of rows and columns of the plane. That follows the plane, which containsRLines
AndCCharacters 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'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

Poj monthly -- 2007.08.05, Rainer

PS: in fact, this is a game with a link that can be played in and then done this question very well (Don't Be addicted)

Http://user.qzone.qq.com/1019256391? Ptlang = 2052 #! APP = 2 & via = qz. hashrefresh & Pos = 1309244406

Train of Thought: as long as the box status is displayed, it is easy to simulate the box rolling and determination is not a problem.

Code:

// This is a whimsical question. Use bool to indicate 0 1 2 // an error that can be avoided) feature # include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <queue> # define maxn 510 using namespace STD; int n, m, ANS, CNT, xxc; int SX [2], Sy [2], ex, ey; char s [maxn]; int MP [maxn] [maxn]; bool vis [4] [maxn] [maxn]; // condition of the heavy box + coordinate of the box in the upper left corner struct node {int X [2], Y [2]; int step, type;} cur, now; queue <node> q; bool isok () // you can judge the cur {int I, j, T, XX [2], YY [2]; t = cur. type; XX [0] = cur. X [0]; XX [1] = cur. X [1]; YY [0] = cur. Y [0]; YY [1] = cur. Y [1]; // printf ("T: % d XX [0]: % d YY [0]: % d \ n", T, XX [0], YY [0]); If (vis [T] [XX [0] [yy [0]) return false; If (t = 1) {If (MP [XX [0] [yy [0] = 0 | MP [XX [0] [yy [0] = 2) return false; // If the map is in the easily broken cell} else {for (I = 0; I <2; I ++) status) {If (MP [XX [I] [yy [I] = 0) return false;} return true;} bool BFS () {int I, j; in T NX [2], NY [2], nstep, ntype; memset (VIS, 0, sizeof (VIS); While (! Q. empty () Q. pop (); If (xxc) {If (SX [1]-SX [0] = 1) cur. type = 3; else cur. type = 2; cur. X [0] = SX [0]; cur. X [1] = SX [1]; cur. Y [0] = Sy [0]; cur. Y [1] = Sy [1];} else {cur. type = 1; cur. X [0] = SX [0]; cur. Y [0] = Sy [0];} cur. step = 0; vis [cur. type] [SX [0] [Sy [0] = 1; q. push (cur); While (! Q. empty () {now = Q. front (); q. pop (); ntype = now. type; nstep = now. step; NX [0] = now. X [0]; NX [1] = now. X [1]; ny [0] = now. Y [0]; ny [1] = now. Y [1]; If (ntype = 1 & NX [0] = ex & ny [0] = ey) {ans = nstep; return true ;} cur. step = nstep + 1; if (ntype = 1) {cur. type = 2; cur. Y [0] = ny [0]-2; cur. Y [1] = ny [0]-1; cur. X [0] = cur. X [1] = NX [0]; If (isok () // left {vis [2] [cur. X [0] [cur. Y [0] = 1; q. push (cur);} cur. Y [0] = ny [0] + 1; cur. Y [1] = ny [0] + 2; If (isok () // right {vis [2] [cur. X [0] [cur. Y [0] = 1; q. push (cur);} cur. type = 3; cur. X [0] = NX [0]-2; cur. X [1] = NX [0]-1; cur. Y [0] = cur. Y [1] = ny [0]; If (isok () // {vis [3] [cur. X [0] [cur. Y [0] = 1; q. push (cur);} cur. X [0] = NX [0] + 1; cur. X [1] = NX [0] + 2; If (isok () // {vis [3] [cur. X [0] [cur. Y [0] = 1; q. push (cur) ;}} else if (ntype = 2) {cur. type = 2; cur. X [0] = cur. X [1] = NX [0]-1; cur. Y [0] = ny [0]; cur. Y [1] = ny [1]; If (isok () // {vis [2] [cur. X [0] [cur. Y [0] = 1; q. push (cur);} cur. X [0] = cur. X [1] = NX [0] + 1; if (isok () // {vis [2] [cur. X [0] [cur. Y [0] = 1; q. push (cur);} cur. type = 1; cur. X [0] = NX [0]; cur. Y [0] = ny [0]-1; if (isok () // left {vis [1] [cur. X [0] [cur. Y [0] = 1; q. push (cur);} cur. Y [0] = ny [0] + 2; If (isok () // right {vis [1] [cur. X [0] [cur. Y [0] = 1; q. push (cur) ;}} else {cur. type = 1; cur. X [0] = NX [0]-1; cur. Y [0] = ny [0]; If (isok () // {vis [1] [cur. X [0] [cur. Y [0] = 1; q. push (cur);} cur. X [0] = NX [0] + 2; If (isok () // {vis [1] [cur. X [0] [cur. Y [0] = 1; q. push (cur);} cur. type = 3; cur. X [0] = NX [0]; cur. X [1] = NX [1]; cur. Y [0] = cur. Y [1] = ny [0]-1; if (isok () // left {vis [3] [cur. X [0] [cur. Y [0] = 1; q. push (cur);} cur. Y [0] = cur. Y [1] = ny [0] + 1; if (isok () // right {vis [3] [cur. X [0] [cur. Y [0] = 1; q. push (cur) ;}}return false ;}int main () {int I, j; while (scanf ("% d", & N, & M ), N | M) {memset (MP, 0, sizeof (MP); xxc =-1; for (I = 1; I <= N; I ++) {scanf ("% s", S); For (j = 1; j <= m; j ++) {If (s [J-1] = '. ') {MP [I] [J] = 1;} else if (s [J-1] = 'O') {MP [I] [J] = 1; ex = I; ey = J;} else if (s [J-1] = 'E') {MP [I] [J] = 2 ;} else if (s [J-1] = 'X') {xxc ++; MP [I] [J] = 1; SX [xxc] = I; sy [xxc] = J ;}}if (BFS () printf ("% d \ n", ANS); else printf ("impossible \ n ");} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.