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

#include <iostream>
#include <queue>
#define INF 300000

using namespace std;

typedef pair<int, int> p;

int w, h, n, result, steps;
int visited[1000][1000];
bool map[1000][1000];
p start, last_start, goal, cur, cheese[10];
queue<p> myqueue;

void init(int in) {
  start = last_start;

  while (!myqueue.empty())
    myqueue.pop();

  for (int i = 0; i < w; ++i) {
    for (int j = 0; j < h; ++j) {
      if (map[i][j] == false) {
        visited[i][j] = -1;
      } else {
        visited[i][j] = INF;
      }
      goal = cheese[in];
    }
  }
  /*for(int i=0; i<w; ++i){
      for(int j=0; j<h; ++j){

          cout << visited[i][j] << " ";

      }
      cout << endl;
  }*/

  return;
}

int main() {
  cin >> w >> h >> n;

  char input;
  for (int i = 0; i < w; ++i) {
    for (int j = 0; j < h; ++j) {
      cin >> input;
      if (input == 'X')
        map[i][j] = false;
      else
        map[i][j] = true;
      if (input == 'S')
        last_start = make_pair(i, j);
      if (input != 'X' && input != 'S' && input != '.') {
        cheese[input - '0'] = make_pair(i, j);
      }
    }
  }

  result = 0;
  for (int i = 1; i <= n; ++i) {
    init(i);
    myqueue.push(start);
    visited[start.first][start.second] = 0;

    while (!myqueue.empty()) {

      /*for(int k=0; k<w; ++k){
          for(int j=0; j<h; ++j){

              cout << visited[k][j] << " ";

          }
          cout << endl;
      }
      cout << endl;*/

      cur = myqueue.front();
      myqueue.pop();

      if (cur == goal) {
        // cout << "goal " << cur.first << " , " << cur.second << "\\n";
        break;
      }

      // visited[cur.first][cur.second] = 0;

      int x = cur.first, y = cur.second;
      if (x - 1 >= 0 && visited[x - 1][y] == INF) {
        myqueue.push(make_pair(x - 1, y));
        visited[x - 1][y] = visited[x][y] + 1;
      }
      if (y - 1 >= 0 && visited[x][y - 1] == INF) {
        myqueue.push(make_pair(x, y - 1));
        visited[x][y - 1] = visited[x][y] + 1;
      }
      if (x + 1 < w && visited[x + 1][y] == INF) {
        myqueue.push(make_pair(x + 1, y));
        visited[x + 1][y] = visited[x][y] + 1;
      }
      if (y + 1 < h && visited[x][y + 1] == INF) {
        myqueue.push(make_pair(x, y + 1));
        visited[x][y + 1] = visited[x][y] + 1;
      }
    }

    result += visited[goal.first][goal.second];
    // cout << visited[goal.first][goal.second] << endl;
    last_start = goal;
  }

  cout << result << endl;
  return 0;
}


Error: MLE

Fixed submission:

#include <iostream>
#include <queue>

using namespace std;

typedef pair<int, int> p;

int w, h, n, result, steps;
char parent[1000][1000];
bool visited[1000][1000], map[1000][1000];
p start, last_start, goal, cur, cheese[10];
queue<p> myqueue;

void init(int in) {
  start = last_start;

  while (!myqueue.empty())
    myqueue.pop();

  for (int i = 0; i < w; ++i) {
    for (int j = 0; j < h; ++j) {
      parent[i][j] = '0';
      if (map[i][j] == false) {
        visited[i][j] = true;
      } else {
        visited[i][j] = false;
      }
      goal = cheese[in];
    }
  }
  return;
}

int main() {
  cin >> w >> h >> n;

  char input;
  for (int i = 0; i < w; ++i) {
    for (int j = 0; j < h; ++j) {
      cin >> input;
      if (input == 'X')
        map[i][j] = false;
      else
        map[i][j] = true;
      if (input == 'S')
        last_start = make_pair(i, j);
      if (input != 'X' && input != 'S' && input != '.') {
        cheese[input - '0'] = make_pair(i, j);
        // cout << "cheese " << input-'0' << " : " << i << " " << j << endl;
      }
    }
  }

  result = 0;
  for (int i = 1; i <= n; ++i) {
    init(i);
    myqueue.push(start);

    while (!myqueue.empty()) {
      cur = myqueue.front();
      myqueue.pop();

      if (cur == goal) {
        break;
      }

      visited[cur.first][cur.second] = true;

      int x = cur.first, y = cur.second;
      if (x - 1 >= 0 && !visited[x - 1][y]) {
        myqueue.push(make_pair(x - 1, y));
        parent[x - 1][y] = '1';
      }
      if (y - 1 >= 0 && !visited[x][y - 1]) {
        myqueue.push(make_pair(x, y - 1));
        parent[x][y - 1] = '2';
      }
      if (x + 1 < w && !visited[x + 1][y]) {
        myqueue.push(make_pair(x + 1, y));
        parent[x + 1][y] = '3';
      }
      if (y + 1 < h && !visited[x][y + 1]) {
        myqueue.push(make_pair(x, y + 1));
        parent[x][y + 1] = '4';
      }
    }
    int counter = 0;
    p bcur = goal;
    while (bcur != start) {
      counter++;
      if (parent[bcur.first][bcur.second] == '1')
        bcur.first++;
      else if (parent[bcur.first][bcur.second] == '2')
        bcur.second++;
      else if (parent[bcur.first][bcur.second] == '3')
        bcur.first--;
      else if (parent[bcur.first][bcur.second] == '4')
        bcur.second--;
    }
    result += counter;
    // cout << counter << endl;
    last_start = goal;
  }

  cout << result << endl;
  return 0;
}