题目
-
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. -
input
6 3
0000000000
0000000300
0054000300
1054502230
2211122220
1111111223 -
output
0000000000
0000000000
0000000000
0000000000
1054000000
2254500000
题意
N*10的图中有很多字母,如果相同的字母(四)连通超过K个,则会消除,然后受到重力掉下来,每次下坠完成后再同时消除,知道无可以消除的字母,则输出最后的图
思路
对每个字母都搜索他周围的字母看看有没有相同的超过K个,我用的BFS。。超过K个则消除,全部遍历完了就落下一次
代码
#include <bits/stdc++.h>
using namespace std;
int N,K;
signed char m[105][13];
pair<int,int>point;
queue< pair<int,int> >que,cle;
int dr[]= {0,-1,0,1};
int dc[]= {-1,0,1,0};
int r2,c2;
bool bfs(int r,int c) {
point.first=r,point.second=c;
signed char color=m[r][c];
que.push(point);
while(!que.empty()) {
r=que.front().first,c=que.front().second;
que.pop();
for(int i=0; i<4; i++) {
r2=r+dr[i],c2=c+dc[i];
if(r2>=0&&r2<N&&c2>=0&&c2<10) {
if( m[r2][c2] == color ) {
point.first=r2,point.second=c2;
cle.push(point);
que.push(point);
m[r2][c2]=-m[r2][c2];
}
}
}
}
if(cle.size()>=K) {
while(!cle.empty()) {
r=cle.front().first,c=cle.front().second;
cle.pop();
m[r][c]='0';
}
return true;
}
else {
while(!cle.empty()) {
r=cle.front().first,c=cle.front().second;
cle.pop();
m[r][c]=-m[r][c];
}
return false;
}
}
void down() {
for(int c=0; c<10; ++c)
for(int r=N-1; r>=0; --r)
if(m[r][c]=='0')
for(int i=r-1; i>=0; i--)
if(m[i][c]!='0') {
m[r][c]=m[i][c];
m[i][c]='0';
break;
}
}
bool clear() {
bool flag=false;
for(int r=0; r<N; r++)
for(int c=0; c<10; c++) {
if(m[r][c]!='0') {
if(flag==false) flag=bfs(r,c);
else bfs(r,c);
}
}
return flag;
}
int main() {
scanf("%d%d",&N,&K);
for(int i=0; i<N; ++i)
scanf("%s",m[i]);
while(clear())
down();
for(int i=0; i<N; ++i)
printf("%s\n",m[i]);
return 0;
}