Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "bits/stdc++.h"
- using namespace std;
- const int maxN = 501;
- char grid[maxN][maxN];
- bool visited[maxN][maxN];
- int dx[4] = {1, 0, -1, 0};
- int dy[4] = {0, 1, 0, -1};
- void dfs (int i, int j, int n, int m) {
- //ith row jth col
- visited[i][j] = true;
- for (int k = 0; k < 4; ++k) {
- int ii = i + dx[k];
- int jj = j + dy[k];
- 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')) {
- dfs(ii, jj, n, m);
- }
- }
- }
- int main () {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- int n, m;
- cin >> n >> m;
- for (int i = 1; i <= n; ++i) {
- for (int j = 1; j <= m; ++j) {
- cin >> grid[i][j];
- if (grid[i][j] == '.') grid[i][j] = 'D';
- }
- }
- for (int i = 1; i <= n; ++i) {
- for (int j = 1; j <= m; ++j) {
- if (grid[i][j] == 'W') {
- dfs(i, j, n, m);
- }
- }
- }
- bool yes = true;
- for (int i = 1; i <= n; ++i) {
- for (int j = 1; j <= m; ++j) {
- if (grid[i][j] == 'S' and visited[i][j]) {
- yes = false;
- break;
- }
- }
- }
- if (yes) {
- cout << "Yes\n";
- for (int i = 1; i <= n; ++i) {
- for (int j = 1; j <= m; ++j) {
- cout << grid[i][j];
- }
- cout << '\n';
- }
- } else {
- cout << "No\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement