Problem: Example

Input

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


Output

Yes

Solution (in CPP):

Solution:
#include<bits/stdc++.h>
using namespace std;

#define LL long long int
#define uLL unsigned long long int

#define S(a) scanf("%d",&a)
#define S2(a,b) scanf("%d%d",&a,&b)
#define S3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define SLL(a) scanf("%lld",&a)
#define SLL2(a,b) scanf("%lld%lld",&a,&b)
#define SLL3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define SC(a) scanf("%c",&a)
#define P(a) printf("%d",a)
#define PS(a) printf("%s",a)
#define PLL(a) printf("%lld",a)
#define PCASE(kk) printf("Case %d: ",kk++)
#define PCASENL printf("Case %d:\n",kk++)
#define NL puts("")

#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define pi (2.0*acos(0.0))
#define pii pair<int,int>

long long int gcd(long long int c,long long int d)
{
    if(d==0) return c;
    else return gcd(d,c%d);
}

//knight and king move....

//int Dx[]={-2,-1,1,2,1,2,-2,-1};
//int Dy[]={-1,-2,2,1,-2,-1,1,2};
int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};
//////////////////////////////////////////////////

int n,m,cell_tim[210][210],sx,sy,ex,ey;
char ch[210][210];

int chk(int x,int y)
{
    if(x<1 || x>n)return 0;
    if(y<1 || y>m)return 0;
    if(ch[x][y]=='#')return 0;
    return 1;
}
void bfs(int xx,int yy)
{
    queue< int >q;
    q.push( xx );
    q.push( yy );
    q.push(  0 );
    cell_tim[xx][yy]=0;
    while(!q.empty())
    {
        int x=q.front();    q.pop();
        int y=q.front();    q.pop();
        int ti=q.front();   q.pop();
        for(int i=0;i<4;i++)
        {
            int nx=x+dx[i];
            int ny=y+dy[i];
            if(chk(nx,ny)==1 && cell_tim[nx][ny]>ti+1)
            {
                cell_tim[nx][ny]=ti+1;
                q.push( nx );
                q.push( ny );
                q.push( ti+1 );
            }
        }
    }
    return ;
}

int dfs1(int xx,int yy)
{
    queue< int >q;
    q.push( xx );
    q.push( yy );
    q.push(  0 );
    cell_tim[xx][yy]=0;
    while(!q.empty())
    {
        int x=q.front();    q.pop();
        int y=q.front();    q.pop();
        int ti=q.front();   q.pop();
        if(x==ex && y==ey) return 1;
        for(int i=0;i<4;i++)
        {
            int nx=x+dx[i];
            int ny=y+dy[i];
            if(chk(nx,ny)==1 && cell_tim[nx][ny]>ti+1)
            {
                cell_tim[nx][ny]=ti+1;
                q.push( nx );
                q.push( ny );
                q.push( ti+1 );
            }
        }
    }
    return 0;
}

int main()
{
    S2(n,m);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            scanf(" %c",&ch[i][j]);
            if(ch[i][j]=='@'){sx=i;sy=j;}
            if(ch[i][j]=='%'){ex=i;ey=j;}
        }
    }

    for(int i=0;i<=n;i++)
    {
        for(int j=0;j<=m;j++)cell_tim[i][j]=1000000000;
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(ch[i][j]=='$')
            {
                bfs(i,j);
            }
        }
    }
    int ans=dfs1(sx,sy);
    if(ans==1)printf("Yes\n");
    else printf("No\n");

    return 0;
}