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


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

std::vector<std::string> buf(8);

// Eñ]ÉiÞûüÅ³ÊÌCfbNXÆAEÌCfbNXðwèµÄãÌCfbNXðÔ·
inline int uppderindex(int front, int right) {
  int table[][6] = {{0, 3, 5, 2, 4, 0}, {4, 0, 1, 6, 0, 3}, {2, 6, 0, 0, 1, 5},
                    {5, 1, 0, 0, 6, 2}, {3, 0, 6, 1, 0, 4}, {0, 4, 2, 5, 3, 0}};
  return table[front][right] - 1;
}
// ½Î¤ÌCfbNXðÔ·
inline int otherindex(int h) { return 5 - h; }
inline int otherc(int c) { return c + (c > 96 ? -32 : 32); }
// ³ÊðwèµÄOüÌCfbNXðÔ·
int aroundindex(int front, int idx) {
  for (int i = 0, cnt = 0; i < 6; i++) {
    if (front != i && otherindex(front) != i) {
      if (cnt == idx)
        return i;
      cnt++;
    }
  }
  return 0;
}

int dindexlist[4];
int uindexlist[4];
int firstindex;
int firstindex2;

bool same(int *arr) {
  for (int i = 0; i < 4; i++) {
    if (otherc(buf[arr[i]][otherindex(dindexlist[i])]) !=
        buf[(arr + 4)[i]][uindexlist[i]])
      return false;
  }
  return true;
}

bool check_around2(int *d, int idx, int nest, int *arr) {
  int c = otherc(buf[*d][idx]);
  if (nest == 3) {
    // ±êÍÅÌ³¢±ëÆêv·é©ð©éàÌÈÌÅÁÊ
    for (int around = 0; around < 4; around++) {
      int ar = aroundindex(firstindex2, around);
      if (c ==
          buf[*(d - 3)]
             [ar]) { // ¡Ì¶ª éÆl¦éÌÍÊ|I
        // ÅÌ³¢±ëÌãCfbNX
        arr[0] = uppderindex(ar, firstindex2);
        return true;
      }
    }
    return false;
  }

  // Ì³¢±ë
  d++;
  for (int i = 0; i < 6; i++) {
    if (c == buf[*d][i]) {
      for (int around = 0; around < 4; around++) {
        int ar = aroundindex(i, around);

        // CfbNXÌXg¶áÈ­Ä¶ñÉµ½Ù¤ª¢¢©È
        arr[nest + 1] = uppderindex(i, ar);
        if (check_around2(d, ar, nest + 1, arr)) {
          if (nest == 2) {
            // OóÉÂÈªÁÄéÌªmèµÄÜ·
            if (same(d - 7))
              return true;
          } else {
            return true;
          }
        }
      }
    }
  }
  return false;
}

bool check2(int *d, int *idxlist) {
  for (
      int i = 0; i < 6;
      i++) { // ³¢±ëÌ»ê¼êÌ¶ÉÂ¢Ä²×é
    if (check_around2(d, firstindex2 = i, 0, idxlist))
      return true;
  }
  return false;
}

bool check_around(int *d, int idx, int nest, int *arr) {
  int c = otherc(buf[*d][idx]);
  if (nest == 3) {
    // ±êÍÅÌ³¢±ëÆêv·é©ð©éàÌÈÌÅÁÊ
    for (int around = 0; around < 4; around++) {
      int ar = aroundindex(firstindex, around);
      if (c ==
          buf[*(d - 3)]
             [ar]) { // ¡Ì¶ª éÆl¦éÌÍÊ|I
        // ÅÌ³¢±ëÌãCfbNX
        arr[0] = uppderindex(ar, firstindex);
        return true;
      }
    }
    return false;
  }

  // Ì³¢±ë
  d++;
  for (int i = 0; i < 6; i++) {
    if (c == buf[*d][i]) {
      for (int around = 0; around < 4; around++) {
        int ar = aroundindex(i, around);

        // CfbNXÌXg¶áÈ­Ä¶ñÉµ½Ù¤ª¢¢©È
        arr[nest + 1] = uppderindex(i, ar);
        if (check_around(d, ar, nest + 1, arr)) {
          if (nest == 2) {
            // OóÉÂÈªÁÄéÌªmèµÄÜ·
            if (check2(d + 1, uindexlist))
              return true;
          } else {
            return true;
          }
        }
      }
    }
  }
  return false;
}

bool check(int *d, int *idxlist) {
  for (
      int i = 0; i < 6;
      i++) { // ³¢±ëÌ»ê¼êÌ¶ÉÂ¢Ä²×é
    if (check_around(d, firstindex = i, 0, idxlist))
      return true;
  }
  return false;
}

