zoukankan      html  css  js  c++  java
  • dp --- acdream原创群赛(16) --- B

    <传送门>

    B - Apple

    Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)
    SubmitStatus

    Problem Description

    Alice and Bob are coming.
    This time, they are playing with apples. Initially, there are N baskets and M apples. Both baskets and apples are distinguishable. Each turn, (s)he can choose adding a basket or an apple. Alice always plays first. When (s)he complete operation, if the number of ways to split apples into baskets is not less than A, (s)he lose.
    Now Alice wonder whether she can win if both player use best strategy.

    Input

    There are multiple test cases.
    Each test case contains three integers N, M and A.
    1 <= N <= 10,000
    1 <= M <= 30
    2 <= A <= 1,000,000,000

    Output

    For each test case, if Alice can win, output "win" and if Bob can win, output "lose"; otherwise output "draw".

    Sample Input

    3 1 4
    2 2 10
    

    Sample Output

    lose
    win

     【题目大意】

    有N个篮子和M个苹果,篮子和苹果都是有区别的。
    对于每一个回合,他或她可以选择增加一个篮子或者一个苹果。爱丽丝总是第一个动手。
    当他们完成一次操作后,如果将苹果分配到篮子里的方法大于等于A,则输。

    现在要你判断先手的胜负。

    N----篮子数
    M---苹果数
    A---上限

    【题目分析】

    定位:常见的dp博弈 、记忆化搜索

    像这种没有SG函数的博弈已经很少见了,SG函数的博弈才真叫人蛋疼。

    这题的一个细节的地方就是当篮子数=1的时候判断有点麻烦,其他的也不是很难。

    具体看代码(注释很详细):

    //Memory   Time
    // 1680K     0MS
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<iostream>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<iomanip>
    #include<string>
    #include<climits>
    #include<cmath>
    #define MAX 1100
    #define LL long long
    using namespace std;
    int dp[40][30];
    int A;
    bool check(int n,int m){   //组合数
        long long way=1;
        for(int i=1;i<=n;i++){
            way*=m;
            if(way>=A)       //不满足条件
                return false;
        }
        return true;    //满足条件
    }
    
    
    //2--- 平局
    //1-----win
    //0----lose
    
    int dfs(int n,int m){
        if(m==1){                 //这个是细节,开始一直没想到
            if(n*n>=A){
            	return 2;      //只有一个篮子,并且一开始就大于A,平局
            }
            else{   
                int tmp1=-1,tmp2=-1;
                if(check(n+1,m)){
    				tmp1=dfs(n+1,m);
                	if(tmp1==0) return 1;
    			}
                if(check(n,m+1)){
    				tmp2=dfs(n,m+1);
    				if(tmp2==0)return 1;
                }
                if(tmp1==2)   return 2;
                else return 0;
            }
        }
        
    //使用dp数组来记录每一次的搜索值(记忆化搜索)
    
        if(dp[n][m]!=-1)
            return dp[n][m];
        int tmp1=-1,tmp2=-1;
        if(check(n+1,m)){
    		tmp1=dfs(n+1,m);
        	if(tmp1==0) return dp[n][m]=1;
    	}
        if(check(n,m+1)){
    		tmp2=dfs(n,m+1);
    		if(tmp2==0) return dp[n][m]=1;
        }
        if(tmp1==2||tmp2==2) return dp[n][m]=2;
        else return dp[n][m]=0;
    }
    
    int main(){
    //    freopen("cin.txt","r",stdin);
    //    freopen("cout.txt","w",stdout);
        int n,m;
        while(scanf("%d %d %d",&m,&n,&A)!=EOF){
            memset(dp,-1,sizeof(dp));
            int k=dfs(n,m);
    		if(k==2)puts("draw");
    		else if(k==1)puts("win");
    		else puts("lose");
        }
        return 0;
    }
    

      

  • 相关阅读:
    SQLite学习手册(锁和并发控制)
    SQLite学习手册(命令行工具)
    SQLite学习手册(表达式)
    SQLite学习手册(内存数据库)
    SQLite学习手册(在线备份)
    SQLite学习手册(索引和数据分析/清理)
    SQLite学习手册(临时文件)
    随笔之正则表达式
    序列化,反序列化方法
    小东西,点击按钮防连击
  • 原文地址:https://www.cnblogs.com/crazyacking/p/3839661.html
Copyright © 2011-2022 走看看