您的位置:首页 > 其它

hdu 1524 A Chess Game

2017-08-18 10:38 330 查看
题目链接:点这里。。

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

4
2 1 2
0
1 3
0
1 0
2 0 2
0

4
1 1
1 2
0
0
2 0 1
2 1 1
3 0 1 3
0


Sample Output

WIN
WIN
WIN
LOSE
WIN


【题意】

给你一个有向无环图,图中每个点可能放一个,多个,或者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 int
typedef pair<int,int> PII;
#define A first
#define B second
#define pb push_back
#define MK make_pair
#define ll long long
template<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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: