zoukankan      html  css  js  c++  java
  • Catch the cat使用宽度搜索法BFS

    1.典型的宽度优先搜索;

    2.典型的队列使用问题;

    3.刚开始一直WA,原来是边界弄错了,切记,越界就是wa


    以下是代码:

    #include <iostream>
    #include <queue>
    #include <cstring>
    #include <stdio.h>
    
    using namespace std;
    
    int step[100010], flag[100010];
    queue <int> q;
    
    void bfs(int n, int k)
    {
        q.push(n);
        flag[n] = 1;
        while(q.size())
        {
            int x = q.front();
            q.pop();
            if(k == x)  break;
            if(x -1 >= 0 && !flag[x - 1])
            {
                step[x - 1] = step[x] + 1;
                q.push(x - 1);
                flag[x - 1] = 1;
            }
            if(x + 1 <= 100000 && !flag[x + 1])
            {
                step[x + 1] = step[x] + 1;
                q.push(x + 1);
                flag[x + 1] = 1;
            }
            if(x * 2 <= 100000 && !flag[x * 2])
            {
                step[x * 2] = step[x] + 1;
                q.push(x * 2);
                flag[x * 2] = 1;
            }
        }
    }
    
    int main()
    {
        int N, K;
        cin >> N >> K;
        memset(flag, 0,sizeof(flag));
        memset(step, 0,sizeof(step));
        bfs(N, K);
        cout << step[K] <<endl;
        return 0;
    }
    


  • 相关阅读:
    wifi应用领域
    wifi主要功能
    Wi-Fi技术原理
    自适应通信类型与基本原理
    自适应通信发展背景
    自适应通信
    无线通信技术
    无线通信的一些专业术语
    无线通信
    Bluetooth vs. Wi-Fi(IEEE 802.11)
  • 原文地址:https://www.cnblogs.com/dollarzhaole/p/3188938.html
Copyright © 2011-2022 走看看