Problem: Example

Input

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


Output

Yes

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
#include <queue>

using namespace std;

char a[205][205];
int b[205][205];
queue <int> ii;
queue <int> jj;


int main ()

{
    int w,h,i,j,k,s,p,ei,ej,fp,fs;
    cin>>h>>w;
    p=s=fp=fs=0;

    memset(b,0,sizeof(b));

    for(i=0;i<w+2;i++)
    {
        a[0][i]='#';
        a[h+1][i]='#';
    }

    for(i=0;i<h+2;i++)
    {
        a[i][0]='#';
        a[i][w+1]='#';
    }

    for(i=1;i<=h;i++)
    {
        for(j=1;j<=w;j++)
        {
            cin>>a[i][j];
            if(a[i][j]=='%') {ii.push(i);
            jj.push(j);
            b[i][j]=1;}
        }
    }

    int ti,tj;

    while(!ii.empty())
    {
        ti= ii.front();
        tj= jj.front();
        ii.pop();
        jj.pop();

        //cout<<fp<<" "<<b[ti][tj]<<endl;

        if(p==1 && fp == b[ti][tj] && s==0) {cout<<"Yes"<<endl;
        return 0;}

        if(s==1 && fs == b[ti][tj] && p==0)
        {
            cout<<"No"<<endl;
            return 0;
        }

        if(s==1 && p==1 && fs == b[ti][tj])
        {
            cout<<"No"<<endl;
        return 0;
        }

        if(a[ti-1][tj]=='.' && b[ti-1][tj]==0)
        {
            ii.push(ti-1);
            jj.push(tj);
            b[ti-1][tj]=(b[ti][tj])+1;
        }

        if(a[ti+1][tj]=='.' && b[ti+1][tj]==0)
        {
            ii.push(ti+1);
            jj.push(tj);
            b[ti+1][tj]=(b[ti][tj])+1;
        }

        if(a[ti][tj-1]=='.' && b[ti][tj-1]==0)
        {
            ii.push(ti);
            jj.push(tj-1);
            b[ti][tj-1]=(b[ti][tj])+1;
        }

        if(a[ti][tj+1]=='.' && b[ti][tj+1]==0)
        {
            ii.push(ti);
            jj.push(tj+1);
            b[ti][tj+1]=(b[ti][tj])+1;
        }

        //rasta dekha shesh

        if(a[ti-1][tj]=='$')
        {
            ii.push(ti-1);
            jj.push(tj);
            b[ti-1][tj]=(b[ti][tj])+1;
            s=1;
            if(fs==0) fs = b[ti-1][tj];
        }

        if(a[ti+1][tj]=='$')
        {
            ii.push(ti+1);
            jj.push(tj);
            b[ti+1][tj]=(b[ti][tj])+1;
            s=1;
            if(fs==0) fs = b[ti+1][tj];
        }

        if(a[ti][tj-1]=='$')
        {
            ii.push(ti);
            jj.push(tj-1);
            b[ti][tj-1]=(b[ti][tj])+1;
            s=1;
            if(fs==0) fs = b[ti][tj-1];
        }

        if(a[ti][tj+1]=='$')
        {
            ii.push(ti);
            jj.push(tj+1);
            b[ti][tj+1]=(b[ti][tj])+1;
            s=1;
            if(fs==0) fs = b[ti][tj+1];
        }

        //soldier dekha shesh

        if(a[ti-1][tj]=='@')
        {
           ii.push(ti-1);
           jj.push(tj);
           b[ti-1][tj]=(b[ti][tj])+1;
           fp = b[ti-1][tj];
           p=1;
        }

        if(a[ti+1][tj]=='@')
        {
            ii.push(ti+1);
            jj.push(tj);
            b[ti+1][tj]=(b[ti][tj])+1;
            fp = b[ti+1][tj];
            p=1;
        }

        if(a[ti][tj-1]=='@')
        {
            ii.push(ti);
            jj.push(tj-1);
            b[ti][tj-1]=(b[ti][tj])+1;
            fp = b[ti][tj-1];
            p=1;
        }

        if(a[ti][tj+1]=='@')
        {
            ii.push(ti);
            jj.push(tj+1);
            b[ti][tj+1]=(b[ti][tj])+1;
            fp = b[ti][tj+1];
            p=1;
        }

    }
}