hdu 5194 DZY Loves Balls【bfs+打表】

DZY Loves Balls

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 400    Accepted Submission(s): 224


Problem Description
There are n black balls and m white balls in the big box.

Now, DZY starts to randomly pick out the balls one by one. It forms a sequence S. If at the i-th operation, DZY takes out the black ball, Si=1, otherwise Si=0.

DZY wants to know the expected times that '01' occurs in S.
 

Input
The input consists several test cases. (TestCase150)

The first line contains two integers, nm(1n,m12)
 

Output
For each case, output the corresponding result, the format is p/q(p and q are coprime)
 

Sample Input
1 1 2 3
 

Sample Output
1/2 6/5

BFS走了一发,实力超时、然后for循环枚举n,m的所有可能,然后添逗号,然后入数组。。。。。。。忧伤的小伙伴、、、、、

这里我们给出bfs的思路和代码、以及AC代码、、、

先谈变量

struct zuobiao//这里习惯图搜了,用贯了结构体、、、
{
    char name[25];
}now,nex;
int n,m;<span style="white-space:pre">																		</span><pre name="code" class="cpp">int zhongleishu;//一共排列的个数,
int zonghe;<span style="font-family: Arial, Helvetica, sans-serif;">	</span>//n+m<span style="font-family: Arial, Helvetica, sans-serif;">													</span>

然后上BFS部分详解:

int  bfs()
{
    long long int sum=0;
    for(int i=0;i<25;i++)//12+12最大是24个字符
    {
        now.name[i]='\0';
    }
    now.name[0]='1';
    queue<zuobiao>s;
    s.push(now);
    now.name[0]='0';
    s.push(now);//push进去1开头的和0开头的两种情况
    while(!s.empty())
    {
        now=s.front();
        if(strlen(now.name)==zonghe)//如果达到了目标长度
        {
            zhongleishu++;//种类数+1
            char c=now.name[0];
            for(int k=1;k<zonghe;k++)//在长度中测量01的个数
            {
                if(now.name[k]=='1'&&c=='0')
                sum++;//找到1个+1个
                c=now.name[k];
            }
        }
        s.pop();
        for(int i=0;i<=1;i++)
        {
            nex=now;//这里是维护数据的重点
            nex.name[strlen(nex.name)]=i+'0';//在尾部加上1,加上0,分别入队(当然要在合理条件下)
            if(strlen(nex.name)<=zonghe)//如果符合长度条件
            {
                int conthei=0;
                int contbai=0;
                for(int j=0;j<strlen(nex.name);j++)//找黑的和白的数量,
                {
                    if(nex.name[j]=='1')
                    conthei++;
                    if(nex.name[j]=='0')
                    contbai++;
                }
                if(conthei<=n&&contbai<=m)//如果符合n,m条件,入队
                s.push(nex);//其实这里可以剪枝,在结构体中加入黑白球的数量信息,兴许就能直接AC了,但是因为一共数据也就12*12=144个,就打表了,反正也不是很麻烦
            }
        }
    }
    return sum;
}

然后我们通过bfs找到了种类数,和01个数,然后再通过gcd找到最大公约数,我们就得到了最终答案、

打表代码:

    /*
    for(int i=1;i<=12;i++)
    {
        for(int j=1;j<=12;j++)
        {
            n=i;
            m=j;
            zonghe=n+m;
            zhongleishu=0;
            long long int sum=bfs();
            long long yueshu=gcd(sum,zhongleishu);
           printf("%lld/%lld\n",sum/yueshu,zhongleishu/yueshu);
        }
    }*/

