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

#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

int idx;
char s[11];
bool res;

#define ASRT                                                                   \\
  if (!res)                                                                    \\
  return 0

int N() {
  if (!('1' <= s[idx] && s[idx] <= '9')) {
    res = false;
    ASRT;
  }
  int a = 0;
  while (isdigit(s[idx]))
    a = 10 * a + s[idx++] - '0';
  return a;
}

int N0() {
  int a;
  if (s[idx] == '0') {
    idx++;
    a = 0;
  } else
    a = N();
  ASRT;
  return a;
}

int Ex2() {
  int a = N0();
  ASRT;
  while (s[idx] == '*' || s[idx] == '/') {
    char op = s[idx++];
    int b = N0();
    ASRT;
    if (op == '/' && (b == 0 || a % b != 0)) {
      res = false;
      ASRT;
    }

    if (op == '*')
      a *= b;
    if (op == '/')
      a /= b;
  }
  return a;
}

int Ex1() {
  int a = Ex2();
  ASRT;
  while (s[idx] == '+' || s[idx] == '-') {
    char op = s[idx++];
    int b = Ex2();
    ASRT;

    if (op == '+')
      a += b;
    if (op == '-')
      a -= b;
  }
  return a;
}

int Eq() {
  int a = Ex1();
  ASRT;
  if (s[idx++] != '=') {
    res = false;
    ASRT;
  }
  int b = Ex1();
  ASRT;
  if (a != b || s[idx])
    res = false;
  return 0;
}

bool parse() {
  if (strlen(s) <= 2)
    return true;
  idx = 0;
  res = true;
  Eq();
  return res;
}

int h, w;
char B[10][11];

bool calc_no_space() {
  rep(i, h) {
    int p = -1;
    rep(j, w + 1) {
      if (j < w && B[i][j] != '#') {
        if (p == -1)
          p = j;
      } else {
        if (p != -1) {
          rep(k, j - p) s[k] = B[i][p + k];
          s[j - p] = '\\0';
          if (strchr(s, '.') == 0 && !parse())
            return false;
          p = -1;
        }
      }
    }
  }

  rep(j, w) {
    int p = -1;
    rep(i, h + 1) {
      if (i < h && B[i][j] != '#') {
        if (p == -1)
          p = i;
      } else {
        if (p != -1) {
          rep(k, i - p) s[k] = B[p + k][j];
          s[i - p] = '\\0';
          if (strchr(s, '.') == 0 && !parse())
            return false;
          p = -1;
        }
      }
    }
  }

  return true;
}

char key[10];
int n, qx[10], qy[10];
bool dfs(int k, bool *used) {
  if (k == n)
    return true;

  rep(l, n) if (!used[l]) {
    used[l] = true;
    B[qy[k]][qx[k]] = key[l];

    bool ok = true;
    int x1 = qx[k], x2 = qx[k];
    while (x1 >= 0 && B[qy[k]][x1] != '#')
      x1--;
    x1++;
    while (x2 < w && B[qy[k]][x2] != '#')
      x2++;
    rep(j, x2 - x1) s[j] = B[qy[k]][x1 + j];
    s[x2 - x1] = '\\0';
    if (count(s, s + x2 - x1, '.') == 0 && !parse())
      ok = false;

    int y1 = qy[k], y2 = qy[k];
    while (y1 >= 0 && B[y1][qx[k]] != '#')
      y1--;
    y1++;
    while (y2 < h && B[y2][qx[k]] != '#')
      y2++;
    rep(i, y2 - y1) s[i] = B[y1 + i][qx[k]];
    s[y2 - y1] = '\\0';
    if (count(s, s + y2 - y1, '.') == 0 && !parse())
      ok = false;

    if (ok && dfs(k + 1, used))
      return true;

    used[l] = false;
    B[qy[k]][qx[k]] = '.';
  }

  return false;
}