bool check_around_pre(int *d, int idx, int nest) {
  int c = otherc(buf[*d][idx]);
  if (nest == 3) {
    // ±êÍÅÌ³¢±ëÆêv·é©ð©éàÌÈÌÅÁÊ
    for (int around = 0; around < 4; around++) {
      int ar = aroundindex(firstindex, around);
      if (c == buf[*(d - 3)][ar])
        return true;
    }
    return false;
  }

  // Ì³¢±ë
  d++;
  for (int i = 0; i < 6; i++) {
    if (c == buf[*d][i]) {
      for (int around = 0; around < 4; around++) {
        int ar = aroundindex(i, around);
        if (check_around_pre(d, ar, nest + 1))
          return true;
      }
    }
  }
  return false;
}

bool check_pre(int *d) {
  for (
      int i = 0; i < 6;
      i++) { // ³¢±ëÌ»ê¼êÌ¶ÉÂ¢Ä²×é
    if (check_around_pre(d, firstindex = i, 0))
      return true;
  }
  return false;
}

bool get() {
  for (int i = 0; i < 8; ++i) {
    std::getline(std::cin, buf[i]);
    if (buf[i] == "0")
      return false;
  }
  return true;
}
int main() {
  while (get()) {
    int d[8] = {0, 1, 2, 3, 4, 5, 6, 7};
    do {
      // Æè ¦¸2ÂÚÌXgÍìÁÄÌÄçêéñª½·¬éÌÅAæÉ1ÂÅà¶Ý·é©Ç¤©Å}Øè·é
      if (check_pre(d) && check_pre(d + 4) && check(d, dindexlist)) {
        puts("YES");
        goto next;
      }
    } while (std::next_permutation(d + 1, d + 8));
    puts("NO");
  next:;
  }
}

Error: TLE

Fixed submission:



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

std::vector<std::string> buf(8);

// Eñ]ÉiÞûüÅ³ÊÌCfbNXÆAEÌCfbNXðwèµÄãÌCfbNXðÔ·
inline int uppderindex(int front, int right) {
  int table[][6] = {{0, 3, 5, 2, 4, 0}, {4, 0, 1, 6, 0, 3}, {2, 6, 0, 0, 1, 5},
                    {5, 1, 0, 0, 6, 2}, {3, 0, 6, 1, 0, 4}, {0, 4, 2, 5, 3, 0}};
  return table[front][right] - 1;
}
// ½Î¤ÌCfbNXðÔ·
inline int otherindex(int h) { return 5 - h; }
inline int otherc(int c) { return c + (c > 96 ? -32 : 32); }
// ³ÊðwèµÄOüÌCfbNXðÔ·
int aroundindex(int front, int idx) {
  for (int i = 0, cnt = 0; i < 6; i++) {
    if (front != i && otherindex(front) != i) {
      if (cnt == idx)
        return i;
      cnt++;
    }
  }
  return 0;
}

int dindexlist[4];
int uindexlist[4];
int firstindex;
int firstindex2;

bool same(int *arr) {
  for (int i = 0; i < 4; i++) {
    if (otherc(buf[arr[i]][otherindex(dindexlist[i])]) !=
        buf[(arr + 4)[i]][uindexlist[i]])
      return false;
  }
  return true;
}

bool check_around2(int *d, int idx, int nest, int *arr) {
  int c = otherc(buf[*d][idx]);
  if (nest == 3) {
    // ±êÍÅÌ³¢±ëÆêv·é©ð©éàÌÈÌÅÁÊ
    for (int around = 0; around < 4; around++) {
      int ar = aroundindex(firstindex2, around);
      if (c ==
          buf[*(d - 3)]
             [ar]) { // ¡Ì¶ª éÆl¦éÌÍÊ|I
        // ÅÌ³¢±ëÌãCfbNX
        arr[0] = uppderindex(ar, firstindex2);
        return true;
      }
    }
    return false;
  }

  // Ì³¢±ë
  d++;
  for (int i = 0; i < 6; i++) {
    if (c == buf[*d][i]) {
      for (int around = 0; around < 4; around++) {
        int ar = aroundindex(i, around);

        // CfbNXÌXg¶áÈ­Ä¶ñÉµ½Ù¤ª¢¢©È
        arr[nest + 1] = uppderindex(i, ar);
        if (check_around2(d, ar, nest + 1, arr)) {
          if (nest == 2) {
            // OóÉÂÈªÁÄéÌªmèµÄÜ·
            if (same(d - 7))
              return true;
          } else {
            return true;
          }
        }
      }
    }
  }
  return false;
}

