7/12 每日打卡 bfs(广度优先搜索)

今天晚上要打cf,所以就少写点题。写了三道bfs的板子题。我是用的普通的队列实现的bfs,之后会更新优先队列优化的bfs(等我学到了再说)。

hdu 1548 A strange lift  http://acm.hdu.edu.cn/showproblem.php?pid=1548

 没什么可说的,把电梯看成一条线就好了,然后用bfs搜索一下。

#include <bits/stdc++.h>

using namespace std;
int n, a, b;
int num[205];
int vis[205];
struct node
{
    int level;
    int count;
};
bool ok(int x)
{
    if (x < 1 || x > n)
        return false;
    return true;
}
void bfs()
{
    memset(vis, 0, sizeof(vis));
    struct node cur, nex;
    cur.level = a;
    cur.count = 0;
    vis[cur.level] = 1;
    queue<node> qu;
    qu.push(cur);
    while (!qu.empty())
    {
        cur = qu.front();
        qu.pop();
        if (cur.level == b)
        {
            cout << cur.count << endl;
            return;
        }
        else
        {
            for (int i = 1; i <= 2; i++)
            {
                if (i == 1)
                {
                    nex.level = cur.level + num[cur.level];
                    nex.count = cur.count + 1;
                }
                if (i == 2)
                {
                    nex.level = cur.level - num[cur.level];
                    nex.count = cur.count + 1;
                }
                if (ok(nex.level) && !vis[nex.level])
                {
                    vis[nex.level] = 1;
                    qu.push(nex);
                }
            }
        }
    }
    puts("-1");
}

int main()
{

    while (cin >> n, n)
    {
        cin >> a >> b;
        memset(num, 0, sizeof(num));
        for (int i = 1; i <= n; i++)
        {
            cin >> num[i];
        }
        bfs();
    }
}

hdu 1372 knight moves http://acm.hdu.edu.cn/showproblem.php?pid=1372

 这题是bfs的裸题,入门必做啊。国际象棋里的骑士,就和马一样,马走日。

不过我用方向数组来存储马走日的八种情况,会莫名的wa,最后忍无可忍一个一个写了,写的就很丑。。。

#include <bits/stdc++.h>
using namespace std;
char vis[10][10];
struct node
{
    char x;
    int y;
    int steps;
};
char a1, a2;
int b1, b2;
void bfs()
{
    memset(vis, 0, sizeof(vis));
    queue<node> qu;
    struct node sur, nex;
    sur.x = a1;
    sur.y = b1;
    vis[sur.x][sur.y] = 1;
    sur.steps = 0;
    qu.push(sur);
    while (!qu.empty())
    {
        sur = qu.front();
        qu.pop();

        if (sur.x == a2 && sur.y == b2)
        {
            printf("To get from %c%d to %c%d takes %d knight moves.\n", a1, b1, a2, b2, sur.steps);
            return;
        }
        for (int i = 1; i <= 8; i++)
        {
            if (i == 1)
            {
                nex.x = sur.x + 2;
                nex.y = sur.y + 1;
            }
            if (i == 2)
            {
                nex.x = sur.x + 2;
                nex.y = sur.y - 1;
            }
            if (i == 3)
            {
                nex.x = sur.x - 2;
                nex.y = sur.y + 1;
            }
            if (i == 4)
            {
                nex.x = sur.x - 2;
                nex.y = sur.y - 1;
            }
            if (i == 5)
            {
                nex.x = sur.x + 1;
                nex.y = sur.y + 2;
            }
            if (i == 6)
            {
                nex.x = sur.x - 1;
                nex.y = sur.y + 2;
            }
            if (i == 7)
            {
                nex.x = sur.x - 1;
                nex.y = sur.y - 2;
            }
            if (i == 8)
            {
                nex.x = sur.x + 1;
                nex.y = sur.y - 2;
            }

            if (!vis[nex.x - 'a' + 1][nex.y] && nex.x <= 'h' && nex.x >= 'a' && nex.y >= 1 && nex.y <= 8)
            {
                nex.steps = sur.steps + 1;
                qu.push(nex);
                vis[nex.x][nex.y] = 1;
            }
        }
    }
    return;
}
int main()
{

    while (scanf("%c%d %c%d", &a1, &b1, &a2, &b2) != EOF)
    {
        bfs();
        // getchar();
    }
}

