Problem: Example

Input

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


Output

Yes

Solution (in CPP):

Solution:
/* " A computer would deserve to be called intelligent if it
could deceive a human into believing that it was human. "
                                            - Alan Turing */
#include<bits/stdc++.h>
using namespace std;
#define D               double
#define LL              long long
#define ULL             unsigned long long
#define SF              scanf
#define PF              printf
#define FOR(i, a, b)    for(int i = a; i < b; i++)
#define REV(i, a, b)    for(int i = a - 1; i >= b; i--)
#define SET(a, v)       memset(a, v, sizeof a)
#define VI              vector<int>
#define PB              push_back
#define ALL(v)          v.begin(), v.end()
#define MII             map<int, int>
#define MSI             map<string, int>
#define PII             pair<int, int>
#define MP              make_pair
#define X               first
#define Y               second
// return the index     (match ? first match : immediate greater)
#define lbA(a, n, x)    lower_bound(a, a + n, x) - a
#define lbV(v, x)       lower_bound(ALL(v), x) - v.begin()
// return the index     (match ? last match + 1 : immediate greater)
#define ubA(a, n, x)    upper_bound(a, a + n, x) - a
#define ubV(v, x)       upper_bound(ALL(v), x) - v.begin()
template <class T> inline T bigmod(T b, T p, T M) {
    LL ret = 1;
    for(; p > 0; p >>= 1) {
        if(p & 1) ret = (ret * b) % M;
        b = (b * b) % M;
    }
    return (T) ret;
}
template <class T> inline T __lcm(T a, T b) {
    return (a * b) / __gcd(a, b);
}
int dx4[] = {1, 0, -1, 0}; // 4 directions
int dy4[] = {0, 1, 0, -1};
int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1}; // 8 directions
int dy8[] = {1, -1, -1, 0, 1, -1, 0, 1};
int kx8[] = {1, 1, 2, 2, -1, -1, -2, -2}; // Knight Moves
int ky8[] = {2, -2, 1, -1, 2, -2, 1, -1};
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
// int tc;SF("%d",&tc);FOR(cs,1,tc+1){}
const LL                LINF = 1e18;
const int               INF = 1e9, NIL = -1;
const int               N = 200 + 5, M = 1e9 + 7;

int n, m, vis[N][N];
string s[N];

void bfs(int x, int y) {
    vis[x][y] = 1;
    queue<PII>q;
    q.push(MP(x, y));
    while(!q.empty()) {
        PII p = q.front();
        q.pop();
        FOR(i, 0, 4) {
            int u = p.X + dx4[i], v = p.Y + dy4[i];
            if(u < 0 || u >= n || v < 0 || v >= m || s[u][v] == '#' || vis[u][v])
                continue;
            vis[u][v] = vis[p.X][p.Y] + 1;
            q.push(MP(u, v));
        }
    }
}

int main() {
    cin >> n >> m;
    FOR(i, 0, n) cin >> s[i];
    FOR(i, 0, n) {
        FOR(j, 0, m) {
            if(s[i][j] == '%') bfs(i, j);
        }
    }
    int pr = M, so = M;
    FOR(i, 0, n) {
        FOR(j, 0, m) {
//            cout << i << " " << j << " " << vis[i][j] << endl;
            if(s[i][j] == '@') pr = min(pr, vis[i][j]);
            if(s[i][j] == '$') so = min(so, vis[i][j]);
        }
    }
//    cout << pr << " " << so << endl;
    if(pr < so) cout << "Yes" << endl;
    else cout << "No" << endl;
    return 0;
}