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

// @Team    : nupt2017team12
// @Author  : Zst
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>
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;
}

Error: TLE

Fixed submission:


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int N = 205;

char mat[N][N];
int endx, endy;

struct State {
  int x;
  int y;
  int depth;
};

int dirx[] = {0, 0, 1, -1};
int diry[] = {1, -1, 0, 0};
int vis[N][N];

int search(int sx, int sy) {
  memset(vis, 0, sizeof(vis));
  queue<State> q;
  State start;
  start.x = sx;
  start.y = sy;
  start.depth = 0;
  q.push(start);
  while (!q.empty()) {
    State tmp = q.front();
    q.pop();
    // printf("%d %d \\n",tmp.x,tmp.y );
    if (vis[tmp.x][tmp.y]) {
      continue;
    } else {
      vis[tmp.x][tmp.y] = true;
    }

    if (tmp.x == endx && tmp.y == endy) {
      return tmp.depth;
    }

    for (int i = 0; i < 4; i++) {
      State next;
      next.x = tmp.x + dirx[i];
      next.y = tmp.y + diry[i];
      next.depth = tmp.depth + 1;
      char now_char = mat[next.x][next.y];
      if (now_char == '.' || now_char == '$' || now_char == '%' ||
          now_char == '@') {
        q.push(next);
      }
    }
  }
  return INT_MAX;
}

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  getchar();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      scanf("%c", &mat[i][j]);
      if (mat[i][j] == '%') {
        endx = i;
        endy = j;
      }
    }
    getchar();
  }
  int success_len;
  int fail_len = INT_MAX;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (mat[i][j] == '@') {
        success_len = search(i, j);
        // printf("success:%d\\n",success_len );
      } else if (mat[i][j] == '$') {
        fail_len = min(fail_len, search(i, j));
        // printf("fail:%d\\n",fail_len);
      }
    }
  }
  if (success_len < fail_len) {
    puts("Yes");
  } else {
    puts("No");
  }
}