Marbles(博弈SG函数)

Marbles

Gym - 101908B

Using marbles as a currency didn‘t go so well in Cubic?nia. In an attempt to make it up to his friends after stealing their marbles, the Emperor decided to invite them to a game night in his palace.

Of course, the game uses marbles, since the Emperor needs to find some use for so many of them. NN marbles are scattered in a board whose lines are numbered from 00through LL and the columns numbered from 00 through CC. Players alternate turns. In his turn, a player must choose one of the marbles and move it. The first player to move a marble to position (0,0)(0,0) is the winner. The movements are limited so the game could be more interesting; otherwise, the first player could just move a marble to position (0,0)(0,0) and win. A movement consists in choosing an integer uu greater than 00and a ball, whose location is denoted by (l,c)(l,c), and move it to one of the following positions, as long as it is inside the board:

  • (l?u,c)(l?u,c) or;
  • (l,c?u)(l,c?u) or;
  • (l?u,c?u)(l?u,c?u).

Note that more than one marble can occupy the same position on the board.

As the Emperor doesn‘t like to lose, you should help him determine which games he should attend. Also, as expected, the Emperor always take the first turn when playing. Assuming both players act optimally, you are given the initial distribution of the marbles, and should find if it is possible for the Emperor to win if he chooses to play.

Input

The first line contains an integer NN (1≤N≤10001≤N≤1000). Each of the following NN rows contains two integers lili and cici indicating on which row and column the ii-th marble is in (1≤li,ci≤1001≤li,ci≤100).

Output

Your program should print a single line containing the character Y if it is possible for the Emperor to win the game or N otherwise.

Examples

Input

21 32 3

Output

Y

Input

11 2

Output

N题解:  首先,我们需要明白一个事实,在求SG函数时,一个点的后继中如果有必败态就把必败态跳过,就是说求一个点的SG函数考虑的是他所有的非必败态的后继。所以对于本题来说,一个点的后继状态中如果有坐标轴或者对角线,因为坐标轴或者对角线对于当前的先手来说是必败的,所以他肯定不会将点移动到坐标轴或者对角线上,所以后继为坐标轴或者对角线直接跳过,不会参与求当前点的SG值中。 坤神说 一个数的SG值一定是它的后继中的非必败态,或者是这个位置不知道是否为胜或败的状态。nim博弈中,SG值为本身是因为假设一堆石子有5个,那么我们一定要一口气拿完5个才能胜,否则达到1~4的状态都是必败的。

个人理解:一堆石头要是必胜的话,一定要全拿走吧,要不你拿一部分后手把剩下的全拿走就后手赢了。而NIM博弈的SG打表的话应该是

for(int i=0;i<x;i++)
{
  vis[dfs(i)]=1;
}

这么写吧,就是把x之前的状态全标记一个遍,所以对于0-x-1的每一个状态都是以该状态作为先手求SG值,所以0-x-1每个的SG值都是他必胜的状态,所以对于x来说,我拿了一部分,到达了某个非零数,而那个数一定是后手必胜的,所以对于当前这一堆来说,先手唯一的必胜态就是把这一堆石头全拿走,即SG[x]=x.

所以本题还需特判一下是否一开始有点在坐标轴或者对角线上,然后求每个点的SG值时,把坐标轴和对角线的后继状态跳过,因为是必败态,而求SG值时不能考虑必败态。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #define inf 999
 6 using namespace std;
 7 const int maxn=110;
 8 int sg[maxn][maxn];
 9 int get_sg(int x,int y)
10 {
11
12     if(sg[x][y]!=0)
13         return sg[x][y];
14     int vis[1100];
15     memset(vis,0,sizeof(vis));
16     for(int i=1;i<x;i++)
17     {
18         vis[get_sg(i,y)]=1;
19     }
20     for(int i=1;i<y;i++)
21     {
22         vis[get_sg(x,i)]=1;
23     }
24     for(int i=1;i<min(x,y);i++)
25     {
26         vis[get_sg(x-i,y-i)]=1;
27     }
28     for(int i=0;i<1100;i++)
29     {
30         if(!vis[i])
31         {
32             return sg[x][y]=i;
33         }
34     }
35 }
36 int main()
37 {
38     memset(sg,0,sizeof(sg));
39     sg[0][0]=0;
40     for(int i=0;i<maxn;i++)
41         sg[0][i]=inf;
42     for(int i=0;i<maxn;i++)
43         sg[i][0]=inf;
44     for(int i=1;i<maxn;i++)
45     {
46         for(int j=1;j<maxn;j++)
47         {
48             if(i==j)
49                 sg[i][j]=inf;
50         }
51     }
52     get_sg(105,104);
53     int x,y;
54     int n;
55     int flag=0;
56     int sum=0;
57     scanf("%d",&n);
58     while(n--)
59     {
60         scanf("%d%d",&x,&y);
61         if(x==y||x==0||y==0)
62         {
63             flag=1;
64         }
65         sum^=sg[x][y];
66     }
67     if(flag)
68     {
69         puts("Y");
70     }
71     else
72     {
73         if(sum)
74             puts("Y");
75         else
76             puts("N");
77     }
78
79 }

