HDU 5754 Life Winner Bo (博弈)
Life Winner Bo
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5754
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
Source
2016 Multi-University Training Contest 3
##题意:
给出N*M的棋盘和棋子的类型,两人以最优策略轮流走.
棋子初始时在(1,1), 先走到(N,M)者获胜.(只能往右下角方向移动)
问对于给定的情形先手胜、败、平.
##题解:
对于每个棋盘只有一个棋子,那么直接分析每个位置的必胜/必败态即可,对于最大的棋盘(1000*1000)打一次表即可.
(1000,1000)为必败态,从右下角往左上角逐个分析每点的状态:
可以到必败态的一定是必胜态; 只能到必胜态的一定是必败态.
1. king:可走到周围的8个格子.
当前位置(i,j)的状态由(i+1,j) (i,j+1) (i+1,j+1)确定.
2. rook:可以横着竖着走任意个格子.
简单推理可得只有i==j时必败,其余必胜.
3. knight:走"L"型路线.
当前位置(i,j)的状态由(i+2,j+1) (i+1,j+2)确定.
不同的是,题目要求不能移动且没有到右下角时为平手,那么只有knight可能出现平手,即走到边界时不能再移动.
对于当前位置,如果能走到必败态,那么一定是必胜态;若不能走到必败态,那么如果能走到平局位置,则一定是平局;
4. queen:横竖斜走任意格子.
对于每个必败态,将可到达的横竖斜格子全部置成必胜态.
(下面代码之所以跑了两遍,是因为第一次我把没有置1的位置全部置0,但是没有用这个0的位置去更新其他点,导致WA).
5. 这里提供样例(5*5)的四种棋子在各个位置的必胜必败态(1必胜 0必败 2平手), 供大家调错.
king:

rook:

knight:

queen:

