378C - Maze - CodeForces Solution


dfs and similar *1600

Please click on ads to support us..

C++ Code:

#include <iostream>
using namespace std;

char maze[500][500];
int vis[500][500];
//Add a wall if there is just one neibour cell
//NO

//Just visit s-k cells. s is the empty cells then. the remaining would be walls. 

int n, m, k, s, cnt = 0;
bool isOut(int i, int j){
    return (i >= n) || (j >= m) || (j < 0) || (i < 0) ? 1: 0;
}

void DFS(int i, int j){
    if(maze[i][j] == '#' || isOut(i, j) || vis[i][j] || cnt == s-k) 
        return;
    vis[i][j] = 1;
    cnt++;
    DFS(i+1, j);
    DFS(i-1, j);
    DFS(i, j+1);
    DFS(i, j-1);
}


int main(){
    cin >> n >> m >> k;
    s = n*m;
    int x = -1, y = -1;
    for (size_t i = 0; i < n; i++)
    {
        for (size_t j = 0; j < m; j++)
        {   
            vis[i][j] = 0;
            cin >> maze[i][j];
            if(maze[i][j] == '#'){
                s--;
            }else{
                if(x == -1 && y == -1)
                    x = i, y = j;
            }
        }
    }
    DFS(x, y);
    for (size_t i = 0; i < n; i++)
    {
        for (size_t j = 0; j < m; j++)
        {
            if(maze[i][j] != '#' && !vis[i][j])
                cout << 'X';
            else   
                cout << maze[i][j];
        }
        cout << "\n";
    }
    
    return 0;
}


Comments

Submit
0 Comments
More Questions

1676D - X-Sum
1679A - AvtoBus
1549A - Gregor and Cryptography
918C - The Monster
4B - Before an Exam
545B - Equidistant String
1244C - The Football Season
1696B - NIT Destroys the Universe
1674A - Number Transformation
1244E - Minimizing Difference
1688A - Cirno's Perfect Bitmasks Classroom
219A - k-String
952A - Quirky Quantifiers
451B - Sort the Array
1505H - L BREAK into program
171E - MYSTERIOUS LANGUAGE
630D - Hexagons
1690D - Black and White Stripe
1688D - The Enchanted Forest
1674C - Infinite Replacement
712A - Memory and Crow
1676C - Most Similar Words
1681A - Game with Cards
151C - Win or Freeze
1585A - Life of a Flower
1662A - Organizing SWERC
466C - Number of Ways
1146A - Love "A"
1618D - Array and Operations
1255A - Changing Volume