Pots 广度优先搜索

啥都不说。现附上代码(借鉴了一个学长“莫若诩殇”, 顿时感觉明白了)

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <string>
#include <memory.h>
#define N 1010
struct node
{
    int x, y, step;
};

int main()
{
    int a, b, c;
    while(~scanf("%d %d %d", &a, &b, &c))
    {
        node que[N], q, p;
        int book[N][N];
        memset(book, 0, sizeof(book));
        int head = 0, tail = 0;
        p.x = 0;
        p.y = 0;
        p.step = 0;
        que[tail] = p;
        tail++;
        int flag = 0;
        while(head < tail)
        {
            q = que[head];//父节点
            if(q.x == c || q.y == c)
            {
                flag = 1;
                printf("%d\n", q.step);
                break;
            }
            int i;
            for(i = 0; i < 6; i++)
            {
                if(i == 0)//满A
                {
                    p.x = q.x;
                    p.y = b;
                }
                else if(i == 1)//满B
                {
                    p.x = a;
                    p.y = q.y;
                }
                else if(i == 2)//清A
                {
                    p.x = 0;
                    p.y = q.y;
                }
                else if(i == 3)//q;B
                {
                    p.x = q.x;
                    p.y = 0;
                }
                else if(i == 4)//A -> B
                {
                    p.y = q.y + q.x;
                    if(p.y > b)
                    {
                        p.y = b;
                        p.x = q.x - (b - q.y);
                    }
                    else
                    {
                        p.x = 0;
                    }
                }
                else//B -> A
                {
                    p.x = q.x + q.y;
                    if(p.x > a)
                    {
                        p.x = a;
                        p.y = q.y - (a - q.x);
                    }
                    else
                    {
                        p.y = 0;
                    }
                }
                if(book[p.x][p.y] == 0)//不是以前走过的,否则就多走了
                {
                    book[p.x][p.y] = 1;
                    p.step = q.step + 1;
                    que[tail++] = p;
                }
            }
            head++;
        }
        if(!flag)
        {
            printf("impossible\n");
        }
    }
}
就那六步 just so so
其实这个和广度优先非常相似,可能完全一样吧;
原来不知道如何储存这些步骤,一昧的只找简单的步数,哎,没找到;
还是这个比较好!!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值