Problem:
Help the Princess!
The people of a certain kingdom make a revolution against the bad government of the princess. The revolutionary army invaded the royal palace in which the princess lives. The soldiers of the army are exploring the palace to catch the princess. Your job is writing a program to decide that the princess can escape from the royal palace or not.
For simplicity, the ground of the palace is a rectangle divided into a grid. There are two kinds of cells in the grid: one is a cell that soldiers and the princess can enter, the other is a cell that soldiers or the princess cannot enter. We call the former an empty cell, the latter a wall. The princess and soldiers are in different empty cells at the beginning. There is only one escape hatch in the grid. If the princess arrives the hatch, then the princess can escape from the palace. There are more than or equal to zero soldiers in the palace.
The princess and all soldiers take an action at the same time in each unit time. In other words, the princess and soldiers must decide their action without knowing a next action of the other people. In each unit time, the princess and soldiers can move to a horizontally or vertically adjacent cell, or stay at the current cell. Furthermore the princess and soldiers cannot move out of the ground of the palace. If the princess and one or more soldiers exist in the same cell after their move, then the princess will be caught. It is guaranteed that the princess can reach the escape hatch via only empty cells if all soldiers are removed from the palace.
If there is a route for the princess such that soldiers cannot catch the princess even if soldiers make any moves, then the princess can escape the soldiers. Note that if the princess and a soldier arrive the escape hatch at the same time, the princess will be caught. Can the princess escape from the palace?
Input
Each dataset is formatted as follows.
$H$ $W$
$map_1$
$map_2$
...
$map_H$
The first line of a dataset contains two positive integers $H$ and $W$ delimited by a space, where $H$ is the height of the grid and $W$ is the width of the grid ($2 \leq H, W \leq 200$).
The $i$-th line of the subsequent $H$ lines gives a string $map_i$, which represents situation in the ground of palace.
$map_i$ is a string of length $W$, and the $j$-th character of $map_i$ represents the state of the cell of the $i$-th row and the $j$-th column.
'@', '\$', '%', '.', and '#' represent the princess, a soldier, the escape hatch, an empty cell, and a wall, respectively. It is guaranteed that there exists only one '@', only one '%', and more than or equal to zero '\$' in the grid.
Output
Output a line containing a word "Yes", if the princess can escape from the palace. Otherwise, output "No".
Sample Input 1
2 4
%.@\$
..\$\$
Output for the Sample Input 1
Yes
Sample Input 2
3 4
.%..
.##.
.@\$.
Output for the Sample Input 2
Yes
Sample Input 3
2 3
%\$@
###
Output for the Sample Input 3
No
Sample Input 4
2 3
@#\$
.%.
Output for the 
...(truncated)

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
int n, m;
int x, y;
char str[210][210];
char tmp[210][210];
int dirx[] = {1, -1, 0, 0};
int diry[] = {0, 0, 1, -1};
int val (int a, int b) {
    if (a<0 || b<0 || a>=n || b>=m) return 0;
    return 1;
}
int bfs () {
    //printf("%d %d\n", x, y);
    int flag = 0;
    for (int i=0; i<n; i++) {
        for (int j=0; j<m; j++) {
            tmp[i][j] = str[i][j];
        }
    }
    for (int i=0; i<n; i++) {
        for (int j=0; j<m; j++) {
            if (str[i][j] == '@') {
               // printf("%d %d\n",i , j);
                flag = 1;
                if (i==x && j==y) {
                   // printf("%d %d\n", i, j);
                    return 1;
                }
                else {
                    for (int k=0; k<4; k++) {
                        if (val(i+dirx[k], j+diry[k]) && (str[i+dirx[k]][j+diry[k]] == '.' || str[i+dirx[k]][j+diry[k]] == '%')) {
                            tmp[i+dirx[k]][j+diry[k]] = '@';
                        }
                    }
                }
            }
            else if (str[i][j] == '$') {
                for (int k=0; k<4; k++) {
                    if (val(i+dirx[k], j+diry[k]) && str[i+dirx[k]][j+diry[k]] != '#') {
                        tmp[i+dirx[k]][j+diry[k]] = '$';
                    }
                }
            }
        }
    }
    for (int i=0; i<n; i++) {
        for (int j=0; j<m; j++) {
            str[i][j] = tmp[i][j];
        }
    }
    if (!flag) return 0;
    else return bfs();
}
int main () {
    //freopen("in.txt", "r", stdin);
    scanf("%d %d", &n, &m);
    for (int i=0; i<n; i++)
        scanf("%s", str[i]);
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            if (str[i][j] == '%') {
                x = i;
                y = j;
                break;
            }
        }
    }
    if (bfs()) printf("Yes\n");
    else printf("No\n");
    return 0;
}
accuracy: 0.34615384615384615
time_ms: 10
memory_kb: 3552
final_rank: 15

code_snippet: #include <bits/stdc++.h>
//file stream
#define output freopen("output.txt","w",stdout)
#define input freopen("input.txt","r",stdin)
//functions
#define pb(x)  push_back(x)
// if needed pair
#define f first
#define s second
#define mp(x,y) make_pair(x,y)


//inputs 1 var
#define s1i(n) scanf("%d",&n)
#define s1u(n) scanf("%u",&n)
#define s1l(n) scanf("%lld",&n)
#define s1lu(n) scanf("%llu",&n)
#define s1d(n) scanf("%lf",&n)
#define s1s(n) scanf("%s",n)
//inputs 2 var
#define s2i(n,m) scanf("%d %d",&n,&m)
#define s2u(n,m) scanf("%u %u",&n,&m)
#define s2l(n,m) scanf("%lld %lld",&n,&m)
#define s2lu(n,m) scanf("%llu %llu",&n,&m)
#define s2d(n,m) scanf("%lf %lf",&n,&m)
//inputs 3 var
#define s3i(n,m,l) scanf("%d %d %d",&n,&m,&l)
#define s3u(n,m,l) scanf("%u %u %u",&n,&m,&l)
#define s3l(n,m,l) scanf("%lld %lld %lld",&n,&m,&l)
#define s3lu(n,m,l) scanf("%llu %llu %llu",&n,&m,&l)
#define s3d(n,m,l) scanf("%lf %lf %lf",&n,&m,&l)

//output 1 var
#define p1i(n) printf("%d",n)
#define p1u(n) printf("%u",n)
#define p1l(n) printf("%lld",n)
#define p1lu(n) printf("%llu",n)
#define p1d(n,pre) printf("%.*f",pre,n)
#define p1s(n) printf("%s",n)
//output 2 var
#define p2i(n,m) printf("%d %d",n,m)
#define p2u(n,m) printf("%u %u",n,m)
#define p2l(n,m) printf("%lld %lld",n,m)
#define p2lu(n,m) printf("%llu %llu",n,m)
#define p2d(n,m,pre) printf("%.*f %.*f",pre,n,pre,m)
//inputs 3 var less important
#define p3i(n,m,l) printf("%d %d %d",n,m,l)
#define p3u(n,m,l) printf("%u %u %u",n,m,l)
#define p3l(n,m,l) printf("%lld %lld %lld",n,m,l)
#define p3lu(n,m,l) printf("%llu %llu %llu",n,m,l)
//output misc
#define nline() putchar(10)
#define space() putchar(' ')
#define pch(c) putchar(c)
#define tcase(i) printf("Case %d:",i)
//loop
#define fr0(i,n) for(i=0;i<n;i++)
#define fr1(i,n) for(i=1;i<=n;i++)
//memory reset
#define set0(x) memset(x,0,sizeof x)
#define setn1(x) memset(x,-1,sizeof x)
#define setinf(x) memset(x,125,sizeof x)
//bit operation single variable
#define On(x,i)  (x|=(1<<(i)))
#define Off(x,i) (x&= ~(1<<(i)))
#define isOn(x,i) (x&(1<<(i)))
#define Toggle(x,i) (x^=(1<<(i)))
#define tmod(x,i)  (x&(~(-1<<i)))

