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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

struct P {
  int x, y;
  P() {}
  P(int x, int y) : x(x), y(y) {}
};

int x, y, sx, sy, gx, gy;
int dp[6][1001][1001];

vector<P> p[6];

int ttt;

int solve(int i, int x, int y) {
  if (dp[i][x][y] >= 0) {
    return dp[i][x][y];
  }
  if (i == 4) {
    return dp[i][x][y] = abs(x - gx) + abs(y - gy);
  }

  int next = (ttt + i) % 5 + 1;
  int ret = 1111111;
  for (int j = 0; j < p[next].size(); j++) {
    ret = min(ret, solve(i + 1, p[next][j].x, p[next][j].y) +
                       abs(x - p[next][j].x) + abs(y - p[next][j].y));
  }
  return dp[i][x][y] = ret;
}

/*
int solve(int type,int x,int y){
  int ret = 1111111;
  int temp = 0;

  int typei = type%5+1;
  int xi = x;
  int yi = y;
  for(int i=0;i<p[typei].size();i++){
    x = p[typei][i].x;
    y = p[typei][i].y;
    temp += abs(x-p[typei][i].x)+abs(y-p[typei][i].y);

    int typei = type%5+1;
    int xi = x;
    int yi = y;
    for(int i=0;i<p[typei].size();i++){

      int typei = type%5+1;
      int xi = x;
      int yi = y;
      for(int i=0;i<p[typei].size();i++){

        int typei = type%5+1;
        int xi = x;
        int yi = y;
        for(int i=0;i<p[typei].size();i++){

        }

      }

    }

  }

}
*/

int main(void) {
  while (1) {
    scanf("%d %d", &x, &y);
    if (!x && !y)
      break;

    bool typef[6] = {false};
    int t = 0;
    char m;

    for (int i = 1; i < 6; i++) {
      p[i].clear();
    }

    for (int j = 1; j <= y; j++) {
      for (int i = 1; i <= x; i++) {
        scanf(" %c", &m);
        if (m == 'S') {
          sx = i;
          sy = j;
        } else if (m == 'G') {
          gx = i;
          gy = j;
        } else if (m != '.') {
          p[m - '0'].push_back(P(i, j));
          if (!typef[m - '0']) {
            t++;
            typef[m - '0'] = true;
          }
        }
      }
    }
    if (t < 4) {
      puts("NA");
      continue;
    }

    int type, ret = 1111111;
    if (t == 4) {
      for (int i = 1; i <= 5; i++) {
        if (!typef[i]) {
          type = i;
          break;
        }
      }
      memset(dp, -1, sizeof(dp));
      ttt = type;
      ret = solve(0, sx, sy);
    } else {
      for (int i = 1; i <= 5; i++) {
        memset(dp, -1, sizeof(dp));
        // cout<< "owata:" << i << endl;
        ttt = i;
        int temp = solve(0, sx, sy);
        if (ret > temp) {
          ret = temp;
          type = i;
        }
      }
    }
    printf("%d %d\\n", type, ret);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

struct P {
  int x, y;
  P() {}
  P(int x, int y) : x(x), y(y) {}
};

int x, y, sx, sy, gx, gy;
int dp[6][1001][1001];

vector<P> p[6];

int ttt;

int solve(int i, int x, int y) {
  if (dp[i][x][y] != -1) {
    return dp[i][x][y];
  }
  if (i == 4) {
    return dp[i][x][y] = abs(x - gx) + abs(y - gy);
  }

  int next = (ttt + i) % 5 + 1;
  int ret = 1111111;
  for (int j = 0; j < p[next].size(); j++) {
    ret = min(ret, solve(i + 1, p[next][j].x, p[next][j].y) +
                       abs(x - p[next][j].x) + abs(y - p[next][j].y));
  }
  return dp[i][x][y] = ret;
}

/*
int solve(int type,int x,int y){
  int ret = 1111111;
  int temp = 0;

  int typei = type%5+1;
  int xi = x;
  int yi = y;
  for(int i=0;i<p[typei].size();i++){
    x = p[typei][i].x;
    y = p[typei][i].y;
    temp += abs(x-p[typei][i].x)+abs(y-p[typei][i].y);

    int typei = type%5+1;
    int xi = x;
    int yi = y;
    for(int i=0;i<p[typei].size();i++){

      int typei = type%5+1;
      int xi = x;
      int yi = y;
      for(int i=0;i<p[typei].size();i++){

        int typei = type%5+1;
        int xi = x;
        int yi = y;
        for(int i=0;i<p[typei].size();i++){

        }

      }

    }

  }

}
*/

int main(void) {
  while (1) {
    scanf("%d %d", &x, &y);
    if (!x && !y)
      break;

    bool typef[6] = {false};
    int t = 0;
    char m;

    for (int j = 1; j <= y; j++) {
      for (int i = 1; i <= x; i++) {
        scanf(" %c", &m);
        if (m == 'S') {
          sx = i;
          sy = j;
        } else if (m == 'G') {
          gx = i;
          gy = j;
        } else if (m != '.') {
          p[m - '0'].push_back(P(i, j));
          if (!typef[m - '0']) {
            t++;
            typef[m - '0'] = true;
          }
        }
      }
    }
    if (t < 4) {
      puts("NA");
      continue;
    }

    int type, ret = 1111111;
    if (t == 4) {
      for (int i = 1; i <= 5; i++) {
        if (!typef[i]) {
          type = i;
          break;
        }
      }
      memset(dp, -1, sizeof(dp));
      ttt = type;
      ret = solve(0, sx, sy);
    } else {
      for (int i = 1; i <= 5; i++) {
        memset(dp, -1, sizeof(dp));
        ttt = i;
        int temp = solve(0, sx, sy);
        if (ret > temp) {
          ret = temp;
          type = i;
        }
      }
    }
    printf("%d %d\\n", type, ret);
  }
}