首页 > ACM题库 > HDU-杭电 > HDU 4753-Fishhead’s Little Game-记忆化搜索-[解题报告]HOJ
2015
09-17

HDU 4753-Fishhead’s Little Game-记忆化搜索-[解题报告]HOJ

Fishhead’s Little Game

问题描述 :

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

Polygon

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

Polygon

adding edge 6 �C> 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.

Polygon

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.

输入:

  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.

输出:

  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.

样例输入:

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

样例输出:

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.

网赛的一道题目,当时没做出来。

由题意可知,最多只有12条边未知。

所以最多只有(1<<12)种状态。

所以记忆化搜索这种状态下,枚举添加任意一条边之后的状态的最优值。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
using namespace std;
int dp[10001];
int visit[10001];
int vis[25];
int oth[25];
int ts;
int num(int vist[])
{
    int i;
    int sum=0;
    for(i=1;i<=9;i++)
    {
        if(vist[i]&&vist[i+3]&&vist[i+12+(i-1)/3]&&vist[i+13+(i-1)/3])sum++;
    }
    return sum;
}
int dos(int x)
{
    int sum=0,i;
    int vist[26];
    for(i=0;i<=24;i++)vist[i]=0;
    for(i=1;i<=ts;i++)
    {
        if(x&(1<<(i-1)))
        {
            vist[oth[i-1]]=1;
        }
    }
    for(i=1;i<=24;i++)
    {
        if(vis[i])vist[i]=1;
    }
    sum=num(vist);
    return sum;
}
int dfs(int x)
{
    if(visit[x]!=-1)return visit[x];
    int ans=0;
    for(int i=1;i<=ts;i++)
    {
        if(!(x&(1<<(i-1))))
        {
            int y;
            y=x+(1<<(i-1));
            int ss;
            ss=dfs(y);
            ans=max(9-dp[x]-ss,ans);
        }
    }
    visit[x]=ans;
    return ans;
}
int main()
{
    int T,i,n,a,b;
    int cas;
    int anum,bnum,s;
    cas=0;
    scanf("%d",&T);
    while(T--)
    {
        cas++;
        anum=bnum=0;
        s=0;
        scanf("%d",&n);
        memset(vis,0,sizeof(vis));
        memset(visit,-1,sizeof(visit));
        for(i=0;i<n;i++)
        {
            scanf("%d%d",&a,&b);
            if(a>b)swap(a,b);
            if(b-a==4)ts=12+a;
            else ts=a-a/4;
            vis[ts]=1;
            if(i%2==0)
            {
                anum+=num(vis)-s;
            }
            else bnum+=num(vis)-s;
            s=num(vis);
        }
        ts=0;
        for(i=1;i<=24;i++)if(vis[i]==0)oth[ts++]=i;
        for(i=0;i<(1<<ts);i++)dp[i]=dos(i);
        int have;
        have=9-s;
        int fs;
        fs=dfs(0);
        //printf("%d %d %d %d\n",anum,bnum,have,fs);
        printf("Case #%d: ",cas);
        if(n%2==0)
        {
            if(anum+fs>bnum+have-fs)cout<<"Tom200"<<endl;
            else cout<<"Jerry404"<<endl;
        }
        else
        {
            if(anum+have-fs>bnum+fs)cout<<"Tom200"<<endl;
            else cout<<"Jerry404"<<endl;
        }
    }
    return 0;
}

参考:http://blog.csdn.net/rowanhaoa/article/details/13599487