Problem p01881 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef int Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def, pii> pip;
typedef vector<pip> vip;

#define mt make_tuple
typedef tuple<int, int, int> tp;
typedef vector<tp> vt;

const double PI = acos(-1);
const double EPS = 1e-7;
const int inf = 1e9;
const ll INF = 2e18;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int n, m;
vs in;
vvi d, dp;
bool dfs(int a, int b, int c) {
  if (dp[a][b] < c)
    return 0;
  rep(i, 4) {
    int x = a + dx[i];
    int y = b + dy[i];
    if (x < 0 || y < 0 || x >= n || y >= m || in[x][y] == '#' ||
        d[x][y] <= c + 1 || c + 1 >= dp[x][y])
      continue;
    if (in[x][y] == '%') {
      cout << "Yes" << endl;
      return 1;
    }
    if (dfs(x, y, c + 1))
      return 1;
  }
  return 0;
}
int main() {
  cin >> n >> m;
  in = vs(n);
  rep(i, n) cin >> in[i];
  dp = vvi(n, vi(m, inf));
  priority_queue<tp> que;
  rep(i, n) rep(j, m) if (in[i][j] == '$') {
    que.push(tp(i, j, 0));
    dp[i][j] = 0;
  }
  while (!que.empty()) {
    int x, y, co;
    tie(x, y, co) = que.top();
    que.pop();
    if (dp[x][y] < co)
      continue;
    rep(i, 4) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx < 0 || ny < 0 || nx >= n || ny >= m || in[nx][ny] == '#' ||
          dp[nx][ny] <= co + 1)
        continue;
      dp[nx][ny] = co + 1;
      que.push(tp(nx, ny, co + 1));
    }
  }
  d = vvi(n, vi(m, inf));
  rep(i, n) rep(j, m) if (in[i][j] == '@') {
    d[i][j] = 0;
    if (!dfs(i, j, 0))
      cout << "No" << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef int Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def, pii> pip;
typedef vector<pip> vip;

#define mt make_tuple
typedef tuple<int, int, int> tp;
typedef vector<tp> vt;

const double PI = acos(-1);
const double EPS = 1e-7;
const int inf = 1e9;
const ll INF = 2e18;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int n, m;
vs in;
vvi d, dp;
bool dfs(int a, int b, int c) {
  if (dp[a][b] < c)
    return 0;
  rep(i, 4) {
    int x = a + dx[i];
    int y = b + dy[i];
    if (x < 0 || y < 0 || x >= n || y >= m || in[x][y] == '#' ||
        d[x][y] <= c + 1 || c + 1 >= dp[x][y])
      continue;
    if (in[x][y] == '%') {
      cout << "Yes" << endl;
      return 1;
    }
    d[x][y] = c + 1;
    if (dfs(x, y, c + 1))
      return 1;
  }
  return 0;
}
int main() {
  cin >> n >> m;
  in = vs(n);
  rep(i, n) cin >> in[i];
  dp = vvi(n, vi(m, inf));
  priority_queue<tp> que;
  rep(i, n) rep(j, m) if (in[i][j] == '$') {
    que.push(tp(i, j, 0));
    dp[i][j] = 0;
  }
  while (!que.empty()) {
    int x, y, co;
    tie(x, y, co) = que.top();
    que.pop();
    if (dp[x][y] < co)
      continue;
    rep(i, 4) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx < 0 || ny < 0 || nx >= n || ny >= m || in[nx][ny] == '#' ||
          dp[nx][ny] <= co + 1)
        continue;
      dp[nx][ny] = co + 1;
      que.push(tp(nx, ny, co + 1));
    }
  }
  d = vvi(n, vi(m, inf));
  rep(i, n) rep(j, m) if (in[i][j] == '@') {
    d[i][j] = 0;
    if (!dfs(i, j, 0))
      cout << "No" << endl;
  }
}