完整的AC代码:

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int output[12][12][2]={1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,2,3,1,1
,6,5
,4,3
,10,7
,3,2
,14,9
,8,5
,18,11
,5,3
,22,13
,12,7
,3,4
,6,5
,3,2
,12,7
,15,8
,2,1
,21,10
,24,11
,9,4
,30,13
,33,14
,12,5
,4,5
,4,3
,12,7
,2,1
,20,9
,12,5
,28,11
,8,3
,36,13
,20,7
,44,15
,3,1
,5,6
,10,7
,15,8
,20,9
,5,2
,30,11
,35,12
,40,13
,45,14
,10,3
,55,16
,60,17
,6,7
,3,2
,2,1
,12,5
,30,11
,3,1
,42,13
,24,7
,18,5
,15,4
,66,17
,4,1
,7,8
,14,9
,21,10
,28,11
,35,12
,42,13
,7,2
,56,15
,63,16
,70,17
,77,18
,84,19
,8,9
,8,5
,24,11
,8,3,40,13
,24,7
,56,15
,4,1
,72,17
,40,9
,88,19
,24,5
,9,10
,18,11
,9,4
,36,13
,45,14
,18,5
,63,16
,72,17
,9,2
,90,19
,99,20
,36,7
,10,11
,5,3
,30,13
,20,7
,10,3
,15,4
,70,17
,40,9
,90,19
,5,1
,110,21
,60,11
,11,12
,22,13
,33,14
,44,15
,55,16
,66,17
,77,18
,88,19
,99,20
,110,21
,11,2
,132,23
,12,13
,12,7
,12,5
,3,1
,60,17
,4,1
,84,19
,24,5
,36,7
,60,11
,132,23
,6,1};
struct zuobiao
{
    char name[25];
}now,nex;
int n,m;
int zhongleishu;
int zonghe;
int  bfs()
{
    long long int sum=0;
    for(int i=0;i<25;i++)
    {
        now.name[i]='\0';
    }
    now.name[0]='1';
    queue<zuobiao >s;
    s.push(now);
    now.name[0]='0';
    s.push(now);
    while(!s.empty())
    {
        now=s.front();
        if(strlen(now.name)==zonghe)
        {
            zhongleishu++;
            char c=now.name[0];
            for(int k=1;k<zonghe;k++)
            {
                if(now.name[k]=='1'&&c=='0')
                sum++;
                c=now.name[k];
            }
        }
        s.pop();
        for(int i=0;i<=1;i++)
        {
            nex=now;
            nex.name[strlen(nex.name)]=i+'0';
            if(strlen(nex.name)<=zonghe)
            {
                int conthei=0;
                int contbai=0;
                for(int j=0;j<strlen(nex.name);j++)
                {
                    if(nex.name[j]=='1')
                    conthei++;
                    if(nex.name[j]=='0')
                    contbai++;
                }
                if(conthei<=n&&contbai<=m)
                s.push(nex);
            }
        }
    }
    return sum;
}
int gcd(long long int x,long long int y)
{
    if(x%y==0)return y;
    else  return gcd(y,x%y);
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        int fuck=n*m;
        printf("%d/%d\n",output[n-1][m-1][0],output[n-1][m-1][1]);
        /*
        zonghe=n+m;
        zhongleishu=0;
        long long int sum=bfs();
        long long yueshu=gcd(sum,zhongleishu);
       printf("%lld/%lld\n",sum/yueshu,zhongleishu/yueshu);*/
    }
}






根据原作 https://pan.quark.cn/s/459657bcfd45 的源码改编 Classic-ML-Methods-Algo 引言 建立这个项目,是为了梳理和总结传统机器学习(Machine Learning)方法(methods)或者算法(algo),和各位同仁相互学习交流. 现在的深度学习本质上来自于传统的神经网络模型,很大程度上是传统机器学习的延续,同时也在不少时候需要结合传统方法来实现. 任何机器学习方法基本的流程结构都是通用的;使用的评价方法也基本通用;使用的一些数学知识也是通用的. 本文在梳理传统机器学习方法算法的同时也会顺便补充这些流程,数学上的知识以供参考. 机器学习 机器学习是人工智能(Artificial Intelligence)的一个分支,也是实现人工智能最重要的手段.区别于传统的基于规则(rule-based)的算法,机器学习可以从数据中获取知识,从而实现规定的任务[Ian Goodfellow and Yoshua Bengio and Aaron Courville的Deep Learning].这些知识可以分为四种: 总结(summarization) 预测(prediction) 估计(estimation) 假想验证(hypothesis testing) 机器学习主要关心的是预测[Varian在Big Data : New Tricks for Econometrics],预测的可以是连续性的输出变量,分类,聚类或者物品之间的有趣关联. 机器学习分类 根据数据配置(setting,是否有标签,可以是连续的也可以是离散的)和任务目标,我们可以将机器学习方法分为四种: 无监督(unsupervised) 训练数据没有给定...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值