bool check2(int *d, int *idxlist) {
  for (
      int i = 0; i < 6;
      i++) { // ³¢±ëÌ»ê¼êÌ¶ÉÂ¢Ä²×é
    if (check_around2(d, firstindex2 = i, 0, idxlist))
      return true;
  }
  return false;
}

bool check_around(int *d, int idx, int nest, int *arr) {
  int c = otherc(buf[*d][idx]);
  if (nest == 3) {
    // ±êÍÅÌ³¢±ëÆêv·é©ð©éàÌÈÌÅÁÊ
    for (int around = 0; around < 4; around++) {
      int ar = aroundindex(firstindex, around);
      if (c ==
          buf[*(d - 3)]
             [ar]) { // ¡Ì¶ª éÆl¦éÌÍÊ|I
        // ÅÌ³¢±ëÌãCfbNX
        arr[0] = uppderindex(ar, firstindex);
        return true;
      }
    }
    return false;
  }

  // Ì³¢±ë
  d++;
  for (int i = 0; i < 6; i++) {
    if (c == buf[*d][i]) {
      for (int around = 0; around < 4; around++) {
        int ar = aroundindex(i, around);

        // CfbNXÌXg¶áÈ­Ä¶ñÉµ½Ù¤ª¢¢©È
        arr[nest + 1] = uppderindex(i, ar);
        if (check_around(d, ar, nest + 1, arr)) {
          if (nest == 2) {
            // OóÉÂÈªÁÄéÌªmèµÄÜ·
            if (check2(d + 1, uindexlist))
              return true;
          } else {
            return true;
          }
        }
      }
    }
  }
  return false;
}

bool check(int *d, int *idxlist) {
  for (
      int i = 0; i < 6;
      i++) { // ³¢±ëÌ»ê¼êÌ¶ÉÂ¢Ä²×é
    if (check_around(d, firstindex = i, 0, idxlist))
      return true;
  }
  return false;
}

bool check_around_pre(int *d, int idx, int nest) {
  int c = otherc(buf[*d][idx]);
  if (nest == 3) {
    // ±êÍÅÌ³¢±ëÆêv·é©ð©éàÌÈÌÅÁÊ
    for (int around = 0; around < 4; around++) {
      int ar = aroundindex(firstindex, around);
      if (c == buf[*(d - 3)][ar])
        return true;
    }
    return false;
  }

  // Ì³¢±ë
  d++;
  for (int i = 0; i < 6; i++) {
    if (c == buf[*d][i]) {
      for (int around = 0; around < 4; around++) {
        int ar = aroundindex(i, around);
        if (check_around_pre(d, ar, nest + 1))
          return true;
      }
    }
  }
  return false;
}

int cnt;
bool check_pre(int *d) {
  for (
      int i = 0; i < 6;
      i++) { // ³¢±ëÌ»ê¼êÌ¶ÉÂ¢Ä²×é
    if (check_around_pre(d, firstindex = i, 0))
      return true;
  }
  cnt++;
  return false;
}

bool check_pre_2(int *d) {
  int check = 0;
  for (int i = 0; i < 6; i++) {
    int c0 = otherc(buf[*d][i]);
    int c1 = otherc(buf[*(d + 6)][i]);
    for (int j = 0; j < 6; j++) {
      if (buf[*(d + 1)][j] == c0)
        check |= 1;
      if (buf[*(d + 3)][j] == c0)
        check |= 1 << 1;
      if (buf[*(d + 4)][j] == c0)
        check |= 1 << 2;
      if (buf[*(d + 2)][j] == c1)
        check |= 1 << 3;
      if (buf[*(d + 5)][j] == c1)
        check |= 1 << 4;
      if (buf[*(d + 7)][j] == c1)
        check |= 1 << 5;
    }
  }
  return check == (1 << 6) - 1;
}

bool get() {
  for (int i = 0; i < 8; ++i) {
    std::getline(std::cin, buf[i]);
    if (buf[i] == "0")
      return false;
  }
  return true;
}
int main() {
  for (int i = 0; i < 6; i++) {
    for (int j = 0; j < 4; j++) {
      std::cout << aroundindex(i, j);
    }
    puts("");
  }

  while (get()) {
    int d[8] = {0, 1, 2, 3, 4, 5, 6, 7};
    do {
      // Æè ¦¸2ÂÚÌXgÍìÁÄÌÄçêéñª½·¬éÌÅAæÉ1ÂÅà¶Ý·é©Ç¤©Å}Øè·é
      if (check_pre_2(d) && check_pre(d) && check_pre(d + 4) &&
          check(d, dindexlist)) {
        puts("YES");
        goto next;
      }
    } while (std::next_permutation(d, d + 8));
    puts("NO");
  next:;
  }
}