Advertisement
newb_ie

Untitled

Oct 31st, 2021
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. #include "bits/stdc++.h"
  2. using namespace std;
  3.  
  4. const int maxN = 501;
  5. char grid[maxN][maxN];
  6. bool visited[maxN][maxN];
  7.  
  8. int dx[4] = {1, 0, -1, 0};
  9. int dy[4] = {0, 1, 0, -1};
  10.  
  11.  
  12. void dfs (int i, int j, int n, int m) {
  13. //ith row jth col
  14. visited[i][j] = true;
  15. for (int k = 0; k < 4; ++k) {
  16. int ii = i + dx[k];
  17. int jj = j + dy[k];
  18. if (ii >= 1 and ii <= n and jj >= 1 and jj <= m and !visited[ii][jj] and (grid[ii][jj] == 'S' or grid[ii][jj] == 'W')) {
  19. dfs(ii, jj, n, m);
  20. }
  21. }
  22. }
  23.  
  24. int main () {
  25. ios::sync_with_stdio(false);
  26. cin.tie(nullptr);
  27. cout.tie(nullptr);
  28. int n, m;
  29. cin >> n >> m;
  30. for (int i = 1; i <= n; ++i) {
  31. for (int j = 1; j <= m; ++j) {
  32. cin >> grid[i][j];
  33. if (grid[i][j] == '.') grid[i][j] = 'D';
  34. }
  35. }
  36. for (int i = 1; i <= n; ++i) {
  37. for (int j = 1; j <= m; ++j) {
  38. if (grid[i][j] == 'W') {
  39. dfs(i, j, n, m);
  40. }
  41. }
  42. }
  43. bool yes = true;
  44. for (int i = 1; i <= n; ++i) {
  45. for (int j = 1; j <= m; ++j) {
  46. if (grid[i][j] == 'S' and visited[i][j]) {
  47. yes = false;
  48. break;
  49. }
  50. }
  51. }
  52. if (yes) {
  53. cout << "Yes\n";
  54. for (int i = 1; i <= n; ++i) {
  55. for (int j = 1; j <= m; ++j) {
  56. cout << grid[i][j];
  57. }
  58. cout << '\n';
  59. }
  60. } else {
  61. cout << "No\n";
  62. }
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement