shawon_majid

DS contest 6 problem F-E

Jun 20th, 2021 (edited)
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. //Bismillahir Rahman-ir Rahim
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. #define debug(x) cout << '>' << #x << " : " << x << endl;
  5. #define all(c) c.begin(), c.end()
  6. #define F first
  7. #define S second
  8. typedef unsigned long long ull;
  9. typedef long long ll;
  10.  
  11. int dx[] = {1, -1, 0, 0};
  12. int dy[] = {0, 0, 1, -1};
  13.  
  14. char mat[5000][5000];
  15.  
  16. bool valid(int x, int y, int n, int m){
  17.     if(x >= 0 and x <= n and y >= 0 and y <= m and mat[x][y] != '#' and mat[x][y] != 'X') return 1;
  18.     return 0;
  19. }
  20.  
  21. int main() {
  22.  
  23.     freopen("input.txt", "r", stdin);
  24.     freopen("output.txt", "w", stdout);
  25.  
  26.     int n, m, d;
  27.     cin >> n >> m >> d;
  28.    
  29.     for(int i = 0; i < n; i++){
  30.         string str;
  31.         cin >> str;
  32.         for(int j = 0; j < m; j++){
  33.             mat[i][j] = str[j];
  34.         }
  35.     }
  36.     bool br = 0;
  37.     for(int i = 0; i < n; i++){
  38.         for(int j = 0; j < m; j++){
  39.             if(d <= 0){
  40.                 br = 1;
  41.                 break;
  42.             }
  43.             if(mat[i][j] == '.'){
  44.                 mat[i][j] = 'X';
  45.                 bool ok = 1;
  46.                 //pair <int,int> cur = {i, j};
  47.  
  48.                 //go to the neighbours of i ,j
  49.  
  50.                 for(int k = 0; k < 4; k++){
  51.                     if(valid(i+dx[k], j+dy[k], n, m)){
  52.                         // check if it's blocked
  53.                         int curi = i+dx[k], curj = j+dy[k];
  54.                         bool blocked = 1;
  55.                         for(int l = 0; l < 4; l++){
  56.                             if(valid(curi + dx[l], curj + dy[l], n, m)){
  57.                                 blocked = 0;
  58.                                 break;
  59.                             }
  60.                         }
  61.                         if(blocked){
  62.                             ok = 0;
  63.                             break;
  64.                         }
  65.                     }
  66.                 }
  67.  
  68.  
  69.                 if(!ok){
  70.                     mat[i][j] = '.';
  71.                 }
  72.                 else{
  73.                     d--;
  74.                 }
  75.             }
  76.         }
  77.         if(br) break;
  78.     }    
  79.     for(int i = 0; i < n; i++){
  80.         for(int j = 0; j < m; j++){
  81.             cout << mat[i][j];
  82.         }
  83.         cout << endl;
  84.     }
  85.     cout << endl;
  86.  
  87.     return 0;
  88. }
  89.  
Add Comment
Please, Sign In to add comment