[kuangbin带你飞]专题一 简单搜索- M - 非常可乐

#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
#define INF 0x3f3f3f
int gcd(int a, int b) {return b==0 ? a:gcd(b,a%b);}

int s, n, m;
int flag;
int vis[110][110][110];

struct node
{
    int x, y, z,step;
    node(){}
    node (int x, int y, int z, int step):x(x),y(y),z(z),step(step){}
};
void bfs()
{
    memset(vis,0,sizeof(vis));
    queue<node>q;
    node p(s,0,0,0);
    vis[s][0][0] = 1;
    q.push(p);
    while(!q.empty())
    {
        p = q.front(); q.pop();
        if((p.x==p.y&&p.x==s/2)||(p.y==p.z&&p.y==s/2)||(p.x==p.z&&p.x==s/2)){
            flag = 1;
            printf("%d\n", p.step);
            break;
        }   
        int x = p.x; int y = p.y; int z = p.z;

        node a;

        // x->y
        if(x!=0){
            a.z = z; a.step = p.step+1;
            if(x+y<=n){
                a.x = 0; a.y = x+y;
            }else{
                a.x = x-(n-y); a.y = n;
            }
            if(!vis[a.x][a.y][a.z]){
                vis[a.x][a.y][a.z] = 1;
                q.push(a);
            }
        }

        // y->x
        if(y!=0){
            a.z = z; a.step = p.step+1;
            if(x+y<=s){
                a.x = x+y; a.y = 0;
            }else{
                a.x = s; a.y = y-(s-x);
            }
            if(!vis[a.x][a.y][a.z]){
                vis[a.x][a.y][a.z] = 1;
                q.push(a);
            }
        }

        // x->z
        if(x!=0){
            a.y = y; a.step = p.step+1;
            if(x+z<=m){
                a.x = 0; a.z = x+z;
            }else{
                a.x = x-(m-z); a.z = m;
            }
            if(!vis[a.x][a.y][a.z]){
                vis[a.x][a.y][a.z] = 1;
                q.push(a);
            }
        }

        // z->x
        if(z!=0){
            a.y = y; a.step = p.step+1;
            if(x+z<=s){
                a.x = x+z; a.z = 0;
            }else{
                a.x = s; a.z = z-(s-x);
            }
            if(!vis[a.x][a.y][a.z]){
                vis[a.x][a.y][a.z] = 1;
                q.push(a);
            }
        }

        // y->z;
        if(y!=0){
            a.x = x; a.step = p.step+1;
            if(y+z<=m){
                a.y = 0; a.z = y+z;
            }else{
                a.y = y-(m-z); a.z = m;
            }
            if(!vis[a.x][a.y][a.z]){
                vis[a.x][a.y][a.z] = 1;
                q.push(a);
            }
        }

        // z->y
        if(z!=0){
            a.x = x; a.step = p.step+1;
            if(y+z<=n){
                a.y = y+z; a.z = 0;
            }else{
                a.y = n; a.z = z-(n-y);
            }
            if(!vis[a.x][a.y][a.z]){
                vis[a.x][a.y][a.z] = 1;
                q.push(a);
            }
        }
    }
}

int main()
{
    while(~scanf("%d %d %d", &s, &n, &m)&&s&&n&&m)
    {
        if(s&1){
            printf("NO\n");
            continue;
        }
        flag = 0;
        bfs();
        if(!flag) printf("NO\n");
    }   
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值