题面:
Mooyo Mooyo
time limit per test : 1 second
memory limit per test : 256 megabytes
input : standard input
output : standard output
Problem Description
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) and 10 cells wide. Here is an example with N=6:
0000000000
0000000300
0054000300
1054502230
2211122220
1111111223
Each cell is either empty (indicated by a 0), or a haybale in one of nine different colors (indicated by characters 1…9). Gravity causes haybales to fall downward, so there is never a 0 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 K 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 K 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 K 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 N and K(1≤K≤10N). The remaining N lines specify the initial state of the board.
Output
Please output N lines, describing a picture of the final board state.
Sample Input
6 3
0000000000
0000000300
0054000300
1054502230
2211122220
1111111223
Sample Output
0000000000
0000000000
0000000000
0000000000
1054000000
2254500000
题意描述
当某一个数字形成的块内数字的个数大于等于K的时候,就会整个块消失.并且存在重力,即非0数字下面若是0,则非0数字会往下走,直到到底或者遇上非0数字.要求输出最终形成的图.
题目分析
直接模拟.进行检查,看图内有没有数字块内数字个数是大于等于K的,有则标记上,图遍历完之后进行消除被标记的块,然后进行重力交换处理.接着再检查图内有没有需要消除的块,直到图内没有需要消除的块为止.
具体代码
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef pair < int , int > pii;
char MAP[105][15];
bool flag[105][15];
int cx[4]={0,1,0,-1}, cy[4]={1,0,-1,0};
int N, K, cnt;
void DFS(int sx, int sy)
{
if(sx < 0 || sy < 0 || sx >= N || sy >= 10)
{
return;
}
for(int i = 0; i < 4 ; i++)
{
int xx = sx+cx[i];
int yy = sy+cy[i];
if(MAP[xx][yy] == MAP[sx][sy] && !flag[xx][yy])
{
cnt++;
flag[xx][yy] = true;
DFS(xx , yy);
}
}
}
void del()
{
for(int i = 0; i < N; i++)
{
for(int j = 0; j < 10; j++)
{
if(flag[i][j])
{
MAP[i][j] = '0';
}
}
}
}
void solve()
{
bool mark = true;
for(int i = 0; i < N; i++)
{
for(int j = 0; j < 10; j++)
{
if(MAP[i][j] != '0' && !flag[i][j])
{
memset(flag, 0, sizeof(flag));
cnt = 0;
DFS(i , j);
if(cnt >= K)
{
mark = false;
del();
}
}
}
}
for(int j = 0; j < 10; j++)
{
for(int i = N-1; i >= 0; i--)
{
if(MAP[i][j] == '0')
{
int t = i;
while(MAP[t][j] == '0' && t >= 0)
{
t--;
}
if(t < 0)
{
break;
}
MAP[i][j] = MAP[t][j];
MAP[t][j] = '0';
}
}
}
if(!mark)
{
solve();
}
}
int main()
{
scanf("%d%d", &N, &K);
for(int i = 0; i < N; i++)
{
scanf("%s", MAP[i]);
}
solve();
for(int i = 0; i < N; i++)
{
printf("%s\n", MAP[i]);
}
return 0;
}