Problem p01881 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

#define maxn 215
#define ll long long

char s[maxn][maxn];
bool vis[maxn][maxn];

struct node {
  int x, y;
  int k;
};

vector<node> sol;
node pr;
node sa;
queue<node> Q;

int main() {
  int h, w;
  scanf("%d%d", &h, &w);
  getchar();
  for (int i = 0; i < h; i++)
    scanf("%s", s + i);
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '@') {
        pr.x = i;
        pr.y = j;
        pr.k = 0;
      }
      if (s[i][j] == '$') {
        node so;
        so.x = i;
        so.y = j;
        so.k = 1;
        sol.push_back(so);
      }
      if (s[i][j] == '%') {
        sa.x = i;
        sa.y = j;
        sa.k = 0;
      }
    }
  memset(vis, false, sizeof(vis));
  for (int i = 0; i < sol.size(); i++) {
    Q.push(sol[i]);
    vis[sol[i].x][sol[i].y] = true;
  }
  Q.push(pr);
  vis[pr.x][pr.y] = true;
  bool flag = false;
  while (!Q.empty()) {
    node now = Q.front();
    Q.pop();
    int x = now.x;
    int y = now.y;
    if (x == sa.x && y == sa.y) {
      if (now.k == 0)
        flag = true;
      else
        flag = false;
      break;
    }
    if (x + 1 < h && !vis[x + 1][y] && s[x + 1][y] != '#') {
      node ny = now;
      ny.x++;
      vis[x + 1][y] = true;
      Q.push(ny);
    }
    if (x - 1 >= 0 && !vis[x - 1][y] && s[x - 1][y] != '#') {
      node ny = now;
      ny.x--;
      vis[x - 1][y] = true;
      Q.push(ny);
    }
    if (y - 1 >= 0 && !vis[x][y - 1] && s[x][y - 1] != '#') {
      node ny = now;
      ny.y--;
      vis[x][y - 1] = true;
      Q.push(ny);
    }
    if (y + 1 < w && !vis[x][y + 1] && s[x][y + 1] != '#') {
      node ny = now;
      ny.y++;
      vis[x][y + 1] = true;
      Q.push(ny);
    }
  }
  if (flag)
    printf("Yes\\n");
  else
    printf("No\\n");
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <vector>
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef vector<int> V;
typedef pair<int, int> P;
const int inf = 0x3f3f3f3f;
const ll INF = 2e18;
const double PI = acos(-1.0);
const double eps = 1e-9;
const int N = 5 + 2e2;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
int dp[N][N];
char s[N][N];
bool vis[N][N];
int ans1, ans2, n, m;

void bfs(int i, int j) {
  dp[i][j] = 0;
  ans1 = ans2 = 0;
  queue<P> q;
  while (!q.empty())
    q.pop();
  q.push(P(i, j));
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    vis[p.x][p.y] = 1;
    if (ans1 && ans2)
      break;
    if (s[p.x][p.y] == '@')
      ans1 = dp[p.x][p.y];
    if (s[p.x][p.y] == '$')
      ans2 = dp[p.x][p.y];
    for (int k = 0; k < 4; k++) {
      int xx = dx[k] + p.x, yy = dy[k] + p.y;
      if (xx == 0 || yy == 0 || xx > n || yy > m)
        continue;
      if (vis[xx][yy])
        continue;
      if (s[xx][yy] == '#')
        continue;
      dp[xx][yy] = dp[p.x][p.y] + 1;
      q.push(P(xx, yy));
    }
  }
}

int main() {
  while (~scanf("%d%d", &n, &m)) {
    memset(dp, 0x3f, sizeof dp);
    memset(vis, 0, sizeof vis);
    for (int i = 1; i <= n; i++)
      scanf("%s", s[i] + 1);
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++) {
        if (s[i][j] == '%')
          bfs(i, j);
      }
    if (ans1 < ans2 || ans2 == 0)
      puts("Yes");
    else
      puts("No");
  }
  return 0;
}