//data type
typedef long long ll;
typedef unsigned long long ull;
//bit operation array
//constant
const double EPS = 1e-9;
using namespace std;
int tc,o,prc,sld,i,j,r,c,x,y;

char B[250][250];
queue<pair<int,int> > q;
map<pair<int,int> ,int> level,visited;
pair<int,int>u;
main()
{
    {
        s2i(r,c);
        fr0(i,r)
        {
            scanf("%s",B[i]);
            fr0(j,c)
            {
                if(B[i][j]=='%')
                {
                    x=i,y=j;continue;
                }
            }
        }
        q.push(mp(x,y));
        level[mp(x,y)]=0;
        visited[mp(x,y)]=1;
        sld=5000;prc=50000;
        while(!q.empty())
        {
            u=q.front();q.pop();
            if(B[u.f][u.s]=='@')prc=level[u];
            if(B[u.f][u.s]=='$')sld=min(sld,level[u]);

            if(u.f+1<r && B[u.f+1][u.s]!='#' && !visited[mp(u.f+1,u.s)])
            {
                level[mp(u.f+1,u.s)]=level[u]+1;
                visited[mp(u.f+1,u.s)]=1;
                q.push(mp(u.f+1,u.s));
            }

            if(u.s+1<c && B[u.f][u.s+1]!='#' && !visited[mp(u.f,u.s+1)])
            {
                level[mp(u.f,u.s+1)]=level[u]+1;
                visited[mp(u.f,u.s+1)]=1;
                q.push(mp(u.f,u.s+1));
            }


            if(u.f-1>=0 && B[u.f-1][u.s]!='#' && !visited[mp(u.f-1,u.s)])
            {
                level[mp(u.f-1,u.s)]=level[u]+1;
                visited[mp(u.f-1,u.s)]=1;
                q.push(mp(u.f-1,u.s));
            }

            if(u.s+1>=0 && B[u.f][u.s-1]!='#' && !visited[mp(u.f,u.s-1)])
            {
                level[mp(u.f,u.s-1)]=level[u]+1;
                visited[mp(u.f,u.s-1)]=1;
                q.push(mp(u.f,u.s-1));
            }
        }
        if(prc<sld)printf("Yes\n");
        else printf("No\n");

    }
}
accuracy: 1
time_ms: 20
memory_kb: 8104
final_rank: 7

code_snippet: /*
 * Biborno
 * fb.com/mfmejbah
 * fmejbah@gmail.com
 *
 * B
 * Date: 15 Nov 2016
 */

#include <bits/stdc++.h>

using namespace std;

int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};
int h, w, d;
char g[201][201];

bool bfs( int px, int py, int ex, int ey ) {
  bool vis[h][w];
  int dis[h][w];
  memset(vis, 0, sizeof(vis));
  queue<pair<int, int> > Q;
  vis[px][py] = 1;
  dis[px][py] = 0;
  Q.push(make_pair(px, py));
  while(!Q.empty()) {
    pair<int, int> p = Q.front();
    Q.pop();
    for( int i = 0; i < 4; i++ ) {
      int x = p.first + dx[i];
      int y = p.second + dy[i];
      if( x >= 0 && x < h && y >= 0 && y < w && !vis[x][y] && (g[x][y] == '.' || g[x][y] == '%') ) {
        vis[x][y] = 1;
        dis[x][y] = dis[p.first][p.second] + 1;
        Q.push(make_pair(x, y));
      }
      if( x == ex && y == ey ) {
        d = dis[x][y];
        return true;
      }
    }
  }
  return false;
}

int bfss( int px, int py, int ex, int ey ) {
  bool vis[h][w];
  int dis[h][w];
  memset(vis, 0, sizeof(vis));
  memset(dis, 0, sizeof(dis));
  queue<pair<int, int> > Q;
  vis[px][py] = 1;
  Q.push(make_pair(px, py));
  while(!Q.empty()) {
    pair<int, int> p = Q.front();
    Q.pop();
    for( int i = 0; i < 4; i++ ) {
      int x = p.first + dx[i];
      int y = p.second + dy[i];
      if( x >= 0 && x < h && y >= 0 && y < w && !vis[x][y] && (g[x][y] == '.' || g[x][y] == '%') ) {
        vis[x][y] = 1;
        dis[x][y] = dis[p.first][p.second] + 1;
        Q.push(make_pair(x, y));
      }
      if( x == ex && y == ey ) {
         return dis[x][y];
      }
    }
  }
  return 0;
}
int main()
{
  //freopen("B.in", "r", stdin);
  //freopen("B.out", "w", stdout);
  cin >> h >> w;
  int px, py, ex, ey;
  for( int i = 0; i < h; i++ )
    for( int j = 0; j < w; j++ ) {
      cin >> g[i][j];
      if( g[i][j] == '@' ) px = i, py = j;
      if( g[i][j] == '%' ) ex = i, ey = j;
    }
  if(bfs(px, py, ex, ey)) {
    bool f = 1;
    for( int i = 0; i < h; i++ )
      for( int j = 0; j < w; j++ )
        if( g[i][j] == '$' )
          if( bfss(i, j, ex, ey) == d ) {
            f = 0;
            break;
          }
    if(f)puts("Yes");
    else puts("No");
  } else puts("No");
}
accuracy: 0.17307692307692307
time_ms: 20
memory_kb: 3448
final_rank: 17

code_snippet: #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>

using namespace std;

