zoukankan      html  css  js  c++  java
  • SGU328a coloring game博弈sg函数

    C - A Coloring Game
    Time Limit:500MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

    Description

    Two players play a graph coloring game. They make moves in turn, first player moves first. Initially they take some undirected graph. At each move, a player can color an uncolored vertex with either white or black color (each player can use any color, possibly different at different turns). It's not allowed to color two adjacent vertices with the same color. A player that can't move loses. 
    After playing this game for some time, they decided to study it. For a start, they've decided to study very simple kind of graph — a chain. A chain consists of N vertices, v1, v2,..., vN, and N-1 edges, connecting v1 with v2, v2 with v3,..., vN-1 with vN. 
    Given a position in this game, and assuming both players play optimally, who will win?

    Input

    The first line of input contains the integer N, . 
    The second line of input describes the current position. It contains N digits without spaces. ith digit describes the color of vertex vi: 0 — uncolored, 1 — black, 2 — white. No two vertices of the same color are adjacent.

    Output

    On the only line of output, print " 
    FIRST
    " (without quotes) if the player moving first in that position wins the game, and " 
    SECOND
    " (without quotes) otherwise.

    Sample Input

     1 #include <algorithm>
     2 #include <map>
     3 #include <cstdio>
     4 #include <cstdlib>
     5 #include <vector>
     6 #include <iostream>
     7 #include <cmath>
     8 #include <cstring>
     9 #include <string>
    10 
    11 using namespace std;
    12 int n;
    13 int sg(int l,int s,int e)//0的个数,s首端点,e末端点 
    14 {
    15     if(s==0 ||e==0)//端点有一边为0一边不为零的返回0的个数 
    16     {
    17         return l;
    18     }
    19     else
    20         return s==e;//端点相同的先手胜返回1,否则后手胜返回0 
    21 }
    22 string g;
    23 int main()
    24 {
    25     cin>>n>>g;
    26     bool flag=true;
    27     for(int j=0; j<g.size(); j++)
    28         if(g[j]!='0')    
    29             flag=false;
    30     if(flag)//全是0的情况,0先手胜,00后手胜,000先手胜,依次循环 
    31     {
    32         if(g.size()%2)
    33         {
    34             cout<<"FIRST"<<endl;
    35         }
    36         else
    37             cout<<"SECOND"<<endl;
    38         return 0;
    39     }
    40     int b=0;
    41     for(int j=0; j<g.size(); j++)
    42     {
    43         if(g[j]=='0')
    44         {
    45             int st=j;
    46             int s,e;
    47             if(j==0)
    48                 s=0;
    49             else
    50                 s=g[j-1]-'0';
    51             for(; j<g.size(); j++)
    52             {
    53                 if(j+1==g.size())
    54                 {
    55                     e=0;
    56                     b^=sg(j-st+1,s,e);
    57                 //    cout<<b<<endl;
    58                     break;
    59                 }
    60                 else if(g[j+1]!='0')
    61                 {
    62                     e=g[j+1]-'0';
    63                     b^=sg(j-st+1,s,e);
    64                 //    cout<<b<<endl;
    65                     break;
    66                 }
    67             }
    68         }
    69     }
    70     if(b!=0)
    71         cout<<"FIRST"<<endl;
    72     else
    73         cout<<"SECOND"<<endl;
    74 }
    View Code
    sample input
    sample output
    5
    00100
    
    SECOND
    
    sample input
    sample output
    4
    1020
    
    FIRST
    
  • 相关阅读:
    linux---网络管理
    linux---Vim使用方法大全
    virtualenv离线创建虚拟环境
    python迭代器生成器进阶
    python函数对象
    Django等框架模板渲染中使用Vue控制数据
    django渲染模板与vue的语法冲突解决
    Linux网络抓包
    WSGI Server(uwsgi原理)
    python日志logging模块
  • 原文地址:https://www.cnblogs.com/Skyxj/p/3205351.html
Copyright © 2011-2022 走看看