ブラシノート全体の検索BFSとディープ検索DFS

bfs

広範な検索の一般的な形式:
1.加重配列stを定義し
ます
。2 .キューの初期化3.while(キューは空ではありません)

  • チームの頭から要素を削除する
  • 条件を満たす要素を展開して接続し、キューに参加します
    4. C ++によって提供されるキューを使用できますが、キューをシミュレートするために構造体配列を定義することは複雑ではありません。

アルギオンに捧げる花束

コード1-キューの使用:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>

using namespace std;
#define x first
#define y second
typedef pair<int ,int> PII;
const int N = 210;

int r,c;
char maz[N][N];
int cnt[N][N];
bool st[N][N];


int bfs(PII start , PII end)
{
    queue<PII> q;
    int d[4] = {0 , 0, 1 ,-1} , f[4] = {1 , -1 , 0 , 0}; 
    memset(cnt , 0 ,sizeof cnt);
    memset(st , 0 , sizeof st);
    q.push(start);   st[start.x][start.y] = true;
    while(q.size())
    {
        PII t = q.front();
        q.pop();
       
        for(int i = 0 ; i < 4; i++)
        {
             int x = t.x + d[i], y = t.y + f[i];
             //边界判断
             if(x < 0 || x >= r || y < 0 || y >= c)   continue;
             //如果已经访问或者是墙
             if(st[x][y] || maz[x][y] == '#')    continue;
             
             cnt[x][y] = cnt[t.x][t.y] + 1;
             st[x][y] = true;
             if(x == end.x && y == end.y)   return cnt[x][y];
             q.push({x,y});
        }
    }
    return -1;
}

int main(void)
{
    int t;
    cin >> t;
    PII start, end;
    
    while(t--)
    {
        cin >> r >> c;
        for(int i = 0; i < r; i++)  scanf("%s" ,maz[i]);
        
        for(int i = 0; i < r; i++)
            for(int j = 0; j < c; j++)
                if(maz[i][j] == 'E')   start = {i , j};
                else if(maz[i][j] == 'S') end = {i , j};
        
        int ans = bfs(start , end);
        if(ans != -1)   printf("%d\n",ans);
        else    printf("oop!\n");
    }
}

コード2-シミュレートされたキュー

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;
#define x first
#define y second
typedef pair<int ,int> PII;
const int N = 210;

int r,c;
char maz[N][N];
int cnt[N][N];
bool st[N][N];


int bfs(PII start , PII end)
{
    //如果不每一次重新定义queue,那么上一次的会残留
    PII q[r * c];
    int d[4] = {0 , 0, 1 ,-1} , f[4] = {1 , -1 , 0 , 0}; 
    memset(cnt , 0 ,sizeof cnt);
    memset(st , 0 , sizeof st);
    int hh = 0,tt = 0;
    q[hh] = start;
    st[start.x][start.y] = true;
    
    while(hh <= tt)
    {
        //pop的返回值是void ,所以需要使用front
        PII t = q[hh++];
       
        for(int i = 0 ; i < 4; i++)
        {
             int x = t.x + d[i], y = t.y + f[i];
             //边界检查
             if(x < 0 || x >= r || y < 0 || y >= c)   continue;
             //是否已经访问,是否为墙
             if(st[x][y] || maz[x][y] == '#')    continue;
             
             
             cnt[x][y] = cnt[t.x][t.y] + 1;
             st[x][y] = true;
             if(x == end.x && y == end.y)   return cnt[x][y];
             q[++tt] = {x, y};
        }
    }
    return -1;
}

int main(void)
{
    int t;
    cin >> t;
    PII start, end;
    
    while(t--)
    {
        cin >> r >> c;
        for(int i = 0; i < r; i++)  scanf("%s" ,maz[i]);
        
        for(int i = 0; i < r; i++)
            for(int j = 0; j < c; j++)
                if(maz[i][j] == 'E')   start = {i , j};
                else if(maz[i][j] == 'S') end = {i , j};
        
        int ans = bfs(start , end);
        if(ans != -1)   printf("%d\n",ans);
        else    printf("oop!\n");
    }
}

dfs

一般的に、ディープサーチはソリューションを見つけることができますが、ワイドサーチはすべてのソリューションを横断します。たとえば、最短経路を検索する場合、ワイド検索を使用することをお勧めします(ディープ検索も可能ですが、面倒です)
ワイド検索はスタックをバーストしませんが、ディープ検索は可能です
が、ディープ検索のコードは比較的単純です

地球温暖化

コード1 -dfs

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;
const int N = 1010;

int d[4] = {-1 , 0 , 1 , 0} , f[4] = {0 , 1 , 0 , -1};
char g[N][N];
bool st[N][N];
int n;
int ch;
void dfs(int x ,int y)
{
    g[x][y] = '!';
    bool is = true;
    for(int i = 0 ; i < 4; i++)
    {
        int a = x + d[i] , b = y + f[i];
        if(a < 0 || a >= n || b < 0 || b >= n)  continue;
        if(g[a][b] == '.')      is = false;
        if(g[a][b] == '#')      dfs(a , b);
    }
    if(is)   ch = 1;
}

int main(void)
{
    cin >> n;
    
    for(int i = 0; i < n; i++)    scanf("%s" , g[i]);
    
    bool is;
    int cnt = 0;
    for(int i = 0; i < n ; i++)
        for(int j = 0; j < n; j++)
            if(g[i][j] == '#')
            {
                    dfs(i , j);
                    if(!ch) cnt++;
                    ch = 0;
            }
    cout << cnt << endl;
    
    return 0;
}

コード2 -bfs

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

#define x first
#define y second

using namespace std;

typedef pair<int, int> PII;

const int N = 1010;

int n;
char g[N][N];
bool st[N][N];
PII q[N * N];
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};

void bfs(int sx, int sy, int &total, int &bound)
{
    int hh = 0, tt = 0;
    q[0] = {sx, sy};
    st[sx][sy] = true;

    while (hh <= tt)
    {
        PII t = q[hh ++ ];

        total ++ ;
        bool is_bound = false;
        for (int i = 0; i < 4; i ++ )
        {
            int x = t.x + dx[i], y = t.y + dy[i];
            if (x < 0 || x >= n || y < 0 || y >= n) continue;  // 出界
            if (st[x][y]) continue;
            if (g[x][y] == '.')
            {
                is_bound = true;
                continue;
            }

            q[ ++ tt] = {x, y};
            st[x][y] = true;
        }

        if (is_bound) bound ++ ;
    }
}

int main()
{
    scanf("%d", &n);

    for (int i = 0; i < n; i ++ ) scanf("%s", g[i]);

    int cnt = 0;
    for (int i = 0; i < n; i ++ )
        for (int j = 0; j < n; j ++ )
            if (!st[i][j] && g[i][j] == '#')
            {
                int total = 0, bound = 0;
                bfs(i, j, total, bound);
                if (total == bound) cnt ++ ;
            }

    printf("%d\n", cnt);

    return 0;
}

おすすめ

転載: www.cnblogs.com/zy200128/p/12675972.html