1041. Pushing Boxes


#include <iostream>
#include <cstring>

using namespace std;

int h, w, p;
int boxes[401][401];
int cases = 0;

void down() {
	int moves;
	cin >> moves;
	int count = 0;
	int max = 0;
	for(int j = 0; j < w; j++) {
		count = 0;
		for(int i = 0; i < h; i++) {
			if(boxes[i][j]) {
				count++;
			}
		}
		max = (max > count ? max : count);
	}
	moves = (moves < h - max ? moves : h - max);
	for(int i = 0; i < moves; i++) {
		for(int j = 0; j < w; j++) {
			if(boxes[i][j]) {
				int mostBottom = i;
				while(boxes[mostBottom+1][j] && mostBottom + 1 < h) {
					mostBottom++;
				}
				if(mostBottom+1 < h) {
					boxes[i][j] = 0;
					boxes[mostBottom+1][j] = 1;
				} 
				else {
					break;
				}
			}
		}
	}
}

void left() {
	int moves;
	cin >> moves;
	int count = 0;
	int max = 0;
	for(int i = 0; i < h; i++) {
		count = 0;
		for(int j = 0; j < w; j++) {
			if(boxes[i][j]) {
				count++;
			}
		}
		max = (max > count ? max : count);
	}
	moves = (moves < w - max ? moves : w - max);
	for(int i = 0; i < h; i++) {
		for(int j = w - 1; j >= w - moves; j--) {
			if(boxes[i][j]) {
				int mostLeft = j;
				while(boxes[i][mostLeft-1] && mostLeft - 1 >= 0) {
					mostLeft--;
				}
				if(mostLeft-1 >= 0) {
					boxes[i][j] = 0;
					boxes[i][mostLeft-1] = 1;
				}
				else {
					break;
				}
			}
		}
	}
}

void up() {
	int moves;
	cin >> moves;
	int count = 0;
	int max = 0;
	for(int j = 0; j < w; j++) {
		count = 0;
		for(int i = 0; i < h; i++) {
			if(boxes[i][j]) {
				count++;
			}
		}
		max = (max > count ? max : count);
	}
	moves = (moves < h - max ? moves : h - max);
	for(int i = h - 1; i >= h - moves; i--) {
		for(int j = 0; j < w; j++) {
			if(boxes[i][j]) {
				int topest = i;
				while(boxes[topest-1][j] && topest-1 >= 0) {
					topest--;
				}
				if(topest-1 >= 0) {
					boxes[i][j] = 0;
					boxes[topest-1][j] = 1;
				}
				else {
					break;
				}
			}
		}
	}
}

void right() {
	int moves;
	cin >> moves;
	int count = 0;
	int max = 0;
	for(int i = 0; i < h; i++) {
		count = 0;
		for(int j = 0; j < w; j++) {
			if(boxes[i][j]) {
				count++;
			}
		}
		max = (max > count ? max : count);
	}
	moves = (moves < w - max ? moves : w - max);
	for(int i = 0; i < h; i++) {
		for(int j = 0; j < moves; j++) {
			if(boxes[i][j]) {
				int mostRight = j;
				while(boxes[i][mostRight+1] && mostRight + 1 < w) {
					mostRight++;
				}
				if(mostRight+1 < w) {
					boxes[i][j] = 0;
					boxes[i][mostRight+1] = 1;
				}
				else {
					break;
				}
			}
		}
	}
}

int main() {
	while(cin >> h >> w && h != 0 && w != 0) {
		cases++;
		memset(boxes, 0, sizeof(boxes));
		cin >> p;
		for(int i = 0; i < p; i++) {
			int x, y;
			cin >> x >> y;
			boxes[x][y] = 1;
		}
		string order;
		while(cin >> order && order != "done") {
			if(order == "down") {
				down();
			}
			else if(order == "left") {
				left();
			}
			else if(order == "up") {
				up();
			}
			else if(order == "right") {
				right();
			}
		}
		cout << "Data set " << cases << " ends with boxes at locations";
		for(int i = 0; i < h; i++) {
			for(int j = 0; j < w; j++) {
				if(boxes[i][j]) {
					cout << ' ' << "(" << i << "," << j << ")";
				}
			}
		}
		cout << "." << endl;
	}
	return 0;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值