• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
FightingForWorldFinal
博客园    首页    新随笔    联系   管理    订阅  订阅

SGU328a coloring game博弈sg函数

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

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
posted @ 2013-07-22 15:02  Sky-J  阅读(340)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3