const int f[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct Q{
    int x;
    int y;
    int num;
    bool cmp(Q a){
        return a.x==x&&a.y==y;
    }
    Q(int _x=0,int _y=0,int _num=0):x(_x),y(_y),num(_num){}
};
int n,m;
char map[210][210];
Q shi[400000];
int sl;
Q z;
Q g;
int ansG;
int ansS=0x7fffffff;
bool h[210][210];

int BFS(Q b){
    memset(h,0,sizeof(h));
    queue<Q> q;
    h[b.x][b.y]=1;
    q.push(b);
    while(!q.empty()){
        Q a=q.front();
        q.pop();
        if(a.cmp(z)){
            return a.num;
        }
        for(int i=0;i<4;i++){
            int xx=a.x+f[i][0];
            int yy=a.y+f[i][1];
            int num=a.num+1;
            if(xx>=0&&yy>=0&&xx<n&&yy<m&&!h[xx][yy]&&(map[xx][yy]=='.'||map[xx][yy]=='%')){
				h[xx][yy]=1;
                q.push(Q(xx,yy,num));
            }
        }
    }
    return -1;
}

int main()
{
    cin>>n>>m;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            cin>>map[i][j];
            if(map[i][j]=='%'){
                z=Q(i,j);
            }else if(map[i][j]=='@'){
                g=Q(i,j);
            }else if(map[i][j]=='$'){
                shi[sl++]=Q(i,j);
            }
        }
    }
    ansG=BFS(g);
    if(ansG<0){
		cout<<"No"<<endl;
		return 0;
	}
    for(int i=0;i<sl;i++){
        int w=BFS(shi[i]);
        if(w>0){
			ansS=min(ansS,w);
		}
    }
    if(ansG<ansS){
        cout<<"Yes"<<endl;
    }else{
        cout<<"No"<<endl;
    }
    return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 8220
final_rank: 9

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

