hdu 1524 A Chess Game
来源:互联网 发布:java 泛型 类型转换 编辑:程序博客网 时间:2024/05/17 03:23
题目链接:点这里。。
Problem Description
Let's design a new chess game. There are N positions to hold M chesses in this game. Multiple chesses can be located in the same position. The positions are constituted as a topological graph, i.e. there are directed edges connecting some positions, and no cycle exists. Two players you and I move chesses alternately. In each turn the player should move only one chess from the current position to one of its out-positions along an edge. The game does not end, until one of the players cannot move chess any more. If you cannot move any chess in your turn, you lose. Otherwise, if the misfortune falls on me... I will disturb the chesses and play it again.Do you want to challenge me? Just write your program to show your qualification!
Input
Input contains multiple test cases. Each test case starts with a number N (1 <= N <= 1000) in one line. Then the following N lines describe the out-positions of each position. Each line starts with an integer Xi that is the number of out-positions for the position i. Then Xi integers following specify the out-positions. Positions are indexed from 0 to N-1. Then multiple queries follow. Each query occupies only one line. The line starts with a number M (1 <= M <= 10), and then come M integers, which are the initial positions of chesses. A line with number 0 ends the test case.
Output
There is one line for each query, which contains a string "WIN" or "LOSE". "WIN" means that the player taking the first turn can win the game according to a clever strategy; otherwise "LOSE" should be printed.
Sample Input
42 1 201 301 02 0 2041 11 2002 0 12 1 13 0 1 30
Sample Output
WINWINWINLOSEWIN
【题意】
给你一个有向无环图,图中每个点可能放一个,多个,或者0个旗子,两个人相互博弈,每次选择一个旗子,和一条连接这个旗子的路,让他移动一下。最后不能移动的人输。问你是否能先手必胜。
【分析】
显然,我们将每一个旗子都当作一个单独的游戏。这样多局游戏就是单局游戏的nim和。然后我们这里记忆化搜索一下所有的节点的SG值。然后直接求nim和判断是否为0就行了。
【代码】
#include<iostream>#include<cstdio>#include<cstring>#include<string.h>#include<algorithm>#include<vector>#include<cmath>#include<stdlib.h>#include<time.h>#include<stack>#include<set>#include<map>#include<queue>#include<sstream>using namespace std;#define rep0(i,l,r) for(int i = (l);i < (r);i++)#define rep1(i,l,r) for(int i = (l);i <= (r);i++)#define rep_0(i,r,l) for(int i = (r);i > (l);i--)#define rep_1(i,r,l) for(int i = (r);i >= (l);i--)#define MS0(a) memset(a,0,sizeof(a))#define MS_1(a) memset(a,-1,sizeof(a))#define MSinf(a) memset(a,0x3f,sizeof(a))#define sin1(a) scanf("%d",&(a))#define sin2(a,b) scanf("%d%d",&(a),&(b))#define sll1(a) scanf("%lld",&(a))#define sll2(a,b) scanf("%lld%lld",&(a),&(b))#define sdo1(a) scanf("%lf",&(a))#define sdo2(a,b) scanf("%lf%lf",&(a),&(b))#define inf 0x3f3f3f3f//#define lson i<<1,l,mid//#define rson ((i<<1)|1),mid+1,r#define uint unsigned inttypedef pair<int,int> PII;#define A first#define B second#define pb push_back#define MK make_pair#define ll long longtemplate<typename T>void read1(T &m) { T x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-')f=-1; ch=getchar(); } while(ch>='0'&&ch<='9') { x=x*10+ch-'0'; ch=getchar(); } m = x*f;}template<typename T>void read2(T &a,T &b) { read1(a); read1(b);}template<typename T>void read3(T &a,T &b,T &c) { read1(a); read1(b); read1(c);}template<typename T>void out(T a) { if(a>9) out(a/10); putchar(a%10+'0');}template<typename T>void outn(T a) { if(a>9) out(a/10); putchar(a%10+'0'); puts("");}using namespace std;///------------------------------------------------------------------------------------int sg[1005];vector<int>mat[1005];int in[1005];int T;int getsg(int ind){ if(sg[ind]!=-1) return sg[ind]; bool flag[1005]; memset(flag,false,sizeof(flag)); rep0(i,0,int(mat[ind].size())) flag[getsg(mat[ind][i])]=true; rep0(i,0,1005) if(!flag[i]) return (sg[ind]=i);}void init(void){ MS_1(sg); rep0(i,0,T) { sg[i] = getsg(i); }}int main() {// freopen("in.txt","r",stdin); while(sin1(T)!=EOF) { memset(in,0,sizeof(in)); rep0(i,0,T) mat[i].clear(); int t,x; rep0(i,0,T) { read1(t); while(t--) { read1(x); mat[i].pb(x); in[x]++; } } init(); while(read1(t),t) { int ans = 0; while(t--) { read1(x); ans^=sg[x]; } puts(ans?"WIN":"LOSE"); } } return 0;}
阅读全文
0 0
- hdu 1524 A Chess Game
- hdu 1524 A Chess Game
- HDU 1524 - A Chess Game(SG)
- HDU 1524 A Chess Game 【SG函数】
- hdu 1524 A Chess Game (SG)
- hdu 1524 A Chess Game 博弈论
- HDU 1524 A Chess Game(SG函数)
- HDU 1524 A Chess Game(SG博弈)
- (step 8.2.13)hdu 1524(A Chess Game)
- HDU ACM 1524 A Chess Game->博弈(SG函数)
- POJ 2425 && HDU 1524 A Chess Game(博弈论)
- HDU 1524 A Chess Game (SG函数模板题)
- HDU 1524 A Chess Game [SG函数]【博弈】
- HDU-1524-A Chess Game && POJ-2425 【sg】
- HDOJ 1524 A Chess Game SG函数
- hdu1524 A Chess Game
- A Chess Game
- POJ A Chess Game
- js 防止提交按钮连击
- ZOJ 3329 概率dp求期望
- Android 圆角ImageView
- htts环境搭建准备(上)新手需要
- 【hdu1698-Just aHook】(线段树成段更新)
- hdu 1524 A Chess Game
- Linux设置环境变量,修改/etc/environment后系统无法识别命令
- Pandas之read_csv和read_excel用法
- 使用pg_upgrade工具为PostgreSQL进行版本升级
- GitHub修改头像
- Echarts-java后端封装option ---(二)饼图测试用例
- HDU
- 将自己的ubuntu电脑设置为一台可远程访问的服务器
- Mac安装MySQL、修改MySQL的初始化密码、默认字符集为utf-8以及配置完my.cnf的文件后仍不生效的情况下的处理方案