hdu1495非常可乐【广搜】

天国语言不解释,之前真没想到这种题居然还可以用广搜做,看这题想起来之前有一个学弟问我的木棍拼正方形的广搜了

提交之前各种白痴错误,if条件写错地方,条件写错,最开始vis[cur.s][cur.n][cur.m]=1写成next输出一堆中间变量才发现敲打

不过还好是1A的,虽说我的代码又臭又长,实在想不出来来回倒可乐的方法除了判断当前情况是否能够满足s->m m->s s->n n->s m->n n->m再判断能否装满以外还有什么能用广搜的做法

#include <cstdio>

int gcd(int x, int y)
{
    return y ? gcd(y, x % y) : x;
}

int main()
{
    int a, b, c;
    
    while (scanf("%i%i%i", &a, &b, &c), a + b + c) {
        (a /= gcd(b, c)) & 1 && ~puts("NO") || printf("%i\n", a - 1);
    }
    return 0;
}

这种找规律的变态除外→_→

我的代码 头一次手打这么长的

/********
hdu1495
2015.12.11
187MS 2704K 6047 B G++
********/
#include <iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
struct node
{
    int s,n,m,step;
};
bool vis[105][105][105];
int S,N,M;
int bfs()
{
   // cout<<"ok1"<<endl;
    queue<node>Q;
   // cout<<"ok2"<<endl;
    node cur,next;
    cur.s=S,cur.m=0,cur.n=0,cur.step=0;
    vis[cur.s][cur.n][cur.m]=1;
  //  cout<<"ok3"<<endl;
    Q.push(cur);
    while(!Q.empty())
    {
        cur=Q.front();
        Q.pop();
     //   printf("s=%d,n=%d,m=%d,step=%d\n",cur.s,cur.n,cur.m,cur.step);
        if(cur.s==S/2&&cur.m==S/2) return cur.step;
        if(cur.s!=0&&cur.n<N)//s->n
        {
           // cout<<"1"<<endl;
            if(N-cur.n<=cur.s)//n full
            {
                next=cur;
                next.s-=(N-cur.n);
                next.n=N;
                if(!vis[next.s][next.n][next.m])
                {
                    vis[next.s][next.n][next.m]=1;
                    next.step++;
                    Q.push(next);
                }
            }
            else if(N-cur.n>cur.s)
            {
                next=cur;
                next.n+=cur.s;
                next.s=0;
                if(!vis[next.s][next.n][next.m])
                {
                    vis[next.s][next.n][next.m]=1;
                    next.step++;
                    Q.push(next);
                }
            }
        }
        if(cur.n!=0&&cur.s<S)//n->s
        {
           // cout<<"2"<<endl;
            if(S-cur.s<=cur.n)//s full
            {
                next=cur;
                next.n-=(S-cur.s);
                next.s=S;
                if(!vis[next.s][next.n][next.m])
                {
                    vis[next.s][next.n][next.m]=1;
                    next.step++;
                    Q.push(next);
                }
            }
            else
            {
                next=cur;
                next.s+=cur.n;
                next.n=0;
                if(!vis[next.s][next.n][next.m])
                {
                    vis[next.s][next.n][next.m]=1;
                    next.step++;
                    Q.push(next);
                }
            }
        }
        if(cur.s!=0&&cur.m<M)//n->m
        {
            //cout<<"3"<<endl;
            if(M-cur.m<=cur.n)//m full
            {
                next=cur;
                next.s-=(M-cur.m);
                next.m=M;
                if(vis[next.s][next.n][next.m])
                {
                    vis[next.s][next.n][next.m]=1;
                    next.step++;
                    Q.push(next);
                }
            }
            else
            {
                next=cur;
                next.m+=cur.n;
                next.n=0;
                if(!vis[next.s][next.n][next.m])
                {
                    vis[next.s][next.n][next.m]=1;
                    next.step++;
                    Q.push(next);
                }
            }
        }
        if(cur.m!=0&&cur.n<N)//m->n
        {
           // cout<<"4"<<endl;
            if(N-cur.n<=cur.m)//n full
            {
                next=cur;
                next.m-=(N-cur.n);
                next.n=N;
                if(!vis[next.s][next.n][next.m])
                {
                    vis[next.s][next.n][next.m]=1;
                    next.step++;
                    Q.push(next);
                }
            }
            else
            {
                next=cur;
                next.n+=(cur.m);
                next.m=0;
                if(!vis[next.s][next.n][next.m])
                {
                    vis[next.s][next.n][next.m]=1;
                    next.step++;
                    Q.push(next);
                }
            }
        }
        if(cur.s!=0&&cur.m<M)//s->m
        {
            //cout<<"5"<<endl;
            if(M-cur.m<=cur.s)//m full
            {
                next=cur;
                next.s-=(M-cur.m);
                next.m=M;
                if(!vis[next.s][next.n][next.m])
                {
                    vis[next.s][next.n][next.m]=1;
                    next.step++;
                    Q.push(next);
                }
            }
            else
            {
                next=cur;
                next.m+=cur.s;
                next.s=0;
                if(!vis[next.s][next.n][next.m])
                {
                    vis[next.s][next.n][next.m]=1;
                    next.step++;
                    Q.push(next);
                }
            }
        }
        if(cur.m!=0&&S-cur.s>0)//m->s
        {
           // cout<<"6"<<endl;
            if(S-cur.s<=cur.m)//s full
            {
                next=cur;
                next.m-=(S-cur.s);
                next.s=S;
                if(!vis[next.s][next.n][next.m])
                {
                    vis[next.s][next.n][next.m]=1;
                    next.step++;
                    Q.push(next);
                }
            }
            else
            {
                next=cur;
                next.s+=cur.m;
                next.m=0;
                if(!vis[next.s][next.n][next.m])
                {
                    vis[next.s][next.n][next.m]=1;
                    next.step++;
                    Q.push(next);
                }
            }
        }
    }
    return -1;
}
int main()
{
    //freopen("cin.txt","r",stdin);
    while(~scanf("%d%d%d",&S,&N,&M))
    {
        if(S==0&&N==0&&M==0) break;
        memset(vis,0,sizeof(vis));
        if(N>M) swap(N,M);
        if(N==M)
        {
            printf("1\n");
        }
        else if(S&1)
        {
            printf("NO\n");
        }
        else
        {
            //cout<<"0"<<endl;
            int ans=bfs();
            if(ans==-1) printf("NO\n");
            else printf("%d\n",ans);
        }

    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值