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,   v 1,   v 2,...,   v N, and   N-1 edges, connecting   v 1  with   v 2,   v 2  with   v 3,...,   v N-1  with   v N.  
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.   i th digit describes the color of vertex   v i: 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

转载于:https://www.cnblogs.com/Skyxj/p/3205351.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值