OJ题目:click here~~~
题目分析:九宫格,只利用九宫格的边。开始,边都没有添加。两个人轮流添加边,如果某人添加了某边之后,形成了x个新的1*1的方格,则此人得x分。边全部添加完后,得分高者获胜。已经进行到第n轮,问是先手赢,还是后手赢。
struct Point{
int x ;
int y ;
Point(){}
Point(int a ,int b):x(a) , y(b){}
};
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 x){
return Point((x - 1)/4 , (x - 1)%4) ;
}
void add_edge(Point p1 , Point p2 , bool value){
if(p1.x > p2.x || p1.y > p2.y)
swap(p1 , p2) ;
if(p1.x == p2.x)
edge_row[p1.x][p1.y] = value ;
else
edge_col[p1.x][p1.y] = value ;
}
int squre(Point a){
if(a.x < 0 || a.x > 2 || a.y < 0 || a.y > 2) return 0 ;
bool r = true ;
r = r&&edge_row[a.x][a.y] ;
r = r&&edge_row[a.x + 1][a.y] ;
r = r&&edge_col[a.x][a.y] ;
r = r&&edge_col[a.x][a.y + 1] ;
if(r) return 1;
else 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 squre(Point(a.x - 1 , a.y)) + squre(a) ;
return squre(Point(a.x , a.y - 1)) + squre(a) ;
}
void input(){
tom_turn = true ;
tom_score = jerry_score = 0 ;
int i , a , b ;
scanf("%d",&edge_num) ;
memset(edge_row , 0 , sizeof(edge_row)) ;
memset(edge_col , 0 , sizeof(edge_col)) ;
for(i = 0;i < edge_num;i++){
scanf("%d%d",&a ,&b) ;
Point p1 = get_point(a) ;
Point p2 = get_point(b) ;
add_edge(p1 , p2 , true) ;
if(tom_turn)
tom_score += get_score(p1 , p2) ;
else
jerry_score += get_score(p1 , p2) ;
tom_turn = !tom_turn ;
}
}
bool dfs(int now , int pre , int 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) ;
add_edge(a , b , true) ;
int s = get_score(a , b) ;
win = !dfs(pre , now + s , !turn) ;
add_edge(a , b , false) ;
if(win) return true ;
}
if(!edge_col[j][i]){
did = true ;
Point a = Point(j , i) ;
Point b = Point(j + 1 , i) ;
add_edge(a , b , true) ;
int s = get_score(a , b) ;
win = !dfs(pre , now + s , !turn) ;
add_edge(a , b , false) ;
if(win) return true ;
}
}
}
if(!did){
if(now == pre)
return !tom_turn ;
return now > pre ;
}
return false ;
}
int main(){
//freopen("in.txt" , "r" , stdin) ;
int Case ;
cin >> Case ;
for(int i = 1;i <= Case;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 ;
}