模拟方块的下落,如果方块覆盖到1或者碰到底部,即停止。
关键:从样例可以看到,方块的最后一行可以越出棋盘,因此棋盘的边界可以进行适当的延伸(扩大二维数组),处理碰到底部的问题,解决方案是在底部加上一个全1的边框。
#include <stdio.h>
int board[20][15];
int block[4][4];
int main() {
for (int i = 1; i <= 15; i++)
for (int j = 1; j <= 10; j++)
scanf("%d", &board[i][j]);
for (int a = 0; a < 4; a++)
for (int b = 0; b < 4; b++)
scanf("%d", &block[a][b]);
for (int j = 1; j <= 10; j++)// 底部加上全1边框
board[16][j] = 1;
int col;
scanf("%d", &col);
bool over = false;
int i;
for (i = 0; i <= 15; i++) { // 模拟方块的掉落
for (int a = 0; a < 4; a++)
for (int b = 0; b < 4; b++)
if (block[a][b] && board[i + a][col + b])
over = true;
if (over) break;
}
for (int a = 0; a < 4; a++) // 合并方块到棋盘上
for (int b = 0; b < 4; b++)
board[i + a - 1][col + b] += block[a][b];
for (int i = 1; i <= 15; i++) {
for (int j = 1; j <= 10; j++)
printf("%d ", board[i][j]);
printf("\n");
}
return 0;
}