1 条题解
-
0
这是一道模拟箱子在房间内受墙壁推动而移动的问题。该程序通过模拟墙壁的移动操作,实现了对房间内箱子位置的更新,并按照要求输出了最终的结果。
详细代码
#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; }
- 1
信息
- ID
- 623
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 10
- 标签
- 递交数
- 1
- 已通过
- 1
- 上传者