"HDU1730" Northcott Game (Nim problem)

Source: Internet
Author: User

Northcott Game

Time limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 3101 Accepted Submission (s): 1355


Problem Description Tom and Jerry are playing a Northcott game, but Tom always loses, so he doubts if the game has some kind of winning strategy, depressed Tom now to you for help, can you help him?
The rules of the game are this:
, the game is performed on a chessboard of n rows m (1≤n≤1000 and 2≤m≤100) with one sunspot (black) and one white (white) per line. The black party first, each time the player can move any one of their own pieces to the same line of any one space, of course, the process is not allowed to cross the enemy pieces of the line. The two sides move until one party is unable to move, and the player who moves the last step wins. Tom always goes down first (black). Figure 1 is an initial situation, figure Two is Tom moved a piece after the situation (the first row of sunspots left two steps).



Figure 1



Figure 2

Input data has multiple groups. The first behavior for each set of data is two integers n and m, separated by a space. Then there are n rows, with two numbers per line Ti,ji (1≤ti, ji≤m) representing the number of columns Tom and Jerry have in the row.
   Note: There are an indefinite number of blank lines between the test data for each group. You have to deal with the end of the file.

Output one line of your results for each set of test data. If Tom has a winning strategy in the current situation, output "I win!", otherwise output "bad luck!".

Sample INPUT3 64 51 21 23 64 51 31 2

Sample Outputbad luck! I win!

This problem is typical of game theory nim. We'll get to the NIM problem first.

Theoretical foreshadowing ....

1. Define P-position and N-position: where P stands for previous,n on behalf of next. Intuitively, the last move of the people have a winning strategy is the situation is p-position, that is, "the initiator will be defeated", now turn to move the person has a winning strategy is the situation is n-position, that is, "the initiator can guarantee win."

(1). The situation where no movement is possible (i.e. terminal position) is p-position;

(2). The situation that can be moved to P-position is n-position;

(3). All movements lead to n-position situation is p-position.

2, the P/N status has the following properties:

(1), if the face of the end of the state to win the end of the state as a victory or otherwise the end of the state is a must fail.
(2), a situation is the state of the necessary and sufficient conditions is that the situation after a certain decision-making will become a must fail.
(3), a situation is the necessary and sufficient conditions for the state of the situation, regardless of the decision-making will become a winning state

3, P point: That must be defeated, a player is located at this point, as long as the other party without errors, will be defeated;

N point: That is to win points, a player located at this point, as long as their own no mistakes, then win.

4, take the Stone game algorithm realization

Step 1: Mark all the end positions as required points (p points);

Step 2: Mark the point where all one-step operations can enter the required point (P-point) to Win (n points)

Step 3: If all the steps starting at a certain point can only enter the winning point (n point), then mark the point as a must point (P point);

Step 4: If the new Fail (P point) is not found in step 3, the algorithm terminates; otherwise, return to step 2

1, the problem model: There are three piles of various items, two people take turns from a heap of any number of items, the provision of at least one at a time, more than open, and finally the winner of the light.

2, solve the idea: use (a,b,c) to indicate a situation, flickering lights show (0,0,0) is the first singular situation, no matter who face the singular situation, will inevitably fail. The second singular situation is (0,n,n), as long as the opponent takes away as many items as the final result (0,0,0).

To solve this problem, we need to find out the rule of failure: (A,B,C) is a must-fail state equivalent to a^b^c=0 (^ represents an XOR operation).

Proof: (1) Any situation (A,B,C ') of any P (a,b,c) =0; there must be P (a,b,c ') not equal to 0. Otherwise you can get c=c '.

(2) Any situation where P (a,b,c) is not equal to 0 can move towards P (a,b,c) =0

(3) for (4,9,13) This easy to verify is singular situation

There are two 8, two 4, two 1, non-0 pairs appearing, which is the essence of Nim and zero. If someone takes 13 in 8 or 1, then you take out the corresponding 9 of the 8 or 1, if others take out 13 of 4, you take out 4 of 4, others if the 13 in the 3, the 10 decomposition, and then find a way to meet the non-0 pairs can be.

3, promote one: if we are facing a non-singular situation (a,b,c), how to become a singular situation? Suppose a < b< C, we just change C to a^b, because we have the following result: a^b^ (a^b) = (a^a) ^ (b^b) =0^0=0. To change C to a^b, subtract C C (a^b) from

4, promotion two: When the number of gravel heap n heap, it is promoted to be when the number of each pile is also or after the value of zero is a must-fail state.

Back to this question we just need to each line between the number of spaces between each other or the final result to determine whether it is 0.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <cstdlib>6 using namespacestd;7 8 intMain () {9     intN, M;Ten      while(SCANF ("%d%d", &n, &m)! =EOF) { Onem =0;//It's not a good thing to be aware of M here. I'm too lazy to define a variable. A         intA, B; -  -          while(n--){ thescanf"%d%d", &a, &b); -M ^= ABS (A-B)-1;  -         } -  +         if(m) { -printf"I win!\n"); +         } A         Else{ atprintf"Bad luck!\n"); -         } -     } -     return 0; -}
Alpha

"HDU1730" Northcott Game (Nim problem)

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.