Hihocoder_ Push the box

Source: Internet
Author: User

I. TopicsTopic 1: Pushing boxestime limit:10000ms single point time limit:1000ms memory limit:256MB
Description

Push Box is a classic game. , the gray lattice means that the area cannot be passed, the blue box is the case, the black circle represents the player, and the lattice containing the dots represents the target point.


Specify the following rules:

1, a game will only have a box, a player and a target point.

2. Control the movement of the player via the arrow keys.

3, the figure of the gray lattice represents the wall, the player and the box can not pass.

4, pushed to the wall of the box, you can no longer push the box away from the wall, because the player can not reach the box on the side of the wall to push the box. This means that the box can only be moved in a "pushed" manner, not in a "pulled" manner. However, if the player pushes the box to the wall and there are no obstructions on either side of the vertical wall, the player can move the box in these two different directions. If the box enters the corner, there is no way to push the box again.

5, the player can not walk out of the scene. The player pushes the box to the edge of the scene, and if you continue clicking the arrow keys that move the player and the box toward the wall, the box and people will remain motionless. If there is a wall in the direction of the player, it cannot go forward. But these clicks are considered a reasonable input.

6, once the box reaches the target point, it can no longer move. However, the player can still move freely within the scene. If you continue to push the box, the player will remain in place with the box.

Now, given a click of a directional key, determine if this scheme will eventually stop the box at the target point. For convenience, we represent a blank lattice with 0, 4 means that we cannot pass the area, 1 for the player, 3 for the box, and 2 for the target.

input

The first row of data contains three integers, n,m,s. where n (0 < n <= 100) represents the width of the lattice, M (0 < M <= 100) represents the height of the lattice, s (0 < s <= 200) represent the number of test points.

The next M-line, each row will have n characters, describing the current face.

The next S-line, each line represents a test point. Each line starts with an integer t (0 < T <= 10000), followed by a space and a T-character. This T-character consists only of the four letters of the D,U,L,R, which represent the arrow keys that strike down, up, left, and right.

Output

For each test point, the final box is output at the target point. If yes, the output is yes, and if not, the output is no.



Sample input
5 4 3000001300000200000004 Rurd6 URDLDR6 Rrrurd
Sample output
Yesyesno

two. Problem solving skillsThis problem is mainly to consider a variety of boundary conditions, and does not contain too many algorithms.

Three. Implementing the Code
#include <iostream> #include <vector>using namespace Std;class box{private:vector<vector<char> & Gt    Board;    unsigned char Width;    unsigned char Height;    unsigned char X;    unsigned char Y;    BOOL Issucceed;public://constructor Box (int N, int M);    Box (const box& Origin);    void Buildboard ();    void MoveUp ();    void MoveDown ();    void MoveLeft ();    void MoveRight ();    void Move (const char& singlemove);    BOOL Move (const vector<char> &moves); void Print ();}; Box::box (int N, int M): Board (), Width (N), Height (M), X (255), Y (255), Issucceed (false) {}box::box (const box& Origin): B Oard (Origin.board), Width (Origin.width), Height (Origin.height), X (origin.x), Y (ORIGIN.Y), Issucceed (Origin.issucce ed) {}void Box::buildboard () {for (int indexofrows = 0; Indexofrows < Height;        indexofrows++) {vector<char> Row; for (int indexofcols = 0; Indexofcols < Width; indexofcols++) {CHar inputchar = 0;            Cin >> Inputchar;            Inputchar = Inputchar-' 0 ';                if (Inputchar = = 1) {X = Indexofcols;            Y = indexofrows;        } row.push_back (Inputchar);    } board.push_back (Row);    }}void Box::moveup () {if (Y < 1) {return;    } int nexty = Y-1;        The upper is the box with the right point if (board[nexty][x] = = 5) {issucceed = true;    Return        }//The upper is box if (board[nexty][x] = = 3) {if (Y = = 1) {return;        }//The upper of the box is the wall if (board[nexty-1][x] = = 4) {return;        }//Updating board[nexty-1][x] + = 3;        BOARD[NEXTY][X] = 1;        if (board[y][x]! = 2) {board[y][x] = 0;        } Y = Nexty;        if (board[nexty-1][x] = = 5) {issucceed = true; } rEturn;    }//The upper is wall if (board[nexty][x] = = 4) {return; }//The upper is zero if (board[nexty][x] = = 0) {if (board[y][x]! = 2) {Board[y][x]        = 0;        } Board[nexty][x] = 1;        Y = nexty;    Return        } if (board[nexty][x] = = 2) {board[y][x] = 0;        Y = nexty;    Return    }}void Box::movedown () {if (Y > (Height-2)) {return;    } int nexty = Y + 1;        The upper is the box with the right point if (board[nexty][x] = = 5) {issucceed = true;    Return        }//The upper is box if (board[nexty][x] = = 3) {if (Y = = (Height-2)) {return;        }//The upper of the box is the wall if (board[nexty + 1][x] = = 4) {return;        }//Updating Board[nexty + 1][x] + = 3;        BOARD[NEXTY][X] = 1;    if (board[y][x]! = 2) {board[y][x] = 0;    } Y = Nexty;        if (board[nexty + 1][x] = = 5) {issucceed = true;    } return;    }//The upper is wall if (board[nexty][x] = = 4) {return; }//The upper is zero if (board[nexty][x] = = 0) {if (board[y][x]! = 2) {Board[y][x]        = 0;        } Board[nexty][x] = 1;        Y = nexty;    Return        } if (board[nexty][x] = = 2) {board[y][x] = 0;        Y = nexty;    Return    }}void Box::moveleft () {if (X < 1) {return;    } int nextx = X-1;        The upper is the box with the right point if (board[y][nextx] = = 5) {issucceed = true;    Return        }//The upper is box if (board[y][nextx] = = 3) {if (X = = 1) {return;        }//The upper of the box is the wall if (board[y][nextx-1] = = 4) {return;     }//Updating board[y][nextx-1] + = 3;   BOARD[Y][NEXTX] = 1;        if (board[y][x]! = 2) {board[y][x] = 0;        } X = Nextx;        if (board[y][nextx-1] = = 5) {issucceed = true;    } return;    }//The upper is wall if (board[y][nextx] = = 4) {return; }//The upper is zero if (board[y][nextx] = = 0) {if (board[y][x]! = 2) {Board[y][x]        = 0;        } BOARD[Y][NEXTX] = 1;        X = NEXTX;    Return        } if (board[y][nextx] = = 2) {board[y][x] = 0;        X = NEXTX;    Return    }}void Box::moveright () {if (X > (Width-2)) {return;    } int nextx = X + 1;        The upper is the box with the right point if (board[y][nextx] = = 5) {issucceed = true;    Return        }//The upper is box if (board[y][nextx] = = 3) {if (X = = (Width-2)) {return; }//The upper of the box is the wall if (BoardY        [Nextx + 1] = = 4) {return;        }//Updating BOARD[Y][NEXTX + 1] + = 3;        BOARD[Y][NEXTX] = 1;        if (board[y][x]! = 2) {board[y][x] = 0;        } X = Nextx;        if (BOARD[Y][NEXTX + 1] = = 5) {issucceed = true;    } return;    }//The upper is wall if (board[y][nextx] = = 4) {return; }//The upper is zero if (board[y][nextx] = = 0) {if (board[y][x]! = 2) {Board[y][x]        = 0;        } BOARD[Y][NEXTX] = 1;        X = NEXTX;    Return        } if (board[y][nextx] = = 2) {board[y][x] = 0;        X = NEXTX;    Return    }}void box::move (const char &singlemove) {//print ();        Switch (singlemove) {case ' U ': MoveUp ();    Break        Case ' d ': MoveDown ();    Break        Case ' L ': MoveLeft ();    Break        Case ' R ': MoveRight ();    Break Default:breaK    }//cout << "x is" << x << Endl;    cout << "y is" << y << Endl; Print ();}    BOOL Box::move (const vector<char> &moves) {const int SIZE = Moves.size (); for (int Index = 0; Index < SIZE;        index++) {Move (Moves[index]);        if (issucceed) {return true; }} return false;}    void Box::P rint () {const int SIZE = Board.size (); for (int RowIndex = 0; RowIndex < SIZE;        rowindex++) {vector<char> Row = Board[rowindex];        int COLS = Row.size (); for (int colindex = 0; Colindex < COLS;            colindex++) {char Tmp = row[colindex] + ' 0 ';        cout << Tmp;    } cout << Endl;    }}int Main () {int N = 0, M = 0;    int S = 0;    CIN >> N >> M >> S;    Box box_1 (N, M);    Box_1.buildboard ();        while (s--) {Box box_tmp (box_1);        int number = 0;     Vector<char> Moves;   CIN >> number;            while (number--) {char sinlgemove = ' 0 ';            Cin >> Sinlgemove;        Moves.push_back (Sinlgemove);        } if (Box_tmp.move (Moves)) {cout << "YES" << Endl;        } else {cout << "NO" << Endl; }} return 0;}



Four. ExperienceThis problem is all about boundary conditions and code speed, and there is not much to think about algorithmic problems.


Copyright, welcome reprint, reproduced Please indicate the source, thank you




Hihocoder_ Push the box

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.