hdu1517 A Multiplication Game

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4601    Accepted Submission(s): 2609


Problem Description
Stan and Ollie play the game of multiplication by multiplying an integer p by one of the numbers 2 to 9. Stan always starts with p = 1, does his multiplication, then Ollie multiplies the number, then Stan and so on. Before a game starts, they draw an integer 1 < n < 4294967295 and the winner is who first reaches p >= n.
 

Input
Each line of input contains one integer number n.
 

Output
For each line of input output one line either 

Stan wins. 

or 

Ollie wins.

assuming that both of them play perfectly.
 

Sample Input
   
   
   
   
162 17 34012226
 

Sample Output
   
   
   
   
Stan wins. Ollie wins. Stan wins.
 
题意:给你一个数n, 1 < n < 4294967295,起始数是1,两个人玩游戏,每人可以在起始点上乘上2~9的任何一个数,谁先乘一个数大于等于n就获胜,问哪一个必胜。
思路:这题的要点是理解必胜点和必败点,当一个点的后继点至少有一个是必败点那么这点是必胜点,当一个点的后继点全部都是必胜点那么该点是必败点。因为我们只需要判断1是必胜点还是必败点,所以只要从1开始深搜就行了,为了防止超时,我们用map<int,node>mp中的node记录这个数是不是访问过了并且它是必胜点还是必败点。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 99999999
struct node{
    int vis,sg;
};
map<int,node>mp;
ll n;
int dfs(ll num)
{
    int i,j;
    int flag;
    if(num*9>=n){
        mp[num].vis=1;
        mp[num].sg=1;
        return 1;
    }
    mp[num].vis=1;
    for(i=2;i<=9;i++){
        if(mp[num*i].vis==1){
            if(mp[num*i].sg==0){
                mp[num].sg=1;break;
            }
        }
        else{
            dfs(num*i);
            if(mp[num*i ].sg==0){
                mp[num].sg=1;break;
            }
        }
    }
    return mp[num].sg;
}

int main()
{
    ll m,i,j,ll;
    while(scanf("%lld",&n)!=EOF)
    {
        mp.clear();
        if(dfs(1)==0)printf("Ollie wins.\n");
        else printf("Stan wins.\n");
    }
    return 0;
}



你可能感兴趣的:(博弈)