-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdfs.cpp
executable file
·103 lines (87 loc) · 2.51 KB
/
dfs.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <stack>
using namespace std;
using pair_t = pair<short, short>;
using matrix_t = vector<vector<char>>;
bool dfs(int& dots, matrix_t& matrix, int i, int j, int rows, int cols) {
stack<pair_t> s;
s.push(pair_t{i, j});
while (!s.empty()) {
auto p = s.top();
s.pop();
auto x = p.first;
auto y = p.second;
if (matrix[x][y] == '#' || matrix[x][y] == '*') {
continue;
}
--dots;
matrix[x][y] = '*';
if (x > 0) {
s.push(pair_t{x - 1, y});
}
if (x < rows - 1) {
s.push(pair_t{x + 1, y});
}
if (y > 0) {
s.push(pair_t{x, y - 1});
}
if (y < cols - 1) {
s.push(pair_t{x, y + 1});
}
}
return dots == 0;
}
// Another implementation
void fill(vector<vector<char>>& grid, const int& x, const int& y) {
const char visited_symbol = 'x';
int width = grid.size();
int height = grid[0].size();
queue<pair<int,int>> q;
q.emplace(x, y);
while (!q.empty()) {
int i = q.front().first;
int j = q.front().second;
q.pop();
if (grid[i][j] != visited_symbol) {
grid[i][j] = visited_symbol;
if (i + 1 < width && grid[i + 1][j] == '1')
q.emplace(i + 1, j);
if (j + 1 < height && grid[i][j + 1] == '1')
q.emplace(i, j + 1);
if (i - 1 >= 0 && grid[i - 1][j] == '1')
q.emplace(i - 1, j);
if (j - 1 >= 0 && grid[i][j - 1] == '1')
q.emplace(i, j - 1);
}
}
}
int main() {
int rows, cols;
cin >> rows >> cols;
matrix_t matrix;
matrix.resize(rows);
int dots = 0;
for (auto& row : matrix) {
row.reserve(cols);
copy_n(istream_iterator<char>(cin), cols, back_inserter(row));
for (auto& col : row) {
if (col == '.') {++dots;}
}
}
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
if (matrix[i][j] == '.') {
if (dfs(dots, matrix, i, j, rows, cols)) {
cout << "Yes";
return 0;
}
cout << "No";
return 0;
}
}
}
return 0;
}