Sokoban

Sokoban

时间: 1ms        内存:128M

描述:

Soko-ban is a Japanese word for a warehouse worker, and the name of a classic computer game created in the 1980s. It is a one-player game with the following premise. A single worker is in an enclosed warehouse with one or more boxes. The goal is to move those boxes to a set of target locations, with the number of target locations equalling the number of boxes. The player indicates a direction of motion for the worker using the arrow keys (up, down, left, right), according to the following rules.
  • If the indicated direction of motion for the worker leads to an empty location (i.e., one that does not have a box or wall), the worker advances by one step in that direction.
  • If the indicated direction of motion would cause the worker to move into a box, and the location on the other side of the box is empty, then both the worker and the box move one spot in that direction (i.e., the worker pushes the box).
  • If the indicated direction of motion for a move would cause the worker to move into a wall, or to move into a box that has another box or a wall on its opposite side, then no motion takes place for that keystroke.
The goal is to simultaneously have all boxes on the target locations. In that case, the player is successful (and as a formality, all further keystrokes will be ignored).
The game has been studied by computer scientists (in fact, one graduate student wrote his entire Ph.D. dissertation about the analysis of sokoban). Unfortunately, it turns out that finding a solution is very difficult in general, as it is both NP-hard and PSPACE-complete. Therefore, your goal will be a simpler task: simulating the progress of a game based upon a player's sequence of keystrokes. For the sake of input and output, we describe the state of a game using the following symbols:
Symbol Meaning
. empty space
# wall
+ empty target location
b box
B box on a target location
w worker
W worker on a target location
For example, the initial configuration diagrammed at the beginning of this problem appears as the first input case below.

输入:

Each game begins with a line containing integers R and C, where 4 ≤ R ≤ 15 represents the number of rows, and 4 ≤ C ≤ 15 represents the number of columns. Next will be R lines representing the R rows from top to bottom, with each line having precisely C characters, from left-to-right. Finally, there is a line containing at most 50 characters describing the player's sequence of keystrokes, using the symbols U, D, L, and R respectively for up, down, left, and right. You must read that entire sequence from the input, even though a particular game might end successfully prior to the end of the sequence. The data set ends with the line 0 0.
We will guarantee that each game has precisely one worker, an equal number of boxes and locations, at least one initially misplaced box, and an outermost boundary consisting entirely of walls.

输出:

For each game, you should first output a line identifying the game number, beginning at 1, and either the word complete or incomplete, designating whether or not the player successfully completed that game. Following that should be a representation of the final board configuration.

示例输入:

8 9
#########
#...#...#
#..bb.b.#
#...#w#.#
#...#b#.#
#...++++#
#...#..##
#########
ULRURDDDUULLDDD
6 7
#######
#..####
#.+.+.#
#.bb#w#
##....#
#######
DLLUDLULUURDRDDLUDRR
0 0

示例输出:

Game 1: incomplete
#########
#...#...#
#..bb...#
#...#.#.#
#...#.#.#
#...+W+B#
#...#b.##
#########
Game 2: complete
#######
#..####
#.B.B.#
#.w.#.#
##....#
#######

提示:

参考答案(内存最优[1268]):



/* Sokoban, MCPC 2011 Problem G, C++ solution by Michael Goldwasser */
#include <iostream>
#include <vector>
#include <string>
using namespace std;

// let's avoid symbol confusion
#define EMPTY              '.'
#define WALL               '#'
#define TARGET             '+'
#define BOX                'b'
#define BOX_ON_TARGET      'B'
#define WORKER             'w'
#define WORKER_ON_TARGET   'W'

#define MAX_R 15



int R,C;
string board[MAX_R];   // represent "2D" board as array of strings

void printBoard(ostream& out = cout) {
    for (int j=0; j < R; j++) out << board[j] << endl;
}

bool isBox(char c) {
    return (c == BOX || c == BOX_ON_TARGET);
}

bool isVacant(char c) {
    return (c == EMPTY || c == TARGET);
}

