II play with GG

时间限制:C/C++ 1秒,其他语言2秒

空间限制:C/C++ 262144K,其他语言524288K

64bit IO Format: %lld

题目描述

IG won the S championship and many people are excited, ii and gg are no exception. After watching the game, the two of them also want to play a game.

There is now an infinite chessboard with only one piece. Initially the pieces are placed at the (x, y) position (the board coordinates are counted from 0). They took turns to move the pieces. ii moves first.

There are three ways to move

1. Move the piece to the left by one space (from (x, y) to (x-1, y)).

2. Move the piece down one space (from (x, y) to (x, y-1)).

3. Move the piece to the lower left by one space (from (x, y) to (x-1, y-1)).

It should be noted that the pieces cannot be removed from the board.

The first  person who can not operate is judged negative (in other words, the first person who moves the piece to (0,0) wins).

Now give the initial coordinates x, y of the piece. Under the premise that both take the optimal strategy, please output the name of the winner (ii or gg).

输入描述:

The input contains only one line. Enter two integers x  y to represent the initial coordinates of the piece. (0<=x, y<=1000)。

输出描述:

the winner's name (ii or gg)。

示例1

输入

复制

1 1

输出

复制

ii

示例2

输入

复制

124 654

输出

复制

gg

题意给出一点位置,ii先动,每次可以对该点进行三种操作,先把点移至(0,0)点就为胜利者。

这是经典的SG函数解决博弈论。通过观察可以处理边界:

010101010

1

0

1

大于零代表的是ii会赢。第(i,j)坐标的值就是mex((i-1,j-1),(i,j-1),(j,i-1)),mex代表的是这三个数中没有出现的最小的数(比如mex(1,2,3)=0,mex(0,2,3)=1,mex(0,1,2)=3.

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1100;
int n,m;
bool vis[maxn][maxn];
int dir[3][2]={-1,-1,-1,0,0,-1};
int si,sj;
bool cmp(int x,int y)
{
if(x<y) return 1;
else return 0;
}
void init()
{
vis[0][0]=0;
for(int i=1;i<=1005;i++)
{
if(i&1) vis[i][0]=1,vis[0][i]=1;
else vis[i][0]=0,vis[0][i]=0;
}

for(int i=1;i<=1005;i++)
{
for(int j=1;j<=1005;j++)
{
int a[3];
for(int k=0;k<3;k++)
{
int xx=i+dir[k][0];
int yy=j+dir[k][1];
if(0<=xx&&0<=yy)
a[k]=vis[xx][yy];
//printf("Xx:%d yy:%d vis:%d\n",xx,yy,vis[xx][yy]);
}
int ans=0;
sort(a,a+3,cmp);
for(int k=0;k<3;k++) if(a[k]==ans) ans++;
vis[i][j]=ans;
//printf("ans:%d\n",ans);
}
}
}
int main()
{
init();
cin>>si>>sj;
if(vis[si][sj]==1) cout<<"ii";
else cout<<"gg";
return 0;
}