Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <map>
- #include <cmath>
- #include <set>
- #include <vector>
- #include <queue>
- using namespace std;
- const int maxn = 1050;
- char mat[maxn][maxn];
- int main() {
- ios_base::sync_with_stdio(false);
- int n, m, k;
- cin >> n >> m >> k;
- int si, sj, ei, ej;
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < m; j++) {
- cin >> mat[i][j];
- if(mat[i][j] == 'M') {
- si = i;
- sj = j;
- }
- if(mat[i][j] == 'V') {
- ei = i;
- ej = j;
- }
- }
- }
- mat[si][sj] = '.';
- mat[ei][ej] = '.';
- int di[] = {-1, 1, 0, 0};
- int dj[] = {0, 0, -1, 1};
- vector<vector<bool> > visited(n + 1, vector<bool> (m + 1, false));
- visited[si][sj] = true;
- queue<int> q;
- q.push(si);
- q.push(sj);
- vector<pair<int, int> > points_from_walls;
- while(!q.empty()) {
- int ci = q.front(); q.pop();
- int cj = q.front(); q.pop();
- if(ci == ei and cj == ej) {
- cout << "DA" << endl;
- return 0;
- }
- for(int i = 0; i < 4; i++) {
- int ti = ci + di[i];
- int tj = cj + dj[i];
- if(ti >= 0 and ti < n and tj >= 0 and tj < m and !visited[ti][tj]) {
- if(mat[ti][tj] == '#') {
- points_from_walls.push_back(make_pair(ci, cj));
- }
- else {
- visited[ti][tj] = true;
- q.push(ti);
- q.push(tj);
- }
- }
- }
- }
- vector<vector<int> > dist(n + 1, vector<int>(m + 1, -1));
- for(pair<int, int> x : points_from_walls) {
- q.push(x.first);
- q.push(x.second);
- dist[x.first][x.second] = 0;
- }
- dist[si][sj] = 0;
- while(!q.empty()) {
- int ci = q.front(); q.pop();
- int cj = q.front(); q.pop();
- for(int i = 0; i < 4; i++) {
- int ti = ci + di[i];
- int tj = cj + dj[i];
- if(ti >= 0 and ti < n and tj >= 0 and tj < m and dist[ti][tj] == -1) {
- dist[ti][tj] = dist[ci][cj] + 1;
- q.push(ti);
- q.push(tj);
- }
- }
- }
- q.push(ei);
- q.push(ej);
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < m; j++) {
- visited[i][j] = false;
- }
- }
- visited[ei][ej] = true;
- while(!q.empty()) {
- int ci = q.front(); q.pop();
- int cj = q.front(); q.pop();
- if(mat[ci][cj] == '.' and dist[ci][cj] <= k) {
- cout << "DA" << endl;
- return 0;
- }
- for(int i = 0; i < 4; i++) {
- int ti = ci + di[i];
- int tj = cj + dj[i];
- if(ti >= 0 and ti < n and tj >= 0 and tj < m and mat[ti][tj] != '#' and !visited[ti][tj]) {
- visited[ti][tj] = true;
- q.push(ti);
- q.push(tj);
- }
- }
- }
- cout << "NE" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement