Poj 3322 bloxorz I (BFS + auxiliary array + state compression + some of your own search comprehension)

Source: Internet
Author: User

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 containsR
Lines 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
 
I knocked on the train of thought for a morning. The train of thought was similar to that of Fengfeng, but I had no experience in program structuring, so the Train of Thought did not look very clear.
Then it seems that the question has missed three initial states: vertical, horizontal, and vertical...
The method of secondary Array Construction by peak and peak is really too high-end .... (This cainiao really hasn't come up with this kind of practice... Well... For future reference)
Although the code I wrote is messy, I still want to find time to change it and paste it out .... Well, the following is what I typed after reading the code of Fengfeng .... Post it first...
 
Solution: this state -------> (through some transformation) ------> next state
     
Determine the array dimension accordingly
(Note: The same is true for sta [] []. Current status ------> (change direction) ---------> next status)
 
Code:
// Box occupies two cells, so a struct is used to indicate that the X-axis and Y-axis are (x1, Y1), and the X-axis and Y-axis are (x1, Y1) and (X2, Y2) respectively) // State indicates the status. // steps indicates the number of current steps. // The changes from one step to the next are related to the current status and the direction of the flip. // The current status is 0 = standing, 1 = lying horizontally, 2 = Lying vertically // 0 = up, 1 = down, 2 = left, 3 = right // open a two-dimensional secondary array to represent the Coordinate Transformation // dx1 [3] [4], dy1 [3] [4] .. Are secondary arrays. // note that the path and status to pass must be marked. Therefore, vis [] [] [], coordinates of the first two dimensions of the record // The obsolete state of the third dimension record # include <cstdio> # include <iostream> # include <cstring> using namespace STD; struct node {int x1; int X2; int Y1; int Y2; int state; int steps;} start; // start indicates the starting position of node Q [1000000]; // if it is set to 100000, runtime errorbool vis [510] [510] [3]; char maps [510] [510]; int DDX [4] = }; int ddy [4] = {,-}; int dx1 [3] [4] = }, {-, }}; int dy1 [3] [4] = {,-}, {,-}, {,-}; Int dx2 [3] [4] = }, {-,}, {-,}; int dy2 [3] [4] = }, {,-}; int st [3] [4] = {,}, {,}, {,}; int R, c; int endx, Endy; int inmaps (int x, int y) // determine whether {return x> = 0 & x <R & Y> = 0 & Y <C;} bool isok (node & T) is in the specified range) {If (! Inmaps (T. X1, T. Y1) |! Inmaps (T. x2, T. y2) return false; If (maps [T. x1] [T. y1] = '#' | maps [T. x2] [T. y2] = '#') return false; If (maps [T. x1] [T. y1] = 'E' & T. state = 0) return false; return true;} // records the initial position and status. // records the horizontal and vertical coordinates of the destination. Void create () {int I, j; for (I = 0; I <r; I ++) {for (j = 0; j <C; j ++) {If (maps [I] [J] = 'O') {endx = I; Endy = J ;} else if (maps [I] [J] = 'X') {start. x1 = start. x2 = I; start. y1 = start. y2 = J; int dx = I + DDX [1]; int DY = J + ddy [1]; int R X = I + DDX [3]; int ry = J + ddy [3]; If (inmaps (RX, ry) & maps [RX] [ry] = 'X') {start. state = 2; start. x2 = RX; start. y2 = ry; maps [RX] [ry] = maps [I] [J] = '. ';} else if (inmaps (dx, Dy) & maps [dx] [dy] = 'X') {start. state = 1; start. x2 = DX; start. y2 = Dy; maps [dx] [dy] = maps [I] [J] = '. ';} else {start. state = 0; maps [I] [J] = '. ';}}} start. steps = 0;} // Why do we need to create two node struct M and N? // my understanding is: Because expand cleverly transfers references // if it cannot be extended, do not join the queue, and do not change the original node // use the copy as the expan D () function parameter bool expand (node & T, int d) {T. x1 + = dx1 [T. state] [d]; T. y1 + = dy1 [T. state] [d]; T. x2 + = dx2 [T. state] [d]; T. y2 + = dy2 [T. state] [d]; T. state = sT [T. state] [d]; T. steps ++; If (! Isok (t) return false; If (! Vis [T. x1] [T. y1] [T. state]) {vis [T. x1] [T. y1] [T. state] = 1; return true;} return false;} void BFS () {node M, N; int front = 0, rear = 0; Q [rear ++] = start; while (front <rear) {M = Q [Front]; for (INT I = 0; I <4; I ++) {n = m; If (expand (n, I) {q [rear ++] = N; If (N. x1 = endx & N. y1 = Endy & N. state = 0) {printf ("% d \ n", N. steps); Return ;}}front ++;} printf ("impossible \ n");} int main () {While (~ Scanf ("% d", & R, & C) {If (r = 0 & C = 0) break; For (INT I = 0; I <r; I ++) scanf ("% s", maps [I]); memset (VIS, 0, sizeof (VIS); Create (); BFS () ;}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.