Long time no whole problem, not without good title, just lazy too lazy too lazy ... Hurriedly tidy up a few questions to mend your own sins ...
Description
Bo is a ' life Winner '. He likes playing chessboard games with his girlfriend G.
The size of the chessboard is NXM. The top left corner is numbered (all) 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 are located at (.) And the winner is the person who first moves the chesspiece to (n,m). At one point,if the chess can ' t is moved and it isn ' t located at (n,m), they end in a draw.
In General,the Chesspiece can is only is moved right or down. Formally,suppose It is located at (x, y), it can be moved to the next point -Y -) only if X -≥x and Y -≥y. Also it can ' t be moved to the outside of chessboard.
Besides,there is four kinds of chess (they has 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, who would 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.
Inputin The first line,there is a number T as a case number.
In the next T Lines,there is three numbers type,n and M.
"Type" means the kind of the chess.
T≤1000,2≤n,m≤1000,1≤type≤4outputfor each Question,print the answer. Sample INPUT4 1 5 5 2 5 5 3 5 5 4 5 5Sample outputg G D B Test Instructions:N*m's chessboard, a piece to go from the upper left corner (0,0) to the lower right corner, two people take turns moving, who moves to the last step who wins, move the rules as follows, there are four kinds of pieces:1 King Kings Right, down, or right down one pane2, Castle the car to the right, or downward move any lattice3, Knight Horse to 2*3 the other corner of the matrix movement4. Queen Queen move to any position of the same row, column, SlashIdeas:
It is obvious that this is a game topic. He was fortunate to have four pieces, respectively, corresponding to four kinds of way, that is, four games together.
The chessboard as two piles of stone, chess pieces to move to take is two piles of stone, who first take who wins, a heap of stones n-1, a pile of m-1.
Analyze four kinds of games separately
1 King Kings to the right, down, or down to the right to move a grid, simplifying into a pebble the problem is that you can take one (right, down) or one in both piles (right down). Finally take (0,0) that is very obvious, the end result is even, each even can only become odd even, even odd, Kiki, however, these three can be once into even. So there is the answer, who face the situation of even the people who lose, the other side as long as to keep him in the face of the occasional situation can be guaranteed to take away the last stone. This is for n-1 and m-1, for N,m is when n,m are odd when the first person to face the inevitable, or the first person to win.
2, Castle car to the right, or downward move any lattice, simplifying into a stone problem is to remove any one from a heap, simple NIM game can be solved. The same can be understood as the beginning of the guarantee and the end point on a diagonal, must be the initiator win, so the winning strategy is to move to the diagonal-but if the beginning is a square, then the initiator must be defeated.
3, the Knight horse to the 2*3 of the matrix of the other corner of the movement, Knight move the same way as the horse, it is more difficult to deal with, because it has a draw situation (such as a person has been to the right, leading to the opposite road can not go) so that the opponent can not win ... Such a situation seems troublesome, in fact, we can also use the same thinking to analyze the win and the state of defeat. We first distinguish between the outcome of the situation and draw the situation, through drawing, we can find (2,3) and (3,2) Such a situation is certain to win, and think (3,3) This situation can not be achieved, further, we found that this situation is bound to fail, And all the other things smaller than it are all a draw. Further, we find that this is 5,6: we only need to get to the point where we can, before we know (2,3) is the first defeat, and here, if we think (2,3) as the starting point, the (5,6) as the end, The square of its composition is exactly the same as that of the previous one: This shows that this is a situation that can distinguish between winning and losing, starting with (7,7), (10,10) .... All are the initiator will lose, but if we can go to such a defeat situation, we will win-and the rest of the situation, all for the draw. Here we can write the final result: when the end point minus the beginning of the coordinates can be divisible by 3, the initiator will be defeated, for the winning conditions, we only need to determine whether (2,3) and (3,2) can achieve the inevitable situation on it.
4, Queen Queen moved to the same row, column, slash any position, simplifying into a stone problem is can be in a bunch of any, can also be in the two piles to take the same. The classical Witzov game problem, we can solve directly.
Code:
#include <bits/stdc++.h>#include<cstdlib>#include<cstring>#include<cstdio>#include<cmath>#include<iostream>#include<algorithm>#include<string>#include<queue>#include<stack>#include<map>#include<Set>#defineIO Ios::sync_with_stdio (false); \Cin.tie (0); Cout.tie (0); typedefLong LongLL;Const Long LongINF =0x3f3f3f3f;Const Long LongMoD = 1e9+7;Const DoublePI = ACOs (-1.0);Const intMAXN =100000;Const Charweek[7][Ten]= {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};Const Charmonth[ A][Ten]= {"Janurary","February","March","April"," May","June","July", "August","September","October","November","December" };Const intdaym[2][ -] = {{0, to, -, to, -, to, -, to, to, -, to, -, to}, {0, to, in, to, -, to, -, to, to, -, to, -, to}};Const intdir4[4][2] = {{1,0}, {0,1}, {-1,0}, {0, -1}};Const intdir8[8][2] = {{1,0}, {0,1}, {-1,0}, {0, -1}, {1,1}, {-1, -1}, {1, -1}, {-1,1}};using namespacestd;intMain () {IO; intT; CIN>>T; intlogo=1; while(t--) { intt,n,m; CIN>>t>>n>>m; N--; M--; if(t==1) { if(n%2==0&&m%2==0) cout<<"G"<<Endl; Elsecout<<"B"<<Endl; } if(t==2) { if(n==m) cout<<"G"<<Endl; Elsecout<<"B"<<Endl; } if(t==3) { if(n==m&&n%3==0) cout<<"G"<<Endl; Else if(n1==m&& (n1)%3==1) cout<<"B"<<Endl; Else if(n==m-1&&n%3==1) cout<<"B"<<Endl; Elsecout<<"D"<<Endl; } if(t==4) { intak=0; intA=N; intb=m; DoubleK; K= (sqrt (5.0)+1.0)/2.0; if(a>b) {T=A; A=b; b=T; } t=b-A; AK=(int) (t*k); if(a==AK) cout<<"G"<<Endl; Elsecout<<"B"<<Endl; } } return 0;}
HDU-5754 Life Winner Bo (game theory)