[Luogu Brush Questions] Blue Bridge Cup Topic Breakthrough - Depth First Search - dfs (6)

Table of contents

Written in front:

Topic: Introduction to P1683 - Luogu | New Ecology of Computer Science Education (luogu.com.cn)

Title description:

Input format:

Output format:

Input sample:

Sample output:

Problem-solving ideas:

code:

AC !!!!!!!!!!

Write at the end:


Written in front:

How can I learn an algorithm well?

I personally think that systematic brushing of questions is particularly important,

Therefore, in order to learn depth-first search well, in order to use violent search to deal with the Blue Bridge Cup,

Without further ado, let's start quizzing right away!

Title: P1683 Introduction - Luogu | New Ecology of Computer Science Education (luogu.com.cn)

Title description:

Input format:

The first line contains two positive integers W and H, representing the width and length of the path, respectively.

The following H row is an H × W character matrix. Each character represents a tile.

Among them, . the safe brick # represents the unsafe brick and @ represents the first brick.

Output format:

Output a line containing only one number,

That is, the maximum number of bricks you can walk safely from the first brick (including the first brick).

Input sample:

11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........

Sample output:

59

Problem-solving ideas:

When we use depth-first search,

The first point to note is the order of the search,

Because we want to make sure,

We have written a recursive structure capable of traversing all cases.

(It is always good to be more familiar with the basic idea of ​​recursive search above)

 Next is the specific idea :

The idea of ​​this question is:

1. First traverse all the positions to find the starting point,

2. Then use that point @ as the starting point, search in four directions up, down, left, and right,

3. The number of searches is recorded once, and the searched position is marked to prevent repeated searches.

Continue to search:

4. After searching all locations, just return the number of records.

Then the following is the code implementation:

code:

//包常用头文件
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int n, m;

const int N = 30;

//用一个二维数组接收地图
char g[N][N];

//计数
int res = 0;

//记录偏移量
int q[] = {1, 0, -1, 0};
int w[] = {0, 1, 0, -1};

void dfs(int x, int y)
{
    //四个方位搜索
    for(int i = 0; i < 4; i++)
    {
        int a = x + q[i];
        int b = y + w[i];
        
        //如果到边界了,就不搜索
        if(a < 0 || b < 0 || a >= n || b >= m) continue;
        
        //如果不是'.'就不搜索
        if(g[a][b] != '.') continue;
        
        //搜索完就标记
        g[a][b] = '#';
        res++;
        dfs(a, b);
    }
}

int main()
{
    scanf("%d %d", &m, &n);
    
    //接收地图
    for(int i = 0; i < n; i++)
    {
        scanf("%s", g[i]);
    }
    
    //遍历地图,找到@
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            if(g[i][j] == '@')
            {
                //@作为第一个位置,res++
                res++;
                dfs(i, j);
                break;//只有一个起始位置,找到就直接出来
            }
        }
    }
    printf("%d", res);
    return 0;
}

AC !!!!!!!!!!

Write at the end:

The above is the content of this article, thank you for reading.

If you like this article, please like and comment, and write down your opinions.

If you want to learn programming with me, you might as well follow me, we will learn and grow together.

I will output more high-quality content in the future, welcome to watch.

Guess you like

Origin blog.csdn.net/Locky136/article/details/129581604