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

#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> cell;

#define x first
#define y second
#define mp make_pair

const int N = 205;

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

char g[N][N];
int h, w, sx, sy, d[N][N];
int princess;
vector<int> soldiers;

bool valid(int x, int y) {
  return x >= 1 and y >= 1 and x <= h and y <= w and g[x][y] != '#';
}

void bfs(void) {
  queue<cell> q;
  q.push(mp(sx, sy));
  memset(d, -1, sizeof d);
  d[sx][sy] = 0;

  while (not q.empty()) {
    cell u = q.front();
    q.pop();
    for (int k = 0; k < 4; ++k) {
      int nx = u.x + dx[k], ny = u.y + dy[k];
      if (!valid(nx, ny))
        continue;
      if (d[nx][ny] != -1)
        continue;

      d[nx][ny] = d[u.x][u.y] + 1;
      q.push(mp(nx, ny));

      if (g[nx][ny] == '@')
        princess = d[nx][ny];
      else if (g[nx][ny] == '$')
        soldiers.push_back(d[nx][ny]);
    }
  }
}

int main(int argc, char const *argv[]) {
  scanf("%d %d", &h, &w);
  for (int i = 1; i <= h; ++i) {
    scanf("%s", g[i] + 1);
    for (int j = 1; j <= w; ++j) {
      if (g[i][j] == '%')
        sx = i, sy = j;
    }
  }

  bfs();

  int flag = 1;
  for (int i = 0; i < (int)soldiers.size(); ++i) {
    if (soldiers[i] <= princess) {
      flag = 0;
      break;
    }
  }

  puts(flag ? "Yes" : "No");
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define LL long long int
#define uLL unsigned long long int

#define S(a) scanf("%d", &a)
#define S2(a, b) scanf("%d%d", &a, &b)
#define S3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define SLL(a) scanf("%lld", &a)
#define SLL2(a, b) scanf("%lld%lld", &a, &b)
#define SLL3(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)
#define SC(a) scanf("%c", &a)
#define P(a) printf("%d", a)
#define PS(a) printf("%s", a)
#define PLL(a) printf("%lld", a)
#define PCASE(kk) printf("Case %d: ", kk++)
#define PCASENL printf("Case %d:\\n", kk++)
#define NL puts("")

#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define pi (2.0 * acos(0.0))
#define pii pair<int, int>

long long int gcd(long long int c, long long int d) {
  if (d == 0)
    return c;
  else
    return gcd(d, c % d);
}

// knight and king move....

// int Dx[]={-2,-1,1,2,1,2,-2,-1};
// int Dy[]={-1,-2,2,1,-2,-1,1,2};
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
//////////////////////////////////////////////////

int n, m, cell_tim[210][210], sx, sy, ex, ey;
char ch[210][210];

int chk(int x, int y) {
  if (x < 1 || x > n)
    return 0;
  if (y < 1 || y > m)
    return 0;
  if (ch[x][y] == '#')
    return 0;
  return 1;
}

void bfs(int x, int y, int ti) {
  queue<pair<int, pair<int, int>>> q;

  q.push(mp(x, mp(y, 0)));
  //    vis[x][y]=0;
  while (!q.empty()) {
    pair<int, pair<int, int>> fr = q.front();
    q.pop();
    x = fr.first;
    y = fr.second.first;
    ti = fr.second.second;
    cell_tim[x][y] = ti;
    //        cout<<x<<" "<<y<<" "<<ti<<endl;
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      // ti=fr.second.second+1;
      if (chk(nx, ny) == 1 && cell_tim[nx][ny] > ti + 1) {
        //                cell_time[nx][ny]=ti+1;
        q.push(mp(nx, mp(ny, ti + 1)));
      }
    }
  }
  return;
}

int dfs1(int x, int y, int ti) {
  if (chk(x, y) == 0)
    return 0;
  if (cell_tim[x][y] <= ti)
    return 0;
  // cout<<x<<" "<<y<<" "<<ti<<endl;
  if (x == ex && y == ey)
    return 1;
  cell_tim[x][y] = ti;
  int ret = 0;
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    ret |= dfs1(nx, ny, ti + 1);
    if (ret)
      break;
  }
  return ret;
}

int main() {
  S2(n, m);

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      scanf(" %c", &ch[i][j]);
      if (ch[i][j] == '@')
        sx = i, sy = j;
      if (ch[i][j] == '%')
        ex = i, ey = j;
    }
  }

  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++)
      cell_tim[i][j] = 1000000000;
  }
  //    cout<<"asdf"<<endl;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (ch[i][j] == '$') {
        bfs(i, j, 0);
      }
    }
  }

  //    for(int i=1;i<=n;i++)
  //    {
  //        for(int j=1;j<=m;j++)
  //        {
  //            cout<<cell_time[i][j]<<" ";
  //        }
  //        NL;
  //    }

  int ans = dfs1(sx, sy, 0);
  if (ans == 1)
    printf("Yes\\n");
  else
    printf("No\\n");

  return 0;
}