HDU 1495 非常可乐

这篇博客详细解析了HDU 1495编程题‘非常可乐’的解题思路与算法实现,涵盖了问题的关键点和解决策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1495

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<queue>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 107;
int s, n, m;
int vis[MAXN][MAXN][MAXN];
int min_step = INF;
struct node
{
    int z, x, y, step;
    node(){}
    node(int _z, int _x, int _y ,int _step):z(_z), x(_x), y(_y), step(_step){}
};
void bfs()
{
    vis[s][0][0] = 1;
    queue<node> q;
    q.push(node(s, 0, 0, 0));
    while(!q.empty())
    {
        node now = q.front(); q.pop();
        int x = now.x, y = now.y, z = now.z;
        if((x == y && x == s/2) || (x == z && x == s/2) || (y == z && y == s/2))
        {
            min_step = min(min_step, now.step);
        }
        int nx, ny, nz;
        if(x != n)
        {
            if(z != 0)
            {
                nx = x + z - n;
                if(nx <= 0) nx = x + z, nz = 0 ; // z全倒入x,x仍没装满
                else nx = n, nz = z + x - n; // x装满了,且z有剩余
                if(!vis[nz][nx][y])
                {
                    vis[nz][nx][y] = 1;
                    q.push(node(nz, nx, y, now.step + 1));
                }

            }
            if(y != 0)
            {
                nx = x + y - n;
                if(nx <= 0) nx = x + y, ny = 0;
                else nx = n, ny = y + x - n;
                if(!vis[z][nx][ny])
                {
                    vis[z][nx][ny] = 1;
                    q.push(node(z, nx, ny, now.step + 1));
                }

            }
        }
        if(y != m)
        {
            if(z != 0)
            {
                ny = y + z - m;
                if(ny <= 0) ny = y + z, nz = 0;
                else ny = m, nz = z + y - m;
                if(!vis[nz][x][ny])
                {
                    vis[nz][x][ny] = 1;
                    q.push(node(nz, x, ny, now.step + 1));
                }

            }
            if(x != 0)
            {
                ny = x + y - m;
                if(ny <= 0) ny = x + y, nx = 0;
                else ny = m, nx = x + y - m;
                if(!vis[z][nx][ny])
                {
                    vis[z][nx][ny] = 1;
                    q.push(node(z,nx, ny, now.step + 1));
                }

            }
        }
        if(z != s)
        {
            if(x != 0)
            {
                nz = x + z;
                nx = 0;
                if(!vis[nz][nx][y])
                {
                    vis[nz][nx][y] = 1;
                    q.push(node(nz, nx, y, now.step + 1));
                }

            }
            if(y != 0)
            {
                nz = y + z;
                ny = 0;
                if(!vis[nz][x][ny])
                {
                    vis[nz][x][ny] = 1;
                    q.push(node(nz, x, ny, now.step + 1));
                }

            }
        }
        if(x != 0)
        {
            if(y != m)
            {
                ny = y + x - m;
                if(ny <= 0) ny = x + y, nx = 0;
                else ny = m, nx = x + y - m;
                if(!vis[z][nx][ny])
                {
                    vis[z][nx][ny] = 1;
                    q.push(node(z, nx, ny, now.step + 1));
                }

            }
            if(z != s)
            {
                nz = z + x;
                nx = 0;
                if(!vis[nz][nx][y])
                {
                    vis[nz][nx][y] = 1;
                    q.push(node(nz, nx, y, now.step + 1));
                }

            }
        }
        if(y != 0)
        {
            if(x != n)
            {
                nx = x + y - n;
                if(nx <= 0) nx = x + y, ny = 0;
                else nx = n, ny = x + y - n;
                if(!vis[z][nx][ny])
                {
                    vis[z][nx][ny] = 1;
                    q.push(node(z, nx, ny, now.step + 1));
                }

            }
            if(z != s)
            {
                nz = z + y;
                ny = 0;
                if(!vis[nz][x][ny])
                {
                    vis[nz][x][ny] = 1;
                    q.push(node(nz, x, ny, now.step + 1));
                }

            }
        }
        if(z != 0)
        {
            if(x != n)
            {
                nx = z + x - n;
                if(nx <= 0) nx = x + z, nz = 0;
                else nx = n, nz = x + z - n;
                if(!vis[nz][nx][y])
                {
                    vis[nz][nx][y] = 1;
                    q.push(node(nz, nx, y, now.step + 1));
                }

            }
            if(y != m)
            {
                ny = z + y - m;
                if(ny <= 0) ny = z + y, nz = 0;
                else ny = m, nz = z + y - m;
                if(!vis[nz][x][ny])
                {
                    vis[nz][x][ny] = 1;
                    q.push(node(nz, x, ny, now.step + 1));
                }

            }
        }
    }
}
int main()
{
    while(cin >> s >> n >> m && (m + n + s))
    {
        if(s % 2 == 1) // 奇数不可平分
        {
             cout <<"NO" << endl;
             continue;
        }

        min_step = INF;
        memset(vis, 0, sizeof(vis));
        bfs();
        if(min_step == INF) cout << "NO" << endl;
        else cout << min_step << endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值