Fishhead’s Little Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1122 Accepted Submission(s): 342
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.
这个题看了好久题解想了好久其实就是直接暴力的枚举所有的之后添加边的所有状态最多也就2^12种状态
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
int vis[25];//记录出现的边。
int id[25];//记录还剩下哪条边。
int dp[10000];//dp[i]状态下形成的所形成的正方形数。
int dis[10000];//在dis[i]状态下最多生成多少边。
int t;
int judge(int vist[]){
int sum=0;
for(int 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 status(int x)
{
int sum=0,i;
int vist[26];
for(i=0;i<=24;i++)vist[i]=0;
for(i=1;i<=t;i++)
{
if(x&(1<<(i-1)))
{
vist[id[i-1]]=1;
}
}
for(i=1;i<=24;i++)
{
if(vis[i])vist[i]=1;
}
sum=judge(vist);
return sum;
}//返回每种状态最终形成的正方数。
int DFS(int x)
{
if(dis[x]!=-1)return dis[x];
int ans=0;
for(int i=1;i<=t;i++) //枚举t个位置
{
if(!(x&(1<<(i-1)))) //判断这个位置是否已经被使用。
{
int y;
y=x+(1<<(i-1));//把这个位置添加进去。
int ss;
ss=DFS(y);//ss为这个边添加进去后最多生成多少边。
ans=max(9-dp[x]-ss,ans);//记录最多添加多少边。9-dp[x]是还剩下多少边。
}
}
dis[x]=ans;
return ans;
}
int main(){
int T; scanf("%d",&T); int kcase=1;
while(T--){
int ans1=0,ans2=0;//Tom形成的正方形数 和jerry形成的正方形数
int s=0;
memset(vis, false, sizeof(vis));
memset(dis, -1, sizeof(dis));
int n; scanf("%d",&n);
for(int i=1;i<=n;i++){
int a,b; scanf("%d %d",&a,&b);
if(a>b) swap(a, b);
if(b-a==4) t=12+a;
else t=a-a/4;
vis[t]=true;
if(i&1)
ans1+=(judge(vis)-s);
else
ans2+=(judge(vis)-s);
s=judge(vis);
// printf("~~~%d %d\n",ans1,ans2);
}//初始状态直接计算所得的正方形数。
t=0;
for(int i=1;i<=24;i++) if(!vis[i]) id[t++]=i;//记录下还有哪些边没有被添加。
for(int i=0;i<(1<<t);i++){
dp[i]=status(i);
}//枚举所有未添加边可能形成的状态。如0101010,0001001。
int ans=9-s;//还剩下多少边。
int make=DFS(0);//从添加0条边开始搜索求出最优状态。
// printf("%d %d %d %d\n",ans1,ans2,ans,make);
printf("Case #%d: ",kcase++);
if(n&1){
if(ans2+make>ans1+ans-make) printf("Jerry404\n");
else printf("Tom200\n");
}
else{
if(ans1+make<ans2+ans-make) printf("Jerry404\n");
else printf("Tom200\n");
}
}
return 0;
}