int main() {
	int i;
    int game(1);
    while (true) {
        cin >> R >> C;
        if (R == 0) break;

        int misplacedBoxes = 0;
        int wr=-1,wc=-1;  // worker's current position
        for (i=0; i < R; i++) {
            cin >> board[i];

            for (int j=0; j < C; j++) {
                switch (board[i][j]) {
                case BOX:
                    misplacedBoxes++;
                    break;

                case WORKER:
                case WORKER_ON_TARGET:
                    wr = i; wc = j;
                    break;
                }
            }
        }

        string moves;
        cin >> moves;

        for (i=0; i < moves.size() && misplacedBoxes > 0; i++) {    // process moves[i]
            int dr, dc;
            switch (moves[i]) {
            case 'U':
                dc = 0;
                dr = -1;
                break;
            case 'D':
                dc = 0;
                dr = 1;
                break;
            case 'L':
                dc = -1;
                dr = 0;
                break;
            case 'R':
                dc = 1;
                dr = 0;
                break;
            }

            char &current = board[wr][wc];
            char &neighbor = board[wr+dr][wc+dc];
            bool willMove = false;

            if (isVacant(neighbor))
                willMove = true;
            else if (isBox(neighbor)) {
                // possible move.
                char &lookahead = board[wr+2*dr][wc+2*dc];
                if (isVacant(lookahead)) {
                    willMove = true;
                    if (lookahead == TARGET) {
                        lookahead = BOX_ON_TARGET;
                        misplacedBoxes--;
                    } else {
                        lookahead = BOX;
                    }

                    if (neighbor == BOX_ON_TARGET) {
                        neighbor = TARGET;
                        misplacedBoxes++;
                    } else {
                        neighbor = EMPTY;
                    }
                }
            }

            if (willMove) {
                current = (current == WORKER_ON_TARGET) ? TARGET : EMPTY;
                neighbor = (neighbor == TARGET) ? WORKER_ON_TARGET : WORKER;
                wr += dr;
                wc += dc;
            }
        }

        cout << "Game " << game++ << ": " << ((misplacedBoxes == 0) ? "complete" : "incomplete") << endl;
        printBoard();
    }
}

参考答案(时间最优[0]):



/* Sokoban, MCPC 2011 Problem G, C++ solution by Michael Goldwasser */
#include <iostream>
#include <vector>
#include <string>
using namespace std;

// let's avoid symbol confusion
#define EMPTY              '.'
#define WALL               '#'
#define TARGET             '+'
#define BOX                'b'
#define BOX_ON_TARGET      'B'
#define WORKER             'w'
#define WORKER_ON_TARGET   'W'

#define MAX_R 15



int R,C;
string board[MAX_R];   // represent "2D" board as array of strings

void printBoard(ostream& out = cout) {
    for (int j=0; j < R; j++) out << board[j] << endl;
}

bool isBox(char c) {
    return (c == BOX || c == BOX_ON_TARGET);
}

bool isVacant(char c) {
    return (c == EMPTY || c == TARGET);
}

int main() {
	int i;
    int game(1);
    while (true) {
        cin >> R >> C;
        if (R == 0) break;

        int misplacedBoxes = 0;
        int wr=-1,wc=-1;  // worker's current position
        for (i=0; i < R; i++) {
            cin >> board[i];

            for (int j=0; j < C; j++) {
                switch (board[i][j]) {
                case BOX:
                    misplacedBoxes++;
                    break;

                case WORKER:
                case WORKER_ON_TARGET:
                    wr = i; wc = j;
                    break;
                }
            }
        }

        string moves;
        cin >> moves;

        for (i=0; i < moves.size() && misplacedBoxes > 0; i++) {    // process moves[i]
            int dr, dc;
            switch (moves[i]) {
            case 'U':
                dc = 0;
                dr = -1;
                break;
            case 'D':
                dc = 0;
                dr = 1;
                break;
            case 'L':
                dc = -1;
                dr = 0;
                break;
            case 'R':
                dc = 1;
                dr = 0;
                break;
            }

            char &current = board[wr][wc];
            char &neighbor = board[wr+dr][wc+dc];
            bool willMove = false;

            if (isVacant(neighbor))
                willMove = true;
            else if (isBox(neighbor)) {
                // possible move.
                char &lookahead = board[wr+2*dr][wc+2*dc];
                if (isVacant(lookahead)) {
                    willMove = true;
                    if (lookahead == TARGET) {
                        lookahead = BOX_ON_TARGET;
                        misplacedBoxes--;
                    } else {
                        lookahead = BOX;
                    }

                    if (neighbor == BOX_ON_TARGET) {
                        neighbor = TARGET;
                        misplacedBoxes++;
                    } else {
                        neighbor = EMPTY;
                    }
                }
            }

            if (willMove) {
                current = (current == WORKER_ON_TARGET) ? TARGET : EMPTY;
                neighbor = (neighbor == TARGET) ? WORKER_ON_TARGET : WORKER;
                wr += dr;
                wc += dc;
            }
        }

        cout << "Game " << game++ << ": " << ((misplacedBoxes == 0) ? "complete" : "incomplete") << endl;
        printBoard();
    }
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注