Problem: Example

Input

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


Output

Yes

Solution (in CPP):

Solution:
#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
const double EPS = 1e-9;
typedef pair<int, int> P;
typedef unsigned int ui;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
const ld eps=1e-8;
int d1[4] = { -1,0,1,0 };
int d2[4] = { 0,1,0,-1 };
int main() {
	int h, w; cin >> h >> w;
	char pal[202][202];
	rep(i, 202) {
		rep(j, 202) {
			pal[i][j] = '#';
		}
	}
	string s; P chk;
	rep1(i, h) {
		cin >> s;
		rep1(j, w) {
			pal[i][j] = s[j - 1];
			if (s[j - 1] == '%') {
				chk = { i,j };
			}
		}
	}
	int d[202][202];
	rep(i, 202) {
		rep(j, 202) {
			d[i][j] = (int)MOD;
		}
	}
	d[chk.first][chk.second] = 0;
	vector<P> v; int used[202][202] = {};
	v.push_back(chk);
	while (!v.empty()) {
		int nx = v[0].first; int ny = v[0].second;
		v.erase(v.begin());
		rep(i, 4) {
			if (used[nx + d1[i]][ny + d2[i]] == 0 && pal[nx + d1[i]][ny + d2[i]] != '#') {
				used[nx + d1[i]][ny + d2[i]] = 1;
				v.push_back({ nx + d1[i],ny + d2[i] });
				d[nx + d1[i]][ny + d2[i]] = min(d[nx + d1[i]][ny + d2[i]], d[nx][ny] + 1);
			}
		}
	}
	int cnt1; int cnt2 = (int)MOD;
	rep1(i, h) {
		rep1(j, w) {
			if (pal[i][j] == '@') {
				cnt1 = d[i][j];
			}
			else if (pal[i][j] == '$') {
				cnt2 = min(cnt2, d[i][j]);
			}
		}
	}
	if(cnt1>=cnt2){
		cout << "No" << endl;
	}
	else {
		cout << "Yes" << endl;
	}
	return 0;
}