hdu4753Fishhead’s Little Game【状态压缩记忆化搜索+博弈】


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
 

千万 不要被博弈吓到!!!这题看着有博弈的色彩其实还是dfs选择最优解!

题意:俩人在如图网格上轮流画边,每组成一个正方形,这个人分数+1,要是画的边组成了两个正方形,这个人分数+2.给你前n轮的动作,(这个是按顺序来的,开始我还纳闷,要是给的不按顺序来要怎么做2333)假设两人都足够聪明,预测谁赢

首先,不要怕麻烦,把边的表示法好好写出来,然后一边读入一边累加个人积分,这玩意又被我想复杂了,其实数据这么小就读入一条遍历一遍又能怎样慢啊。然后,关键的部分到了,记忆化搜索+状态压缩华丽登场~

dfs(状态,剩余分数)ok(状态,要图的边号)很暴力的做法,看代码就好啦~

/************
hdu4753
2016.3.14-2016.3.28
62MS	1612K	3054B	G++
*************/
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int mp[24][24];///给每条边进行编号
int vis[25];///标记是否已经访问过该边
int dp[(1<<13)+3],cnt,x[25];
int tom,jerry,n;
int circle[9][4]={
    1,4,8,5,    2,5,9,6,    3,6,10,7,
    8,11,15,12, 9,12,16,13, 10,13,17,14,
    15,18,22,19,16,19,23,20,17,20,24,21,
};
void init(){
    memset(mp,0,sizeof(mp));
    memset(vis,0,sizeof(vis));
    mp[1][2]=1;mp[2][3]=2;mp[3][4]=3;
    mp[1][5]=4;mp[2][6]=5;mp[3][7]=6;mp[4][8]=7;
    mp[5][6]=8;mp[6][7]=9;mp[7][8]=10;
    mp[5][9]=11;mp[6][10]=12;mp[7][11]=13;mp[8][12]=14;
    mp[9][10]=15;mp[10][11]=16;mp[11][12]=17;
    mp[9][13]=18;mp[10][14]=19;mp[11][15]=20;mp[12][16]=21;
    mp[13][14]=22;mp[14][15]=23;mp[15][16]=24;
}
int judge()
{
    int sum=0;
    for(int i=0;i<9;i++)
    {
        if(vis[circle[i][0]]&&vis[circle[i][1]]&&vis[circle[i][2]]&&vis[circle[i][3]])
            sum++;
    }
    return sum;
}
int ok(int ste,int k)
{
    int vv[25];
    memset(vv,0,sizeof(vv));
    for(int i=0;i<=cnt;i++)
    {
        if(ste&(1<<i)) vv[x[i]]=1;
    }
    int s1=0;
    for(int i=0;i<9;i++)
    {
        if((vis[circle[i][0]]||vv[circle[i][0]])&&(vis[circle[i][1]]||vv[circle[i][1]])&&(vis[circle[i][2]]||vv[circle[i][2]])&&(vis[circle[i][3]]||vv[circle[i][3]]))
        s1++;
    }
    int s2=0;
    vv[x[k]]=1;
    for(int i=0;i<9;i++)
    {
        if((vis[circle[i][0]]||vv[circle[i][0]])&&(vis[circle[i][1]]||vv[circle[i][1]])&&(vis[circle[i][2]]||vv[circle[i][2]])&&(vis[circle[i][3]]||vv[circle[i][3]]))
        s2++;
    }
    return s2-s1;
}
int dfs(int ste,int k)
{
    if(dp[ste]!=-1)return dp[ste];
    int sum=0;
    for(int i=0;i<=cnt;i++)
    {
        int s=(1<<i);
        if((ste&s)==0)
        {
            s=ste|s;
            int t=ok(ste,i);
            int ss=dfs(s,k-t);
            if(k-ss>sum)sum=k-ss;
        }
    }
    return dp[ste]=sum;
}
int main()
{
   // freopen("cin.txt","r",stdin);
    int t,cas=1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        init();
        int s1=0,s=0;
        tom=jerry=0;
        for(int i=0;i<n;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            if(a>b)swap(a,b);
            vis[mp[a][b]]=1;
            s1=judge();
            if(i&1)
                jerry+=(s1-s);
            else tom+=(s1-s);
            s=s1;
        }
        cnt=-1;
        for(int i=1;i<=24;i++)if(!vis[i])x[++cnt]=i;
        s=9-judge();
        memset(dp,-1,sizeof(dp));
        int num=dfs(0,s);
        printf("Case #%d: ",cas++);
        if(n&1)
        {
            if(tom+s-num>jerry+num)
                printf("Tom200\n");
            else
                printf("Jerry404\n");
        }
        else
        {
            if(tom+num>jerry+s-num)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、付费专栏及课程。

余额充值