Problem Description
Bo is a “Life Winner”.He likes playing chessboard games with his girlfriend G.
The size of the chessboard is N×M.The top left corner is numbered(1,1) and the lower right corner is numberd (N,M).
For each game,Bo and G take turns moving a chesspiece(Bo first).At first,the chesspiece is located at (1,1).And the winner is the person who first moves the chesspiece to (N,M).At one point,if the chess can’t be moved and it isn’t located at (N,M),they end in a draw.
In general,the chesspiece can only be moved right or down.Formally,suppose it is located at (x,y),it can be moved to the next point (x′,y′) only if x′≥x and y′≥y.Also it can’t be moved to the outside of chessboard.
Besides,There are four kinds of chess(They have movement rules respectively).
1.king.
2.rook(castle).
3.knight.
4.queen.
(The movement rule is as same as the chess.)
For each type of chess,you should find out that who will win the game if they both play in an optimal strategy.
Print the winner’s name(“B” or “G”) or “D” if nobody wins the game.
Input
In the first line,there is a number T as a case number.
In the next T lines,there are three numbers type,N and M.
“type” means the kind of the chess.
T≤1000,2≤N,M≤1000,1≤type≤4
Output
For each question,print the answer.
Sample Input
4
1 5 5
2 5 5
3 5 5
4 5 5
Sample Output
G
G
D
B
问给一种棋子,两人轮流走,最后到N,M的赢。
1、王(King):横、竖、斜都可以走,每次限走一格
2、车(Rook):横、竖均可走,不能斜走,格数不受限制,除王车易位的情况下,平时不能越子
3、马(Knight):每步棋先横走或竖走一格,再斜走一格(或者横两格竖一格,竖两格横一格),可以越子
4、后(Queen):横、竖、斜都可以走,格数不受限制,但不能越子
1.王。。SG暴力扫一下。
2.车。。n,m任意选,nim博弈
3.马。。因为我们只考虑必胜必败态,其他的可能赢或输的点,我总是不走过去,会使得这个是平局。。手动模拟一下一个表。观察下规律后,然后也是暴力预处理一下。
4.后。。和车一样考虑,就是可以拿任意堆任意个,或者拿两堆同时拿任意个,是一个标准的威佐夫博弈;
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
bool SG[1005][1005];
int SG3[1005][1005];
int a,n,m;
bool visit[1005];
void getSG1(){
memset(SG,0,sizeof(SG));
for(int i=1;i<=1001;i++){
for(int j=1;j<=1001;j++){
memset(visit,0,sizeof(visit));
if(i-1>0) visit[SG[i-1][j]]=true;
if(j-1>0) visit[SG[i][j-1]]=true;
if(i-1>0&&j-1>0)visit[SG[i-1][j-1]]=true;
if(i-1>0&&j+1<=m) visit[SG[i-1][j+1]]=true;
for(int k=0;;k++){
if(visit[k]==0){
SG[i][j]=k;
break;
}
}
}
}
}
void getSG3(){
memset(SG3,-1,sizeof(SG3));
SG3[1][1]=0;
for(int i=4;i<=1001;i=i+3){
SG3[i][i]=0;
SG3[i-1][i-2]=1;
SG3[i-2][i-1]=1;
}
}
int main(){
int t;
scanf("%d",&t);
getSG1();
getSG3();
while(t--){
scanf("%d %d %d",&a,&n,&m);
if(n>m) swap(n,m);
if(a==1){
if(SG[n][m]) printf("B\n");
else printf("G\n");
}
else if(a==2){
if((n^m)) printf("B\n");
else printf("G\n");
}
else if(a==3){
if(SG3[n][m]==1) printf("B\n");
else if(SG3[n][m]==-1)printf("D\n");
else printf("G\n");
}
else if(a==4){
n--;
m--;
if(n<m) swap(n,m);
int k=n-m;
n=(int)(k*(1+sqrt(5))/2.0);
if(n==m) printf("G\n");
else printf("B\n");
}
}
return 0;
}