Fennec VS. Snuke

FennecVS.Snuke游戏策略
本文介绍了一个有趣的板上游戏FennecVS.Snuke,玩家需轮流将未着色的格子涂成黑色或白色。通过最优策略确定最终胜者,并附带了一段使用DFS算法实现的C++代码。

Fennec VS. Snuke


Time limit : 2sec / Memory limit : 256MB

Score : 400 points

Problem Statement

Fennec and Snuke are playing a board game.

On the board, there are N cells numbered 1 through N, and N−1 roads, each connecting two cells. Cell ai is adjacent to Cell bi through the i-th road. Every cell can be reached from every other cell by repeatedly traveling to an adjacent cell. In terms of graph theory, the graph formed by the cells and the roads is a tree.

Initially, Cell 1 is painted black, and Cell N is painted white. The other cells are not yet colored. Fennec (who goes first) and Snuke (who goes second) alternately paint an uncolored cell. More specifically, each player performs the following action in her/his turn:

  • Fennec: selects an uncolored cell that is adjacent to a black cell, and paints it black.
  • Snuke: selects an uncolored cell that is adjacent to a white cell, and paints it white.

A player loses when she/he cannot paint a cell. Determine the winner of the game when Fennec and Snuke play optimally.

Constraints

  • 2≤N≤105
  • 1≤ai,bi≤N
  • The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a1 b1
:
aN−1 bN−1

Output

If Fennec wins, print Fennec; if Snuke wins, print Snuke.


Sample Input 1

7
3 6
1 2
3 1
7 4
5 7
1 4

Sample Output 1

Fennec

For example, if Fennec first paints Cell 2 black, she will win regardless of Snuke's moves.


Sample Input 2

4
1 4
4 2
2 3

Sample Output 2

Snuke



//n个格子,编号为1-n,1开始是黑色,n开始是白色。有m条边,且为树,说明格子的相邻情况,然后Fnc先开始涂黑色,涂色规则是:格子没被涂过色,并且相邻有黑色格子
然后Snu涂色,类似的规则,snu涂白色,相邻要有白色。轮流涂色,直到有一方不能涂了,另一方获胜。

显然,他们玩游戏会采取这样的策略,fnc先向着n点去涂色,snu向着1点去涂色,这样可以尽可能获得更多的地盘,然后就是比谁的地盘大咯
用神奇DFS实现
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <algorithm>
 5 #include <vector>
 6 using namespace std;
 7 #define LL long long
 8 #define MX 100010
 9 
10 int n;
11 int total;
12 vector<int> G[MX];
13 int colr[MX];
14 
15 void DFS(int x,int pre,int c,int &tot)
16 {
17     if (colr[x]==-c) return;
18     tot++;
19     for (int i=0;i<G[x].size();i++)
20         if (G[x][i]!=pre)
21             DFS(G[x][i],x,c,tot);
22 }
23 
24 void dfs(int u,int pre,int s,int &ok)
25 {
26     if (u==n)
27     {
28         ok=s;
29         return;
30     }
31     for (int i=0;i<G[u].size();i++)
32     {
33         if (ok) break;
34         if (G[u][i]!=pre)
35             dfs(G[u][i],u,s+1,ok);
36     }
37     if (ok)
38     {
39         if (u!=1&&s<=ok/2) colr[u]=1;
40         if (u!=n&&s>ok/2) colr[u]=-1;
41     }
42     return;
43 }
44 
45 int main()
46 {
47     scanf("%d",&n);
48     for (int i=1;i<n;i++)
49     {
50         int u,v;
51         scanf("%d%d",&u,&v);
52         G[u].push_back(v);
53         G[v].push_back(u);
54     }
55     colr[1]=1; //hei
56     colr[n]=-1;//bai
57 
58     int ok=0;
59     dfs(1,-10,0,ok);
60 /*
61     for (int i=1;i<=n;i++)
62         printf("%d ",colr[i]);
63     printf("\n");
64 */
65 
66     int num_1=0;
67     DFS(1,-10,1,num_1);
68     int num_2=0;
69     DFS(n,-10,-1,num_2);
70     if (num_1-num_2>=1)
71         printf("Fennec\n");
72     else
73         printf("Snuke\n");
74     return 0;
75 }
View Code

 



转载于:https://www.cnblogs.com/haoabcd2010/p/7186396.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值