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

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;

const int INF = 100000000;
// ®©¹éêði[·éXg
vector<int> d[13];

// »ê¼êÌê©çá¤êÖÚ®·éÌÉKvÈ}nb^£
int mhd[13][13];
const int V = 13;

void warshall_floyd() {
  for (int k = 0; k < V; k++)
    for (int i = 0; i < V; i++)
      for (int j = 0; j < V; j++)
        mhd[i][j] = min(mhd[i][j], mhd[i][k] + mhd[k][j]);
}

int main() {

  d[0].push_back(2);

  d[1].push_back(2);
  d[1].push_back(5);

  d[2].push_back(0);
  d[2].push_back(1);
  d[2].push_back(3);
  d[2].push_back(6);

  d[3].push_back(2);
  d[3].push_back(7);

  d[4].push_back(5);

  d[5].push_back(1);
  d[5].push_back(4);
  d[5].push_back(6);
  d[5].push_back(9);

  d[6].push_back(2);
  d[6].push_back(5);
  d[6].push_back(7);
  d[6].push_back(10);

  d[7].push_back(3);
  d[7].push_back(6);
  d[7].push_back(8);
  d[7].push_back(11);

  d[8].push_back(7);

  d[9].push_back(5);
  d[9].push_back(10);

  d[10].push_back(6);
  d[10].push_back(9);
  d[10].push_back(11);
  d[10].push_back(12);

  d[11].push_back(10);
  d[11].push_back(7);

  d[12].push_back(10);

  for (int i = 0; i < 13; i++)
    for (int j = 0; j < 13; j++)
      mhd[i][j] = INF;

  for (int i = 0; i < 13; i++) {
    mhd[i][i] = 0;
    // ¼Ú¹ª éÆ±ëÉRXg1ðãü
    for (int j = 0; j < d[i].size(); j++) {
      int to = d[i][j];
      mhd[i][to] = 1;
    }
  }

  warshall_floyd();

  while (1) {
    set<vector<int>> no;
    vector<int> v;
    int cost = 0;
    for (int j = 0; j < 13; j++) {
      int t;
      cin >> t;
      if (t == -1)
        return 0;
      v.push_back(t);
      if (t != 0)
        cost += mhd[t][j];
    }
    // Å©ç»ëÁÄ¢éê
    if (cost == 0) {
      cout << 0 << endl;
      continue;
    }
    queue<pair<int, vector<int>>> *prv = new queue<pair<int, vector<int>>>();
    queue<pair<int, vector<int>>> *nxt = new queue<pair<int, vector<int>>>();
    prv->push(make_pair(cost, v));
    int cnt = 0;
    bool isGoal = false;
    // 20èÜÅTõð©¯é
    while (prv->size()) {
      while (prv->size()) {
        vector<int> b = prv->front().second;
        int cos = prv->front().first;
        prv->pop();
        vector<int> emp;
        for (int i = 0; i < b.size(); i++)
          if (b[i] == 0)
            emp.push_back(i);
        // óÌêðêÂIÑAJÚ·é
        for (int i = 0; i < emp.size(); i++) {
          int num = emp[i];
          for (int j = 0; j < d[num].size(); j++) {
            // ¡ñg¤board
            vector<int> bt = b;
            // Ú®æ
            int to = d[num][j];
            if (bt[to] == 0)
              continue;
            swap(bt[to], bt[num]);
            if (no.find(bt) != no.end())
              continue;
            no.insert(bt);
            // Ú®æÌ]¿l(0ÉÈÁ½çI¹)
            int toCost = cos - mhd[to][b[to]] + mhd[b[to]][num];
            // goal
            if (toCost == 0) {
              isGoal = true;
              break;
            }
            // »Ýn_©çS[É½Çè¯é©Ç¤©
            // ³Èç}ëè
            if (!(toCost > (19 - cnt)))
              nxt->push(make_pair(toCost, bt));
          }
          if (isGoal)
            break;
        }
        if (isGoal)
          break;
      }
      if (isGoal)
        break;
      swap(prv, nxt);
      cnt++;
      // 20èðz¦Ä¢é
      if (cnt >= 20) {
        break;
      }
    }
    if (isGoal) {
      cout << cnt + 1 << endl;
    } else {
      cout << "NA" << endl;
    }
    delete prv, nxt;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <vector>

using namespace std;

int d[5][5];
const int INF = 100000000;

int minDepth = INF;

const int dx[] = {-1, 0, 0, 1};
const int dy[] = {0, 1, -1, 0};

// ツ閉敖優ツ静ヲツづ探ツ催オ
int bfs() {
  set<vector<vector<int>>> used;

  queue<vector<vector<int>>> *prv = new queue<vector<vector<int>>>();
  queue<vector<vector<int>>> *nxt = new queue<vector<vector<int>>>();
  vector<vector<int>> v;
  for (int i = 0; i < 5; i++) {
    vector<int> vv;
    for (int j = 0; j < 5; j++) {
      vv.push_back(d[i][j]);
    }
    v.push_back(vv);
  }
  used.insert(v);

  prv->push(v);
  int cnt = 0;
  while (prv->size()) {
    if (cnt == 21)
      break;
    cout << cnt << endl;
    while (prv->size()) {
      vector<vector<int>> cv = prv->front();
      prv->pop();
      if (cv[1][1] == 1 && cv[1][2] == 2 && cv[1][3] == 3 && cv[2][0] == 4 &&
          cv[2][1] == 5 && cv[2][2] == 6 && cv[2][3] == 7 && cv[2][4] == 8 &&
          cv[3][1] == 9 && cv[3][2] == 10 && cv[3][3] == 11) {
        return cnt;
      }
      // ツゼツδ債づ個暗環置ツづーツ探ツ催オ
      vector<pair<int, int>> zeroP;
      for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
          if (cv[i][j] == 0)
            zeroP.push_back(make_pair(i, j));
        }
      }
      // ツ督ョツつゥツつケツづゥツ督ョツつゥツつオツ陛サツづーツつキツづ猟づ篠篠つキ
      for (int i = 0; i < zeroP.size(); i++) {
        int cx = zeroP[i].second;
        int cy = zeroP[i].first;
        // ツ四ツ陛サツ古シツつキツづ猟づ篠篠つキ
        for (int j = 0; j < 4; j++) {
          int nx = cx + dx[j];
          int ny = cy + dy[j];
          if (nx >= 0 && ny >= 0 && nx < 5 && ny < 5) {
            if (ny == 0 && nx == 2) {
              if (cv[ny][nx] != INF && cv[ny][nx] != 0) {
                swap(cv[ny][nx], cv[cy][cx]);
                if (used.find(cv) == used.end()) {
                  nxt->push(cv);
                  used.insert(cv);
                }
                swap(cv[ny][nx], cv[cy][cx]);
              }
            } else if (ny == 1 && nx >= 1 && nx <= 3) {
              if (cv[ny][nx] != INF && cv[ny][nx] != 0) {
                swap(cv[ny][nx], cv[cy][cx]);
                if (used.find(cv) == used.end()) {
                  nxt->push(cv);
                  used.insert(cv);
                }
                swap(cv[ny][nx], cv[cy][cx]);
              }
            } else if (ny == 2 && nx >= 0 && nx <= 4) {
              if (cv[ny][nx] != INF && cv[ny][nx] != 0) {
                swap(cv[ny][nx], cv[cy][cx]);
                if (used.find(cv) == used.end()) {
                  nxt->push(cv);
                  used.insert(cv);
                }
                swap(cv[ny][nx], cv[cy][cx]);
              }
            } else if (ny == 3 && nx >= 1 && nx <= 3) {
              if (cv[ny][nx] != INF && cv[ny][nx] != 0) {
                swap(cv[ny][nx], cv[cy][cx]);
                if (used.find(cv) == used.end()) {
                  nxt->push(cv);
                  used.insert(cv);
                }
                swap(cv[ny][nx], cv[cy][cx]);
              }
            } else if (ny == 4 && nx == 2) {
              if (cv[ny][nx] != INF && cv[ny][nx] != 0) {
                swap(cv[ny][nx], cv[cy][cx]);
                if (used.find(cv) == used.end()) {
                  nxt->push(cv);
                  used.insert(cv);
                }
                swap(cv[ny][nx], cv[cy][cx]);
              }
            }
          }
        }
      }
    }
    swap(prv, nxt);
    cnt++;
  }
  return INF;
}

