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


#include <map>
#include <queue>
#include <stdio.h>
using namespace std;

int a[8] = {0};
int direction[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
map<int, int> exitmap;

struct array {
  int map[2][4];
  int step;
};

bool isInMap(int x, int y) {
  if (x < 0 || x >= 2)
    return 0;
  if (y < 0 || y >= 4)
    return 0;
  else
    return 1;
}

void bfs() {
  array begin = {{0, 1, 2, 3, 4, 5, 6, 7}, 0};
  queue<array> que;
  que.push(begin);
  exitmap.insert(make_pair(1234567, 0));
  while (!que.empty()) {
    int x, y;
    array temp = que.front();
    que.pop();
    for (int i = 0; i < 2; i++)
      for (int j = 0; j < 4; j++)
        if (temp.map[i][j] == 0) {
          x = i;
          y = j;
        }
    for (int k = 0; k < 4; k++) {
      if (isInMap(x + direction[k][0], y + direction[k][1])) {
        array newarray = temp;
        int r = newarray.map[x][y];
        newarray.map[x][y] =
            newarray.map[x + direction[k][0]][y + direction[k][1]];
        newarray.map[x + direction[k][0]][y + direction[k][1]] = r;
        newarray.step = temp.step + 1;
        int num = 0;
        for (int i = 0; i < 2; i++)
          for (int j = 0; j < 4; j++)
            num = num * 10 + newarray.map[i][j];
        if (exitmap.find(num) == exitmap.end()) {
          que.push(newarray);
          exitmap.insert(make_pair(num, newarray.step));
        }
      }
    }
  }
}

int main() {
  bfs();
  int temp;
  while (~scanf("%d", &temp)) {
    int num = temp;
    for (int i = 1; i < 8; i++) {
      scanf("%d", &temp);
      num = num * 10 + temp;
    }
    printf("%d\\n", exitmap[num]);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
int a[8];
int step;

bool endsearch;
map<vector<int>, bool> visited;

bool istarget(vector<int> vec) {
  for (int i = 0; i < 8; i++) {
    if (vec[i] != i) {
      return false;
    }
  }
  return 1;
}
int pos0(vector<int> vec) {
  int i;
  for (i = 0; i < 8; i++) {
    if (vec[i] == 0)
      return i;
  }
}
queue<vector<int>> bfs(queue<vector<int>> que) {
  int quesize = que.size();
  step++;
  for (int i = 0; i < quesize; i++) {
    if (istarget(que.front())) {
      endsearch = 1;
      step--;
      return que;
    } else {
      if (!visited[que.front()]) {
        visited[que.front()] = 1;
        int position = pos0(que.front());

        vector<int> tmp(que.front());
        if (position < 4) {
          tmp[position] = que.front()[position + 4];
          tmp[position + 4] = que.front()[position];
          que.push(tmp);
        } else {
          tmp[position] = que.front()[position - 4];
          tmp[position - 4] = que.front()[position];
          que.push(tmp);
        }
        if (position != 0 && position != 4) {
          vector<int> tmp1(que.front());
          tmp1[position] = que.front()[position - 1];
          tmp1[position - 1] = que.front()[position];
          que.push(tmp1);
        }
        if (position != 3 && position != 7) {
          vector<int> tmp1(que.front());
          tmp1[position] = que.front()[position + 1];
          tmp1[position + 1] = que.front()[position];
          que.push(tmp1);
        }

        que.pop();
      } else {
        que.pop();
      }
    }
  }
  return que;
}

int main() {
  while (cin >> a[0] >> a[1] >> a[2] >> a[3] >> a[4] >> a[5] >> a[6] >> a[7]) {
    vector<int> vec(a, a + 8);
    queue<vector<int>> que;
    visited.clear();
    step = 0;
    endsearch = 0;
    que.push(vec);
    while (que.size() != 0) {
      que = bfs(que);
      if (endsearch) {
        cout << step << endl;
        break;
      }
    }
  }
  // system("pause");
  return 0;
}