# 2013 ACM/ICPC 南京网络赛F题

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

struct Point
{
int x, y;
Point()
{}
Point(int x, int y):x(x), y(y)
{}
};

bool edge_row[5][5];
bool edge_col[5][5];
bool tom_turn;
int edge_num;
int tom_score, jerry_score;

Point get_point(int a)
{
return Point((a - 1) / 4, (a - 1) % 4);
}

void add_edge(Point a, Point b, bool value)
{
if (a.x > b.x || a.y > b.y)
swap(a, b);
if (a.x == b.x)
edge_row[a.x][a.y] = value;
else
edge_col[a.x][a.y] = value;
}

int square(Point a)
{
if (a.x < 0 || a.y < 0 || a.x > 2 || a.y > 2)
return 0;
bool ret = true;
ret = ret && edge_row[a.x][a.y];
ret = ret && edge_col[a.x][a.y];
ret = ret && edge_row[a.x + 1][a.y];
ret = ret && edge_col[a.x][a.y + 1];
if (ret)
return 1;
return 0;
}

int get_score(Point a, Point b)
{
if (a.x > b.x || a.y > b.y)
swap(a, b);
if (a.x == b.x)
return square(Point(a.x - 1, a.y)) + square(a);
return square(Point(a.x, a.y - 1)) + square(a);
}

void input()
{
tom_turn = true;
tom_score = jerry_score = 0;
scanf("%d", &edge_num);
memset(edge_row, 0, sizeof(edge_row));
memset(edge_col, 0, sizeof(edge_col));
for (int i = 0; i < edge_num; i++)
{
int a, b;
scanf("%d%d", &a, &b);
Point p1 = get_point(a);
Point p2 = get_point(b);
if (tom_turn)
tom_score += get_score(p1, p2);
else
jerry_score += get_score(p1, p2);
tom_turn = !tom_turn;
}
}

bool dfs(int next_score, int previous_score, bool tom_turn)
{
bool win = false;
bool did = false;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 3; j++)
{
if (!edge_row[i][j])
{
did = true;
Point a = Point(i, j);
Point b = Point(i, j + 1);
int score = get_score(a, b);
win = !dfs(previous_score, next_score + score, !tom_turn);
if (win)
return true;
}
if (!edge_col[j][i])
{
did = true;
Point a = Point(j, i);
Point b = Point(j + 1, i);
int score = get_score(a, b);
win = !dfs(previous_score, next_score + score, !tom_turn);
if (win)
return true;
}
}
}
if (!did)
{
if (next_score == previous_score)
return !tom_turn;
return next_score > previous_score;
}
return false;
}

int main()
{
int case_num;
scanf("%d", &case_num);
for (int i = 1; i <= case_num; i++)
{
input();
bool tom_win;
if (tom_turn)
tom_win = dfs(tom_score, jerry_score, tom_turn);
else
tom_win = !dfs(jerry_score, tom_score, !tom_turn);
printf("Case #%d: ", i);
if (tom_win)
printf("Tom200\n");
else
printf("Jerry404\n");
}
return 0;
}
View Code

posted @ 2013-09-22 08:50 金海峰 阅读(...) 评论(...) 编辑 收藏