hdu 1495 非常可乐 Problem - 1495

这个题就是因为杯子没有刻度,所以每次倒可乐都要把一个杯子倒满或者倒空,然后用三个杯子里的可乐数量定义状态和标记数组。也是非常基础的bfs。

// Problem: 非常可乐
// Contest: HDOJ
// URL: http://acm.hdu.edu.cn/showproblem.php?pid=1495
// Memory Limit: 32 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>

using namespace std;

int s, n, m;
int vis[105][105][105];
struct node
{
    int as;
    int an;
    int am;
    int count;
};
bool check(node a)
{
    if ((a.as == a.an && a.am == 0) || (a.as == a.am && a.an == 0) || (a.an == a.am) && a.as == 0)
        return true;
    return false;
}
bool ok(node a)
{
    if (a.as > s || a.as < 0 || a.an > n || a.an < 0 || a.am > m || a.am < 0)
        return false;
    return true;
}
void bfs()
{
    memset(vis, 0, sizeof(vis));
    int count = 0;
    struct node cur, nex;
    cur.as = s;
    cur.an = 0;
    cur.am = 0;
    cur.count = 0;
    vis[cur.as][cur.an][cur.am] = 1;
    queue<node> qu;
    qu.push(cur);
    while (!qu.empty())
    {
        cur = qu.front();
        qu.pop();
        if (check(cur))
        {
            cout << cur.count << endl;
            return;
        }
        else
        {
            for (int i = 1; i <= 6; i++)
            {
                if (i == 1)
                {
                    if (cur.as > n - cur.an)
                    {
                        nex.an = n;
                        nex.as = cur.as + cur.an - n;
                        nex.am = cur.am;
                    }
                    else
                    {
                        nex.am = cur.am;
                        nex.an = cur.an + cur.as;
                        nex.as = 0;
                    }
                }
                if (i == 2)
                {
                    if (cur.as > m - cur.am)
                    {
                        nex.an = cur.an;
                        nex.am = m;
                        nex.as = cur.as + cur.am - m;
                    }
                    else
                    {
                        nex.an = cur.an;
                        nex.am = cur.am + cur.as;
                        nex.as = 0;
                    }
                }
                if (i == 3)
                {
                    if (cur.an > s - cur.as)
                    {
                        nex.am = cur.am;
                        nex.as = s;
                        nex.an = cur.an + cur.as - s;
                    }
                    else
                    {
                        nex.am = cur.am;
                        nex.as = cur.as + cur.an;
                        nex.an = 0;
                    }
                }
                if (i == 4)
                {
                    if (cur.an > m - cur.am)
                    {
                        nex.as = cur.as;
                        nex.am = m;
                        nex.an = cur.an - m + cur.am;
                    }
                    else
                    {
                        nex.as = cur.as;
                        nex.an = 0;
                        nex.am = cur.am + cur.an;
                    }
                }
                if (i == 5)
                {
                    if (cur.am > s - cur.as)
                    {
                        nex.an = cur.an;
                        nex.as = s;
                        nex.am = cur.am + cur.as - s;
                    }
                    else
                    {
                        nex.an = cur.an;
                        nex.am = 0;
                        nex.as = cur.as + cur.am;
                    }
                }
                if (i == 6)
                {
                    if (cur.am > n - cur.an)
                    {
                        nex.as = cur.as;
                        nex.an = n;
                        nex.am = cur.am + cur.an - n;
                    }
                    else
                    {
                        nex.as = cur.as;
                        nex.am = 0;
                        nex.an = cur.an + cur.am;
                    }
                }
                if (ok(nex) && !vis[nex.as][nex.an][nex.am])
                {
                    nex.count = cur.count + 1;
                    vis[nex.as][nex.an][nex.am] = 1;
                    qu.push(nex);
                }
            }
        }
    }
    puts("NO");
    return;
}

int main()
{
    while (cin >> s >> n >> m, s + n + m) // s==n+m
    {
        bfs();
    }
}

过两天学一下优先队列优化的bfs,这种裸的bfs时间复杂度还是高了点,新生赛的时候用裸的bfs就超时了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值