您的位置:首页 > Web前端

HDU 5754 Life Winner Bo(找规律 + 威佐夫博弈)--2016 Multi-University Training Contest 3

2016-07-26 19:20 645 查看
传送门

Bo is a "Life Winner".He likes playing chessboard games with his girlfriend G.

The size of the chessboard is N×MN\times M.The top left corner is numbered(1,1)(1,1) and the lower right corner is numberd (N,M)(N,M).

For each game,Bo and G take turns moving a chesspiece(Bo first).At first,the chesspiece is located at (1,1)(1,1).And the winner is the person who first moves the chesspiece to (N,M)(N,M).At one point,if the chess can’t be moved and it isn’t located at (N,M)(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)(x,y),it can be moved to the next point (x′,y′)(x',y') only if x′≥xx'\geq x and y′≥yy'\geq 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.
[align=left]Input[/align] In the first line,there is a number TT as a case number.

In the next TT lines,there are three numbers type,NN and MM.

"type" means the kind of the chess.

T≤1000,2≤N,M≤1000,1≤type≤4T\leq 1000,2\leq N,M\leq 1000,1\leq type\leq 4
[align=left]Output[/align] For each question,print the answer.
[align=left]Sample Input[/align]
4
1 5 5
2 5 5
3 5 5
4 5 5

[align=left]Sample Output[/align]
G
G
D
B


题目大意:

有4种操作分别是:

1.king.

2.rook(castle).

3.knight.

4.queen.

然后这些操作对应的是国际象棋里面的一些操作(具体操作可以百度)
然后现在我们站在(1,1)点,要去往(N,M)点,按照国际象棋操作的时候只能往右或者往下走,然后有两个人进行按照规则进行操作,谁最先到达(N,M)点谁就胜出。

解题思路:

我们依次分析每一种棋子。
①王。
首先注意一个3*3的棋盘,开始在(1,1),问走到(3,3)谁有必胜策略。
穷举所有情况,容易发现这是后手赢。
对于NN和MM更大的情况,我们把横坐标每隔3、纵坐标每隔3的点都画出来,这些点都是符合后手胜的。
(因为无论先手怎么移动,后手都能重新移动到这些格子,直到到了终点)
如果初始点不在这些点上,就必然是先手胜。因为先手可以立刻移动到上述的点。
②车。
注意到,如果目前的位置距离终点的xx和yy坐标差相等,一定是后手胜。
(因为先手只能向下或者向右走一段路;无论他往哪里走,后手往另一维走相同的步数,依然保持这一样一种状态。)
反之,先手必然能走到一处相等的位置,转化为上述问题,所以一定是先手胜。
③马。
同样还是画图可以得到规律。
在大多数情况下都是平局。在模3域下,某些地方会存在先后手赢。
④皇后。
画画图后,我们可以将问题转化为:
“有两堆石子,每次可以在一堆里取任意(非空)颗(相当于是车的走法),或者在两堆里取相同(非空)颗(相当于是象的走法),取到最后一颗石子的人获胜,问先后手谁有必胜策略。”这就是一个威佐夫博弈,需要注意的是我们应该已经在n–,m–的基础上的威佐夫博弈,因为我们站在(1,1)点。然后根据公式就行了。

My Code:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <cmath>
using namespace std;

int main()
{
int T;
int N,M,type;
cin>>T;
while(T--)
{
scanf("%d%d%d",&type,&N,&M);
if(type==1)
{
if(N>M) swap(N,M);
if(N%2==0||M%2==0) puts("B");
else  puts("G");
}
else if(type==2)
{
if(N!=M) puts("B");
else  puts("G");
}
else if(type==4)
{
if(N>M) swap(N,M);
N--,M--;
int tmp = (int)((1.0*sqrt(5.0)+1)/2*(M-N));
if(tmp == N)
puts("G");
else
puts("B");
}
else if(type==3)
{
if(N>M) swap(N,M);
if(N==M&&N%3==1)           puts("G");
else if((N+1)==M&&M%3==0)  puts("B");
else                       puts("D");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: