hdu4090 GemAnd Prince

来源:互联网 发布:vb中trim是什么意思 编辑:程序博客网 时间:2024/05/02 03:44

按规则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;}


原创粉丝点击