hdu 4753 Fishhead’s Little Game(状压dp)

Fishhead’s Little Game

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 603    Accepted Submission(s): 148


Problem Description
   There is a 3 by 3 grid and each vertex is assigned a number.


   It looks like JiuGongGe, but they are different, for we are not going to fill the cell but the edge. For instance,


adding edge 6 –> 10

   The rule of this game is that each player takes turns to add an edge. You will get one point if the edge you just added, together with edges already added before, forms a new square (only square of size 1 is considered). Of course, you get two points if that edge forms two squares. Notice that an edge can be added only once.


forming two squares to get two points

  Tom200 and Jerry404 is playing this little game, and have played n rounds when Fishhead comes in. Fishhead wants to know who will be the winner. Can you help him? Assume that Tom200 and Jerry404 are clever enough to make optimal decisions in each round. Every Game starts from Tom200.
 

Input
  The first line of the input contains a single integer T (T <= 100), the number of test cases.
  For each case, the first line contains an integers n (12 <= n <= 24), which means they have taken total n rounds in turn. Next n lines each contains two integers a, b (a, b <= 16) representing the two endpoints of the edge.
 

Output
  For each case output one line "Case #X: ", representing the Xth case, starting from 1. If Tom200 wins, print "Tom200" on one line, print "Jerry404" otherwise.
 

Sample Input
  
  
1 15 1 2 1 5 2 6 5 9 6 10 9 10 5 6 2 3 3 7 7 11 10 11 3 4 6 7 7 8 4 8
 

Sample Output
  
  
Case #1: Tom200
Hint
  In case 1, Tom200 gets two points when she add edge 5 -> 6, two points in edge 6 -> 7, one point in 4 -> 8.
 

Source
 

题意:图上有24条边,先规定n步走法,若某一步使某一个正方形的四条边都凑齐,那么这个人得1分,问走完接下来的24-n步,最后谁赢

题解:将每一条边编号0~23,用一个数字代表一个状态,先保存下得分的正方形的数字,先模拟走完n步,然后剩下的24-n步(此处题目保证了小于12步)


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int c[18][18],dp[10008],has[18],all;
int right[9]={153,306,612,19584,39168,78336,2506752,5013504,10027008};
int MAX(int a,int b)
{
    return a>b?a:b;
}
void init()
{
    c[1][2]=0;    c[2][3]=1;    c[3][4]=2;
    c[1][5]=3;    c[2][6]=4;    c[3][7]=5;    c[4][8]=6;
    c[5][6]=7;    c[6][7]=8;    c[7][8]=9;
    c[5][9]=10;   c[6][10]=11;  c[7][11]=12;  c[8][12]=13;
    c[9][10]=14;  c[10][11]=15; c[11][12]=16;
    c[9][13]=17;  c[10][14]=18; c[11][15]=19; c[12][16]=20;
    c[13][14]=21; c[14][15]=22; c[15][16]=23;
}
int getpoint(int now)
{
    int res=0,i;

    for(i=0;i<9;i++)
        if((now&right[i])==right[i]) res++;

    return res;
}
int dfs(int now,int hnow)
{
    int i,temp,npoint,ans=-0xffffff;

    if(dp[hnow]!=-1) return dp[hnow];
    npoint=getpoint(now);
    for(i=0;i<all;i++)
    {
        if((1<<i)&hnow) continue;
        temp=getpoint(now|(1<<has[i]));
        temp=temp-npoint-dfs(now|(1<<has[i]),hnow|(1<<i));
        ans=MAX(ans,temp);
    }

    return dp[hnow]=ans;
}
int main()
{
    int side,n,tom,jer,now;
    int t,i,x,y,cas=1,res;

    //freopen("t.txt","r",stdin);
    init();
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(tom=jer=side=now=i=0;i<n;i++)
        {
            scanf("%d%d",&x,&y);
            if(x>y) x^=y^=x^=y;
            now=now|(1<<c[x][y]);
            if(side&1) jer=getpoint(now)-tom;
            else tom=getpoint(now)-jer;
            side=(side+1)%2;
        }

        memset(dp,-1,sizeof(dp));
        res=tom-jer;
        for(all=i=0;i<24;i++)
        {
            if((1<<i)&now) continue;
            has[all++]=i;
        }
        dp[(1<<all)-1]=0;
        if(side&1) res-=dfs(now,0);
        else res+=dfs(now,0);

        printf("Case #%d: ",cas++);
        if(res>0) printf("Tom200\n");
        else printf("Jerry404\n");
    }

    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值