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

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

const int INF = 100000000;

int main() {

  int m;
  cin >> m;
  for (int v = 0; v < m; v++) {
    int n;
    cin >> n;
    string str;
    cin >> str;

    int pos = 0;
    int cost = 0;
    for (int i = 0; i <= 2 * n; i += 2) {
      if (i == 0) {
        bool b = false;
        if (str[i] == 'Y') {
          cost += 1;
          pos = n + 1;
          b = true;
        }
        if (str[2 * n] == 'Y') {
          if (!b)
            cost += 3;
          else
            cost += 2;
          pos = 3 * n + 3;
        }
      } else if (i == 2 * n) {
        if (pos != 0) {
          if (str[4 * n - 1] == 'Y') {
            if (pos >= 3 * n + 3 && pos <= 4 * n + 3) {
              cost += (4 * n + 3 - pos) * 2 + 2;
            } else {
              cost += (4 * n + 3 - (pos + 2 * n + 2)) * 2 + 2;
            }
            pos = 4 * n + 3;
          }
          if (str[2 * n - 1] == 'Y') {
            if (pos >= n + 1 && pos <= 2 * n + 1) {
              cost += (2 * n + 1 - pos) * 2 + 2;
            } else {
              cost += (2 * n + 1 - (pos - 2 * n - 2)) * 2 + 2;
            }
            pos = 2 * n + 1;
          }
        } else {
          bool b = false;
          if (str[2 * n - 1] == 'Y') {
            //						if(!b)
            cost += 2 * n + 1;
            b = true;
            //					else
            //					cost+=2;
            pos = 2 * n + 1;
          }
          if (str[4 * n - 1] == 'Y') {
            if (!b)
              cost += 2 * n + 3;
            else
              cost += 2;
            pos = 4 * n + 3;
            // b=true;
          }
        }
      } else {
        if (pos != 0) {
          if (str[2 * n + i] == 'Y' || str[2 * n + i - 1] == 'Y') {
            if (pos >= 3 * n + 3 && pos <= 4 * n + 3) {
              cost += (3 * n + 3 + i / 2 - pos) * 2 + 2;
            } else {
              cost += (3 * n + 3 + i / 2 - (pos + 2 * n + 2)) * 2 + 2;
            }
            pos = 3 * n + 3 + i / 2;
          }
          if (str[i - 1] == 'Y' || str[i] == 'Y') {
            if (pos >= n + 1 && pos <= 2 * n + 1) {
              cost += (n + 1 + i / 2 - pos) * 2 + 2;
            } else {
              cost += (n + 1 + i / 2 - (pos - 2 * n - 2)) * 2 + 2;
            }
            pos = n + 1 + i / 2;
          }
        } else {
          bool b = false;
          if (str[i - 1] == 'Y' || str[i] == 'Y') {
            cost += i + 1;
            pos = n + 1 + i / 2;
            b = true;
          }
          if (str[2 * n + i] == 'Y' || str[2 * n + i - 1] == 'Y') {
            if (!b)
              cost += i + 3;
            else
              cost += 2;
            pos = 3 * n + 3 + i / 2;
          }
        }
      }
    }

    if (pos >= 3 * n + 3 && pos <= 4 * n + 3) {
      cost += (4 * n + 3 - pos) * 2 + 3;
    } else {
      cost += (4 * n + 3 - (pos + 2 * n + 2)) * 2 + 2;
    }

    cout << (cost) / 2 << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

const int MAX_V = 4000;
// d[u][v]Íu©çvÖÌRXgBßÉ×Ú·éGbWÌRXgðüêÄ¨¢½è·êÎæ¢Í¸B
int d[MAX_V][MAX_V];

// ¸_
int V;

// S_ÎÅZoHâèðÆ­O(V^3)AÓª ÁÄà®ì
void warshall_floyd() {
  for (int i = 0; i < V; i++) {
    for (int j = 0; j < V; j++) {
      for (int k = 0; k < V; k++) {
        d[j][k] = min(d[j][k], d[j][i] + d[i][k]);
      }
    }
  }
}

const int INF = 100000000;

int main() {

  int m;
  cin >> m;
  for (int v = 0; v < m; v++) {

    for (int i = 0; i < MAX_V; i++)
      fill(d[i], d[i] + MAX_V, INF);
    for (int i = 0; i < MAX_V; i++)
      d[i][i] = 0;

    int n;
    cin >> n;
    string str;
    cin >> str;

    for (int i = 0; i < 4; i++) {
      for (int j = 0; j <= n; j++) {
        if (i == 0) {
          d[j][n + j + 1] = d[n + j + 1][j] = 1;
          if (j != n) {
            d[j][j + 1] = 2;
            d[j + 1][j] = 2;
          }
        } else if (i == 1) {
          d[n + j + 1][2 * n + 2 + j] = 1;
          d[2 * n + 2 + j][n + j + 1] = 1;
        } else if (i == 2) {
          d[2 * n + 2 + j][3 * n + j + 3] = d[3 * n + j + 3][2 * n + 2 + j] = 1;
          if (j != n) {
            d[2 * n + 2 + j][2 * n + 3 + j] = 2;
            d[2 * n + 3 + j][2 * n + 2 + j] = 2;
          }
        }
      }
    }
    V = 4 * n + 4;
    warshall_floyd();
    int pos = 0;
    int cost = 0;
    for (int i = 0; i <= 2 * n; i += 2) {
      if (i == 0) {
        if (str[i] == 'Y') {
          cost += 1;
          pos = n + 1;
        }
        if (str[2 * n] == 'Y') {
          cost += 3;
          pos = 3 * n + 3;
        }
      } else if (i == 2 * n) {
        if (pos != 0) {
          if (str[2 * n - 1] == 'Y') {
            if (pos >= n + 1 && pos <= 2 * n + 1) {
              cost += (2 * n + 1 - pos) * 2 + 2;
            } else {
              cost += (2 * n + 1 - (pos - 2 * n - 2)) * 2 + 2;
            }
            pos = 2 * n + 1;
          }
          if (str[4 * n - 1] == 'Y') {
            if (pos >= 3 * n + 3 && pos <= 4 * n + 3) {
              cost += (4 * n + 3 - pos) * 2 + 2;
            } else {
              cost += (4 * n + 3 - (pos + 2 * n + 2)) * 2 + 2;
            }
            pos = 4 * n + 3;
          }
        } else {
          bool b = false;
          if (str[4 * n - 1] == 'Y') {
            cost += 2 * n + 3;
            pos = 3 * n + 3;
            b = true;
          }
          if (str[2 * n - 1] == 'Y') {
            if (!b)
              cost += 2 * n + 1;
            else
              cost += 2;
            pos = 2 * n + 1;
          }
        }
      } else {
        if (pos != 0) {
          if (str[2 * n + i] == 'Y' || str[2 * n + i - 1] == 'Y') {
            if (pos >= 3 * n + 3 && pos <= 4 * n + 3) {
              cost += (3 * n + 3 + i / 2 - pos) * 2 + 2;
            } else {
              cost += (3 * n + 3 + i / 2 - (pos + 2 * n + 2)) * 2 + 2;
            }
            pos = 3 * n + 3 + i / 2;
          }
          if (str[i - 1] == 'Y' || str[i] == 'Y') {
            if (pos >= n + 1 && pos <= 2 * n + 1) {
              cost += (n + 1 + i / 2 - pos) * 2 + 2;
            } else {
              cost += (n + 1 + i / 2 - (pos - 2 * n - 2)) * 2 + 2;
            }
            pos = n + 1 + i / 2;
          }
        } else {
          bool b = false;
          if (str[i - 1] == 'Y' || str[i] == 'Y') {
            cost += i + 1;
            pos = n + 1 + i / 2;
            b = true;
          }
          if (str[2 * n + i] == 'Y' || str[2 * n + i - 1] == 'Y') {
            if (!b)
              cost += i + 3;
            else
              cost += 2;
            pos = 3 * n + 3 + i / 2;
          }
        }
      }
    }

    if (pos >= 3 * n + 3 && pos <= 4 * n + 3) {
      cost += (4 * n + 3 - pos) * 2 + 3;
    } else {
      cost += (4 * n + 3 - (pos + 2 * n + 2)) * 2 + 2;
    }

    cout << (cost) / 2 << endl;
  }

  return 0;
}