##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 1010
#define mod 100000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;
int king[maxn][maxn];
int rook[maxn][maxn];
int knight[maxn][maxn];
int queen[maxn][maxn];
bool is_ok(int x, int y) {
return x>=1 && y>=1 && x<=1000 && y<=1000;
}
int main(int argc, char const *argv[])
{
//IN;
memset(king, -1, sizeof(king));
memset(rook, -1, sizeof(rook));
memset(knight, -1, sizeof(knight));
memset(queen, -1, sizeof(queen));
king[1000][1000] = 0;
rook[1000][1000] = 0;
knight[1000][1000] = 0;
queen[1000][1000] = 0;
for(int i=1000; i>=1; i--) {
for(int j=1000; j>=1; j--) {
if(king[i][j] != -1) continue;
if(is_ok(i+1,j) && !king[i+1][j]) {
king[i][j] = 1; continue;
}
if(is_ok(i,j+1) && !king[i][j+1]) {
king[i][j] = 1; continue;
}
if(is_ok(i+1,j+1) && !king[i+1][j+1]) {
king[i][j] = 1; continue;
}
king[i][j] = 0;
}
}
for(int i=1000; i>=1; i--) {
for(int j=1000; j>=1; j--) {
if(i==j) rook[i][j] = 0;
else rook[i][j] = 1;
}
}
for(int i=1000; i>=1; i--) {
for(int j=1000; j>=1; j--) {
if(knight[i][j] != -1) continue;
if(!is_ok(i+2,j+1) && !is_ok(i+1,j+2)) {
knight[i][j] = 2;
continue;
}
if(is_ok(i+2,j+1) && !knight[i+2][j+1]) {
knight[i][j] = 1; continue;
}
if(is_ok(i+1,j+2) && !knight[i+1][j+2]) {
knight[i][j] = 1; continue;
}
if(is_ok(i+2,j+1) && knight[i+2][j+1]==2) {
knight[i][j] = 2; continue;
}
if(is_ok(i+1,j+2) && knight[i+1][j+2]==2) {
knight[i][j] = 2; continue;
}
knight[i][j] = 0;
}
}
for(int i=1000; i>=1; i--) {
for(int j=1000; j>=1; j--) {
if(queen[i][j] != -1) {
if(queen[i][j] == 0) {
for(int k=1; k<=1000; k++) {
if(is_ok(i-k,j-k)) queen[i-k][j-k] = 1;
else break;
}
for(int k=1; k<=1000; k++) {
if(is_ok(i,j-k)) queen[i][j-k] = 1;
else break;
}
for(int k=1; k<=1000; k++) {
if(is_ok(i-k,j)) queen[i-k][j] = 1;
else break;
}
}
continue;
}
queen[i][j] = 0;
}
}
for(int i=1000; i>=1; i--) {
for(int j=1000; j>=1; j--) {
if(queen[i][j] != -1) {
if(queen[i][j] == 0) {
for(int k=1; k<=1000; k++) {
if(is_ok(i-k,j-k)) queen[i-k][j-k] = 1;
else break;
}
for(int k=1; k<=1000; k++) {
if(is_ok(i,j-k)) queen[i][j-k] = 1;
else break;
}
for(int k=1; k<=1000; k++) {
if(is_ok(i-k,j)) queen[i-k][j] = 1;
else break;
}
}
continue;
}
}
}
int t; cin >> t;
while(t--)
{
int tp,n,m; scanf("%d %d %d", &tp,&n,&m);
if(tp == 1) {
int ans = king[1000-n+1][1000-m+1];
if(ans) printf("B");
else printf("G");
}
if(tp == 2) {
int ans = rook[1000-n+1][1000-m+1];
if(ans) printf("B");
else printf("G");
}
if(tp == 3) {
int ans = knight[1000-n+1][1000-m+1];
if(ans == 1) printf("B");
else if(ans == 2) printf("D");
else printf("G");
}
if(tp == 4) {
int ans = queen[1000-n+1][1000-m+1];
if(ans) printf("B");
else printf("G");
}
printf("\n");
}
return 0;
}
HDU 5754 Life Winner Bo (博弈)的更多相关文章
- HDU 5754 Life Winner Bo 组合博弈
Life Winner Bo Problem Description Bo is a "Life Winner".He likes playing chessboard gam ...
- HDU 5754 Life Winner Bo (找规律and博弈)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5754 给你四种棋子,棋子一开始在(1,1)点,两个人B和G轮流按每种棋子的规则挪动棋子,棋子只能往右下 ...
- HDU 5754 Life Winner Bo(各类博弈大杂合)
http://acm.hdu.edu.cn/showproblem.php?pid=5754 题意: 给一个国际象棋的棋盘,起点为(1,1),终点为(n,m),现在每个棋子只能往右下方走,并且有4种不 ...
- HDU 5754 Life Winner Bo (各种博弈) 2016杭电多校联合第三场
题目:传送门 题意:一个国际象棋棋盘,有四种棋子,从(n,m)走到(1,1),走到(1,1)的人赢,先手赢输出B,后手赢输出G,平局输出D. 题解:先把从(n,m)走到(1,1)看做是从(1,1)走到 ...
- 【博弈论】HDU 5754 Life Winner Bo
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5754 题目大意: 4种棋子,象棋中的 1王,2车,3马,4后,选其一,B和G轮流走,不能往左上走,一 ...
- HDU 5754 Life Winner Bo
四种棋子实质上都是一样的思路: 如果某位置的棋子,它下一步可以走到的位置中 能找到有后手胜的位置,那么该位置先手必胜. 如果某位置的棋子,它下一步可以走到的位置中 全是先手胜,那么该位置后手必胜. 其 ...
- hdu 5754 Life Winner Bo 博弈论
对于king:我是套了一个表. 如果起点是P的话,则是后手赢,否则前手赢. 车:也是画图推出来的. 马:也是推出来的,情况如图咯. 对于后:比赛时竟然推错了.QAQ最后看了题解:是个威佐夫博奕.(2, ...
- hdu-5754 Life Winner Bo(博弈)
题目链接: Life Winner Bo Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/ ...
- hdu_5754_Life Winner Bo(博弈)
题目链接:hdu_5754_Life Winner Bo 题意: 一个棋盘,有国王,车,马,皇后四种棋子,bo先手,都最优策略,问你赢的人,如果双方都不能赢就输出D 题解: 全部都可以直接推公式, 这 ...
随机推荐
- Android相对布局(RelativeLayout)
Android相对布局(RelativeLayout) 备注:这里的视图和元素是等同的概念. RelativeLayout是一个允许子视图相对于其他兄弟视图或是父视图显示的视图组(通过ID指定).每个 ...
- bzoj2436
不难发现两边的活动是交替进行的,我们可以dp 先对时间离散化,设f[i,j]到时间i一个会场选j个活动,另一个会场最多有多少活动,那么f[i,j]=max(f[k,j]+s[k,i],f[k,j-s[ ...
- codeforces 334A - Candy Bags
忘了是偶数了,在纸上画奇数画了半天... #include<cstdio> #include<cstring> #include<cstdlib> #include ...
- Java [Leetcode 165]Compare Version Numbers
题目描述: Compare two version numbers version1 and version2.If version1 > version2 return 1, if versi ...
- eclipse的使用
类似于数据库系统的三层目录结构,一般而言IDE在逻辑上都有三层目录结构:工作空间(或解决方案) -> 工程 -> 文件. 当然,如果构建java project,那么目录结构是可以更深,因 ...
- 【转】Android SwitchButton(滑动开关)
原文网址:http://blog.csdn.net/wangjinyu501/article/details/27961303 版本:1.0 日期:2014.5.17 2014.6.1 版权:© 20 ...
- css中将div定位居中
一直,我是认为定一个width,然后写一句margin:0 auto,就可以,但是有时也会不管用. 例如当我要定一个宽度为700的div,用相对定位定在中间.任你怎么拉伸都是居中.而position: ...
- Java Error和Exception区别
Error和Exception都继承自Throwable: 二者不同之处: Exception: 1.可以是可被控制(checked)或者不可控制(unchecked): 2.表示一个由程序员导致的错 ...
- Android-判断当前网络是否可用
1.声明权限 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 2. ...
- equals(),hashcode()方法详解
Java中的equals方法和hashCode方法是Object中的,所以每个对象都是有这两个方法的,有时候我们需要实现特定需求,可能要重写这两个方法,今天就来介绍一些这两个方法的作用. equals ...