hdu 4090 GemAnd Prince

来源:互联网 发布:彩虹六号960m优化设置 编辑:程序博客网 时间:2024/05/22 06:37

GemAnd Prince

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 622 Accepted Submission(s): 220


Problem Description
Nowadays princess Claire wants one more guard and posts the ads throughout the kingdom. For her unparalleled beauty, generality, goodness and other virtues, many people gather at the capital and apply for the position. Because princess Claire is very clever, she doesn't want a fool to be her guard. As Claire is clever, she invents a game to test the applicants. The game is described as follows.
The game begins with a rectangular board of n rows and m columns, containing n*m grids. Each grid is filled with a gem and each gem is covered by one color, denoted by a number.(as the following shows).





If a gem has the same color with another one, and shares the same corner or the same border with it, the two are considered to be adjacent. Two adjacent gems are said to be connective. And we define that if A and B are connective, B and C are connective, then A and C are connective, namely the adjacency is transitive. Each time we can choose a gem and pick up all of the gems connected to it, including itself, and get a score equal to the square of the number of the gems we pick this time(but to make the game more challenging, the number of gems to be picked each time must be equal or larger than three).Another rule is that if one gem is picked, all the gems above it(if there is any)fall down to fill its grid,and if there is one column containing no gems at all, all the columns at its right(also if there is any) move left to fill the column. These rules can be shown as follows.


As the picture [a] above,all the gems that has color 1 are connective. After we choose one of them to be picked, all the gems that are connected to it must also be picked together, as the picture [b] shows (here we use 0 to denote the holes generated by the absence of gems).
Then the rest gems fall, as shown in picture [c]. Then the rest gems move left, as shown in picture [d]. Because we picked six gems at this time, our score increases 6*6=36.And furthermore, because we cannot find another gem, which has at least three gems connected to it(including itself),to be picked, the game comes to an end.
Each applicant will face such a board and the one who gets the highest score will have the honor to serve princess Claire.
Aswmtjdsj also wants to serve for princess Claire. But he realizes that competing with so many people, even among whom there are powerful ACMers, apparently there is little chance to succeed. With the strong desire to be the lucky dog, Aswmtjdsj asks you for help. Can you help make his dream come true?

Input
There are no more than 15 test cases, separated by a blank line, end with EOF. Each case has n+1 lines, the first line of a case has three integers n, m, k (1<=n, m<=8, 1<=k<=6). Each of the next n lines contains m integers. The integer at (i+1)th line and jth column denotes the color of the gem at the grid (i, j), where the grid(1, 1) denotes the top left one, while the grid(n, m) is the lower right one. The integer in the grid is among [1, k].

Output
For each case you should output the highest score you can get in one single line.

Sample Input
3 3 31 1 31 2 11 1 25 4 32 2 3 31 1 3 33 2 2 23 1 1 13 1 2 2

Sample Output
36103

Source
2011 Asia Beijing Regional Contest

Recommend
lcy
这题要优化,可能是,定义结构太慢了吧,先我用的全局变量,怎么都超时,结果,传了一下,数组,就a了,不明白啊!,这题,注意,是要八个方向进行宽搜,因为,题目说了,共一个角,是当成相连的情况的!而且,我们可以知道,如果,当前状态,加上,估计的最大值,还是没有已经得到的答案要大,那么直接剪掉,就这么一点点的小优化就可以了!
#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;struct node {int p[8][8],step,n,m;node(){memset(p,0,sizeof(p));}};struct tree{int x,y;};tree queue[100000];int n,m,k;int lnum[10],maxx,dir[8][2]={{0,1},{0,-1},{1,0},{-1,0},{-1,-1},{1,1},{-1,1},{1,-1}};node bfs(node map,int sx,int sy,int visit[][8]){    int s,e;tree st;    memset(visit,0,sizeof(visit));    int goal=map.p[sx][sy],i,j;    s=e=0;visit[sx][sy]=1;queue[s].x=sx;queue[s].y=sy,map.p[sx][sy]=0;    while(s<=e)    {        st=queue[s];        for(int i=0;i<8;i++)        {            int x=st.x+dir[i][0],y=st.y+dir[i][1];            if(!visit[x][y]&&x>=0&&x<n&&y>=0&&y<m&&map.p[x][y]==goal)            visit[x][y]=1,queue[++e].x=x,queue[e].y=y,map.p[x][y]=0;        }        s++;    }    node re;    if(e+1>=3)    {        int x,ans;        for( i=0;i<m;i++)        {            ans=n-1;              for( j=n-1;j>=0;j--)                if(map.p[j][i]!=0)                map.p[ans--][i]=map.p[j][i];              for(j=ans;j>=0;j--)                map.p[j][i]=0;        }       // map.print();        bool flag=true;int lt[8],ln;        for(i=0,x=-1,ln=0;i<m;i++)        {            flag=true;            for(j=0;j<n&&flag;j++)            if(map.p[j][i]!=0)            {                flag=false;            }            if(!flag)            lt[ln++]=i;        }        if(ln!=m)        {             for(i=0;i<n;i++)            {                for(j=0;j<ln;j++)                    map.p[i][j]=map.p[i][lt[j]];                for(;j<m;j++)                map.p[i][j]=0;            }        }        re=map;        re.step=e+1;    }    else re.step=-1;    return re;}int dfs(node map,int score){    int i,j,use[8][8]={0},sum;    maxx=max(maxx,score);    for(i=1,sum=score;i<=k;i++)    {        if(lnum[i]>=3)        sum+=lnum[i]*lnum[i];    }    if(sum<=maxx)    return 1;   memset(use,0,sizeof(use));    for(i=0;i<n;i++)    for(j=0;j<m;j++)    if(map.p[i][j]!=0&&!use[i][j])    {        node now;now.step=-1;        now=bfs(map,i,j,use);        if(now.step!=-1)        {            lnum[map.p[i][j]]-=now.step;            dfs(now,score+now.step*now.step);            lnum[map.p[i][j]]+=now.step;        }    }}int main (){    int i,j;node map;    while(scanf("%d%d%d",&n,&m,&k)!=EOF)    {        memset(lnum,0,sizeof(lnum));        for(i=0;i<n;i++)            for(j=0;j<m;j++)            {                  scanf("%d",&map.p[i][j]);lnum[map.p[i][j]]++;            }        maxx=0;        map.n=n;map.m=m;        dfs(map,0);        printf("%d\n",maxx);    }    return 0;}