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,  n m(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);*/
    }
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值