hdu4753 State compression DP game (Memory search notation)

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=4753

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


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


Adding Edge 6–> 10
The rule of this game was that each player takes turns to add an edge. You'll get one point if the edge of just added, together with edges already added before, forms a new square (only Squa Re of size 1 is considered). Of course, you get both points if that edge forms the squares. Notice that a edge can be added only once.


Forming-squares to get-points
Tom200 and Jerry404 is playing this little game, and has played N rounds when FishHead comes in. FishHead wants to know who'll be the winner. Can you help him? Assume that Tom200 and Jerry404 is 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 <=), the number of test cases.
For each case, the first line contains an integers n (<= n <=), which means they has taken total n rounds in Turn. Next n lines each contains, integers a, B (a, B <=) representing the The edge.

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

Sample Input
1151 21 52 65 96 109 105 62 33 77 1110 113 46 77 84 8

Sample Output
Case #1: Tom200Hint in case 1, Tom200 gets both points when she add edge 5--6, both points in Edge 6, 7, One point in 4-8.
/**hdu4753 State compression DP game (Memory search notation) topic: For a 3*3 checkerboard Edge () plus the current edge after a few more than a few grid four sides are filled, a bit, now Tom and Jerry alternating fill, always Tom first, now has filled n side, ask the rest of the side Both adopt the optimal strategy, who will win the problem-solving ideas: Because the unknown side up to only 12, we can take the state compression, with the search way to write note: Can not directly press 24 bits, will burst memory of */#include <stdio.h> #include < string.h> #include <algorithm> #include <iostream>using namespace Std;int mp[24][24];///each edge is numbered int vis [25];///tag has already accessed the side int dp[(1<<13) +3],cnt,x[25];int tom,jerry,n;int circle[9][4]={///corresponds to 9 lattice of the respective four sides 1,    4,5,8,2,5,6,9,3,6,7,10,8,11,12,15,9,12,13,16,10,13,14,17,15,18,19,22,16,19,20,23,17,20,21,24};void Init () {    Memset (Mp,0,sizeof (MP));    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]]&AMP;&AMP;VIS[CIRCLE[I][1]]&AMP;&AMP;VIS[CIRCLE[I][2]]&AMP;&AMP;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,s2=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++;    } 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 maxx=0;        for (int i=0;i<=cnt;i++) {int s=1<<i; if ((s&ste) ==0) {s=ste|s;            int T=ok (ste,i);            int Sum=dfs (S,K-T);        if (K-sum>maxx) maxx=k-sum; }} return Dp[ste]=maxx;}    int main () {int t,tt=0;    scanf ("%d", &t);    Init ();        while (t--) {memset (vis,0,sizeof (VIS));        scanf ("%d", &n);        tom=0,jerry=0;        int s=0,ste=0;            for (int i=0;i<n;i++) {int x, y;            scanf ("%d%d", &x,&y);            if (x>y) swap (x, y);            Vis[mp[x][y]]=1;            int S1=judge ();            if (i&1) {jerry+= (s1-s);            } else {tom+= (s1-s);           } s=s1;        printf ("(%d%d) \ n", Tom,jerry);        } 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:", ++TT);  if ((n&1) ==0) {          if (tom+num>jerry+s-num) printf ("tom200\n");        else printf ("jerry404\n");            } else {if (tom+s-num>jerry+num) printf ("tom200\n");        else printf ("jerry404\n"); }} return 0;}


hdu4753 State compression DP game (Memory search notation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.