hdu-2147:kiki’s game 博弈

來源:互聯網
上載者:User

P點:指後手贏

N點:指先手贏

現在關於P,N的求解有三個規則

(1):最終態都是P

(2):按照遊戲規則,到達當前態的前態都是N的話,當前態是P

(3):按照遊戲規則,到達當前態的前態至少有一個P的話,當前態是N

由規則可以求出狀態矩陣,由以下代碼可列印出

#pragma comment(linker,"/STACK:102400000,102400000")#include<iostream>#include<stdio.h>#include<algorithm>using namespace std;int sg[2001][2001];void Get_sg(){int i,j;memset(sg,-1,sizeof(sg));sg[1][2000]=0;for(i=1;i<=2000;i++)for(j=2000;j>=1;j--){if(i==1 && j==2000) continue;if(i==1){if(sg[i][j+1]==0)sg[i][j]=1;else sg[i][j]=0;}else if(j==2000){if(sg[i-1][j]==0)sg[i][j]=1;else sg[i][j]=0;}else {if(sg[i-1][j]==1 && sg[i-1][j+1]==1 && sg[i][j+1]==1){sg[i][j]=0;}elsesg[i][j]=1;}}}int main(){int n,m,i,j;Get_sg();while(scanf("%d%d",&n,&m),n+m){if(!sg[n][2001-m]) cout<<"What a pity!"<<endl;else cout<<"Wonderful!"<<endl;}return 0;}

直接交上去會逾時,所有可以根據矩陣找到規律

代碼如下:

#include <iostream>using namespace std;int main(){    int a,b;    while(cin>>a>>b&&a&&b)    {        a%=2;        b%=2;        if(a&&b)cout<<"What a pity!"<<endl;        else cout<<"Wonderful!"<<endl;    }} 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.