// g <- pair < v , cost > 
template < class T >
vector<T> Dijkstra(vector<vector<pair<int,T>>> &g, vector<int> starts) {
    const auto INF = numeric_limits<T>::max();
    vector<T> dist(g.size(), INF);
    priority_queue<pair<T,int>> Q;
    for(int s : starts) Q.emplace(0, s);
    while(!Q.empty()){
        T cost = Q.top().first;
        int v = Q.top().second;
        Q.pop();
        cost = - cost;
        if(dist[v] == INF){
            dist[v] = cost;
            for(auto e : g[v]){
                if(dist[e.first] == INF){
                    Q.emplace(-(cost + e.second), e.first);
                }
            }
        }
    }
    return dist;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    int H,W; cin >> H >> W;
    vector<string> grid(H);
    rep(i,H) cin >> grid[i];
    auto f = [&](int i, int j){ return i * W + j; };

    vector<vector<pair<int,int>>> G(H * W);
    int di[] = {0, 1};
    int dj[] = {1, 0};
    rep(i,H)rep(j,W)rep(d,2) {
        int ni = i + di[d], nj = j + dj[d];
        if(0 <= ni && ni < H && 0 <= nj && nj < W) {
            if(grid[i][j] != '#' && grid[ni][nj] != '#') {
                G[f(i, j)].push_back({f(ni, nj), 1});
                G[f(ni, nj)].push_back({f(i, j), 1});
            }
        }
    }

    int GOAL;
    rep(i,H)rep(j,W)if(grid[i][j] == '%') GOAL = f(i, j);
    int PRINCESS;
    vector<int> SOLDIERS;
    rep(i,H)rep(j,W){
        if(grid[i][j] == '@') PRINCESS = Dijkstra(G, vector<int>{f(i, j)})[GOAL];
        if(grid[i][j] == '$') SOLDIERS.push_back(f(i, j));
    }
    cout << (PRINCESS < Dijkstra(G, SOLDIERS)[GOAL] ? "Yes" : "No") << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 6672
final_rank: 4

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

// g <- pair < v , cost > 
template < class T >
vector<T> Dijkstra(vector<vector<pair<int,T>>> &g, vector<int> starts) {
    vector<T> dist(g.size(), -1);
    priority_queue<pair<T,int>> Q;
    for(int s : starts) Q.emplace(0, s);
    while(!Q.empty()){
        T cost = Q.top().first;
        int v = Q.top().second;
        Q.pop();
        cost = - cost;
        if(dist[v] == -1){
            dist[v] = cost;
            for(auto e : g[v]){
                if(dist[e.first] == -1){
                    Q.emplace(-(cost + e.second), e.first);
                }
            }
        }
    }
    for(auto &d : dist) if(d == -1) d = numeric_limits<T>::max();
    return dist;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    int H,W; cin >> H >> W;
    vector<string> grid(H);
    rep(i,H) cin >> grid[i];
    auto f = [&](int i, int j){ return i * W + j; };

    vector<vector<pair<int,int>>> G(H * W);
    int di[] = {0, 1};
    int dj[] = {1, 0};
    rep(i,H)rep(j,W)rep(d,2) {
        int ni = i + di[d], nj = j + dj[d];
        if(0 <= ni && ni < H && 0 <= nj && nj < W) {
            if(grid[i][j] != '#' && grid[ni][nj] != '#') {
                G[f(i, j)].push_back({f(ni, nj), 1});
                G[f(ni, nj)].push_back({f(i, j), 1});
            }
        }
    }

    int GOAL;
    rep(i,H)rep(j,W)if(grid[i][j] == '%') GOAL = f(i, j);
    int PRINCESS;
    vector<int> SOLDIERS;
    rep(i,H)rep(j,W){
        if(grid[i][j] == '@') PRINCESS = Dijkstra(G, vector<int>{f(i, j)})[GOAL];
        if(grid[i][j] == '$') SOLDIERS.push_back(f(i, j));
    }
    cout << (PRINCESS < Dijkstra(G, SOLDIERS)[GOAL] ? "Yes" : "No") << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 6748
final_rank: 5

code_snippet: #include <bits/stdc++.h>

using namespace std;
typedef pair<int, int>PII;
const int N = 220;
int n, m;

char s[N][N];
int bx, by;
int ax, ay;

set<PII>q1, q2;

int st[N][N];

int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int main()
{
    for (int i = 1; i <= 1e8; i ++);
    scanf("%d%d", &n, &m);
    vector<vector<char> >a(n);
    for (int i = 1; i <= n; i++)
    {
        scanf("%s", s[i] + 1);
        int len = strlen(s[i] + 1);
        for (int j = 1; j <= len; j ++)
        {
            if(s[i][j] == '\\') continue;
            a[i - 1].push_back(s[i][j]);
        }
    }
    for (int i = 0; i < n; i ++)
    {
        for (int j = 0; j < m; j ++)
        {
            
            if(a[i][j] == '@')
            {
                ax = i, ay = j;
                q1.insert({i, j});
            }
            else if(a[i][j] == '%')
            {
                bx = i, by = j;
            }
            else if(a[i][j] == '$')
            {
                q2.insert({i, j});
       //         cout << i << " " << j<<'\n';
            }
        }
    }
    while(!q1.empty())
    {
        set<PII>tmp;
        int t = q2.size();
        for (int i = 0; i < t; i ++)
        {
            PII x = *q2.begin();
            q2.erase(q2.begin());
            for (int k = 0; k < 4; k ++)
            {
                int nx = x.first + dx[k];
                int ny = x.second + dy[k];
                if(nx < 0 || nx >= n || ny < 0 || ny >= m || a[nx][ny] == '#'||st[nx][ny] == 2) continue;
                st[nx][ny] = 2;
             //   cout << nx << " " << ny <<'\n';
                tmp.insert({nx, ny});
            }
        }
        q2 = tmp;
        tmp.clear();
        t = q1.size();
        for (int i = 0; i < t; i ++)
        {
        PII x = *q1.begin();
            q1.erase(q1.begin());
        for (int k = 0; k < 4; k ++)
        {
            int nx = x.first + dx[k];
            int ny = x.second + dy[k];
            if(nx < 0 || nx >= n || ny < 0 || ny >= m || a[nx][ny] == '#'||st[nx][ny] == 2) continue;
            //cout << nx << " " << ny << '\n';
            if(nx == bx && ny == by)
            {
                puts("Yes");
                return 0;
            }
            tmp.insert({nx, ny});
        }
        }
        
            q1= tmp;
    }
    puts("No");
}
accuracy: 1
time_ms: 10
memory_kb: 6812
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;

const int maxn = 2e2+10;
int n, m, dx, dy, px, py, sx[maxn], sy[maxn], cnt, a[maxn][maxn];
int b[maxn][maxn];
char s[maxn];

int work(int x, int y)
{
	memset(b, 63, sizeof(b));
	queue<pii > q; q.push({x, y});
	queue<int> st; st.push(0);
	while(!q.empty())
	{
		int nx = q.front().first, ny = q.front().second; q.pop();
		int nt = st.front(); st.pop();
		if(nx >= 1 && nx <= n && ny >= 1 && ny <= m && !a[nx][ny] && nt < b[nx][ny])
		{
			b[nx][ny] = nt;
			q.push({nx+1, ny}); st.push(nt+1);
			q.push({nx-1, ny}); st.push(nt+1);
			q.push({nx, ny+1}); st.push(nt+1);
			q.push({nx, ny-1}); st.push(nt+1);
		}
	}
	return b[dx][dy];
}

int main()
{
	scanf("%d%d", &n, &m);
	for(int i = 1; i <= n; i++, getchar())
	{
		scanf("%s", s+1);
		for(int j = 1; j <= m; j++)
		{
			char c = s[j];
			if(c == '%') dx = i, dy = j;
			else if(c == '@') px = i, py = j;
			else if(c == '$') 
			{
				cnt++;
				sx[cnt] = i, sy[cnt] = j;
			}
			else if(c == '#') a[i][j] = 1;
		}
	}
	int ans1 = 1e9;
	for(int i = 1; i <= cnt; i++)
	{
		ans1 = min(ans1, work(sx[i], sy[i]));
	} 
	int ans2 = work(px, py);
//	printf("%d %d\n", ans1, ans2);
	printf("%s\n", ans1 > ans2 ? "Yes":"No");
	return 0;
}

/*
2 4
%.@$
..$$

3 4
.%..
.##.
.@$.

2 3
%$@
###

2 3
@#$
.%.

*/
accuracy: 0.11538461538461539
time_ms: 20
memory_kb: 3444
final_rank: 20

code_snippet: #include<iostream>
#include<sstream>
#include<fstream>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<string>
#include<map>
#include<set>
#include<ctime>
#include<bitset>
#include<iterator>
#define bug cout<<"-----------"<<endl;
#define ll  long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define pi pair<int, int>
#define fi first
#define se second
using namespace std;
const int N=1e6+5,M=1e9+7;
const ull base=13331;
const double Pi=acos(-1.0);
const ll inf=0x3f3f3f3f3f3f3f3f;
inline int read() {
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int d[205][205],n,m;
int v[205][205];
char a[205][205];
int dirx[5]={0,0,0,1,-1};
int diry[5]={0,1,-1,0,0};
void dfs1(int x,int y,int t){
	if(d[x][y]<=t)return;
	if(a[x][y]=='#')return;
	if(a[x][y]=='$'&&!v[x][y]){
		v[x][y]=1;
		dfs1(x,y,0);
		return;
	}
	d[x][y]=min(t,d[x][y]);
	for(int i=1;i<=4;i++){
		int x1=x+dirx[i];
		int y1=y+diry[i];
		if(x1<1||x1>n)continue;
		if(y1<1||y1>m)continue;
		dfs1(x1,y1,t+1);
	}
}
int v1[205][205];
bool dfs2(int x,int y,int t){
	if(v1[x][y])return false;
	v1[x][y]=1;
	if(a[x][y]=='#')return false;
	if(t>=d[x][y])return false;
	if(a[x][y]=='%')return true;
	for(int i=1;i<=4;i++){
		int x1=x+dirx[i];
		int y1=y+diry[i];
		if(x1<1||x1>n)continue;
		if(y1<1||y1>m)continue;
		if(dfs2(x1,y1,t+1))return true;
	}
	return false;
}
int main(){
	ios::sync_with_stdio(false);
	cin>>n>>m;
	std::vector<pair<int,int> > vec;
	int px,py;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>a[i][j];
			if(a[i][j]=='$'){
				vec.pb(mp(i,j));
			}
			if(a[i][j]=='@'){
				px=i,py=j;
			}
		}
	}
	memset(d,0x3f,sizeof(d));
	int cnt=vec.size();
	for(int i=0;i<cnt;i++){
		if(v[vec[i].fi][vec[i].se])continue;
		v[vec[i].fi][vec[i].se]=1;
		dfs1(vec[i].fi,vec[i].se,0);
	}
	if(dfs2(px,py,0)){
		cout<<"Yes"<<endl;
	}
	else cout<<"No"<<endl;
}
/*
2 4
%.@$
..$$
3 4
.%..
.##.
.@$.
2 3
%$@
###
2 3
@#$
.%.
*/
accuracy: 0.36538461538461536
time_ms: 10
memory_kb: 4872
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;
//#define ll long long
#define int long long
//#define ull unsigned long long
#define PI pair<int,int>
//#define PII pair<int,PI>
//#define PI pair<ll,int>
const int maxm=555;
/*
@ 公主
$ 士兵
% 出口
. 空格
# 墙
*/
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
map<PI,int>mark,mark2;
char a[maxm][maxm];
int n,m;
vector<PI>p;
vector<PI>out;
PI st;
void bfs1(){
    queue<PI>q;
    for(auto i:p){
        q.push(i);
        mark[i]=1;
    }
    while(q.size()){
        PI x=q.front();q.pop();
        for(int k=0;k<4;k++){
            int xx=x.first+dx[k];
            int yy=x.second+dy[k];
            if(xx<=0||xx>n||yy<=0||yy>m)continue;
            if(a[xx][yy]=='#')continue;
            PI t={xx,yy};
            if(mark[t])continue;
            mark[t]=mark[x]+1;
            q.push(t);
        }
    }
}
void bfs2(){
    queue<PI>q;
    q.push(st);
    mark2[st]=1;
    while(q.size()){
        PI x=q.front();q.pop();
        for(int k=0;k<4;k++){
            int xx=x.first+dx[k];
            int yy=x.second+dy[k];
            if(xx<=0||xx>n||yy<=0||yy>m)continue;
            if(a[xx][yy]=='#')continue;
            PI t={xx,yy};
            if(!mark[t]||mark[t]>mark2[x]+1){
                if(mark2[t])continue;
                mark2[t]=mark2[x]+1;
                q.push(t);
            }
        }
    }
}
signed main(){
    ios::sync_with_stdio(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>a[i][j];
            if(a[i][j]=='@'){
                st={i,j};
            }else if(a[i][j]=='$'){
                p.push_back({i,j});
            }else if(a[i][j]=='%'){
                out.push_back({i,j});
            }
        }
    }
    bfs1();
    bfs2();
    int ok=0;
    for(auto i:out){
        if(mark2[i]){
//            cout<<i.first<<' '<<i.second<<' '<<mark2[i]<<endl;
            ok=1;
        }
    }
    if(ok)cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
    return 0;
}
/*
2 3
@#$
.%.

No
*/
/*


*/
accuracy: 1
time_ms: 10
memory_kb: 6224
final_rank: 2

