#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;
}
479

被折叠的 条评论
为什么被折叠?



