Language: C++

//
//  main.cpp
//  ac
//
//  Created by 胡天翼 on 2017/2/21.
//  Copyright ? 2017年 胡天翼. All rights reserved.
//

#include <iostream>
#include <set>
#include <vector>
#include <queue>
#include <cmath>
#include <algorithm>
#include <map>
#define read freopen("in.txt","r",stdin)
#define maxlongint 2147483647
typedef  long long LL;
typedef  unsigned long long ULL;
#pragma comment(linker, "/STACK:102400000,102400000")
#define fori for(int i=1;i<=n;i++)
#define forj for(int j=1;j<=m;j++)
#define fork for(int k=1;k<=n;k++)
#define FOR(i,n) for(int i=1;i<=n;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define DREP(i,a,b) for(int i=a;i>=b;i--)
#define DOWN(i,n) for(int i=n;i>=1;i--)
#define enter cout<<endl;
#define in push_back
#define out pop_back
#define sqr(x) ((x)*(x))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define offcin ios::sync_with_stdio(false)
#define s(n) scanf("%d",&n)
#define sll(n) scanf("%lld",&n)
#define sd(x,y) scanf("%d%d",&x,&y)
#define sch(s) scanf("%s",s)
#define fillfalse(v) memset(v,false,sizeof(v))
#define filltrue(v) memset(v,true,sizeof(v))
#define f0(a)    memset(a,0,sizeof(a))
#define Fillplus(a)    memset(a,-1,sizeof(a))
#define lowbit(x) x&(-x)
using namespace std;
const int maxn = 1e5+5;
const int mod=1e9+7;
int x,y,z,n,m;
char a[400][400];
bool v[400][400];
int xx,yy,x2,y2,ans,pp;
struct app
{
    int x,y,a;
};
app tmp;
string s;
bool in(int x,int y)
{
    return x<=n&&x>=1&&y>=1&&y<=m;
}
int main() {
    cin>>n>>m;
    fori {cin>>s; REP(j,1,m) {a[i][j]=s[j-1]; if(a[i][j]=='@') {xx=i;yy=j;} if(a[i][j]=='%') {x2=i;y2=j;}}}
    queue<app> Q;
    tmp.x=xx;
    tmp.y=yy;
    tmp.a=0;
    v[xx][yy]=1;
    Q.push(tmp);
    ans=0;
    while(!Q.empty())
    {
        tmp=Q.front();
        Q.pop();
        x=tmp.x;
        y=tmp.y;
        z=tmp.a;
//        cout<<x<<" "<<y<<" "<<z<<endl;
        if(!v[x+1][y]&&in(x+1,y)&&a[x+1][y]!='#')
        {
            v[x+1][y]=1;
            if(x+1==x2&&y==y2) {ans=z+1; break;}
            tmp.x=x+1;
            tmp.y=y;
            tmp.a=z+1;
            Q.push(tmp);
        }
        if(!v[x-1][y]&&in(x-1,y)&&a[x-1][y]!='#')
        {
            v[x-1][y]=1;
            if(x-1==x2&&y==y2) {ans=z+1; break;}
            tmp.x=x-1;
            tmp.y=y;
            tmp.a=z+1;
            Q.push(tmp);
        }
        if(!v[x][y+1]&&in(x,y+1)&&a[x][y+1]!='#')
        {
            v[x][y+1]=1;
            if(x==x2&&y+1==y2) {ans=z+1; break;}
            tmp.x=x;
            tmp.y=y+1;
            tmp.a=z+1;
            Q.push(tmp);
        }
        if(!v[x][y-1]&&in(x,y-1)&&a[x][y-1]!='#')
        {
            v[x][y-1]=1;
            if(x==x2&&y-1==y2) {ans=z+1; break;}
            tmp.x=x;
            tmp.y=y-1;
            tmp.a=z+1;
            Q.push(tmp);
        }
    }
    if(ans==0) {cout<<"No"<<endl; return 0;}
    pp=0;
    while(!Q.empty()) Q.pop();
    f0(v);
    tmp.x=x2;
    tmp.y=y2;
    tmp.a=0;
    v[x2][y2]=1;
    Q.push(tmp);
    while(!Q.empty())
    {
        tmp=Q.front();
        Q.pop();
        x=tmp.x;
        y=tmp.y;
        z=tmp.a;
        if(z>=ans) break;
        if(!v[x+1][y]&&in(x+1,y)&&a[x+1][y]!='#')
        {
            v[x+1][y]=1;
            if(a[x+1][y]=='$') {pp=1; break;}
            tmp.x=x+1;
            tmp.y=y;
            tmp.a=z+1;
            Q.push(tmp);
        }
        if(!v[x-1][y]&&in(x-1,y)&&a[x-1][y]!='#')
        {
            v[x-1][y]=1;
            if(a[x-1][y]=='$') {pp=1; break;}
            tmp.x=x-1;
            tmp.y=y;
            tmp.a=z+1;
            Q.push(tmp);
        }
        if(!v[x][y+1]&&in(x,y+1)&&a[x][y+1]!='#')
        {
           
            v[x][y+1]=1;
            if(a[x][y+1]=='$') {pp=1; break;}
            tmp.x=x;
            tmp.y=y+1;
            tmp.a=z+1;
            Q.push(tmp);
        }
        if(!v[x][y-1]&&in(x,y-1)&&a[x][y-1]!='#')
        {
            v[x][y-1]=1;
            if(a[x][y-1]=='$') {pp=1; break;}
            tmp.x=x;
            tmp.y=y-1;
            tmp.a=z+1;
            Q.push(tmp);
        }
    }
    
    if(pp) cout<<"No"<<endl; else cout<<"Yes"<<endl;


}

Compiler output:
a.cc: In function 'int main()':
a.cc:41:18: error: 'memset' was not declared in this scope
   41 | #define f0(a)    memset(a,0,sizeof(a))
      |                  ^~~~~~
a.cc:119:5: note: in expansion of macro 'f0'
  119 |     f0(v);
      |     ^~
a.cc:16:1: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
   15 | #include <map>
  +++ |+#include <cstring>
   16 | #define read freopen("in.txt","r",stdin)