原文地址:https://www.cnblogs.com/1013star/p/10088807.html

时间: 2024-10-05 11:44:32

Marbles(博弈SG函数)的相关文章

UVA 10561 - Treblecross(博弈SG函数)

UVA 10561 - Treblecross 题目链接 题意:给定一个串,上面有'X'和'.',可以在'.'的位置放X,谁先放出3个'X'就赢了,求先手必胜的策略 思路:SG函数,每个串要是上面有一个X,周围的4个位置就是禁区了(放下去必败),所以可以以X分为几个子游戏去求SG函数的异或和进行判断,至于求策略,就是枚举每个位置就可以了 代码: #include <stdio.h> #include <string.h> #include <algorithm> usi

hdu 3032(博弈sg函数)

题意:与原来基本的尼姆博弈不同的是,可以将一堆石子分成两堆石子也算一步操作,其它的都是一样的. 分析:由于石子的堆数和每一堆石子的数量都很大,所以肯定不能用搜索去求sg函数,现在我们只能通过找规律的办法求得sg的规律. 通过打表找规律可以得到如下规律:if(x%4==0) sg[x]=x-1; if(x%4==1||x%4==2) sg[x]=x; if(x%4==3) sg[x] = x+1. 打表代码: #include<iostream> #include<cstdio> #

UVA 11534 - Say Goodbye to Tic-Tac-Toe(博弈sg函数)

UVA 11534 - Say Goodbye to Tic-Tac-Toe 题目链接 题意:给定一个序列,轮流放XO,要求不能有连续的XX或OO,最后一个放的人赢,问谁赢 思路:sg函数,每一段...看成一个子游戏,利用记忆化求sg值,记忆化的状态要记录下左边和右边是X还是O即可 代码: #include <stdio.h> #include <string.h> const int N = 105; int t, sg[3][3][N]; char str[N]; int ge

HDOJ 5724 博弈SG函数

链接: http://blog.csdn.net/tc_to_top/article/details/51958964 题意: n行20列的棋盘,对于每行,如果当前棋子右边没棋子,那可以直接放到右边,如果有就跳过放到其后面的第一个空位子,A先操作,最后谁无法操作则输,给定每行棋子状态,问先手是否必胜 题解: 组合博弈问题,直接sg函数,因为列只有20,可以状压搞,枚举每个状态,找到该状态下可行的操作然后标记 代码: 31 int sg[1 << 21]; 32 int vis[21]; 33

(转)博弈 SG函数

此文为以下博客做的摘要: https://blog.csdn.net/strangedbly/article/details/51137432 ---------------------------------------------------------------------------------------- 1.定义P-position和N-positon P表示Previous,N表示Next. 即上一个移动的人有必胜策略的局面是P-position,"先手必败"或&qu

[hdu-5795]A Simple Nim 博弈 尼姆博弈 SG函数打表找规律

[题目]题目链接 Two players take turns picking candies from n heaps,the player who picks the last one will win the game.On each turn they can pick any number of candies which come from the same heap(picking no candy is not allowed).To make the game more int

Light OJ 1296 - Again Stone Game (博弈sg函数递推)

F - Again Stone Game Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Description Alice and Bob are playing a stone game. Initially there are n piles of stones and each pile contains some stone. Alice stars the

Light OJ 1199 - Partitioning Game (博弈sg函数)

D - Partitioning Game Time Limit:4000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Description Alice and Bob are playing a strange game. The rules of the game are: Initially there are n piles. A pile is formed by some cell

LightOJ 1315 - Game of Hyper Knights(博弈sg函数)

G - Game of Hyper Knights Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Description A Hyper Knight is like a chess knight except it has some special moves that a regular knight cannot do. Alice and Bob are p