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

#include <bits/stdc++.h>
using namespace std;

int r, c;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
char str[205][205];
int dis[205][205];
int chk[205][205];

void bfs(int i, int j) {
  int a, b, tx, ty;
  queue<pair<int, int>> qu;
  pair<int, int> p;
  memset(chk, 0, sizeof chk);
  for (a = 0; a < r; ++a)
    for (b = 0; b < c; ++b)
      dis[a][b] = 4000005;
  chk[i][j] = 1;
  qu.push(make_pair(i, j));
  dis[i][j] = 0;
  while (!qu.empty()) {
    p = qu.front();
    qu.pop();
    for (a = 0; a < 4; ++a) {
      tx = dx[a] + p.first;
      ty = dy[a] + p.second;
      if (tx >= 0 && tx < r && ty >= 0 && ty < c && !chk[tx][ty] &&
          str[tx][ty] != '#') {
        chk[tx][ty] = 1;
        dis[tx][ty] = dis[p.first][p.second] + 1;
        qu.push(make_pair(tx, ty));
      }
    }
  }
}

int main() {
  int i, j, k;
  int m1, m2 = 4000005;
  scanf("%d %d", &r, &c);
  for (i = 0; i < r; ++i)
    scanf("%s", str[i]);
  for (i = 0; i < r; ++i) {
    for (j = 0; j < c; ++j) {
      if (str[i][j] == '\\%') {
        bfs(i, j);
        i = r;
        j = c;
      }
    }
  }
  for (i = 0; i < r; ++i) {
    for (j = 0; j < c; ++j) {
      if (str[i][j] == '@')
        m1 = dis[i][j];
      else if (str[i][j] == '$')
        m2 = min(m2, dis[i][j]);
    }
  }

  // cout << m1 << " " << m2 << endl;

  if (m1 < m2)
    printf("Yes\\n");
  else
    printf("No\\n");

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define sf scanf
#define pf printf
#define ll long long
#define fr(I, M, N) for (I = M; I <= N; I++)
#define fr_(I, M, N) for (I = M; I >= N; I--)
#define re return
#define sfn cin >> n
#define bal pf("bal\\n")
#define pb push_back
#define ins insert
#define sz(X) X.size()
#define xx first
#define yy second
#define skip continue
#define memo(X, N) memset(X, N, sizeof(X))
#define all(X) X.begin(), X.end()
#define mp make_pair
#define pi acos(-1)

/*
#define nMX 5000000
#define pMX 500000
int pr[pMX+1],prnum[nMX+1];

int sieve()
{
    int i,j;
    for(i=1;i<=nMX;i++)
        prnum[i]=i+1;
    for(i=1;i<=nMX;i++)
        if(prnum[i]!=-1)
            for(j=2*prnum[i]-1;j<=nMX;j+=prnum[i])
                    prnum[j]=-1;
    j=0;
    for(i=1;i<=nMX;i++)
        if(prnum[i]!=-1)
            pr[++j]=prnum[i];
    re j;
}*/

/*ll m_ncr[10001][10001];
ll ncr(ll i,ll j)
{
    if(j==1) re i;
    if(i==j) re 1;
    if(m_ncr[i][j]!=-1) re m_ncr[i][j];
    re m_ncr[i][j]=ncr(i-1,j) + ncr(i-1,j-1);
}*/

/*ll m_fact[21];
ll fact(ll i)
{
    if(i==1 || i==0) re 1;
    if(m_fact[i]!=-1) re m_fact[i];
    re m_fact[i]=i*fact(i-1);
}*/

// cout<<"Case "<<++cas<<": "<<<<endl;

int cas;
int n, m, tt[202][202];
string s[202];

void upd(int i, int j, int x) {
  if (i > n || j > m || i < 0 || j < 0)
    re;
  if (s[i][j] == '#')
    re;
  if (tt[i][j] <= x)
    re;
  tt[i][j] = x;
  upd(i + 1, j, x + 1);
  upd(i, j + 1, x + 1);
  upd(i - 1, j, x + 1);
  upd(i, j - 1, x + 1);
}

int doit(int i, int j, int x) {
  if (i > n || j > m || i < 0 || j < 0)
    re 0;
  if (s[i][j] == '#')
    re 0;
  if (tt[i][j] <= x)
    re 0;
  tt[i][j] = x;
  if (s[i][j] == '%')
    re 1;
  int r = 0;
  r += doit(i + 1, j, x + 1);
  r += doit(i, j + 1, x + 1);
  r += doit(i - 1, j, x + 1);
  r += doit(i, j - 1, x + 1);
  re r;
}

int main() {
  ios_base::sync_with_stdio(0);
  int i, j, x, cnt = 0, y, k, g, flg, t;
  cin >> n >> m;
  n--;
  m--;
  fr(i, 0, n) { cin >> s[i]; }
  fr(i, 0, n + 1) fr(j, 0, m + 1) tt[i][j] = 1e9;
  fr(i, 0, n) {
    fr(j, 0, m) {
      if (s[i][j] == '$')
        upd(i, j, 0);
    }
  }
  x = -1;
  fr(i, 0, n) {
    fr(j, 0, m) {
      if (s[i][j] == '@') {
        x = doit(i, j, 0);
        break;
      }
    }
    if (x != -1)
      break;
  }
  if (x)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}