int main() {
  for (; scanf("%d%d", &h, &w), h;) {
    rep(i, h) scanf("%s", B[i]);
    scanf("%d", &n);
    rep(i, n) scanf(" %c", key + i);

    n = 0;
    rep(i, h) rep(j, w) if (B[i][j] == '.') qx[n] = j, qy[n] = i, n++;

    bool used[10] = {};
    puts(calc_no_space() && dfs(0, used) ? "Yes" : "No");
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

int idx;
char s[11];
bool res;

#define ASRT                                                                   \\
  if (!res)                                                                    \\
  return 0

int N() {
  if (!('1' <= s[idx] && s[idx] <= '9')) {
    res = false;
    ASRT;
  }
  int a = 0;
  while (isdigit(s[idx]))
    a = 10 * a + s[idx++] - '0';
  return a;
}

int N0() {
  int a;
  if (s[idx] == '0') {
    idx++;
    a = 0;
  } else
    a = N();
  ASRT;
  return a;
}

int Ex() {
  int a = N0();
  ASRT;
  while (s[idx] == '+' || s[idx] == '-' || s[idx] == '*' || s[idx] == '/') {
    char op = s[idx++];
    int b = N0();
    ASRT;
    if (op == '/' && (b == 0 || a % b != 0)) {
      res = false;
      ASRT;
    }

    if (op == '+')
      a += b;
    if (op == '-')
      a -= b;
    if (op == '*')
      a *= b;
    if (op == '/')
      a /= b;
  }
  return a;
}

int Eq() {
  int a = Ex();
  ASRT;
  if (s[idx++] != '=') {
    res = false;
    ASRT;
  }
  int b = Ex();
  ASRT;
  if (a != b || s[idx])
    res = false;
}

bool parse() {
  if (strlen(s) <= 2)
    return true;
  idx = 0;
  res = true;
  Eq();
  return res;
}

int h, w;
char B[10][11];

vector<int> head[2][10], tail[2][10];
void preprocess() {
  rep(i, h) {
    head[0][i].clear();
    tail[0][i].clear();

    int p = -1;
    rep(j, w + 1) {
      if (j < w && B[i][j] != '#') {
        if (p == -1)
          p = j;
      } else {
        if (p != -1) {
          head[0][i].push_back(p);
          tail[0][i].push_back(j);
          p = -1;
        }
      }
    }
  }

  rep(j, w) {
    head[1][j].clear();
    tail[1][j].clear();

    int p = -1;
    rep(i, h + 1) {
      if (i < h && B[i][j] != '#') {
        if (p == -1)
          p = i;
      } else {
        if (p != -1) {
          head[1][j].push_back(p);
          tail[1][j].push_back(i);
          p = -1;
        }
      }
    }
  }
}

bool calc_no_space() {
  rep(i, h) {
    rep(k, head[0][i].size()) {
      rep(l, tail[0][i][k] - head[0][i][k]) s[l] = B[i][head[0][i][k] + l];
      s[tail[0][i][k] - head[0][i][k]] = '\\0';
      if (strchr(s, '.') == 0) {
        if (!parse())
          return false;
        head[0][i].erase(head[0][i].begin() + k);
        tail[0][i].erase(tail[0][i].begin() + k);
        k--;
      }
    }
  }

  rep(j, w) {
    rep(k, head[1][j].size()) {
      rep(l, tail[1][j][k] - head[1][j][k]) s[l] = B[head[1][j][k] + l][j];
      s[tail[1][j][k] - head[1][j][k]] = '\\0';
      if (strchr(s, '.') == 0) {
        if (!parse())
          return false;
        head[1][j].erase(head[1][j].begin() + k);
        tail[1][j].erase(tail[1][j].begin() + k);
        k--;
      }
    }
  }

  return true;
}

bool isvalid() {
  rep(i, h) {
    rep(k, head[0][i].size()) {
      rep(l, tail[0][i][k] - head[0][i][k]) s[l] = B[i][head[0][i][k] + l];
      s[tail[0][i][k] - head[0][i][k]] = '\\0';
      if (!parse())
        return false;
    }
  }

  rep(j, w) {
    rep(k, head[1][j].size()) {
      rep(l, tail[1][j][k] - head[1][j][k]) s[l] = B[head[1][j][k] + l][j];
      s[tail[1][j][k] - head[1][j][k]] = '\\0';
      if (!parse())
        return false;
    }
  }

  return true;
}

int main() {
  for (; scanf("%d%d", &h, &w), h;) {
    rep(i, h) scanf("%s", B[i]);
    int n;
    scanf("%d", &n);
    char key[10];
    rep(i, n) scanf(" %c", key + i);

    n = 0;
    int qx[10], qy[10];
    rep(i, h) rep(j, w) if (B[i][j] == '.') qx[n] = j, qy[n] = i, n++;

    preprocess();

    bool ok = false;
    if (calc_no_space()) {
      int p[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
      do {
        rep(k, n) B[qy[k]][qx[k]] = key[p[k]];
        if (isvalid()) {
          ok = true;
          break;
        }
      } while (next_permutation(p, p + n));
    } else
      for (;;)
        ;
    puts(ok ? "Yes" : "No");
  }

  return 0;
}