其他分享
首页 > 其他分享> > codeforces Mooyo Mooyo

codeforces Mooyo Mooyo

作者:互联网

挺有意思的题 双dfs

题面

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个就会消去。如果多个同时达到则同时消去,消去后方块坠落。
求最后的图是什么样子。

分析

一开始觉得要坠落,又要同时消去有点麻烦就跳了。
后来补题的时候发现也没有那么难。
用2个dfs,一个用来搜联通的方块有多少,如果大于K个则另一个来消去方块。
因为要同时消去,所以要遍历完图再下坠。
下坠完重复遍历直到遍历完都没有消除过一次,那就是结果了。
下坠部分我是从下往上遍历,遇到方块就放在当前列方块的上面,遍历完,把行数-方块数的地方填0就OK

代码

#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long ll;
char s[105][15];
int n,vis[105][15],d[4][2]={ {1,0},{-1,0},{0,1},{0,-1} },cnt;
bool check(int i,int j){
	if(i<0 || j<0 || i>n-1 || j>9) return false;
	return true;
}
void dfs1(int i,int j,char c){
	//if(!check(i,j)) return;
	for(int k=0;k<4;k++){
		int I=i+d[k][0],J=j+d[k][1];
		if(check(I,J) && s[I][J]==c && !vis[I][J]){
			vis[I][J]=1;
			cnt++;
			dfs1(I,J,c);
		}
	}
}
void dfs2(int i,int j,char c){
	//if(!check(i,j)) return;
	s[i][j]='0';
	for(int k=0;k<4;k++){
		int I=i+d[k][0],J=j+d[k][1];
		if(check(I,J) && s[I][J]==c) dfs2(I,J,c);
	}
}
void fail(){
	int i,j;
	for(j=0;j<10;j++){
		int t=0;
		for(i=n-1;i>=0;i--){
			if(s[i][j]!='0') s[n-1-t++][j]=s[i][j];
		}
		for(i=n-1-t;i>=0;i--) s[i][j]='0';
	}
}
int main(){
	int k,i,j;
	scanf("%d%d",&n,&k);
	for(i=0;i<n;i++) scanf("%s",s[i]);
	while(1){
		int flag=0;
		for(i=n-1;i>=0;i--){
			for(j=0;j<10;j++){
				cnt=0;
				if(s[i][j]!='0'){
					memset(vis,0,sizeof(vis));
					dfs1(i,j,s[i][j]);
				}
				if(cnt>=k) flag=1,dfs2(i,j,s[i][j]);
				
			}
		}
		if(flag) fail(),flag=0;
		else break;
	}
	for(int i=0;i<n;i++){
    	printf("%s\n",s[i]);
	}
	return 0;
}

标签:int,cells,codeforces,connected,方块,0000000000,Mooyo
来源: https://blog.csdn.net/weixin_44354699/article/details/88377399