HDU - 1495 非常可乐(BFS)

传送门

分析:要求平分可乐,显然可乐体积为奇数时不可能被平分;为偶数时,可乐转移有6种运动状态:
S–>N S–>M
N–>N S–>M
M–>S M–>N
用BFS跑一遍,枚举每一种状态即可

#include <iostream>
#include <stdio.h>
#include <string>
#include<cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e2+5;
struct node{
    int s,n,m,step;
};
int n,m;
bool vis[maxn][maxn][maxn];
int BFS(int s)
{
    queue<node>q;
    q.push(node{s,0,0,0});
    vis[s][0][0]=true;
    int v=s/2;
    while (!q.empty()){
        node tem=q.front(); q.pop();
        int x=tem.s,y=tem.n,z=tem.m;
        if((x==v&&y==v)||(x==v&&z==v)||(y==v&&z==v)){return tem.step;}
        int tx,ty,tz;
        //1 s-->n
        if(x>n-y) tx=x+y-n,ty=n,tz=z;
        else tx=0,ty=y+x,tz=z;
        if(!vis[tx][ty][tz]) {
        vis[tx][ty][tz]=true;
        q.push(node{tx,ty,tz,tem.step+1});}
        // 2 s-->m
        if(x>m-z) tx=x+z-m,ty=y,tz=m;
        else tx=0,ty=y,tz=x+z;
        if(!vis[tx][ty][tz]) {
        vis[tx][ty][tz]=true;
        q.push(node{tx,ty,tz,tem.step+1}); }
        // 3  n-->m
        if(y>m-z) tx=x,ty=y+z-m,tz=m;
        else tx=x,ty=0,tz=y+z;
        if(!vis[tx][ty][tz]) {
        vis[tx][ty][tz]=true;
        q.push(node{tx,ty,tz,tem.step+1});}
        // 4  n-->s
        if(y>s-x) tx=s,ty=y+x-s,tz=z;
        else tx=x+y,ty=0,tz=z;
        if(!vis[tx][ty][tz]) {
        vis[tx][ty][tz]=true;
        q.push(node{tx,ty,tz,tem.step+1});}
        // 5  m-->s
        if(z>s-x) tx=s,ty=y,tz=z+x-s;
        else tx=x+z,ty=y,tz=0;
        if(!vis[tx][ty][tz]) {
        vis[tx][ty][tz]=true;
        q.push(node{tx,ty,tz,tem.step+1});}
        // 6  m-->n
        if(z>n-y) tx=x,ty=n,tz=z+y-n;
        else tx=x,ty=z+y,tz=0;
        if(!vis[tx][ty][tz]) {
        vis[tx][ty][tz]=true;
        q.push(node{tx,ty,tz,tem.step+1});}

    }
    return -1;
}
int main()
{
    int s;
    while (~scanf("%d%d%d",&s,&n,&m)&&s){
         memset(vis,false,sizeof(vis));
         if(s%2){ printf("NO\n"); continue;}
         int ans=BFS(s);
         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、付费专栏及课程。

余额充值