程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> poj1740 A New Stone Game----分析P/N

poj1740 A New Stone Game----分析P/N

編輯:C++入門知識

A New Stone Game
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 4111   Accepted: 2179
Description
Alice and Bob decide to play a new stone game.At the beginning of the game they pick n(1<=n<=10) piles of stones in a line. Alice and Bob move the stones in turn.
At each step of the game,the player choose a pile,remove at least one stones,then freely move stones from this pile to any other pile that still has stones.
For example:n=4 and the piles have (3,1,4,2) stones.If the player chose the first pile and remove one.Then it can reach the follow states.
2 1 4 2
1 2 4 2(move one stone to Pile 2)
1 1 5 2(move one stone to Pile 3)
1 1 4 3(move one stone to Pile 4)
0 2 5 2(move one stone to Pile 2 and another one to Pile 3)
0 2 4 3(move one stone to Pile 2 and another one to Pile 4)
0 1 5 3(move one stone to Pile 3 and another one to Pile 4)
0 3 4 2(move two stones to Pile 2)
0 1 6 2(move two stones to Pile 3)
0 1 4 4(move two stones to Pile 4)
Alice always moves first. Suppose that both Alice and Bob do their best in the game.
You are to write a program to determine who will finally win the game.
Input
The input contains several test cases. The first line of each test case contains an integer number n, denoting the number of piles. The following n integers describe the number of stones in each pile at the beginning of the game, you may assume the number of stones in each pile will not exceed 100.
The last test case is followed by one zero.
Output
For each test case, if Alice win the game,output 1,otherwise output 0.
Sample Input
3
2 1 3
2
1 1
0
Sample Output
1
0
Source
LouTiancheng@POJ
 
[cpp] 
#include <iostream> 
 
#include <string> 
 
#include <algorithm> 
 
#include <cmath> 
 
#include <vector> 
 
  
 
using namespace std; 
 
  
 
int a[12]; 
 
  
 
int main () 
 

 
    int n, i; 
 
    while (scanf("%d", &n) != EOF && n) 
 
    { 
 
       for (i = 0; i < n; i++) 
 
           scanf("%d", &a[i]); 
 
  
 
       if (n % 2)printf("1\n"); 
 
       else 
 
       { 
 
           sort(a, a + n); 
 
           for (i = 0; i < n - 1; i += 2) 
 
              if (a[i] != a[i + 1]) 
 
              { 
 
                  printf("1\n"); 
 
                  break; 
 
              } 
 
           if (i >= n)printf("0\n"); 
 
       } 
 
    } 
 

 
 
題意:給你n堆石子,每次可以從任意一堆中取走任意顆石子(至少一顆)扔掉,然後從剩下的石子中取任意顆石子加到任意其他堆未取完的堆中,問先手是否有取勝策略,如果有,輸出1,否則輸出0.
這題主要是尋找必敗態。
如果只有一堆石子,先手必勝。
如果有兩堆石子,並且兩堆石子的數量相等,那麼先手采取什麼樣的策略,後手采取一樣的策略,先手必敗。
如果有三堆石子,那麼先手可以在第一步取到只剩兩堆相同數量的石子,先手必勝。
如果有四堆石子,由於三堆石子是必勝態,所以無論是先手還是後手都想逼對方取完某一堆石子,只有在四堆石子都為1時,擦能迫使某一方取完一堆石子,通過分析可知,只有當四堆石子可以分成兩兩相等的兩隊時,先手必敗。
由上我們可以猜測,n 堆石子,當且僅當n為偶數,且n對石子可以分成n/2對兩兩相等的石子時,先手必敗。
證明:
第一條性質:終點是必敗態,滿足。
第二條性質:必勝點一定有種策略可以進入必敗態。
                     由以上可知,必勝點是當n為奇數或者n為偶數且不能分成n/2對兩兩相等的石子。
                     當n為奇數時我們一定可以把其中一堆分到其他堆中,使成n/2對兩兩相等的石子,第二種情況也是成立的。
第三條性質:必敗點只能到達必勝點。顯然成立


作者:qiqijianglu

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved