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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#define SC(x) scanf("%d", &x);
#define pr(x) cout << x << endl;
#define mem(arr, v) memset(arr, v, sizeof arr);
#define bug(x) cout << "#x = " << x << endl;
const int inf = 0x3f3f3f3f;
const int maxn = 1010;
typedef long long ll;
using namespace std;
char Mz[maxn][maxn], c;
bool vis[maxn][maxn];
int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
int M, N, C;
int px[10], py[10];
struct node {
  int x, y, step;
};

int bfs(int des, int sx, int sy) {
  mem(vis, 0);
  vis[sx][sy] = 1;
  node nod = {sx, sy, 0};
  queue<node> q;
  q.push(nod);

  while (!q.empty()) {
    nod = q.front();
    q.pop();

    int x = nod.x, y = nod.y;
    int step = nod.step;

    if (Mz[x][y] == des + '0')
      return step;
    for (int i = 0; i < 4; i++) {
      int dx = x + dir[i][0];
      int dy = y + dir[i][1];
      if (Mz[dx][dy] == 'X' || Mz[dx][dy] == -1 || vis[dx][dy])
        continue;
      nod = {dx, dy, step + 1};
      q.push(nod);
      vis[dx][dy] = 1;
    }
  }
}

int main() {
  //	freopen("in.txt","r",stdin);
  cin >> N >> M >> C;
  mem(Mz, -1) mem(px, 0) mem(py, 0)

      for (int i = 1; i <= N; i++) for (int j = 1; j <= M; j++) {
    cin >> c;
    Mz[i][j] = c;
    if (c == '.' || c == 'X')
      continue;
    if (c == 'S')
      px[0] = i, py[0] = j;
    else
      px[c - '0'] = i, py[c - '0'] = j;
  }
  int res = 0;
  for (int i = 0; i < C; i++) {
    if (px[i] && py[i]) {
      //				dfs(i + 1,px[i],py[i],0);
      res += bfs(i + 1, px[i], py[i]);
    }
  }
  printf("%d\\n", res);
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#define SC(x) scanf("%d", &x);
#define pr(x) cout << x << endl;
#define mem(arr, v) memset(arr, v, sizeof arr);
#define bug(x) cout << "#x = " << x << endl;
const int inf = 0x3f3f3f3f;
const int maxn = 1e3 + 5;
typedef long long ll;
using namespace std;
char Mz[maxn][maxn], c;
bool vis[maxn][maxn];
int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
// int d[maxn][maxn];
int M, N, C;
int px[10], py[10];
int ans;
struct Node {
  int x, y, step;
};
// void dfs(int des,int x,int y,int step) {		// des - destination
//
//	for(int i = 0 ; i < 4 ; i++) {
//		int dx = x + dir[i][0];
//		int dy = y + dir[i][1];
//		if(Mz[dx][dy] == des + '0')			// find the des
//			ans = min(ans,step + 1);
//		if(Mz[dx][dy] != 'X' && Mz[dx][dy] != -1 && !vis[dx][dy]) {
//			vis[dx][dy] = 1;
//			dfs(des,dx,dy,step + 1);
//			vis[dx][dy] = 0;
//		}
//	}
// }

int bfs(int des, int sx, int sy) {
  Node node;
  node = {sx, sy, 0};
  queue<Node> q;
  q.push(node);
  while (!q.empty()) {
    node = q.front();
    q.pop();
    int x = node.x, y = node.y;
    vis[x][y] = 1;
    int step = node.step;
    for (int i = 0; i < 4; i++) {
      int dx = x + dir[i][0];
      int dy = y + dir[i][1];
      if (Mz[dx][dy] == 'X' || Mz[dx][dy] == -1 || vis[dx][dy])
        continue;
      if (Mz[dx][dy] == des + '0')
        return step + 1;
      node = {dx, dy, step + 1};
      q.push(node);
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  //	freopen("in.txt","r",stdin);
  while (cin >> N >> M >> C) {
    mem(Mz, -1) mem(px, 0) mem(py, 0)

        for (int i = 1; i <= N; i++) for (int j = 1; j <= M; j++) {
      cin >> c;
      Mz[i][j] = c;
      if (c == '.' || c == 'X')
        continue;
      if (c == 'S')
        px[0] = i, py[0] = j;
      else
        px[c - '0'] = i, py[c - '0'] = j;
    }
    int res = 0;
    for (int i = 0; i < C; i++) {
      if (px[i] && py[i]) {
        mem(vis, 0);
        vis[px[i]][py[i]] = 1;
        //				dfs(i + 1,px[i],py[i],0);
        res += bfs(i + 1, px[i], py[i]);
      }
    }
    pr(res)
  }
}