hdu1495 非常可乐 bfs

一道比较老但是非常经典的bfs题,第一次做很难想到要用bfs吧。

https://blog.csdn.net/BestFM/article/details/79617669

 

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int S,M,N,temp[105][105][105];
struct node{
    int x,y,z,n;
};
bool place(int i,int j,int k){
    if((j==k&&i==0)||(i==k&&j==0)||(i==j&&k==0))
        return true;
    return false;
}
int BFS(int x,int y,int z){
    if(x%2!=0)
        return 0;
    queue<node>it;
    memset(temp,0,sizeof(temp));
    node o,e;
    e.x=x;
    e.y=0;
    e.z=0;
    e.n=0;
    it.push(e);  //
    temp[e.x][e.y][e.z]=1;
    while(!it.empty()){
        e=it.front();
        it.pop();
        int num;
        if(place(e.x,e.y,e.z)) {
            printf("%d\n",e.n);
            return 1;
        }
        if(e.x!=0){
            if(e.y<y){
                num=y-e.y;
                o.z=e.z;
                o.n=e.n+1;
                if(e.x>num){
                    o.x=e.x-num;
                    o.y=y;
                }
                else{
                    o.x=0;
                    o.y=e.x+e.y;
                }
                if(!temp[o.x][o.y][o.z]){
                    temp[o.x][o.y][o.z]=1;
                    it.push(o);
                }
            }
            if(e.z<z){
                num=z-e.z;
                o.y=e.y;
                o.n=e.n+1;
                if(e.x>num){
                    o.x=e.x-num;
                    o.z=z;
                }
                else{
                    o.x=0;
                    o.z=e.x+e.z;
                }
                if(!temp[o.x][o.y][o.z]){
                    temp[o.x][o.y][o.z]=1;
                    it.push(o);
                }
            }
        }
        if(e.y>0){
            if(e.x<x){
                num=x-e.x;
                o.z=e.z;
                o.n=e.n+1;
                if(e.y>num){
                    o.y=e.y-num;
                    o.x=x;
                }
                else{
                    o.y=0;
                    o.x=e.x+e.y;
                }
                if(!temp[o.x][o.y][o.z]){
                    temp[o.x][o.y][o.z]=1;
                    it.push(o);
                }
            }
            if(e.z<z){
                num=z-e.z;
                o.x=e.x;
                o.n=e.n+1;
                if(e.y>num){
                    o.y=e.y-num;
                    o.z=z;
                }
                else{
                    o.y=0;
                    o.z=e.y+e.z;
                }
                if(!temp[o.x][o.y][o.z]){
                    temp[o.x][o.y][o.z]=1;
                    it.push(o);
                }
            }
        }
        if(e.z>0){
            if(e.y<y){
                num=y-e.y;
                o.x=e.x;
                o.n=e.n+1;
                if(e.z>num){
                    o.z=e.z-num;
                    o.y=y;
                }
                else{
                    o.z=0;
                    o.y=e.z+e.y;
                }
                if(!temp[o.x][o.y][o.z]){
                    temp[o.x][o.y][o.z]=1;
                    it.push(o);
                }
            }
            if(e.x<x){
                num=x-e.x;
                o.y=e.y;
                o.n=e.n+1;
                if(e.z>num){
                    o.z=e.z-num;
                    o.x=x;
                }
                else{
                    o.z=0;
                    o.x=e.x+e.z;
                }
                if(!temp[o.x][o.y][o.z]){
                    temp[o.x][o.y][o.z]=1;
                    it.push(o);
                }
            }
        }
    }
    return 0;
}
int main(){
    while(scanf("%d %d %d",&S,&M,&N),S!=0||M!=0||N!=0){
        int flag=BFS(S,M,N);
        if(flag==0)
            printf("NO\n");
    }
    return 0;
}

这种思想很值得慢慢品味..

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值