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>
- #include <algorithm>
- #include <cstring>
- #include <fstream>
- using namespace std;
- typedef long long ll;
- char mat[1005][1005];
- bool visited[1005][1005];
- int dist[1005][1005];
- 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;
- }
- }
- }
- queue<int> q;
- q.push(si);
- q.push(sj);
- memset(visited, false, sizeof visited);
- visited[si][sj] = true;
- int di[] = {-1, 1, 0, 0};
- int dj[] = {0, 0, -1, 1};
- vector<pair<int, int> > cell_at_border;
- 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] == '#') {
- cell_at_border.push_back(make_pair(ci, cj));
- }
- else {
- q.push(ti);
- q.push(tj);
- visited[ti][tj] = true;
- }
- }
- }
- }
- memset(visited, false, sizeof visited);
- memset(dist, -1, sizeof dist);
- for(int i = 0; i < cell_at_border.size(); i++) {
- q.push(cell_at_border[i].first);
- q.push(cell_at_border[i].second);
- q.push(0);
- visited[cell_at_border[i].first][cell_at_border[i].second] = true;
- dist[cell_at_border[i].first][cell_at_border[i].second] = 0;
- }
- while(!q.empty()) {
- int ci = q.front(); q.pop();
- int cj = q.front(); q.pop();
- int d = q.front(); q.pop();
- dist[ci][cj] = d;
- 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]) {
- visited[ti][tj] = true;
- q.push(ti);
- q.push(tj);
- q.push(d + 1);
- }
- }
- }
- memset(visited, false, sizeof visited);
- q.push(ei);
- q.push(ej);
- while(!q.empty()) {
- int ci = q.front(); q.pop();
- int cj = q.front(); q.pop();
- if(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 !visited[ti][tj] and mat[ti][tj] != '#') {
- visited[ti][tj] = true;
- q.push(ti);
- q.push(tj);
- }
- }
- }
- cout << "NE" << endl;
- return 0;
- }
- /*
- M..
- ###
- ..V
- **/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement