hdu4090 GemAnd Prince

10 篇文章 0 订阅

按规则DFS,细节比较多

BFS会超内存     传送门


//#pragma warning (disable: 4786)
//#pragma comment (linker, "/STACK:16777216")
//HEAD
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <string>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
//LOOP
#define FF(i, a, b) for(int i = (a); i < (b); ++i)
#define FE(i, a, b) for(int i = (a); i <= (b); ++i)
#define FED(i, b, a) for(int i = (b); i>= (a); --i)
#define REP(i, N) for(int i = 0; i < (N); ++i)
#define CLR(A,value) memset(A,value,sizeof(A))
//STL
#define SZ(V) (int)V.size()
#define PB push_back
//INPUT
#define RI(n) scanf("%d", &n)
#define RII(n, m) scanf("%d%d", &n, &m)
#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define RIV(n, m, k, p) scanf("%d%d%d%d", &n, &m, &k, &p)
//OUTPUT
#define WI(n) printf("%d\n", n)
typedef long long LL;
typedef unsigned long long ULL;
typedef vector <int> VI;
const int INF = 100000000;
const double eps = 1e-10;
const int maxn = 10;
const LL MOD = 1e9 + 7;

int n, m, k, tot, ans;
int mat[maxn][maxn];
int dir[][2] = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1},
                {0, 1}, {1, -1}, {1, 0}, {1, 1}};

void change()
{
    int low = n;
    FE(j, 1, m)
    {
        low = n;
        FED(i, n, 1)
            if (mat[i][j])
                mat[low--][j] = mat[i][j];
        while (low >= 1)
            mat[low--][j] = 0;
    }
    low = 1;
    FE(j, 1, m)
        if (mat[n][j])
        {
            if (j != low)
            {
                FE(i, 1, n)
                    mat[i][low] = mat[i][j], mat[i][j] = 0;
            }
            low++;
        }
}

void dfs(int x, int y, int k, bool vis[][maxn])
{
    mat[x][y] = 0;
    tot++;
    vis[x][y] = 1;
    REP(i, 8)
    {
        int xx = x + dir[i][0], yy = y + dir[i][1];
        if (mat[xx][yy] == k && !vis[xx][yy])
            dfs(xx, yy, k, vis);
    }
}

int get()
{
    int sum[maxn] = {0}, s = 0;
    FE(i, 1, n)
        FE(j, 1, m)
            sum[mat[i][j]]++;
    FE(i, 1, k)
        if (sum[i] >= 3)
            s += sum[i] * sum[i];
    return s;
}

void work(int p)
{
    ans = max(ans, p);
    if (p + get() <= ans)
        return;
    bool vis[maxn][maxn] = {0};
    int tmp[maxn][maxn];
    FE(i, 1, n)
    {
        FE(j, 1, m)
        if (!vis[i][j] && mat[i][j])
        {
            tot = 0;
            memcpy(tmp, mat, sizeof (mat));
            dfs(i, j, mat[i][j], vis);
            if (tot >= 3)
            {
                change();
                work(p + tot * tot);
            }
            memcpy(mat, tmp, sizeof(tmp));
        }
    }
}


int main()
{
    while (~RIII(n, m, k))
    {
        CLR(mat, 0);
        FE(i, 1, n)
            FE(j, 1, m)
                RI(mat[i][j]);
        ans = 0;
        work(0);
        WI(ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值