Problem: Example

Input

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


Output

Yes

Solution (in CPP):

Solution:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>
#include <bitset>
#include <time.h>
#include <tuple>
#include <iomanip>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e12;
const ll MOD = 1e9 + 7;

#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
#define rep(i,N) for(ll i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1


ll sq(ll num) { return num*num; }
ll mod_pow(ll x, ll n) {
	if (n == 0)return 1;
	if (n == 1)return x%MOD;
	ll res = sq(mod_pow(x, n / 2));
	res %= MOD;
	if (n % 2 == 1) {
		res *= x;
		res %= MOD;
	}
	return res;
}
ll mod_add(ll a, ll b) { return (a + b) % MOD; }
ll mod_sub(ll a, ll b) { return (a - b + MOD) % MOD; }
ll mod_mul(ll a, ll b) { return a*b % MOD; }

ll h, w;
string s[210];
ll d[210][210];
ll dd[210][210];
ll sx, sy;
ll dx[] = { 0,1,0,-1 };
ll dy[] = { 1,0,-1,0 };

void solve() {
	rep(i, 210)rep(j, 210)d[i][j] = INF;
	d[sy][sx] = 0;

	typedef pair<P, ll> ppi;
	queue<ppi> que;
	que.push(ppi({ sy,sx }, 0));
	while (que.size()) {
		ppi p = que.front();
		que.pop();
		int y = p.first.first;
		int x = p.first.second;
		//cout << "!" << y << " " << x << endl;
		rep(i, 4) {
			int ny = y + dy[i];
			int nx = x + dx[i];
			//cout << ny << " " << nx << endl;
			if (ny >= h || ny < 0 || nx >= w || nx < 0)continue;
			if (s[ny][nx] == '#')continue;
			if (d[ny][nx] > p.second + 1) {
				d[ny][nx] = p.second + 1;
				if (s[ny][nx] == '.')que.push(ppi(P(ny, nx), d[ny][nx]));
			}
		}
	}
}

int main() {
	cin >> h >> w;
	rep(i, h) {
		cin >> s[i];
		rep(j, w) {
			if (s[i][j] == '%') {
				sx = j, sy = i;
			}
		}
	}
	solve();
	ll dd = INF;
	rep(i, h) {
		rep(j, w) {
			if (s[i][j] == '@')dd = d[i][j];
		}
	}
	bool f = 1;
	rep(i, h) {
		rep(j, w) {
			if (s[i][j] == '$') {
				if (dd >= d[i][j])f = 0;
			}
		}
	}
	if (f)cout << "Yes" << endl;
	else cout << "No" << endl;
}