Little D and Super BNB (Niu Ke)

Link: Login—Professional IT written test interview preparation platform_Niuke.com
Source: Niuke.com
 

topic description

Little DDD woke up one day and found that he had traveled to the world of the mini game "Super Bubble Hall". He got a map, a skill and three hints.

This map has a total of nnn rows and mmm columns. The rows are numbered from 111 to nnn from top to bottom, and the columns are numbered from 111 to mmm from left to right. There are open spaces, stones, and weeds on the map.
 

This skill is to place a bomb at your current location that will not affect you. When the bomb explodes, it will generate flames, which will spread in four directions: up, down, left, and right. If the flame spreads in an open space, it will directly spread to the open space. And it starts to spread in the open space. If the flame spreads in the direction of weeds, it will burn the weeds and turn it into an open space, and continue to spread in the newly created open space. If the flame spreads in the direction of stones, it cannot Continue to spread in this direction. Of course, it is impossible for the flames to spread beyond the map.


The three prompts are:

1. You can move in four directions, up, down, left, and right, if there are no stones in that direction and you will not move out of the map.

2. Your skills can only be used once.

3. You can return to the real world only if you answer correctly how much weedy land can be turned into open space by using skills at most.
 

Little DDD really wants to go back to the real world to play Yuanshin, please help him calculate how much weedy land he can turn into open space by using skills at most.

 Note: When the flame spreads to another place and will continue to spread, the spread method is equivalent to re-placing the bomb in that place. \textbf{ Note: When the flame spreads to another place and will continue to spread, the spread method is equivalent to replanting the bomb in that place. } Note: When the flame spreads to another place and will continue to spread, the spread method is equivalent to re-placing the bomb in that place. 

Enter a description:

 
 

The first line contains two integers n, m(1≤n, m≤1000)n, m(1\leq n, m\leq 1000)n, m(1≤n, m≤1000).

The next nnn lines have mmm characters per line, where '.' means open space, '#' means stone, '!' means weeds, and '@' means the current location of the small DDD.

Output description:

The output line contains an integer indicating how much grass he can turn into open space by using skills.

Example 1

enter

Copy 4 4 ..!. .@.# !##! #!!!

4 4
..!.
.@.#
!##!
#!!!

output

copy 2

2
#include <iostream>
using namespace std;
static char A[1000][1000];
int cnt = 0;
void dfs(char A[1000][1000],int i,int j,int n,int m){
    if(i<0||j<0||i>=n||j>=m||A[i][j]=='#'||A[i][j]=='F'){
        return ;
    }
    if(A[i][j]=='!'){
        cnt++;
        A[i][j] = 'F';
    }
    A[i][j] = 'F';
    dfs(A,i-1,j,n,m);
    dfs(A,i,j-1,n,m);
    dfs(A,i+1,j,n,m);
    dfs(A,i,j+1,n,m);
}
int main(){
    int n,m;
    cin>>n>>m;
    for(int i = 0;i < n; i++){
        for(int j = 0;j < m; j++){
            cin>>A[i][j];
        }
    }
    for(int i = 0;i < n; i++){
        for(int j = 0;j < m; j++){
            if(A[i][j]=='@'){
                dfs(A,i,j,n,m);
            }
        }
    }
    cout<<cnt;
    return 0;
}

dfs solution.

Guess you like

Origin blog.csdn.net/qq_63499305/article/details/130020423