HDU 1524-a Chess Game (SG function + DFS)

Source: Internet
Author: User

A Chess Game

Problem Descriptionlet ' s design a new chess game. There is N positions to hold M chesses in this game. Multiple chesses can be located in the same position. The positions is constituted as a topological graph, i.e. there is directed edges connecting some positions, and no CYCL E exists. II Players you and I move chesses alternately. In each turn the player should move is only a 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'll disturb the chesses and play it again.

Do you want to challenge me? Just Write your program to show your qualification!

Inputinput contains multiple test cases. Each test case is starts with a number n (1 <= n <=) on one line. Then the following N lines describe the out-positions of each position. Each line starts with a integer Xi is the number of out-positions for the position I. Then Xi integers following specify the out-positions. Positions is indexed from 0 to N-1. then multiple queries follow. Each query is occupies only one line. The line starts with a number M (1 <= m <=), and then come M integers, which is the initial positions of chesses . A line with number 0 ends the test case.

Outputthere 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 Input42 1 201 301 02 0 20 41 11 2002 0 12 1 13 0 1 30

Sample Outputwinwinwinlosewin

Sourcepku Monthly test instructions is to give a direction-free map, there are a number of pieces on the figure, take turns to move the pieces, who can not move the turn, he lost. Simply ask for the SG function, DFS is doing it.
#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespaceStd;vector<int> r[1010];intg[1010];voidinit () {memset (g,-1,sizeof(g));  for(inti =0; I <=1005; i++) {r[i].clear (); }}intGETSG (intx) {if(g[x]! =-1)returnG[x]; if(r[x].size () = =0)return 0; intvis[1010]; memset (Vis,0,sizeof(VIS));  for(inti =0; I < r[x].size (); i++) {G[r[x][i]]=GETSG (R[x][i]); Vis[g[r[x][i] ]=1; //puts ("check");    }     for(inti =0; ; i++)if(!Vis[i])returni; //puts ("check");}intMain () {intN;  while(~SCANF ("%d", &N)) {init ();  for(inti =0; I < n; i++) {            intM scanf"%d", &m);  for(intj =1; J <= M; J + +) {                intv; scanf ("%d", &v);            R[i].push_back (v); }        }        intm, V, Flag;  while(SCANF ("%d", &m)) {if(M = =0) Break; Flag=0;  while(m--) {scanf ("%d", &v); if(G[v] = =-1) G[v]=GETSG (v); Flag^=G[v]; }            if(Flag = =0) puts ("lose"); ElsePuts"WIN"); }    }}

HDU 1524-a Chess Game (SG function + DFS)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.