Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- //#include <bits/stdc+/+.h>
- #include <vector>
- #include <queue>
- #include <cstring>
- #define ll long long
- using namespace std;
- int n,m;
- int main()
- {
- int n,m;
- cin >> n >> m;
- char mat[n][m];
- int di[]={-1,1,0,0};
- int dj[]={0,0,1,-1};
- int si,sj,ei,ej;
- int k;cin>>k;
- 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;
- vector<vector<bool>> visited(n+1,vector<bool>(m+1,false));
- q.push(si);
- q.push(sj);
- visited[si][sj]=true;
- vector<pair<int,int>> cell;
- int dist[n][m];
- while(!q.empty()){
- int ci = q.front();
- q.pop();
- int cj = q.front();
- q.pop();
- visited[ci][cj] = true;
- if(mat[ci][cj] == 'V'){
- 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.push_back(make_pair(ci,cj));
- }
- else{
- q.push(ti);
- q.push(tj);
- visited[ti][tj]=true;
- }
- }
- }
- }
- for(int i = 0;i<n+1;i++){
- for(int j =0;j<m+1;j++){
- visited[i][j] =false;
- }
- }
- for(int i =0;i<cell.size();i++){
- int ci = cell[i].first;
- int cj = cell[i].second;
- dist[ci][cj]=0;
- q.push(ci);
- q.push(cj);
- q.push(0);
- visited[ci][cj] =true;
- }
- while(!q.empty()){
- int ci = q.front();
- q.pop();
- int cj = q.front();
- q.pop();
- int pat = q.front();
- q.pop();
- dist[ci][cj] = pat;
- 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]){
- q.push(ti);
- q.push(tj);
- q.push(pat+1);
- dist[ti][tj] = pat+1;
- visited[ti][tj] = true;
- }
- }
- }
- for(int i = 0;i<n+1;i++){
- for(int j =0;j<m+1;j++){
- visited[i][j] =false;
- }
- }
- q.push(ei);
- q.push(ej);
- visited[ei][ej] = true;
- while(!q.empty()){
- int ci = q.front();
- q.pop();
- int cj = q.front();
- q.pop();
- if(dist[ci][cj]<=k){
- cout << "DA";
- 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]!='#'){
- q.push(ti);
- q.push(tj);
- visited[ti][tj] =true;
- }
- }
- }
- cout << "NE";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement