挺有意思的题 双dfs
- 题面
- 题意
- 分析
- 代码
题面 【codeforces Mooyo Mooyo】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
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=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
推荐阅读
- 人工智能|干货!人体姿态估计与运动预测
- 分析COMP122 The Caesar Cipher
- 技术|为参加2021年蓝桥杯Java软件开发大学B组细心整理常见基础知识、搜索和常用算法解析例题(持续更新...)
- C语言学习(bit)|16.C语言进阶——深度剖析数据在内存中的存储
- Python机器学习基础与进阶|Python机器学习--集成学习算法--XGBoost算法
- 数据结构与算法|【算法】力扣第 266场周赛
- 数据结构和算法|LeetCode 的正确使用方式
- leetcode|今天开始记录自己的力扣之路
- 人工智能|【机器学习】深度盘点(详细介绍 Python 中的 7 种交叉验证方法!)
- 网络|简单聊聊压缩网络