// ツ深ツつウツ優ツ静ヲツづ探ツ催オ
void dfs(vector<pair<int, int>> zeroP, int depth) {
  if (minDepth < depth)
    return;
  if (d[1][1] == 1 && d[1][2] == 2 && d[1][3] == 3 && d[2][0] == 4 &&
      d[2][1] == 5 && d[2][2] == 6 && d[2][3] == 7 && d[2][4] == 8 &&
      d[3][1] == 9 && d[3][2] == 10 && d[3][3] == 11) {
    minDepth = min(minDepth, depth);
    return;
  } else if (depth == 20)
    return;

  // ツ督ョツつゥツつケツづゥツ督ョツつゥツつオツ陛サツづーツつキツづ猟づ篠篠つキ
  for (int i = 0; i < zeroP.size(); i++) {
    int cx = zeroP[i].second;
    int cy = zeroP[i].first;
    // ツ四ツ陛サツ古シツつキツづ猟づ篠篠つキ
    for (int j = 0; j < 4; j++) {
      int nx = cx + dx[i];
      int ny = cy + dy[i];
      if (nx >= 0 && ny >= 0 && nx < 5 && ny < 5) {
        if (ny == 0 && nx == 2) {
          if (d[ny][nx] != INF && d[ny][nx] != 0) {
            swap(d[ny][nx], d[cy][cx]);
            vector<pair<int, int>> nzp;
            if (i == 0) {
              nzp.push_back(zeroP[1]);
            } else {
              nzp.push_back(zeroP[0]);
            }
            nzp.push_back(make_pair(ny, nx));
            dfs(nzp, depth + 1);
            swap(d[ny][nx], d[cy][cx]);
          }
        } else if (ny == 1 && nx >= 1 && nx <= 3) {
          if (d[ny][nx] != INF && d[ny][nx] != 0) {
            swap(d[ny][nx], d[cy][cx]);
            vector<pair<int, int>> nzp;
            if (i == 0) {
              nzp.push_back(zeroP[1]);
            } else {
              nzp.push_back(zeroP[1]);
            }
            nzp.push_back(make_pair(ny, nx));
            dfs(nzp, depth + 1);
            swap(d[ny][nx], d[cy][cx]);
          }

        } else if (ny == 2 && nx >= 0 && nx <= 4) {
          if (d[ny][nx] != INF && d[ny][nx] != 0) {
            swap(d[ny][nx], d[cy][cx]);
            vector<pair<int, int>> nzp;
            if (i == 0) {
              nzp.push_back(zeroP[1]);
            } else {
              nzp.push_back(zeroP[1]);
              nzp.push_back(make_pair(ny, nx));
              dfs(nzp, depth + 1);
              swap(d[ny][nx], d[cy][cx]);
            }
          }

        } else if (ny == 3 && nx >= 1 && nx <= 3) {
          if (d[ny][nx] != INF && d[ny][nx] != 0) {
            swap(d[ny][nx], d[cy][cx]);
            vector<pair<int, int>> nzp;
            if (i == 0) {
              nzp.push_back(zeroP[1]);
            } else {
              nzp.push_back(zeroP[1]);
            }
            nzp.push_back(make_pair(ny, nx));
            dfs(nzp, depth + 1);
            swap(d[ny][nx], d[cy][cx]);
          }

        } else if (ny == 4 && nx == 2) {
          if (d[ny][nx] != INF && d[ny][nx] != 0) {
            swap(d[ny][nx], d[cy][cx]);
            vector<pair<int, int>> nzp;
            if (i == 0) {
              nzp.push_back(zeroP[1]);
            } else {
              nzp.push_back(zeroP[1]);
            }
            nzp.push_back(make_pair(ny, nx));
            dfs(nzp, depth + 1);
            swap(d[ny][nx], d[cy][cx]);
          }
        }
      }
    }
  }
}

int main() {

  for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
      d[i][j] = INF;
    }
  }
  while (cin >> d[0][2] && d[0][2] != -1) {
    minDepth = INF;
    cin >> d[1][1] >> d[1][2] >> d[1][3];
    for (int i = 0; i < 5; i++) {
      cin >> d[2][i];
    }
    cin >> d[3][1] >> d[3][2] >> d[3][3] >> d[4][2];

    vector<pair<int, int>> emp;
    for (int i = 0; i < 5; i++) {
      for (int j = 0; j < 5; j++) {
        if (d[i][j] == 0)
          emp.push_back(make_pair(i, j));
      }
    }

    // dfs(emp,1);
    minDepth = bfs();

    if (minDepth == INF)
      cout << "NA" << endl;
    else
      cout << minDepth << endl;

    // ツ篠淞づ個渉按猟敖づ個つスツづ淞づ個渉可甘コツ可サ
    for (int i = 0; i < 5; i++) {
      for (int j = 0; j < 5; j++) {
        d[i][j] = INF;
      }
    }
  }

  return 0;
}