Life Winner Bo
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 924 Accepted Submission(s): 342
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.
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
4 1 5 5 2 5 5 3 5 5 4 5 5
G G D B
很简单,都不想说话!为什么用if判断过不了!!case就能过
!!!!
就是把皇后赢能走的位置和马能走的位置打个表。。
我们依次分析每一种棋子。
①王。
首先注意一个3*3的棋盘,开始在(1,1),问走到(3,3)谁有必胜策略。
穷举所有情况,容易发现这是后手赢。
对于NNN和MMM更大的情况,我们把横坐标每隔3、纵坐标每隔3的点都画出来,这些点都是符合后手胜的。
(因为无论先手怎么移动,后手都能重新移动到这些格子,直到到了终点)
如果初始点不在这些点上,就必然是先手胜。因为先手可以立刻移动到上述的点。
②车。
注意到,如果目前的位置距离终点的xxx和yyy坐标差相等,一定是后手胜。
(因为先手只能向下或者向右走一段路;无论他往哪里走,后手往另一维走相同的步数,依然保持这一样一种状态。)
反之,先手必然能走到一处相等的位置,转化为上述问题,所以一定是先手胜。
③马。
同样还是画图可以得到规律。
在大多数情况下都是平局。在模3域下,某些地方会存在先后手赢。
④皇后。
画画图后,我们可以将问题转化为:
“有两堆石子,每次可以在一堆里取任意(非空)颗(相当于是车的走法),或者在两堆里取相同(非空)颗(相当于是象的走法),取到最后一颗石子的人获胜,问先后手谁有必胜策略。”
此题中N≤1000N\leq 1000N≤1000,可以直接用DP的方法解决。
设f[x][y]为横坐标距离终点x步,纵坐标距离终点y步时,必胜的是先手还是后手。
直接转移的话,可以枚举先手的下一步决策进行转移,这样是O(N3)O(N^3)O(N3)的。
注意到转移只是一行、一列或者斜着一列,这些都可以通过前缀和,做到最终O(N2)O(N^2)O(N2)。
其实对于更大的NNN也是可以做的。
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int dp[2010][2010],jump[1010][1010];
void sg()
{
memset(dp,0,sizeof(dp));
for(int i=1;i<=1000;i++)
for(int j=1;j<=1000;j++)
if(dp[i][j]==0)
{
for(int k=1;k<=1000;k++)
{
dp[i+k][j+k]=1;
dp[i][j+k]=1;
dp[i+k][j]=1;
}
}
}
void temp()
{
memset(jump,0,sizeof(jump));
for(int i=1000;i>=3;i=i-3)
{
jump[i][i]=1;
jump[i-2][i-1]=2;
jump[i-1][i-2]=2;
}
}
int main()
{
sg();
temp();
int t;
scanf("%d",&t);
while(t--)
{
int n,m,k;
scanf("%d%d%d",&k,&n,&m);
switch(k)
{
case 1:
if(m&1&&n&1)
printf("G\n");
else
printf("B\n");
break;
case 2://军
if(n==m)
printf("G\n");
else
printf("B\n");
break;
case 3:
if(jump[n][m]==0)
printf("D\n");
else if(jump[n][m]==2)
printf("B\n");
else if(jump[n][m]==1)
printf("G\n");
break;
case 4:
if(dp[n][m])
printf("B\n");
else
printf("G\n");
break;
}
}
return 0;
}