#include <bits/stdc++.h>
//file stream
#define output freopen("output.txt","w",stdout)
#define input freopen("input.txt","r",stdin)
//functions
#define pb(x)  push_back(x)
// if needed pair
#define f first
#define s second
#define mp(x,y) make_pair(x,y)


//inputs 1 var
#define s1i(n) scanf("%d",&n)
#define s1u(n) scanf("%u",&n)
#define s1l(n) scanf("%lld",&n)
#define s1lu(n) scanf("%llu",&n)
#define s1d(n) scanf("%lf",&n)
#define s1s(n) scanf("%s",n)
//inputs 2 var
#define s2i(n,m) scanf("%d %d",&n,&m)
#define s2u(n,m) scanf("%u %u",&n,&m)
#define s2l(n,m) scanf("%lld %lld",&n,&m)
#define s2lu(n,m) scanf("%llu %llu",&n,&m)
#define s2d(n,m) scanf("%lf %lf",&n,&m)
//inputs 3 var
#define s3i(n,m,l) scanf("%d %d %d",&n,&m,&l)
#define s3u(n,m,l) scanf("%u %u %u",&n,&m,&l)
#define s3l(n,m,l) scanf("%lld %lld %lld",&n,&m,&l)
#define s3lu(n,m,l) scanf("%llu %llu %llu",&n,&m,&l)
#define s3d(n,m,l) scanf("%lf %lf %lf",&n,&m,&l)

//output 1 var
#define p1i(n) printf("%d",n)
#define p1u(n) printf("%u",n)
#define p1l(n) printf("%lld",n)
#define p1lu(n) printf("%llu",n)
#define p1d(n,pre) printf("%.*f",pre,n)
#define p1s(n) printf("%s",n)
//output 2 var
#define p2i(n,m) printf("%d %d",n,m)
#define p2u(n,m) printf("%u %u",n,m)
#define p2l(n,m) printf("%lld %lld",n,m)
#define p2lu(n,m) printf("%llu %llu",n,m)
#define p2d(n,m,pre) printf("%.*f %.*f",pre,n,pre,m)
//inputs 3 var less important
#define p3i(n,m,l) printf("%d %d %d",n,m,l)
#define p3u(n,m,l) printf("%u %u %u",n,m,l)
#define p3l(n,m,l) printf("%lld %lld %lld",n,m,l)
#define p3lu(n,m,l) printf("%llu %llu %llu",n,m,l)
//output misc
#define nline() putchar(10)
#define space() putchar(' ')
#define pch(c) putchar(c)
#define tcase(i) printf("Case %d:",i)
//loop
#define fr0(i,n) for(i=0;i<n;i++)
#define fr1(i,n) for(i=1;i<=n;i++)
//memory reset
#define set0(x) memset(x,0,sizeof x)
#define setn1(x) memset(x,-1,sizeof x)
#define setinf(x) memset(x,125,sizeof x)
//bit operation single variable
#define On(x,i)  (x|=(1<<(i)))
#define Off(x,i) (x&= ~(1<<(i)))
#define isOn(x,i) (x&(1<<(i)))
#define Toggle(x,i) (x^=(1<<(i)))
#define tmod(x,i)  (x&(~(-1<<i)))

//data type
typedef long long ll;
typedef unsigned long long ull;
//bit operation array
//constant
const double EPS = 1e-9;
using namespace std;
int tc,o,prc,sld,i,j,r,c,x,y;

char B[250][250];
queue<pair<int,int> > q;
map<pair<int,int> ,int> level,visited;
pair<int,int>u;
main()
{
    {
        s2i(r,c);
        fr0(i,r)
        {
            scanf("%s",B[i]);
            fr0(j,c)
            {
                if(B[i][j]=='%')
                {
                    x=i,y=j;continue;
                }
            }
        }
        q.push(mp(x,y));
        level[mp(x,y)]=0;
        visited[mp(x,y)]=1;
        sld=5000;prc=50000;
        while(!q.empty())
        {
            u=q.front();q.pop();
            if(B[u.f][u.s]=='@')prc=level[u];
            if(B[u.f][u.s]=='$')sld=min(sld,level[u]);

            if(u.f+1<r && B[u.f+1][u.s]!='#' && !visited[mp(u.f+1,u.s)])
            {
                level[mp(u.f+1,u.s)]=level[u]+1;
                visited[mp(u.f+1,u.s)]=1;
                q.push(mp(u.f+1,u.s));
            }

            if(u.s+1<c && B[u.f][u.s+1]!='#' && !visited[mp(u.f,u.s+1)])
            {
                level[mp(u.f,u.s+1)]=level[u]+1;
                visited[mp(u.f,u.s+1)]=1;
                q.push(mp(u.f,u.s+1));
            }


            if(u.f-1>=0 && B[u.f-1][u.s]!='#' && !visited[mp(u.f-1,u.s)])
            {
                level[mp(u.f-1,u.s)]=level[u]+1;
                visited[mp(u.f-1,u.s)]=1;
                q.push(mp(u.f-1,u.s));
            }

            if(u.s+1>=0 && B[u.f][u.s-1]!='#' && !visited[mp(u.f,u.s-1)])
            {
                level[mp(u.f,u.s-1)]=level[u]+1;
                visited[mp(u.f,u.s-1)]=1;
                q.push(mp(u.f,u.s-1));
            }
        }
        if(prc<sld)printf("Yes\n");
        else printf("No\n");

    }
}

Measured memory_bytes: 7720