Problem: Example

Input

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


Output

Yes

Solution (in CPP):

Solution:
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x, y) memset(x, y, sizeof(x))
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b > a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b < a)a = b; }
const int N = 205, M = 0, Z = 1e9 + 7, inf = 0x3f3f3f3f;
template <class T1, class T2>inline void gadd(T1 &a, T2 b) { a = (a + b) % Z; }
int casenum, casei;
int n, m;
char s[N][N];
struct Q
{
	int y, x;
}q[N * N];
int h, t;
int f[N][N];

void inq(int y, int x, int step)
{
	if (y > n || y < 1 || x > m || x < 1 || s[y][x] == '#')return;
	if (step >= f[y][x])return;
	f[y][x] = step;
	q[t++] = { y,x };
}
const int dy[4] = { -1,0,0,1 };
const int dx[4] = { 0,-1,1,0 };
int edy, edx;
int bfs()
{
	while (h < t)
	{
		int y = q[h].y;
		int x = q[h++].x;
		for (int k = 0; k < 4; ++k)
		{
			inq(y + dy[k], x + dx[k], f[y][x] + 1);
		}
	}
	return f[edy][edx];
}

int main()
{
	while(~scanf("%d%d", &n, &m))
	{
		for (int i = 1; i <= n; ++i)
		{
			scanf("%s", s[i] + 1);
		}

		MS(f, 63);
		h = t = 0;
		for (int i = 1; i <= n; ++i)
		{
			for (int j = 1; j <= m; ++j)
			{
				if (s[i][j] == '@')
				{
					inq(i, j, 0);
				}
				else if (s[i][j] == '%')
				{
					edy = i;
					edx = j;
				}
			}
		}
		int dis1 = bfs();

		MS(f, 63);
		h = t = 0;
		for (int i = 1; i <= n; ++i)
		{
			for (int j = 1; j <= m; ++j)
			{
				if (s[i][j] == '$')
				{
					inq(i, j, 0);
				}
			}
		}
		int dis2 = bfs();

		if (dis1 < dis2)
		{
			puts("Yes");
		}
		else
		{
			puts("No");
		}
	}
	return 0;
}
/*
【trick&&吐槽】


【?意】


【分析】


【????度&&?化】


【数据】


*/