code_snippet: #include "bits/stdc++.h"
using namespace std;
typedef pair<int,int> P;
typedef pair<int,P> T;
int h,w;
int dx[5]={1,0,-1,0,0};
int dy[5]={0,-1,0,1,0};
bool fail[5001][200][200]; //iターン目で兵士のいる可能性のある場所
int mincost[200][200];
deque<T> q;

int main()
{
	vector<string> mp;
	cin>>h>>w;
	for(int i=0;i<h;i++)
	{
		string s;
		cin>>s;
		mp.push_back(s);
	}
	bool ans=false;
	fill(fail[0][0],fail[5001][0],false);
	fill(mincost[0],mincost[200],1e8-1);
	//初期配置を定める
	for(int i=0;i<h;i++)
	{
		for(int j=0;j<w;j++)
		{
			if(mp[i][j]=='$')
				fail[0][i][j]=true;
			if(mp[i][j]=='@')
			{
				q.push_back(T(0,P(i,j)));
				mincost[i][j]=0;
			}
		}
	}
	//兵士の移動パターンをすべて調べる
	for(int k=1;k<1001;k++)
	{
		for(int i=0;i<h;i++)
		{
			for(int j=0;j<w;j++)
			{
				if(fail[k-1][i][j])
				{
					for(int l=0;l<5;l++)
					{
						int nx=i+dx[l];
						int ny=j+dy[l];
						if(nx>=0 && nx<h && ny>=0 && ny<w && mp[nx][ny]!='#')
							fail[k][nx][ny]=true;
					}
				}
			}
		}
	}
	// for(int i=0;i<h;i++)
	// {
	// 	for(int j=0;j<w;j++)
	// 	{
	// 		if(fail[1000][i][j])
	// 			cout<<'$';
	// 		else
	// 			cout<<mp[i][j];
	// 	}
	// 	cout<<endl;
	// }
	//BFS
	while(!q.empty())
	{
		T t=q.front();q.pop_front();
		P p=t.second;
		if(t.first==1000)
			continue;
		if(mp[p.first][p.second]=='%')
		{
			ans=true;
			break;
		}
		for(int i=0;i<4;i++)
		{
			int nx=p.first+dx[i];
			int ny=p.second+dy[i];
			if(nx>=0 && nx<h && ny>=0 && ny<w && mp[nx][ny]!='#')
			{
				if(!fail[t.first+1][nx][ny] && mincost[nx][ny]==1e8-1)
				{
					q.push_back(T(t.first+1,P(nx,ny)));
					mincost[nx][ny]=t.first+1;
				}
			}
		}
	}
	if(ans)
		cout<<"Yes"<<endl;
	else
		cout<<"No"<<endl;
	return 0;
}
accuracy: 1
time_ms: 580
memory_kb: 198716
final_rank: 13

code_snippet: #include <bits/stdc++.h>

using namespace std;

using ll = long long;

const double eps = 1e-10;
const int MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1ll<<50;

template<typename T>
void printv(const vector<T>& s) {
  for(int i=0;i<(int)(s.size());++i) {
    cout << s[i];
    if(i == (int)(s.size())-1) cout << endl;
    else cout << " ";
  }
}

int main () {
  cin.tie(0);
  cout << fixed << setprecision(10);

  int h, w; cin>>h>>w;
  vector<int> dx = {-1, 1, 0, 0}, dy = {0, 0, -1, 1};
  vector<vector<char>> a(h, vector<char>(w));
  pair<int, int> s, e;
  map<pair<int, int>, bool> enemy;
  map<pair<int, int>, bool> wall;
  for(int i=0;i<h;++i) {
    for(int j=0;j<w;++j) {
      cin >> a[i][j];
      pair<int, int> p = make_pair(i, j);
      if(a[i][j] == '@') s = p;
      else if(a[i][j] == '%') e = p;
      else if(a[i][j] == '$') enemy[p] = true;
      else if(a[i][j] == '#') wall[p] = true;
    }
  }

  vector<vector<int>> d(h, vector<int>(w, INF));
  queue<pair<int, int>> q;
  map<pair<int, int>, bool> sel;
  q.push(s);
  sel[s] = true;
  d[s.first][s.second] = 0;
  while(!q.empty()) {
    pair<int, int> now = q.front(); q.pop();
    // cout << now.first << ":" << now.second << endl;
    for(int i=0;i<4;++i) {
      pair<int, int> next = make_pair(now.first + dy[i], now.second + dx[i]);
      if(0 <= next.first && next.first < h && 0 <= next.second && next.second < w && !wall[next] && !sel[next]) {
        q.push(next);
        sel[next] = true;
        d[next.first][next.second] = d[now.first][now.second] + 1;
      }
    }
  }

  int d_prin = d[e.first][e.second];
  int d_enemy = INF;

  vector<vector<int>> de(h, vector<int>(w, INF));
  map<pair<int, int>, bool> sele;
  q.push(e);
  sele[e] = true;
  de[e.first][e.second] = 0;
  while(!q.empty()) {
    pair<int, int> now = q.front(); q.pop();
    if(enemy[now]) {
      d_enemy = de[now.first][now.second];
      break;
    }
    for(int i=0;i<4;++i) {
      pair<int, int> next = make_pair(now.first + dy[i], now.second + dx[i]);
      if(0 <= next.first && next.first < h && 0 <= next.second && next.second < w && !wall[next] && !sele[next]) {
        q.push(next);
        sele[next] = true;
        de[next.first][next.second] = de[now.first][now.second] + 1;
      }
    }
  }

  // cout << d_prin << ":" << d_enemy << endl;
  if(d_prin < d_enemy) cout << "Yes" << endl;
  else cout << "No" << endl;
  
}
accuracy: 1
time_ms: 30
memory_kb: 10384
final_rank: 8

