Problem: Example

Input

2 4
%.@\$
..\$\$


Output

Yes

Solution (in CPP):

Solution:
//==========================================================================
//
//                   Bismillahir-Rahmanir-Rahim
//
// ==========================================================================
#include <bits/stdc++.h>
#define        ll                              long long int
#define        f(x,y,z)                        for(int x=y;x<z;x++)
#define        pii                             pair<int,int>
#define        pll                             pair<ll,ll>
#define        CLR(a)                          memset(a,0,sizeof(a))
#define        SET(a)                          memset(a,-1,sizeof(a))
#define        N                               100010
#define        M                               1000000007
#define        pi                              acos(-1.0)
#define        ff                              first
#define        ss                              second
#define        pb                              push_back
#define        inf                             (int)1e9
#define        eps                              1e-9
using namespace std;
int dx[]={0,0,1,-1,-1,-1,1,1};
int dy[]={1,-1,0,0,-1,1,1,-1};
template < class T> inline T biton(T n,T pos){return n |((T)1<<pos);}
template < class T> inline T bitoff(T n,T pos){return n & ~((T)1<<pos);}
template < class T> inline T ison(T n,T pos){return (bool)(n & ((T)1<<pos));}
template < class T> inline T gcd(T a, T b){while(b){a%=b;swap(a,b);}return a;}
template <typename T> string NumberToString ( T Number ) { ostringstream ss; ss << Number; return ss.str(); }
inline int nxt(){int aaa;scanf("%d",&aaa);return aaa;}
inline ll lxt(){ll aaa;scanf("%I64d",&aaa);return aaa;}
inline double dxt(){double aaa;scanf("%lf",&aaa);return aaa;}
template <class T> inline T bigmod(T p,T e,T m){T ret = 1;
for(; e > 0; e >>= 1){
    if(e & 1) ret = (ret * p) % m;p = (p * p) % m;
} return (T)ret;}
#define sayed
#ifdef sayed
     #define debug(args...) {cerr<<"Debug: "; dbg,args; cerr<<endl;}
#else
    #define debug(args...)  // Just strip off all debug tokens
#endif
struct debugger{
    template<typename T> debugger& operator , (const T& v){
        cerr<<v<<" ";
        return *this;
    }
}dbg;
///******************************************START******************************************
int n,m;
int color[205][205];
int level[205][205];
char grid[205][205];
bool mark[205][205];
void bfs(int i,int j){
      color[i][j]=1;pii top;

      queue<pii> q;
      q.push({i,j});
      while(!q.empty()){

          top=q.front();q.pop();
         for(int k=0;k<4;k++){

            int nx=top.ff+dx[k];
            int ny=top.ss+dy[k];
            if(nx<0||nx>=n||ny<0||ny>=m||color[nx][ny]) continue;
            if(grid[nx][ny]=='.'||grid[nx][ny]=='%'||grid[nx][ny]=='$'){
                color[nx][ny]=1;
                level[nx][ny]=level[top.ff][top.ss]+1;
                q.push({nx,ny});
            }

         }

      }
}
int dist[205][205];
bool bfs1(){
    CLR(color);
    CLR(dist);
    queue<pii> q; pii dest;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(grid[i][j]=='$')
            {
                q.push({i,j});
                dist[i][j]=0;

            }  else {
             dist[i][j]=inf;

            }
            if(grid[i][j]=='%') dest={i,j};
        }
    }
     if(q.empty()) return false;
      while(!q.empty()){

         pii top=q.front();q.pop();

         for(int k=0;k<4;k++){

            int nx=top.ff+dx[k];
            int ny=top.ss+dy[k];
            if(nx<0||nx>=n||ny<0||ny>=m||color[nx][ny]) continue;
            if(grid[nx][ny]=='.'||grid[nx][ny]=='%'||grid[nx][ny]=='@'||grid[nx][ny]=='$'){
                color[nx][ny]=1;
                dist[nx][ny]=min(dist[nx][ny],dist[top.ff][top.ss]+1);
                q.push({nx,ny});
            }

         }

      }
      if(dist[dest.ff][dest.ss]<=level[dest.ff][dest.ss]) return true;
      return false;
}

int main(){
     //freopen("out.txt","w",stdout);
    //ios_base::sync_with_stdio(false);
   //cin.tie(0);
   n=nxt(),m=nxt();
   for(int i=0;i<n;i++) scanf("%s",grid[i]);
   for(int i=0;i<n;i++){
    for(int j=0;j<m;j++){
        if(grid[i][j]=='@'){
            bfs(i,j);
            break;
        }
    }
   }

    int ans;
        ans= bfs1();
   if(!ans){
    puts("Yes");
    return 0;
   }
   puts("No");
return 0;
}