AcWing 1098. Castle problem (Flood Fill)

Topic links: Click here
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

Input P 15 P \leq 15 , the thinking about opening a bunch of pre-array lattice wall all around, binary representation is really clever.

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

#define r first
#define c second

using namespace std;
typedef pair<int,int> PII;
const int N = 55, M = N * N;

int dx[4] = {0, -1, 0, 1};
int dy[4] = {-1, 0, 1, 0};

int n, m;
int a[N][N];
bool st[N][N];
PII q[M];

int bfs(int x, int y)
{
    int hh = 0, tt = -1;
    
    int area = 0;
    
    q[++tt] = {x, y};
    st[x][y] = true;
    
    while(hh <= tt)
    {
        PII t = q[hh++];
        area++;
        
        for(int i = 0; i < 4; ++i)
        {
            int nx = t.r + dx[i], ny = t.c + dy[i];
            if(nx < 1 || nx > n || ny < 1 || ny > m)    continue;
            if(st[nx][ny])  continue;
            if(a[t.r][t.c] >> i & 1)    continue;
            
            q[++tt] = {nx, ny};
            st[nx][ny] = true;
        }
    }
    
    return area;
}

int main()
{
    scanf("%d%d", &n, &m);
    
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= m; ++j)
            scanf("%d", &a[i][j]);
            
    int cnt = 0;
    int maxx = 0;
    
    for(int i = 1; i <= n; ++i)
    {
        for(int j = 1; j <= m; ++j)
        {
            if(!st[i][j])
            {
                maxx = max(maxx, bfs(i, j));
                cnt++;
            }
        }
    }
    
    printf("%d\n%d\n", cnt, maxx);
    
    return 0;
}
Published 844 original articles · won praise 135 · Views 150,000 +

Guess you like

Origin blog.csdn.net/qq_42815188/article/details/105056665
Recommended