code_snippet: #include<iostream>
using namespace std;
int h,w;
string s[200];
bool used[200][200];
bool nxt[200][200];
bool ns[200][200];
int d[]={0,1,0,-1,0};
main()
{
	cin>>h>>w;
	for(int i=0;i<h;i++)cin>>s[i];
	for(int i=0;i<h;i++)for(int j=0;j<w;j++)used[i][j]=s[i][j]=='$';
	while(1)
	{
		bool flag=false;
		for(int i=0;i<h;i++)for(int j=0;j<w;j++)ns[i][j]=nxt[i][j]=0;
		for(int i=0;i<h;i++)for(int j=0;j<w;j++)
		{
			if(!used[i][j])continue;
			nxt[i][j]=1;
			for(int r=0;r<4;r++)
			{
				int x=i+d[r],y=j+d[r+1];
				if(x<0||x>=h||y<0||y>=w||s[x][y]=='#')continue;
				nxt[x][y]=1;
			}
		}
		for(int i=0;i<h;i++)for(int j=0;j<w;j++)used[i][j]=nxt[i][j];
		for(int i=0;i<h;i++)for(int j=0;j<w;j++)
		{
			if(s[i][j]!='@')continue;
			for(int r=0;r<4;r++)
			{
				int x=i+d[r],y=j+d[r+1];
				if(x<0||x>=h||y<0||y>=w||s[x][y]=='#'||used[x][y])continue;
				if(s[x][y]=='%')
				{
					cout<<"Yes"<<endl;
					return 0;
				}
				ns[x][y]=1;
				flag=true;
			}
			if(!used[i][j])ns[i][j]=1,flag=true;
			s[i][j]='.';
		}
		if(!flag)
		{
			cout<<"No"<<endl;
			return 0;
		}
		for(int i=0;i<h;i++)
		{
			for(int j=0;j<w;j++)if(ns[i][j])s[i][j]='@';
		}
	}
}
accuracy: 1
time_ms: 40
memory_kb: 3360
final_rank: 3

