標籤:style blog http color os io
博弈論裡面一個非常重要的結論:
如果前一個狀態所有可能都是必敗態,那麼目前狀態一定是必勝態。
如果前一個狀態所有可能有一個是必勝態,那麼目前狀態一定是必敗態。
POJ 2484 A Funny Game
博弈遊戲裡面後手經常佔據優勢。除了A可以一次性全部拿光的情況,其他時候B都可以採取與A相同的策略,這樣每次將石子分為相同的兩組,最後獲勝的一定是B。
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int main(){ int n; while(scanf("%d",&n)&&n) { if(n<=2) puts("Alice"); else puts("Bob"); } return 0;}View Code
POJ 2348
分析每回合可能的情況,(假設a>=b):
當a-b<=b時,這個時候只能減去1倍關係,也就是只有唯一選擇。這樣,前一個狀態是必勝,那麼當前就是必敗;前一個狀態是必敗,當前就是必勝。
當a-b>b時,假設此時有a-xb<=b。這時候有很多種選擇。考慮從a中減去(x-1)b到達的狀態,如果該狀態是必敗態,那麼目前狀態就是必勝態。如果該狀態是必勝態,那麼a-(x-1)b唯一可達的a-xb狀態是必敗態,所以這個時候可以選擇從a中減去xb來獲勝。所以目前狀態一定是必勝態。
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int main(){ int a,b; while(scanf("%d%d",&a,&b)&&!(!a&&!b)) { int c=0; while(1) { if(a<b) swap(a,b); if(a%b==0) break; if(a-b<=b) a=a-b; else break; c++; } if(c%2==1) puts("Ollie wins"); else puts("Stan wins"); } return 0;}View Code
關於階梯博弈可以參加
http://blog.csdn.net/kk303/article/details/6692506
http://www.cnblogs.com/jiangjing/p/3849284.html
POJ 1704
階梯NIM,從右往左,每兩個棋子的距離視為一堆石子,最左邊的棋子與0的距離視為一堆石子。這樣對第奇數堆的石子做NIM遊戲處理即可。
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int main(){ int T; scanf("%d",&T); while(T--) { int n; int p[1005]= {0}; scanf("%d",&n); for(int i=1; i<=n; ++i) scanf("%d",&p[i]); sort(p,p+1+n); int ans=0; for(int i=n; i-1>=0; i-=2) ans=ans^(p[i]-p[i-1]-1); if(ans!=0) puts("Georgia will win"); else puts("Bob will win"); } return 0;}View Code