Mooyo Mooyo
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
With plenty of free time on their hands (or rather, hooves), the cows on Farmer John’s farm often pass the time by playing video games. One of their favorites is based on a popular human video game called Puyo Puyo; the cow version is of course called Mooyo Mooyo. The game of Mooyo Mooyo is played on a tall narrow grid N cells tall (1≤N≤100)(1≤N≤100) and 1010 cells wide. Here is an example with N=6N=6:
00000000000000000000
00000003000000000300
00540003000054000300
10545022301054502230
22111222202211122220
11111112231111111223
Each cell is either empty (indicated by a 00), or a haybale in one of nine different colors (indicated by characters 1…91…9). Gravity causes haybales to fall downward, so there is never a 00 cell below a haybale.
Two cells belong to the same connected region if they are directly adjacent either horizontally or vertically, and they have the same nonzero color. Any time a connected region exists with at least KK cells, its haybales all disappear, turning into zeros. If multiple such connected regions exist at the same time, they all disappear simultaneously. Afterwards, gravity might cause haybales to fall downward to fill some of the resulting cells that became zeros. In the resulting configuration, there may again be connected regions of size at least KK cells. If so, they also disappear (simultaneously, if there are multiple such regions), then gravity pulls the remaining cells downward, and the process repeats until no connected regions of size at least KK exist.
Given the state of a Mooyo Mooyo board, please output a final picture of the board after these operations have occurred.
Input
The first line of input contains NN and K(1≤K≤10N)K(1≤K≤10N). The remaining NN lines specify the initial state of the board.
Output
Please output NN lines, describing a picture of the final board state.
Example
input
Copy
6 3
0000000000
0000000300
0054000300
1054502230
2211122220
1111111223
output
Copy
0000000000
0000000000
0000000000
0000000000
1054000000
2254500000
Note
In the example above, if K=3, then there is a connected region of size at least K with color 1 and also one with color 2. Once these are simultaneously removed, the board temporarily looks like this:
00000000000000000000
00000003000000000300
00540003000054000300
10545000301054500030
22000000002200000000
00000000030000000003
Then, gravity takes effect and the haybales drop to this configuration:
00000000000000000000
00000000000000000000
00000000000000000000
00000000000000000000
10540003001054000300
22545003332254500333
Again, there is a region of size at least K (with color 3). Removing it yields the final board configuration:
00000000000000000000
00000000000000000000
00000000000000000000
00000000000000000000
10540000001054000000
22545000002254500000
题目大意
给定一幅图,图内有各种数字,0代表空。然后相同数字在相邻(不可以斜着)的话称为一个块。当块的数字的个数多于K的时候,就可以消掉,然后所有的数字就会因为重力原因下降。重复以上过程,直到没有发生变化
题目分析
我用的是bfs。写两个bfs,一个判断是否大于k,另外一个消掉相同的数字。然后再写一个函数模仿重力下降。当没办法消掉数字而且重力下降后没变的话,就可以退出循环,输出最后的结果的图。基本上都是for一遍,不怎么用考虑复杂度,毕竟N的范围很小。而且代码模拟很长……
代码
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int map[105][10], dis[105][10];
typedef struct{
int x, y;
}node;
int n, k;
void show(void){
for(int i = 0; i < n; i++){
for(int j = 0; j < 10; j++){
printf("%d", map[i][j]);
}
printf("\n");
}
}
void movedown(void){
int tmp[n];
for(int j = 0; j < 10; j++){
int con = n - 1;
memset(tmp, 0, sizeof(tmp));
for(int i = n - 1; i >= 0; i--){
if(map[i][j] != 0)
tmp[con--] = map[i][j];
}
for(int i = 0; i < n; i++)
map[i][j] = tmp[i];
}
}
check(node next){
return 0 <= next.x && next.x < n && 0 <= next.y && next.y < 10;
}
bool dayuk(int x, int y){
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};
int cnt = 1;
queue<node> que;
node tmp;
tmp.x = x; tmp.y = y;
dis[x][y] = -1;
que.push(tmp);
// printf("%d %d----\n", tmp.x, tmp.y);
while(que.size()){
node now = que.front();
que.pop();
for(int i = 0; i < 4; i++){
node next;
next.x = now.x + dx[i]; next.y = now.y + dy[i];
if(check(next) && dis[next.x][next.y] == 0 && map[next.x][next.y] == map[now.x][now.y]){
cnt++;
que.push(next);
dis[next.x][next.y] = -1;
// printf("%d %d\n", next.x, next.y);
}
}
}
// if(1){
// printf("%d %d----%d\n", tmp.x, tmp.y, map[tmp.x][tmp.y]);
// printf("cnt =%d\n", cnt);
// }
return cnt >= k;
}
void delate_n(int x, int y){
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};
queue<node> que;
node tmp;
int c = map[x][y];
tmp.x = x; tmp.y = y;
que.push(tmp);
while(que.size()){
node now = que.front();
map[now.x][now.y] = 0;
que.pop();
for(int i = 0; i < 4; i++){
node next;
next.x = now.x + dx[i]; next.y = now.y + dy[i];
if(check(next) && dis[next.x][next.y] != -2 && map[next.x][next.y] == c){
que.push(next);
dis[next.x][next.y] = -2;
}
}
}
}
bool delate(void){
bool flag = true;
memset(dis, 0, sizeof(dis));
for(int i = 0; i < n; i++)
for(int j = 0; j < 10; j++){
if(map[i][j] != 0 && dis[i][j] == 0){
if(dayuk(i, j)){
delate_n(i, j);
flag = false;
}
}
}
return flag;
}
void solve(void){
movedown();
while(!delate()){
movedown();
}
}
int main(int argc, char const *argv[]) {
scanf("%d%d", &n, &k);
getchar();
for(int i = 0; i < n; i++){
for(int j = 0; j < 10; j++){
char c = getchar();
map[i][j] = c - '0';
}
getchar();
}
solve();
show();
return 0;
}