Sicily 1041. Pushing Boxes

Sicily 1041. Pushing Boxes


嗯,好久没动过Sicily了。

Problem

Scrap cars in a junk yard are crushed in a device that pushes the car in from the sides, from the front and back, and from the top and bottom. The result is a compact little chunk of metal. In this problem you’re going to model a device that works in a similar manner, but doesn’t crush anything, only pushes boxes around in two dimensions. The boxes are all square with unit length on a side and are situated on the floor of a room. Each wall of the room can be programmed to move inward a certain amount, pushing any boxes it may bump into. Unlike the car-crusher, this device is sensitive and if it senses that boxes are stacked up against a wall and that it might crush them if pressed any farther, it will stop.
problem
For example, suppose we have boxes arranged in a 12-by-16 room as shown below. The upper left-hand corners of the boxes (which is how we will locate them in this problem) are at coordinates (1,13) (box A below), (3,2), (6,2), (6,4), (6,6), (7,6) and (8,9) (box G), where the first coordinate indicates distance from the top wall and the second coordinate indicates distance from the left wall.
Suppose the top wall is programmed to move down 3 units (then retreats, as the walls always will) and then the right wall is programmed to move left 14 units. The first operation can be performed with no problem, but the second one can not be carried out without crushing some boxes. Therefore, the right wall will move only 13 units, the maximum distance it can move until boxes are packed tightly between it and the left wall. The boxes will then be in the configuration shown in the following figure. The locations of the boxes are (3,1), (3,2), (6,0), (6,1), (6,2), (7,2), (8,2).
problem

Input

There will be multiple data sets for this problem. The first line of each data set will be two integers giving the height and width of the room. (We’ll visualize the room as if on a piece of paper, as drawn above.) Each dimension will be no more than 20. The next line will contain an integer n (0 < n <= 10) followed by n pairs of integers, each pair giving the location of a box as the distances from the top and the left walls of the room. The following lines will be of the form direction m, where direction is either down, left, up, right, or done and m is a positive integer. For example, left 2 would mean to try to move the right wall 2 spaces to the left. The “direction” done indicates that you are finished pushing this set of boxes around. There will be no integer m following the direction done, of course. The data sets are followed by a line containing 0 0.

Output

For each data set you are to produce one line of output of the form:
Data set d ends with boxes at locations (r1, c1) (r2, c2) … (rn, cn).
where the (ri, ci) are the locations of the boxes given from top-to-bottom, left-to-right, (separated by one space) and d is the data set number (starting at 1).

Sample Input

12 16
7 1 13 3 2 6 2 6 4 6 6 7 6 8 9
down 3
left 14
done
4 4
3 1 0 2 1 2 3
right 3
up 2
left 1
done
0 0

Sample Output

Data set 1 ends with boxes at locations (3,1) (3,2) (6,0) (6,1) (6,2) (7,2) (8,2).
Data set 2 ends with boxes at locations (0,2) (1,1) (1,2).

Thought

题目的意思是一道模拟题,推箱子,用墙壁来推。比如题目图中,是先把上面的墙壁往下(down)推,再把右边的墙壁往左(left)推,得到第二张图的效果。
直接模拟即可,注意箱子是不是可以推(如果箱子后面有箱子,而且顶到墙壁了就不能推)。

Code

// Problem#: 1041
// Submission#: 4001263
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
#include <string.h>
using namespace std;

int h, w, n, counter = 1;
char room[25][25];
char re[25][25];
int dir[4][2] = { -1, 0, 0, -1, 1, 0, 0, 1 };
int edge[4];

bool isValid(int pi, int pj) {
    return 0 <= pi && pi < h && 0 <= pj && pj < w && room[pi][pj] == ' ';
}

bool moveBox(int d, int pi, int pj) {
    int ni = pi + dir[d][0], nj = pj + dir[d][1];
    if (isValid(ni, nj)) {
        room[ni][nj] = room[pi][pj];
        room[pi][pj] = ' ';
        return true;
    }
    else {
        if (0 <= ni && ni < h && 0 <= nj && nj < w) {
            if (moveBox(d, ni, nj)) {
                room[ni][nj] = room[pi][pj];
                room[pi][pj] = ' ';
                return true;
            }
            else {
                return false;
            }
        }
        else {
            return false;
        }
    }
}

void record() {
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            re[i][j] = room[i][j];
        }
    }
}

void moveBack() {
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            room[i][j] = re[i][j];
        }
    }
}

void move(int d, int step) {
    for (int i = 1; i <= step; i++) {
        int walli, wallj;
        if (d == 0 || d == 2) {
            walli = edge[d] + i * dir[d][0];
            record();
            for (int j = 0; j < edge[1]; j++) {
                if (room[walli][j] != ' ') {
                    if (moveBox(d, walli, j)) {
                        // move successfully
                    }
                    else {
                        // fail
                        moveBack();
                        return;
                    }
                }
            }
        }
        else {
            wallj = edge[d] + i * dir[d][1];
            record();
            for (int j = 0; j < edge[0]; j++) {
                if (room[j][wallj] != ' ') {
                    if (moveBox(d, j, wallj)) {
                        // move successfully
                    }
                    else {
                        moveBack();
                        // fail
                        return;
                    }
                }
            }
        }
    }
}

int main() {

    std::ios::sync_with_stdio(false);

    while (1) {
        cin >> h >> w;
        if (!h && !w) break;

        edge[0] = h;
        edge[1] = w;
        edge[2] = edge[3] = -1;

        memset(room, ' ', sizeof(room));

        cin >> n;
        for (int i = 0; i < n; i++) {
            int pi, pj;
            cin >> pi >> pj;
            room[pi][pj] = 'A' + i;
        }

        while (1) {
            char order[6];
            int step;
            cin >> order;
            if (strcmp(order, "left") == 0) {
                cin >> step;
                move(1, step);
            }
            else if (strcmp(order, "right") == 0) {
                cin >> step;
                move(3, step);
            }
            else if (strcmp(order, "up") == 0) {
                cin >> step;
                move(0, step);
            }
            else if (strcmp(order, "down") == 0) {
                cin >> step;
                move(2, step);
            }
            else if (strcmp(order, "done") == 0) {
                break;
            }
        }

        cout << "Data set " << counter << " ends with boxes at locations";
        counter++;
        for (int j = 0; j < h; j++) {
            for (int k = 0; k < w; k++) {
                if (room[j][k] != ' ') {
                    cout << " (" << j << "," << k << ")";
                }
            }
        }

        cout << ".\n";
    }

    return 0;
}                                 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值