Problem: Example

Input

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


Output

Yes

Solution (in CPP):

Solution:
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
#define ll long long
#define SF scanf
#define PF printf
#define PB push_back
#define MP make_pair
#define mx 5000001
#define MOD 1000000007
#define INP          freopen("in.txt", "r", stdin);
#define OUT          freopen("out.txt", "w", stdout);
#define BOOST        std::ios_base::sync_with_stdio(false);

char st[210][210];
int vis[210][210];
int sz1,sz2,ns,mm;
queue<pair<int,int> >pq;
queue<pair<int,int> >pq2;
int fx[]={1,-1,0,0};
int fy[]={0,0,1,-1};
int level[210][210];
int lev[210][210];
void bfs2()
{

    pair<int,int>pp;

    pp=pq2.front();
    int x=pp.first;
    int y=pp.second;
    lev[x][y]=0;
    vis[x][y]=1;
    while(!pq2.empty())
    {
        pp=pq2.front();
        pq2.pop();
        int p1=pp.first;
        int p2=pp.second;
        for(int p=0;p<4;p++)
        {
            int x=p1+fx[p];
            int y=p2+fy[p];
            if(x>0&&x<=sz1&&y>0&&y<=sz2&&vis[x][y]==0&&(st[x][y]=='.'||st[x][y]=='%'))
            {

                pq2.push(MP(x,y));
                lev[x][y]=lev[p1][p2]+1;
                vis[x][y]=1;
            }


        }
    }
}

void bfs3(int i,int j)
{
    level[i][j]=0;
    pair<int,int>pp;
    vis[i][j]=1;
    pq.push(MP(i,j));
    while(!pq.empty())
    {
        pp=pq.front();
        pq.pop();
        int p1=pp.first;
        int p2=pp.second;
        for(int p=0;p<4;p++)
        {
            int x=p1+fx[p];
            int y=p2+fy[p];
            if(x>0&&x<=sz1&&y>0&&y<=sz2&&vis[x][y]==0&&(st[x][y]=='.'||st[x][y]=='%'))
            {


                level[x][y]=level[p1][p2]+1;
                if(level[x][y]<lev[x][y]||lev[x][y]==0)
                {
                    pq.push(MP(x,y));
                    vis[x][y]=1;
                }

            }


        }
    }
}
int main()
{


    ll n,t,i,j,n1,k,m,f=0,s=0,c=0,p=1,c1=0,p1=0,p2=0,res=0,c2,s1,s2,ss,pp,x,y,q;
    string st1,st2;
   // SF("%lld",&n);


    cin>>sz1>>sz2;
    ns=1000000;
    for(i=1;i<=sz1;i++)
    {
        for(j=1;j<=sz2;j++)
        {
            cin>>st[i][j];
            if(st[i][j]=='%')
            {
                x=i,y=j;
            }
        }
    }
    memset(vis,0,sizeof(vis));
    memset(lev,0,sizeof(lev));
    memset(level,0,sizeof(level));


    for(i=1;i<=sz1;i++)
    {
        for(j=1;j<=sz2;j++)
        {
            if(st[i][j]=='$')
            {
                pq2.push(MP(i,j));
            }

        }
    }
    if(!pq2.empty())
    bfs2();
    memset(vis,0,sizeof(vis));
    memset(level,0,sizeof(level));
    for(i=1;i<=sz1;i++)
    {
        for(j=1;j<=sz2;j++)
        {
            if(st[i][j]=='@')
            {
                bfs3(i,j);
            }

        }
    }
    if(level[x][y]==0||(lev[x][y]!=0&&lev[x][y]<=level[x][y]))
    {
        //cout<<level[x][y]<<" "<<lev[x][y]<<endl;
        cout<<"No"<<endl;

    }
    else
    {
        //cout<<level[x][y]<<" "<<lev[x][y]<<endl;
        cout<<"Yes"<<endl;
    }





return 0;
}