太难了。。。时间只够做第一题。。。第二题没做。 #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

相关推荐

牛客网
牛客网在线编程
牛客网题解
牛客企业服务