关注
太难了。。。时间只够做第一题。。。第二题没做。 #include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <queue>
#define MAXN 300100
#define inf 0x3f3f3f3f
using namespace std;
typedef long long LL;
int dir[4][2] = {-1,0, 0,1,1,0,0,-1};
int ans = 25;
vector<vector<int> > mp(5, vector<int>(5));
bool isIn(int x, int y){
if (x>=0 && x<5 && y>=0 && y<5) return true;
return false;
}
int step(vector<vector<int> > &mp, int sx, int sy, int target){
mp[sx][sy]=-1;
int ans = 1;
for (int i=0;i<4;i++){
int x=sx+dir[i][0];
int y=sy+dir[i][1];
if (isIn(x,y) && mp[x][y]!=-1 && mp[x][y] == target){
ans += step(mp, x, y , target);
}
}
return ans;
}
void down(vector<vector<int> > &mp){
for (int j=0;j<5;j++){
int endrow = 4;
while (endrow >=0 && mp[endrow][j]!=-1) endrow--;
int startrow = endrow;
while (startrow >=0 && mp[startrow][j]==-1) startrow--;
while (startrow >= 0){
swap(mp[startrow][j], mp[endrow][j]);
startrow--;
endrow--;
}
}
}
void dfs(vector<vector<int> > &mp){
bool no_3 = true;
for (int i=0;i<5;i++){
for (int j=0;j<5;j++){
if (mp[i][j]==-1) continue;
vector<vector<int> > temp(mp);
int nums = step(mp, i, j, mp[i][j]);
if (nums < 3) {
mp.assign(temp.begin(), temp.end());
continue;
}else{
no_3 = false;
down(mp);
dfs(mp);
mp.assign(temp.begin(), temp.end());
}
}
}
if (no_3){
int x=0;
for (int i=0;i<5;i++){
for (int j=0;j<5;j++)
if (mp[i][j]!=-1)
x++;
}
ans = min(ans, x);
}
return;
}
int main(){
int x;
for (int i=0;i<5;i++){
for (int j=0;j<5;j++){
scanf("%d", &x);
mp[i][j] = x;
}
}
dfs(mp);
printf("%d\n", ans);
return 0;
}
查看原帖
点赞 7
相关推荐
牛客热帖
更多
正在热议
更多
# 你小心翼翼的闯过多大的祸? #
3469次浏览 67人参与
# 找不到实习会影响秋招吗 #
1399574次浏览 13635人参与
# 实习没事做是福还是祸? #
3746次浏览 62人参与
# 重来一次,你会对开始求职的自己说 #
858次浏览 19人参与
# 2025年终总结 #
133328次浏览 2281人参与
# 考研人,我有话说 #
156529次浏览 1211人参与
# 哪些公司笔/面试难度大? #
7047次浏览 32人参与
# 实习简历求拷打 #
23508次浏览 248人参与
# 你觉得现在还能进互联网吗? #
29932次浏览 201人参与
# 携程工作体验 #
18926次浏览 66人参与
# 大厂VS公务员你怎么选 #
69119次浏览 638人参与
# 扒一扒那些奇葩实习经历 #
140135次浏览 1149人参与
# 找不到好工作选择GAP真的丢人吗 #
93653次浏览 1007人参与
# 那些我实习了才知道的事 #
253004次浏览 1784人参与
# 非技术投递记录 #
672835次浏览 6820人参与
# 机械求职避坑tips #
81038次浏览 531人参与
# 投格力的你,拿到offer了吗? #
154795次浏览 829人参与
# 第一份工作能做外包吗? #
94018次浏览 599人参与
# 作业帮求职进展汇总 #
85333次浏览 559人参与
# 秋招遇到的奇葩面试题 #
101237次浏览 416人参与

