Codeforces 1365D Solve The Maze

Time:2021-10-18

Main idea of the title:

In a\(n * m\)In the matrix, there are open spaces, bad people, good people and walls. You can turn the open space into a wall to block the bad guys.\((n, m)\)For export, is there a scheme that enables all good people in the matrix to go to the export, while all bad people cannot go through the export, and the corresponding output\(Yes\)and\(No\)

Idea:

1. Preprocessing: if the bad person is adjacent to the good person, the bad person can go to the good person next door, and then go to the end through the path of the good person. Therefore, if it does not meet the requirements, it will be output\(No\);

​ If the current square is a bad person, we have to block him all around, so that he won’t go to the exit, turning the surrounding open space into a wall.

2. Imagine how to judge whether good people can get to the exit one by one. Is it too troublesome? On the contrary, judge the exit\((n, m)\)You only have to do it once to get to the good people’s squares\(bfs\)perhaps\(dfs\), mark it if you can go.

code:

#include 
using namespace std;
typedef pair PII;
const int N = 55;
 
int n, m;
char a[N][N];
bool st[N][N];
int flag = 1, dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
 
void dfs(int sx, int sy){
    for(int i = 0; i < 4; i++){
        int x = sx + dx[i], y = sy + dy[i];
        if(x >= 1 && x <= n && y >= 1 && y <= m){
            if(a[x][y] == 'G') flag = 0;
            if(a[x][y] == '.') a[x][y] = '#';
        }
       
    }
}
 
void bfs(int sx, int sy){
     queue q;
     q.push({sx, sy});
     while(q.size()){
         PII t = q.front();
         q.pop();
         for(int i = 0; i < 4; i++){
             int x = t.first + dx[i], y = t.second + dy[i];
             if(a[x][y] == '#' || st[x][y]) continue;
             if(x >= 1 && x <= n && y >= 1 && y <= m){
                st[x][y] = true;
                q.push({x, y});
             }
         }
 
     }   
}
 
int main(){
    int T;
    cin >> T;
    while(T--){
        flag = 1;
        cin >> n >> m;
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= m; j++)
                cin >> a[i][j];
 
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= m; j++){
                if(a[i][j] == 'B')
                    dfs(i, j);
            }
        memset(st, 0, sizeof st);
        if(a[n][m] != '#') bfs(n, m);
 
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= m; j++){
                if(a[i][j] == 'G'){
                    if(st[i][j] == false) flag = 0;
                }
            }
 
        if(flag) cout << "Yes" << endl;
        else cout << "No" << endl;
 
    }
    return 0;
}

Recommended Today

The VBA ide displays a project that is not open. The solution cannot be operated

Reading time |0.24 minutes word count |388 characters primary coverage |1. Introduction & background “VBA ide displays the project that is not open, and the solution cannot be operated” Author | SCscHero Writing time | 2021/12/29 PM7:28 Article type |Series Degree of completion |Completed motto Every great cause has a trivial beginning. 1、 Introduction & […]