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

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

string ans;

class board {
public:
  int h, c;
  string s;
  board(int x, int y, string z) {
    h = x;
    c = y;
    s = z;
  }
  const bool operator<(const board x) const { return h + c > x.h + x.c; }
};

int score(string s) {
  int res = 0, m;
  for (int i = 0; i < 25; i++) {
    if (s[i] != 'x' && s[i] != 'o') {
      for (int j = 0; j < 25; j++) {
        if (s[i] == ans[j]) {
          m = abs(i / 5 - j / 5) + abs(i % 5 - j % 5);
          // cout << s[i] << " " << m << endl;
          break;
        }
      }
      res += m;
    }
  }
  return res;
}

char trans(int x) {
  if (!x)
    return 'o';
  else
    return 'a' + x - 1;
}

int main() {
  string s;
  ans = "xxoxxxabcxdefghxijkxxxoxx";
  int tmp, cost;

  for (;;) {
    cin >> tmp;
    if (tmp < 0)
      break;

    s = "xx";
    s += trans(tmp);
    s += "xxx";
    for (int i = 0; i < 3; i++) {
      cin >> tmp;
      s += trans(tmp);
    }
    s += "x";
    for (int i = 0; i < 5; i++) {
      cin >> tmp;
      s += trans(tmp);
    }
    s += "x";
    for (int i = 0; i < 3; i++) {
      cin >> tmp;
      s += trans(tmp);
    }
    s += "xxx";
    cin >> tmp;
    s += trans(tmp);
    s += "xx";

    priority_queue<board> q;
    q.push(board(score(s), 0, s));

    map<string, bool> m;
    m[s] = 0;
    while (q.size()) {
      board p = q.top();
      q.pop();
      int dx[] = {-5, 1, 5, -1};
      /*
      cout << p.h << " " << p.c;
      for(int i=0;i<25;i++){
        if(!(i%5))cout << endl;
        cout << p.s[i] << " ";
      }
      cout << endl;
      */
      cost = p.c;
      if (p.s == ans)
        break;
      if (p.h + cost > 20) {
        cost = 21;
        break;
      }
      m[p.s] = true;
      for (int i = 0; i < 25; i++) {
        if (p.s[i] == 'o') {
          for (int j = 0; j < 4; j++) {
            int sx = i + dx[j];
            string str = p.s;
            if (sx < 0 || 25 <= sx)
              continue;
            if (str[sx] == 'x' || str[sx] == 'o')
              continue;
            swap(str[i], str[sx]);
            if (!m[str])
              q.push(board(score(str), cost + 1, str));
          }
        }
      }
    }

    if (cost > 20)
      cout << "NA\\n";
    else
      cout << cost << endl;
    // score(s);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

string ans;

class board {
public:
  int h, c;
  string s;
  board(int x, int y, string z) {
    h = x;
    c = y;
    s = z;
  }
  const bool operator<(const board x) const {
    return (h + c > x.h + x.c) || (h + c == x.h + x.c && c > x.c);
  }
};

int score(string s) {
  int res = 0;
  int o[2];
  o[0] = -1;
  o[1] = -1;
  for (int i = 0; i < 25; i++) {
    if (s[i] != 'x') {
      int m;
      if (s[i] != 'o') {
        for (int j = 0; j < 25; j++) {
          if (s[i] == ans[j]) {
            int a = abs(i - j);
            int tmp = a / 5 + a % 5;
            m = tmp;
            break;
          }
        }
      } else {
        if (o[0] < 0) {
          for (int j = 0; j < 25; j++) {
            if (s[i] == ans[j]) {
              int a = abs(i - j);
              if (o[0] < 0)
                o[0] = a / 5 + a % 5;
              else
                o[1] = a / 5 + a % 5;
            }
          }
        } else {
          m = -1;
          for (int j = 0; j < 25; j++) {
            if (s[i] == ans[j]) {
              int a = abs(i - j);
              int tmp = a / 5 + a % 5;
              if (m < 0)
                m = tmp + o[1];
              else
                m = min(m, tmp + o[0]);
            }
          }
        }
      }
      res += m;
    }
  }
  return res / 2;
}

char trans(int x) {
  if (!x)
    return 'o';
  else
    return 'a' + x - 1;
}

int main() {
  string s;
  ans = "xxoxxxabcxdefghxijkxxxoxx";
  int tmp, cost;

  for (;;) {
    cin >> tmp;
    if (tmp < 0)
      break;

    s = "xx";
    s += trans(tmp);
    s += "xxx";
    for (int i = 0; i < 3; i++) {
      cin >> tmp;
      s += trans(tmp);
    }
    s += "x";
    for (int i = 0; i < 5; i++) {
      cin >> tmp;
      s += trans(tmp);
    }
    s += "x";
    for (int i = 0; i < 3; i++) {
      cin >> tmp;
      s += trans(tmp);
    }
    s += "xxx";
    cin >> tmp;
    s += trans(tmp);
    s += "xx";

    priority_queue<board> q1;
    q1.push(board(score(s), 0, s));

    map<string, bool> m1;
    m1[s] = 0;
    while (q1.size()) {
      board p = q1.top();
      q1.pop();
      int dx[] = {-5, 1, 5, -1};
      // cout << p.h << " " << p.c << " " << p.s << endl;
      cost = p.c;
      if (p.s == ans)
        break;
      if (cost > 20)
        break;

      for (int i = 0; i < 25; i++) {
        if (p.s[i] == 'o') {
          for (int j = 0; j < 4; j++) {
            int sx = i + dx[j];
            string str = p.s;
            if (sx < 0 || 25 <= sx)
              continue;
            if (str[sx] == 'x' || str[sx] == 'o')
              continue;
            swap(str[i], str[sx]);
            if (!m1[str]) {
              q1.push(board(score(str), cost + 1, str));
              m1[str] = cost + 1;
            }
          }
        }
      }
    }

    if (cost > 20)
      cout << "NA\\n";
    else
      cout << cost << endl;
  }
}