|
2 | 2 | // Author : Calinescu Valentin |
3 | 3 | // Date : 2015-10-19 |
4 | 4 |
|
5 | | -/********************************************************************************** |
6 | | - * You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 |
7 | | - * stones. The one who removes the last stone will be the winner. You will take the first turn to remove the stones. |
| 5 | +/*************************************************************************************** |
8 | 6 | * |
9 | | - * Both of you are very clever and have optimal strategies for the game. Write a function to determine whether you can win the game given the number of |
10 | | - * stones in the heap. |
11 | | - * |
12 | | - * For example, if there are 4 stones in the heap, then you will never win the game: no matter 1, 2, or 3 stones you remove, the last stone will always be |
13 | | - * removed by your friend. |
| 7 | + * You are playing the following Nim Game with your friend: There is a heap of stones |
| 8 | + * on the table, each time one of you take turns to remove 1 to 3 stones. The one who |
| 9 | + * removes the last stone will be the winner. You will take the first turn to remove |
| 10 | + * the stones. |
| 11 | + * |
| 12 | + * Both of you are very clever and have optimal strategies for the game. Write a |
| 13 | + * function to determine whether you can win the game given the number of stones in the |
| 14 | + * heap. |
| 15 | + * |
| 16 | + * For example, if there are 4 stones in the heap, then you will never win the game: no |
| 17 | + * matter 1, 2, or 3 stones you remove, the last stone will always be removed by your |
| 18 | + * friend. |
| 19 | + * |
| 20 | + * If there are 5 stones in the heap, could you figure out a way to remove the stones |
| 21 | + * such that you will always be the winner? |
| 22 | + * |
| 23 | + * Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating |
| 24 | + * all test cases. |
| 25 | + * |
| 26 | + ***************************************************************************************/ |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +/* |
| 31 | + * Solutions |
| 32 | + * ========= |
14 | 33 | * |
15 | | - **********************************************************************************/ |
16 | | -/********************************************************************************** |
17 | 34 | * Let's look at the example: |
18 | 35 | * |
19 | | - * 0 stones - false |
20 | | - * 1 stone - true |
21 | | - * 2 stones - true |
22 | | - * 3 stones - true |
23 | | - * 4 stones - false |
| 36 | + * 0 stones - false |
| 37 | + * 1 stone - true |
| 38 | + * 2 stones - true |
| 39 | + * 3 stones - true |
| 40 | + * 4 stones - false |
24 | 41 | * |
25 | | - * We notice that all we need for a position to be true is to get the opponent in a position that is false. With 1, 2 and 3 you can take 1, 2 and 3 |
26 | | - * stones respectively to force your opponent into having 0 stones, a position where he cannot win. No matter how many stones we take from 4 we cannot |
27 | | - * force the opponent into a losing positon, so position 4 becomes a losing position. Let's take a look at the next 4 positions: |
| 42 | + * We notice that all we need for a position to be true is to get the opponent in a position |
| 43 | + * that is false. With 1, 2 and 3 you can take 1, 2 and 3 stones respectively to force your |
| 44 | + * opponent into having 0 stones, a position where he cannot win. No matter how many stones |
| 45 | + * we take from 4 we cannot |
| 46 | + * |
| 47 | + * force the opponent into a losing positon, so position 4 becomes a losing position. |
| 48 | + * Let's take a look at the next 4 positions: |
28 | 49 | * |
29 | | - * 5 stones - true |
30 | | - * 6 stones - true |
31 | | - * 7 stones - true |
32 | | - * 8 stones - false |
| 50 | + * 5 stones - true |
| 51 | + * 6 stones - true |
| 52 | + * 7 stones - true |
| 53 | + * 8 stones - false |
33 | 54 | * |
34 | | - * With 5, 6 and 7 stones we can take 1, 2 and 3 stones respectively to force the opponent into position 4. Position 8 is a losing one because we can |
35 | | - * only force the opponent into winning positions. We notice that this group of 4 positions can repeat itself indefinitely, because we only need the |
36 | | - * previous 3 positions to judge whether a position is winning or losing. Thus we can see the pattern: |
| 55 | + * With 5, 6 and 7 stones we can take 1, 2 and 3 stones respectively to force the opponent into |
| 56 | + * position 4. Position 8 is a losing one because we can only force the opponent into winning |
| 57 | + * positions. We notice that this group of 4 positions can repeat itself indefinitely, because |
| 58 | + * we only need the previous 3 positions to judge whether a position is winning or losing. |
| 59 | + * |
| 60 | + * Thus we can see the pattern: |
37 | 61 | * |
38 | | - * n % 4 == 0 - false |
39 | | - * n % 4 != 0 - true |
| 62 | + * n % 4 == 0 - false |
| 63 | + * n % 4 != 0 - true |
40 | 64 | * |
41 | | - **********************************************************************************/ |
| 65 | + */ |
| 66 | + |
42 | 67 | class Solution { |
43 | 68 | public: |
44 | 69 | bool canWinNim(int n) { |
|
0 commit comments