code_snippet: #include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
const double EPS = 1e-9;
typedef pair<int, int> P;
typedef unsigned int ui;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
const ld eps=1e-8;
int d1[4] = { -1,0,1,0 };
int d2[4] = { 0,1,0,-1 };
int main() {
	int h, w; cin >> h >> w;
	char pal[202][202];
	rep(i, 202) {
		rep(j, 202) {
			pal[i][j] = '#';
		}
	}
	string s; P chk;
	rep1(i, h) {
		cin >> s;
		rep1(j, w) {
			pal[i][j] = s[j - 1];
			if (s[j - 1] == '%') {
				chk = { i,j };
			}
		}
	}
	int d[202][202];
	rep(i, 202) {
		rep(j, 202) {
			d[i][j] = (int)MOD;
		}
	}
	d[chk.first][chk.second] = 0;
	vector<P> v; int used[202][202] = {};
	v.push_back(chk);
	while (!v.empty()) {
		int nx = v[0].first; int ny = v[0].second;
		v.erase(v.begin());
		rep(i, 4) {
			if (used[nx + d1[i]][ny + d2[i]] == 0 && pal[nx + d1[i]][ny + d2[i]] != '#') {
				used[nx + d1[i]][ny + d2[i]] = 1;
				v.push_back({ nx + d1[i],ny + d2[i] });
				d[nx + d1[i]][ny + d2[i]] = min(d[nx + d1[i]][ny + d2[i]], d[nx][ny] + 1);
			}
		}
	}
	int cnt1; int cnt2 = (int)MOD;
	rep1(i, h) {
		rep1(j, w) {
			if (pal[i][j] == '@') {
				cnt1 = d[i][j];
			}
			else if (pal[i][j] == '$') {
				cnt2 = min(cnt2, d[i][j]);
			}
		}
	}
	if(cnt1>=cnt2){
		cout << "No" << endl;
	}
	else {
		cout << "Yes" << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3576
final_rank: 1

code_snippet: ///
// File:  2781.cpp
// Author: ymiyamoto
//
// Created on Fri Dec 29 19:30:51 2017
//

#include <cstdint>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

uint32_t wfs(vector<string> map, int32_t y, int32_t x)
{
  vector<vector<int32_t>> visited(map.size(), vector<int32_t>(map[0].size(), -1));
  visited[y][x] = 0;
  queue<pair<int32_t, int32_t>> q;
  q.push({y, x});

  while (!q.empty()) {
    pair<int32_t, int32_t> p = q.front();
    q.pop();

    vector<pair<int32_t, int32_t>> ds = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
    for (auto d : ds) {
      int32_t y2 = p.first + d.first;
      int32_t x2 = p.second + d.second;
      if (0 <= y2 && y2 < (int32_t)map.size() && 0 <= x2 && x2 < (int32_t)map[0].size() && (visited[y2][x2] == -1)) {
        if (map[y2][x2] == '#' || map[y2][x2] == '$') continue;
        visited[y2][x2] = visited[p.first][p.second] + 1;
        q.push({y2, x2});
      }
    }
  }

  for (uint32_t i = 0; i < map.size(); i++) {
    for (uint32_t j = 0; j < map[0].size(); j++) {
      if (map[i][j] == '%') {
        return visited[i][j];
      }
    }
  }
}

int32_t main()
{
  uint32_t H, W;
  cin >> H >> W;
  vector<string> map;
  for (uint32_t i = 0; i < H; i++) {
    string line;
    cin >> line;
    map.push_back(line);
  }

  uint32_t princess;
  uint32_t soldier = UINT32_MAX;
  for (uint32_t y = 0; y < H; y++) {
    for (uint32_t x = 0; x < W; x++) {
      if (map[y][x] == '@') {
        princess = wfs(map, y, x);
      } else if (map[y][x] == '$') {
        soldier = min(soldier, wfs(map, y, x));
      }
    }
  }

  if (princess < soldier) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }

  return 0;
}
accuracy: 1
time_ms: 1870
memory_kb: 3332
final_rank: 12

code_snippet: #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>

using namespace std;

queue<pair<int,int> > q;
int n,m;
int w[205][205];
int d[205][205];
int f[205][205];
char op;
const int dx[4] = {0,0,1,-1};
const int dy[4] = {1,-1,0,0};
int sx,sy;

void bfs(int x,int y)
{
	pair<int,int> now,temp;
	now = make_pair(x,y);
	d[x][y] = 0;
	q.push(now);
	int nx,ny;
	while (!q.empty())
	{
		now = q.front();
		q.pop();
		x = now.first;
		y = now.second;
		for (int i=0;i<4;i++)
		{
			nx = x+dx[i];
			ny = y+dy[i];
			if (d[nx][ny] > d[x][y]+1 && w[nx][ny])
			{
				d[nx][ny] = d[x][y]+1;
				temp = make_pair(nx,ny);
				q.push(temp);
			}
		}
	}
}

bool solve(int x,int y)
{
	pair<int,int> now,temp;
	f[x][y] = 0;
	now = make_pair(x,y);
	q.push(now);
	int nx,ny;
	while (!q.empty())
	{
		now = q.front();
		q.pop();
		x = now.first;
		y = now.second;
		if (w[x][y] == 5) return 1;
		for (int i=0;i<4;i++)
		{
			nx = x+dx[i];
			ny = y+dy[i];
			if (w[nx][ny] == 0 || f[nx][ny] != f[0][0]) continue;
			f[nx][ny] = f[x][y]+1;
			if (f[nx][ny] < d[nx][ny])
			{
				temp = make_pair(nx,ny);
				q.push(temp);
			}
		}
	}
	return 0;
}

int main()
{
	scanf("%d%d",&n,&m);
	memset(w,0,sizeof(w));
	memset(d,0x3f,sizeof(d));
	memset(f,0x3f,sizeof(f));
	for (int i=1;i<=n;i++)
	{
		for (int j=1;j<=m;j++)
		{
			scanf(" %c",&op);
			if (op == '@')
			{
				w[i][j] = 4;
				sx = i;
				sy = j;
			}
			if (op == '%')
			{
				w[i][j] = 5;
			}
			if (op == '$')
			{
				w[i][j] = 2;
			}
			if (op == '.')
			{
				w[i][j] = 1;
			}
		}
	}
/*	printf("n=%d m=%d\n",n,m);
	for (int i=0;i<=n+1;i++)
	{
		for (int j=0;j<=m+1;j++)
			printf("%d",w[i][j]);
		printf("\n");
	}*/

	for (int i=1;i<=n;i++)
	{
		for (int j=1;j<=m;j++)
		{
			if (w[i][j] == 2)
			{
				bfs(i,j);
			}
		}
	}

	if (solve(sx,sy))
	{
		printf("Yes\n");
	}
	else
	{
		printf("No\n");
	}

	return 0;
}
accuracy: 1
time_ms: 1250
memory_kb: 3728
final_rank: 10

code_snippet: #include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<queue>
using namespace std;
int n,H,W;
const int maxn = 1e3;
const int INF = 1e9;
int labyrinth[maxn][maxn],scnt;
int shortest;
int pshort,sshort,eshort,goal;
bool tf;

struct pt
{
	int x,y;
} soldier[100000],princess,escape;
char ch;
bool vis[maxn][maxn];

int dfs(int x,int y,int cur_length)
{
	int temp;
	//printf("%d %d, H:%d W:%d\n",x,y,H,W);
	//labyrinth[y][x] = 3;
	vis[y][x] = true;
	if(x>0&&labyrinth[y][x-1]&&!vis[y][x-1])
	{
		if(labyrinth[y][x-1] == goal)
		{
			return cur_length+1;
		}
		if((temp = dfs(x-1,y,cur_length+1))<shortest)
		{
			shortest = temp;
		}
	}
	if(x<W-1&&labyrinth[y][x+1]&&!vis[y][x+1])
	{
		if(labyrinth[y][x+1] == 2)
		{
			return cur_length+1;
		}
		if((temp = dfs(x+1,y,cur_length+1))<shortest)
		{
			shortest = temp;
		}
	}
	if(y>0&&labyrinth[y-1][x]&&!vis[y-1][x])
	{
		if(labyrinth[y-1][x] == 2)
		{
			return cur_length+1;
		}
		if((temp = dfs(x,y-1,cur_length+1))<shortest)
		{
			shortest = temp;
		}
	}
	if(y<H-1&&labyrinth[y+1][x]&&!vis[y+1][x])
	{
		if(labyrinth[y+1][x] == 2)
		{
			return cur_length+1;
		}
		if((temp = dfs(x,y+1,cur_length+1))<shortest)
		{
			shortest = temp;
		}
	}
	return shortest;
}

int main()
{
	ios::sync_with_stdio(false);
	//freopen("1002.txt","r",stdin);
	//freopen("ans.txt","w+",stdout);
	while(~scanf("%d%d",&H,&W))
	{
		tf = true;
		scnt = 0;
		shortest = INF;
		memset(labyrinth,-1,sizeof(labyrinth));
		getchar();
		for(int i = 0; i<H; ++i)
		{
			for(int j = 0; j < W; ++ j)
			{
				while((ch = getchar())=='\n');
				switch(ch)
				{
				case '@':
					princess.x = j;
					princess.y = i,labyrinth[i][j] = 5;
					break;
				case '.':
					labyrinth[i][j] = 1;
					break;
				case '$':
					soldier[scnt].x = j;
					soldier[scnt++].y = i;
					labyrinth[i][j] = 0;
					break;
				case '%':
					labyrinth[i][j] = 2;
					escape.x = j;
					escape.y = i;
					break;
				case '#':
					labyrinth[i][j] = 0;
					break;
				}
			}
		}
		memset(vis,false,sizeof(vis));
		goal = 2;
		pshort = dfs(princess.x,princess.y,0);
		//printf("pshort:%d\n",pshort);

		goal = 5;
		shortest = INF;
		memset(vis,false,sizeof(vis));
		eshort = dfs(escape.x,escape.y,0);
		//printf("eshort:%d\n",eshort);
		if(eshort<pshort)
			pshort = eshort;
		/*for(int i = 0; i<H; ++i)
		{
			for(int j = 0; j < W; ++ j)
			{
				if(i == princess.y&&j == princess.x)
					printf("X");
				else
				printf("%d",labyrinth[i][j]);
			}
			printf("\n");
		}*/
		if(!scnt&&pshort!=INF)
		{
			tf = true;
		}
		else
		{
			if(pshort != INF)
			{
				goal = 2;
				for(int i = 0; i < scnt; ++i)
				{
					memset(vis,false,sizeof(vis));
					shortest = INF;
					sshort = dfs(soldier[i].x,soldier[i].y,0);
					//printf("sshort:%d\n",sshort);
					if(sshort <= pshort)
					{
						tf = false;
						break;
					}
				}

			}
			else
				tf = false;

		}
		if(tf)
		{
			printf("Yes\n");
		}
		else
		{
			printf("No\n");
		}


	}
}
accuracy: 0.3076923076923077
time_ms: 120
memory_kb: 8516
final_rank: 16

code_snippet: #include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<queue>
using namespace std;
int n,H,W;
const int maxn = 2e2+2;
const int INF = 1e9;
int labyrinth[maxn][maxn],scnt;
int shortest;
int pshort,sshort,eshort,goal;
bool tf;

struct pt
{
    int x,y;
} soldier[maxn],princess,escape;
char ch;
bool vis[maxn][maxn];

int dfs(int x,int y,int cur_length)
{
    int temp;
    //printf("%d %d, H:%d W:%d\n",x,y,H,W);
    //labyrinth[y][x] = 3;
    vis[y][x] = true;
    if(x>0&&labyrinth[y][x-1]&&!vis[y][x-1])
    {
        if(labyrinth[y][x-1] == goal)
        {
            return cur_length+1;
        }
        if((temp = dfs(x-1,y,cur_length+1))<shortest)
        {
            shortest = temp;
        }
    }
    if(x<W-1&&labyrinth[y][x+1]&&!vis[y][x+1])
    {
        if(labyrinth[y][x+1] == 2)
        {
            return cur_length+1;
        }
        if((temp = dfs(x+1,y,cur_length+1))<shortest)
        {
            shortest = temp;
        }
    }
    if(y>0&&labyrinth[y-1][x]&&!vis[y-1][x])
    {
        if(labyrinth[y-1][x] == 2)
        {
            return cur_length+1;
        }
        if((temp = dfs(x,y-1,cur_length+1))<shortest)
        {
            shortest = temp;
        }
    }
    if(y<H-1&&labyrinth[y+1][x]&&!vis[y+1][x])
    {
        if(labyrinth[y+1][x] == 2)
        {
            return cur_length+1;
        }
        if((temp = dfs(x,y+1,cur_length+1))<shortest)
        {
            shortest = temp;
        }
    }
    return shortest;
}

int main()
{
    //freopen("1002.txt","r",stdin);
    //freopen("ans.txt","w+",stdout);
    while(~scanf("%d%d",&H,&W))
    {
        tf = true;
        scnt = 0;
        shortest = INF;
        memset(labyrinth,-1,sizeof(labyrinth));
        getchar();
        for(int i = 0; i<H; ++i)
        {
            for(int j = 0; j < W; ++ j)
            {
                while((ch = getchar())=='\n');
                switch(ch)
                {
                case '@':
                    princess.x = j;
                    princess.y = i,labyrinth[i][j] = 5;
                    break;
                case '.':
                    labyrinth[i][j] = 1;
                    break;
                case '$':
                    soldier[scnt].x = j;
                    soldier[scnt++].y = i;
                    labyrinth[i][j] = 1;
                    break;
                case '%':
                    labyrinth[i][j] = 2;
                    escape.x = j;
                    escape.y = i;
                    break;
                case '#':
                    labyrinth[i][j] = 0;
                    break;
                }
            }
        }
        memset(vis,false,sizeof(vis));
        goal = 2;
        pshort = dfs(princess.x,princess.y,0);
        //printf("pshort:%d\n",pshort);

        goal = 5;
        shortest = INF;
        memset(vis,false,sizeof(vis));
        eshort = dfs(escape.x,escape.y,0);
        //printf("eshort:%d\n",eshort);
        if(eshort<pshort)
            pshort = eshort;
        /*for(int i = 0; i<H; ++i)
        {
            for(int j = 0; j < W; ++ j)
            {
                if(i == princess.y&&j == princess.x)
                    printf("X");
                else
                printf("%d",labyrinth[i][j]);
            }
            printf("\n");
        }*/
        if(!scnt&&pshort!=INF)
        {
            tf = true;
        }
        else
        {
            if(pshort != INF)
            {
                goal = 2;
                for(int i = 0; i < scnt; ++i)
                {
                    memset(vis,false,sizeof(vis));
                    shortest = INF;
                    sshort = dfs(soldier[i].x,soldier[i].y,0);
                    //printf("sshort:%d\n",sshort);
                    if(sshort <= pshort)
                    {
                        tf = false;
                        break;
                    }
                }

            }
            else
                tf = false;

        }
        if(tf)
        {
            printf("Yes\n");
        }
        else
        {
            printf("No\n");
        }


    }
}
accuracy: 0.11538461538461539
time_ms: 10
memory_kb: 3448
final_rank: 19

code_snippet: // @Team    : nupt2017team12
// @Author  : Zst
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <map>
using namespace std;
#define LL long long
#define MOD 1000000007
#define CLR(a,x) memset(a,x,sizeof(a))
#define INF 0x3f3f3f3f
#define pb push_back
#define FOR(i,a,b) for( int i = ( a ); i <= ( b ); ++i )

const int N = 200+7;
char maps[N][N];
int vis[N][N];

int r, c;
int posX, posY;

int princess;
int soldier;

void solve( int x, int y, int times )
{
	vis[x][y] = times;
	if( maps[x][y] == '@' ) {
		princess = min( princess, times );
	} else if( maps[x][y] == '$' ) {
		soldier = min( soldier, times );
	}
	if( x-1 >= 0 ) {
		if( vis[x-1][y] == -1 ) {
			solve( x-1, y, times+1 );
		} else if( times+1 < vis[x-1][y] ) {
			solve( x-1, y, times+1 );
		}
	}
	if( y-1 >= 0 ) {
		if( vis[x][y-1] == -1 ) {
			solve( x, y-1, times+1 );
		} else if( times+1 < vis[x][y-1] ) {
			solve( x, y-1, times+1 );
		}
	}
	if( x+1 < r ) {
		if( vis[x+1][y] == -1 ) {
			solve( x+1, y, times+1 );
		} else if( times+1 < vis[x+1][y] ) {
			solve( x+1, y, times+1 );
		}
	}
	if( y+1 < c ) {
		if( vis[x][y+1] == -1 ) {
			solve( x, y+1, times+1 );
		} else if( times+1 < vis[x][y+1] ) {
			solve( x, y+1, times+1 );
		}
	}
	return;

}


int main()
{
    // freopen( "B.txt", "r", stdin );
    while( scanf( "%d%d", &r, &c ) != EOF ) {
    	princess = soldier = INF;
		CLR( vis, -1 );
	    FOR( i, 0, r-1 ) {
	    	scanf( "%s", maps[i] );
			FOR( j, 0, c-1 ) {
				if( maps[i][j] == '%' ) {
					posX = i;
					posY = j;
				} else if( maps[i][j] == '#' ) {
					vis[i][j] = true;
				}
			}
	    }
	    solve( posX, posY, 0 );
	    if( princess < soldier ) {
	    	printf( "Yes\n");
	    } else {
	    	printf( "No\n");
	    }
	}
	return 0;
}
accuracy: 1
time_ms: 1430
memory_kb: 5324
final_rank: 11

code_snippet: #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include <queue>

using namespace std;

const int maxx=205;
int n,m,k;
char a[maxx][maxx];
int ans = 0,cnt = 0,pos = 0;
int l = 0,r = 0;


const int INF = 100000000;
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
typedef pair <int,int> P;
queue<P> que;
int d[maxx][maxx];
int sx,sy;
int ex,ey;

bool judge(int x,int y){
    if(a[x][y] == '.' || a[x][y] == '%'){
        if(x >= 0 && x < n)
        {
            if(y >= 0 && y < m)
            {
                if(d[x][y] == INF){
                    return true;
                }
            }
        }
    }
    return false;
}

int bfs()
{
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            d[i][j] = INF;//初始化
        }
    }
    que.push(P(sx,sy));
    d[sx][sy] = 0;//并把距???0；
    while(que.size()){//直到?列?空
        P p = que.front();que.pop();
        if(p.first == ex && p.second == ey){
            break;//如果已?是?点，??束搜索。
        }
        for(int i = 0; i < 4; i++)
        {
            int nx = p.first + dx[i],ny = p.second + dy[i];
            if(judge(nx,ny))
            {
                que.push(P(nx,ny));
                d[nx][ny] = d[p.first][p.second] + 1;
            }
        }
    }
    return d[ex][ey];
}


int main()
{
#ifdef LOCAL
//    freopen("/Users/ecooodt/Desktop/c++ and acm/_集?/tp1/2.txt","r",stdin);
#endif
    scanf("%d%d",&n,&m);
    for(int i = 0; i < n; i++)
    {
        getchar();
        for(int j = 0; j < m; j++)
        {
            scanf("%c",&a[i][j]);
            if(a[i][j] == '%')
            {
                ex = i,ey = j;
            }
            if(a[i][j] == '@') sx = i,sy = j;
        }
    }
    pos = bfs();
//    printf("%d ",pos);
    for(int i = 0; i < n; i ++)
    {
        for(int j = 0; j < m; j++)
        {
            if(a[i][j] == '$')
            {
                sx = i,sy = j;
                int t = bfs();
                if(t <= pos) {
//                    printf("%d\n",t);
                    printf("No\n");
                    return 0;
                }
            }
        }
    }
    printf("Yes\n");
    return 0;
}
accuracy: 0.15384615384615385
time_ms: 10
memory_kb: 3320
final_rank: 18