Flood Fill Algorithm|Graph Theory|Connected Block

Flood Fill

Flood Fill algorithm (Chinese name: flood irrigation algorithm), mainly for grid graph calculation, find connected blocks.
Insert picture description here
We adopt two methods: BFS (wide search) and DFS (deep search). BFS is often used to find the shortest path, and DFS is more convenient to solve the floodfill problem.

acwing 1113. Red and Black

There is a rectangular house with square tiles in red and black on the floor.

You stand on one of the black tiles and can only move to the adjacent black tiles (up, down, left, and right).

Please write a program to calculate how many black tiles you can reach in total.

Input format

The input includes multiple data sets.

The first row of each data set is two integers WW and HH, which represent the number of tiles in the xx and yy directions, respectively.

In the following HH lines, each line includes WW characters. Each character represents the color of a tile, the rules are as follows

1)'.': black tiles;
2)'#': red tiles;
3)'@': black tiles, and you are standing on this tile. This character appears only once in each data set.

When two zeros are read in a row, it means that the input is over.

Output format

For each data set, output a row separately, showing the number of tiles you can reach from the initial position (including the tiles at the initial position when counting).

data range

1≤W,H≤201≤W,H≤20

Input sample:

6 9 
....#. 
.....# 
...... 
...... 
...... 
...... 
...... 
#@...# 
.#..#. 
0 0

Sample output:

45

answer:

DFS solution

//深搜写法
#include<iostream>
#include<algorithm>

#define x first
#define y second

using namespace std;

const int N = 25;

int n,m;
char g[N][N];
int dx[] = {
    
    -1,0,1,0}, dy[] = {
    
    0,1,0,-1};

int dfs(int x, int y)
{
    
    
    int res = 1;
    g[x][y] = '#';//将走过的标记为障碍物表示已经走过
    for(int i = 0; i < 4; i ++)
    {
    
    
        int a = x + dx[i], b = y + dy[i];
        if(a >= 0 && a < n && b >= 0 && b < m && g[a][b] == '.')
        {
    
    
            res += dfs(a,b);
        }
    }
    return res;
}

int main()
{
    
    
    while(cin >> m >> n, n || m)
    {
    
    
        for(int i = 0; i < n; i ++ )cin >> g[i];
        int x,y;
        for(int i = 0; i < n; i ++)
            for(int j = 0; j < m; j ++)
                if(g[i][j] == '@')
                {
    
    
                    x = i;y = j;
                }
        cout << dfs(x,y) << endl;
    }
    return 0;
}

BFS solution

//BFS宽搜写法
#include<iostream>
#include<queue>
#include<algorithm>

#define x first
#define y second

using namespace std;

typedef pair<int,int> PII;
const int N = 25;

int n,m;
char g[N][N];
int dx[] = {
    
    -1,0,1,0}, dy[] = {
    
    0,1,0,-1};//上右下左
int bfs(int sx, int sy)
{
    
    
    queue<PII> q;
    q.push({
    
    sx,sy}); //将起点放入队列中
    g[sx][sy] = '#';//起点走过了标记为障碍物表示不能再走
    int res = 0;//表示所有能够搜到的点的数量
    
    while(q.size())//当队列不空的时候,取队列头元素
    {
    
    
        auto t = q.front();
        q.pop();
        res++;//记录走过的点的个数
        
        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 >= m || g[x][y] != '.' )continue;
            g[x][y] = '#';//将该点设置为障碍物,表示已经走过
            q.push({
    
    x,y});//再将当点存进队列里,用作下一循环继续向四周扩散
            
        }    
    }
    return res;
}

int main()
{
    
    
    while(cin >> m >> n, n || m)
    {
    
    
        for(int i = 0; i < n; i ++)
                cin >> g[i];
        int x, y;
        for(int i = 0; i < n; i ++)
            for(int j = 0; j < m; j ++)
                if(g[i][j] == '@')
                {
    
    
                    x = i;y = j;
                }
        cout << bfs(x,y) << endl;
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/diviner_s/article/details/112594630