• [图论][BFS]Fennec VS. Snuke


    题目描述

    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.

    思路:思路很容易想,关键是我忘了怎么用vector来构建邻接表了!!

    AC代码:

    #include <iostream>
    #include<cstdio>
    #include<vector>
    #include<queue>
    #include<cstring>
    using namespace std;
    
    vector<int> edge[100005];//相当于二维数组int edge[100005][可变长];
    queue<int> q;
    int vis[100005];
    int step[100005];
    int pre[100005];
    int flag[100005];
    int n;
    int x=1,y=1;
    
    int bfs1(){//目的是找到从1到n的那条路
       vis[1]=1; step[1]=0; pre[1]=0; q.push(1);
       while(!q.empty()){
         int head=q.front(); q.pop();
         if(head==n) return step[head];
         for(int i=0;i<(int)edge[head].size();i++){
            int nxt=edge[head][i];
            if(vis[nxt]) continue;
            step[nxt]=step[head]+1;
            vis[nxt]=1;
            pre[nxt]=head;
            q.push(nxt);
         }
       }
       return -1;
    }
    
    void bfs2(){//目的是求出在双方最佳策略下,black和white的数量
       while(!q.empty()) q.pop();
       memset(vis,0,sizeof(vis));
       vis[1]=1; q.push(1);
       while(!q.empty()){
         int head=q.front(); q.pop();
         for(int i=0;i<(int)edge[head].size();i++){
            int nxt=edge[head][i];
            if(vis[nxt]||flag[nxt]==2||nxt==n) continue;
            vis[nxt]=1; x++;
            q.push(nxt);
         }
       }
    }
    
    int main()
    {
        scanf("%d",&n);
        for(int i=1;i<=n-1;i++){//邻接表存图
            int a,b;
            scanf("%d%d",&a,&b);
            edge[a].push_back(b);
            edge[b].push_back(a);
        }
        int tmp=bfs1(); tmp--,tmp/=2;//tmp表示在1到n的线路上有几个white
        int now=pre[n]; int cnt=0;
        while(pre[now]!=0){//对线路上的点进行标记——flag[i]=1表示该点black,flag[i]=2表示该点white
            flag[now]=2; cnt++;
            if(cnt>tmp) flag[now]=1;
            now=pre[now];
        }
        bfs2();
        y=n-x;
        if(x<=y) printf("Snuke
    ");
        else printf("Fennec
    ");
        return 0;
    }
    转载请注明出处:https://www.cnblogs.com/lllxq/
  • 相关阅读:
    小组互评以及自评
    构建之法的第十、十一、十二章读书笔记
    调研------典型用户及场景
    “吃神么,买神么”的第二个Sprint计划(总结)
    自动化单元测试工具 EvoSuite 的简单使用 【转载】
    2018(秋)软工作业 6:团队项目-软件设计
    2018(秋)软工作业 5:结对项目之词频统计——增强功能
    2018(秋)软工作业 4:结对项目之词频统计——基本功能
    2018(秋)软工作业 3:个人编程练习
    软件工程教学计划(2018-秋季)
  • 原文地址:https://www.cnblogs.com/lllxq/p/9280959.html
Copyright © 2020-2023  润新知