HDU1524有向無環圖SG定理的應用

A Chess Game

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 916    Accepted Submission(s): 415


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


題意

兩個人在一個有向五環圖上面走棋子,每次只能走一步,最後誰

* 沒有棋子可走就敗,然後棋子可以重疊,並且有n個棋子。要求判斷

* 先手的勝負。


SG函數:


        首先定義mex(minimal excludant)運算,這是施加於一個集合的運算,表示最小的不屬於這個集合的非負整數。
例如mex{0,1,2,4}=3、mex{2,3,5}=0、mex{}=0。


        對於任意狀態 x , 定義 SG(x) = mex(S),其中 S 是 x 後繼狀態的SG函數值的集合。如 x 有三個後繼狀態分別爲 SG(a),SG(b),SG(c),
那麼SG(x) = mex{SG(a),SG(b),SG(c)}。 這樣 集合S 的終態必然是空集,所以SG函數的終態爲 SG(x) = 0,當且僅當 x 爲必敗點P時。
#include "stdio.h"
#include "string.h"
int v[1000];
int a[1000][1000];
int sg[1000];
int getsg(int x)
{
	if(sg[x]!=-1)
		return sg[x];
	int i,j,k;q
	int mex[1000]={0};
	for(i=0;i<v[x];i++)
	{
		mex[getsg(a[x][i])]=1;
	}
	for(i=0;;i++)
	{
		if(mex[i]==0)
			return sg[x]=i;
	}
	
}
int main()
{
	
	int n,m,i,j,k;
	while (scanf("%d",&n)!=EOF)
	{	
		memset(sg,-1,sizeof(sg));
		memset(a,0,sizeof(a));
		memset(v,0,sizeof(v));
		for(i=0;i<n;i++)
		{
			scanf("%d",&v[i]);
			for(j=0;j<v[i];j++)
			{
				scanf("%d",&a[i][j]);
			}
		}
		while(scanf("%d",&m)==1&&(m))
		{
			int mark=0;
			int t;
			for(i=0;i<m;i++)
			{
				scanf("%d",&t);
				mark^=getsg(t);
			}
			if(mark!=0)
				printf("WIN\n");
			else 
				printf("LOSE\n");
			
		}
	}
	return 0;
}




發佈了22 篇原創文章 · 獲贊 1 · 訪問量 7392
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章