Fennec VS. Snuke

本文介绍了一种基于图论的两人博弈游戏算法实现,玩家通过交替操作改变游戏状态,最终确定胜者。通过深度优先搜索(DFS)策略,标记每个玩家的动作路径,比较两者的最短到达时间来决定游戏结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

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 is given from Standard Input in the following format:
N
a1 b1
:
aN−1 bN−1

 

输出

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

 

样例输入

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

 

样例输出

Fennec

 

提示

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

思路:让第一个人从1开始往下寻找点,第二个人从n往上寻找点,然后深搜记录走节点是哪一步,用两个数组分别记录一下,最后遍历一遍两个标记数组输出答案即可


/*Du Jinzhi*/
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <cstring>
#include <string>
#include <queue>
#include <deque>
#include <stack>
#include <stdlib.h>
#include <list>
#include <map>
#include <utility>
#include <set>
#include <bitset>
#include <vector>
#define pi acos(-1.0)
#define inf 0x3f3f3f3f
#define ll long long
#define linf 0x3f3f3f3f3f3f3f3fLL
using namespace std;
const int N = 1e6+5;
const ll mod = 1e9+7;
const ll INF = 1e18;
struct edge{
    int v,w,next;
}e[N];
int head[N];
int cnt=0;
void add(int u,int v)//建立一棵树
{
    e[cnt].v=v;
    e[cnt].w=1;
    e[cnt].next=head[u];
    head[u]=cnt++;
}
int cnt1[N],cnt2[N];//标记数组
void dfs(int u,int t,int p[])//深搜加标记
{
    if(p[u])
        return ;
    p[u]=t;
    for(int i=head[u];~i;i=e[i].next)
    {
        dfs(e[i].v,t+1,p);
    }
}
int main()
{

    int n;
    while(scanf("%d",&n)!=EOF)
    {
        cnt=0;
        memset(head,-1,sizeof(head));
        for(int i=1;i<n;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            add(a,b);
            add(b,a);
        }
        dfs(1,1,cnt1);
        dfs(n,1,cnt2);
        int ans=0;
        for(int i=1;i<=n;i++)//看看哪个先走到这个节点
        {
            if(cnt1[i]<=cnt2[i])
                ans++;
            else
                ans--;

        }
        if(ans>0)
            printf("Fennec\n");
        else
            printf("Snuke\n");
}


    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值