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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;

char map[1005][1005];
int dis[1005][1005];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int row, col, n;
int current = 1;
int firstX, firstY;
int ans = 0;
typedef pair<int, int> pos;
queue<pos> q;

void bfs() {
  while (q.size()) {
    pos t = q.front();
    q.pop();
    if (map[t.first][t.second] - '0' == current) {
      ans += dis[t.first][t.second];
      current++;
      // 清空队列保证下次起点正常
      while (q.size()) {
        q.pop();
      }
      // 重置距离数组
      memset(dis, INF, sizeof dis);
      dis[t.first][t.second] = 0;
      // 设置下一次广搜的起点
      q.push(pos(t.first, t.second));
    }
    // 四个方向进行bfs
    for (int i = 0; i < 4; i++) {
      int x = t.first + dx[i];
      int y = t.second + dy[i];
      if (x < 0 || x >= row || y < 0 || y >= col || map[x][y] == 'X' ||
          dis[x][y] != INF) // INF判断是否走过
        continue;
      else {
        q.push(pos(x, y));
        dis[x][y] = dis[t.first][t.second] + 1;
      }
    }
  }
}

int main() {
  cin >> row >> col >> n;
  // 初始化
  ans = 0;
  memset(dis, INF, sizeof dis);
  for (int i = 0; i < row; i++) {
    for (int j = 0; j < col; j++) {
      cin >> map[i][j];
      if (map[i][j] == 'S') {
        // 设置起点
        q.push(pos(i, j));
        dis[i][j] = 0;
      }
    }
  }
  // 开始广搜
  bfs();
  cout << ans << endl;
  return 0;
}


Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define pi acos(-1)
#define close                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define open                                                                   \\
#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);                           \\
  freopen("out.txt", "w", stdout);                                             \\
#endif
using namespace std;
typedef long long ll;
const int MAX_N = 1000000 + 50;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-10;
const ll INF_ll = 0x7fffffffffffffff;
ll mod = 1e9 + 7;

/*
不要放弃

        Don't give up
*/

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = 10 * x + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline void Out(int a) {
  if (a > 9)
    Out(a / 10);
  putchar(a % 10 + '0');
}

struct Node {
  int x, y, sum, pace;
  int v;
  // friend bool operator < (Node a,Node b){
  //	return a.pace > b.pace;
  // }
} now, nex;

int H, W, N;
char mapp[1001][1010];
int vis[1001][1010];
int sx, sy;
int dirx[4] = {-1, 1, 0, 0};
int diry[4] = {0, 0, -1, 1};

// priority_queue<Node> q;
queue<Node> q;

void bfs() {
  memset(vis, 0, sizeof(vis));
  now.x = sx;
  now.y = sy;
  now.pace = 0;
  now.sum = N;
  now.v = 1;
  vis[sx][sy] = 1;
  q.push(now);
  while (!q.empty()) {
    // now = q.top();
    now = q.front();
    q.pop();
    // cout<<now.x<<"@@@"<<now.y<<"@@@"<<now.sum<<"####"<<now.v<<endl;
    if (now.sum == 0) {
      printf("%d\\n", now.pace);
      break;
    }
    int x1, y1;
    for (int i = 0; i < 4; i++) {
      x1 = now.x + dirx[i];
      y1 = now.y + diry[i];
      if (x1 >= 1 && x1 <= H && y1 >= 1 && y1 <= W && mapp[x1][y1] != 'X') {
        if (mapp[x1][y1] == '.') {
          nex.x = x1;
          nex.y = y1;
          nex.pace = now.pace + 1;
          nex.sum = now.sum;
          nex.v = now.v;
          q.push(nex);
        } else {
          int num = mapp[x1][y1] - '0';
          nex.x = x1;
          nex.y = y1;
          nex.pace = now.pace + 1;
          if (now.v >= num && !vis[x1][y1]) {
            nex.sum = now.sum - 1;
            nex.v = now.v + 1;
            vis[x1][y1] = 1;
            q.push(nex);
          }
        }
      }
    }
  }
}

int main() {
  scanf("%d%d%d", &H, &W, &N);
  for (int i = 1; i <= H; i++) {
    getchar();
    for (int j = 1; j <= W; j++) {
      scanf("%c", &mapp[i][j]);
      if (mapp[i][j] == 'S') {
        sx = i;
        sy = j;
      }
    }
  }
  bfs();
  return 0;
}

/*
                ********
               ************
               ####....#.
             #..###.....##....
             ###.......######              ###            ###
                ...........               #...#          #...#
               ##*#######                 #.#.#          #.#.#
            ####*******######             #.#.#          #.#.#
           ...#***.****.*###....          #...#          #...#
           ....**********##.....           ###            ###
           ....****    *****....
             ####        ####
           ######        ######
##############################################################
#...#......#.##...#......#.##...#......#.##------------------#
###########################################------------------#
#..#....#....##..#....#....##..#....#....#####################
##########################################    #----------#
#.....#......##.....#......##.....#......#    #----------#
##########################################    #----------#
#.#..#....#..##.#..#....#..##.#..#....#..#    #----------#
##########################################    ############
*/