bfs 36分求调整
查看原帖
bfs 36分求调整
869729
lixxyu11楼主2023/4/6 12:00
#include <queue>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 1010;
char a[N][N];
bool st[N][N];
int dx[4] = {0, 0, -1, 1};
int dy[4] = {1, -1, 0, 0};

int n, res, nx, ny;
int total, bound;
bool bound_is;
typedef pair<int, int> PII;
queue<PII> q;
void bfs(int x, int y, int total, int bound)
{
   
    q.emplace(x, y);
    while (q.size())
    {
        auto c = q.front();
        q.pop();
        int sx = c.first;
        int sy = c.second;
        total++;
        st[sx][sy] = true;
        bound_is=false;
        for (int i = 0; i < 4; i++)
        {
            nx = sx + dx[i];
            ny = sy + dy[i];
            if (nx < 0 || nx >= n || ny < 0 || ny >= n)
                continue;
            if (a[nx][ny] == '.')
            {
                bound_is = true;
                continue;
            }
            if (!st[nx][ny])
            {
                
                q.emplace(nx, ny);
            }
        }
        if (bound_is == true)
            bound++;
    }
    if (bound == total)
        res++;
}

int main()
{
    cin >> n;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            cin >> a[i][j];

    
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (!st[i][j] && a[i][j] == '#')
            {
                total = 0, bound = 0;
                bfs(i, j, total, bound);
            }
        }
    }
    cout << res;
    return 0;
}
2023/4/6 12:00
加载中...