Hdu 4090 Gem And Prince(暴搜+剪枝)

点击打开链接

GemAnd Prince

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


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 3 1 1 3 1 2 1 1 1 2 5 4 3 2 2 3 3 1 1 3 3 3 2 2 2 3 1 1 1 3 1 2 2
 

Sample Output
  
  
36 103
 

Source

题解:由于图很小,直接暴搜,然后加上最优性剪枝即可。

最优性剪枝:如果当前图的所有宝石都能消,判断会不会使答案最优,不会则减掉。

代码如下:

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<string.h>
#include<string>
#include<math.h>
#define nn 2100
#define eps 1e-8
#define inff 0x3fffffff
typedef long long LL;
using namespace std;
int dir[8][2]={1,0,-1,0,0,1,0,-1,1,1,1,-1,-1,1,-1,-1};
int n,m,k;
int ys[7];
int ans;
struct node
{
    int x,y;
    node(){};
    node(int xx,int yy)
    {
        x=xx,y=yy;
    }
}sta;
struct sz
{
    int a[8][8];
}tu;
node que[110];
sz zhuan(sz x)
{
    int i,j;
    int ix;
    sz re;
    memset(re.a,0,sizeof(re.a));
    for(i=0;i<m;i++)
    {
        ix=n-1;
        for(j=n-1;j>=0;j--)
        {
            if(x.a[j][i]!=0)
            {
                re.a[ix][i]=x.a[j][i];
                ix--;
            }
        }
    }
    memset(x.a,0,sizeof(x.a));
    ix=0;
    for(i=0;i<m;i++)
    {
        if(re.a[n-1][i]!=0)
        {
            for(j=0;j<n;j++)
            {
                x.a[j][ix]=re.a[j][i];
            }
            ix++;
        }
    }
    return x;
}
bool check(int x,int y)
{
    if(x<0||x>=n||y<0||y>=m)
        return false;
    return true;
}
void dfs(sz s,int val)
{
    if(val>ans)
        ans=val;
    int i,j,g,e;
    bool use[8][8];
    sz tem;
    memset(ys,0,sizeof(ys));
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            use[i][j]=false;
            if(s.a[i][j]!=0)
            {
                ys[s.a[i][j]]++;
            }
        }
    }
    int ix=0;
    for(i=1;i<=k;i++)
    {
        ix+=ys[i]*ys[i];
    }
    if(ix+val<=ans)
        return ;
    node ve[65];
    int dx,dy;
    int top,pop,lv;
    for(g=0;g<n;g++)
    {
        for(e=0;e<m;e++)
        {
            tem.a[g][e]=s.a[g][e];
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(!use[i][j]&&s.a[i][j])
            {
                lv=0;
                top=pop=0;
                use[i][j]=true;
                tem.a[i][j]=0;
                ve[lv++]=node(i,j);
                que[pop++]=node(i,j);
                while(top!=pop)
                {
                    sta=que[top++];
                    for(g=0;g<8;g++)
                    {
                        dx=sta.x+dir[g][0],dy=sta.y+dir[g][1];
                        if(check(dx,dy)&&!use[dx][dy]&&s.a[dx][dy]==s.a[i][j])
                        {
                            tem.a[dx][dy]=0;
                            use[dx][dy]=true;
                            ve[lv++]=node(dx,dy);
                            que[pop++]=node(dx,dy);
                        }
                    }
                }
                if(lv>=3)
                {
                    dfs(zhuan(tem),val+lv*lv);
                }
                for(g=0;g<lv;g++)
                {
                    tem.a[ve[g].x][ve[g].y]=s.a[i][j];
                }
            }
        }
    }
}
int main()
{
    int i,j;
    while(scanf("%d%d%d",&n,&m,&k)!=EOF)
    {
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                scanf("%d",&tu.a[i][j]);
            }
        }
        ans=0;
        dfs(tu,0);
        printf("%d\n",ans);
    }
    return 0;
}

第二次做这题的代码:

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<string>
#include<stack>
#include<math.h>
#include<string.h>
#include<vector>
#include<set>
#include<map>
#include<queue>
#define nn 110000
#define inff 0x7fffffff
#define eps 1e-8
#define mod 1500007
typedef __int64 LL;
const LL inf64=LL(inff)*inff;
using namespace std;
int n,m,K;
int ans;
int num[7];
queue<pair<int,int> >que;
int dir[8][2]={1,0,-1,0,0,1,0,-1,1,1,1,-1,-1,1,-1,-1};
void dfs(int tu[8][8],int val)
{
    int i,j,k,e;
    int ix=0;
    for(i=1;i<=K;i++)
    {
        ix+=num[i]*num[i];
    }
    ans=max(ans,val);
    if(ix==0||ix+val<=ans)
        return ;
    bool use[8][8];
    int tem[8][8];
    pair<int,int>sta;
    int dx,dy;
    int fc;
    memset(use,false,sizeof(use));
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(tu[i][j]!=0&&!use[i][j])
            {
                for(k=0;k<n;k++)
                {
                    for(e=0;e<m;e++)
                    {
                        tem[k][e]=tu[k][e];
                    }
                }
                use[i][j]=true;
                que.push(make_pair(i,j));
                tem[i][j]=0;
                ix=0;
                while(que.size())
                {
                    sta=que.front();
                    ix++;
                    que.pop();
                    for(k=0;k<8;k++)
                    {
                        dx=sta.first+dir[k][0];
                        dy=sta.second+dir[k][1];
                        if(dx>=0&&dx<n&&dy>=0&&dy<m)
                        {
                            if(!use[dx][dy]&&tem[dx][dy]==tu[i][j])
                            {
                                use[dx][dy]=true;
                                tem[dx][dy]=0;
                                que.push(make_pair(dx,dy));
                            }
                        }
                    }
                }
                if(ix<3)
                    continue;
                for(e=0;e<m;e++)
                {
                    fc=n-1;
                    for(k=n-1;k>=0;k--)
                    {
                        if(tem[k][e]!=0)
                        {
                            if(fc==k)
                                fc--;
                            else
                            {
                                tem[fc--][e]=tem[k][e];
                                tem[k][e]=0;
                            }
                        }
                    }
                }
                fc=0;
                for(e=0;e<m;e++)
                {
                    for(k=n-1;k>=0;k--)
                    {
                        if(tem[k][e]!=0)
                        {
                            break;
                        }
                    }
                    if(k>=0)
                    {
                        if(e==fc)
                            fc++;
                        else
                        {
                            for(k=n-1;k>=0;k--)
                            {
                                tem[k][fc]=tem[k][e];
                                tem[k][e]=0;
                            }
                            fc++;
                        }
                    }
                }
                num[tu[i][j]]-=ix;
                dfs(tem,val+ix*ix);
                num[tu[i][j]]+=ix;
            }
        }
    }
}
int main()
{
    int i,j;
    int s[8][8];
    while(scanf("%d%d%d",&n,&m,&K)!=EOF)
    {
        memset(num,0,sizeof(num));
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                scanf("%d",&s[i][j]);
                num[s[i][j]]++;
            }
        }
        ans=0;
        dfs(s,0);
        printf("%d\n",ans);
    }
    return 0;
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值