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

#include <cstdio>

using namespace std;

char cmd[7] = {"UFRDBL"};

int dy[6][6][8] = {{{0, 0, 0, 1, 0, 0, 0, 0},
                    {0, 0, -1, 0, 0, 0, 0, 0},
                    {0, 0, 0, 0, 0, 0, 0, 0},
                    {0, 0, 0, 0, 0, 0, 0, 0},
                    {0, 0, 0, 0, 0, 0, 0, 0},
                    {0, 0, 0, 0, 0, 0, 0, 0}},
                   {{0, 0, 0, 0, 0, 0, 0, 0},
                    {0, 0, 1, 2, 0, 0, 0, 0},
                    {0, -1, 0, 1, 2, 0, 0, 0},
                    {0, -2, -1, 0, 1, 0, 0, 0},
                    {0, 0, -2, -1, 0, 0, 0, 0},
                    {0, 0, 0, 0, 0, 0, 0, 0}},
                   {{0, 0, 0, 3, 0, 0, 0, 0},
                    {0, 0, 0, 1, 0, 0, 0, 0},
                    {0, 0, 0, -2, 0, 1, 3, 0},
                    {0, 0, 0, -2, -1, 0, 1, 0},
                    {0, 0, 0, -2, 0, 0, 0, 0},
                    {0, 0, 0, -2, 0, 0, 0, 0}},
                   {{0, 0, 0, 0, 0, 0, 0, 0},
                    {0, 0, 0, 0, 0, 0, 0, 0},
                    {0, 0, 0, 0, 0, 0, 0, 0},
                    {0, 0, 0, 0, 0, 0, 0, 0},
                    {0, 0, 0, 1, 0, 0, 0, 0},
                    {0, 0, -1, 0, 0, 0, 0, 0}},
                   {{0, 0, 3, 2, 0, 0, 0, 0},
                    {0, 0, 0, 0, 0, 0, 0, 0},
                    {3, 0, 0, 0, 0, -2, 0, 1},
                    {2, 0, 0, 0, 0, -3, -1, 0},
                    {0, 0, 0, 0, 0, 0, 0, 0},
                    {0, 0, -2, -3, 0, 0, 0, 0}},
                   {{0, 0, 2, 0, 0, 0, 0, 0},
                    {0, 0, 2, 0, 0, 0, 0, 0},
                    {0, 1, 2, 0, 0, 0, 0, -1},
                    {-1, 0, 2, 0, 0, 0, 0, -3},
                    {0, 0, -1, 0, 0, 0, 0, 0},
                    {0, 0, -3, 0, 0, 0, 0, 0}}};

int dx[6 /*コマンド数*/][6 /*縦*/][8 /*横*/] = {{{0, 0, 1, 0, 0, 0, 0, 0},
                                                 {0, 0, 0, -1, 0, 0, 0, 0},
                                                 {6, 6, -2, -2, -2, -2, -2, -2},
                                                 {0, 0, 0, 0, 0, 0, 0, 0},
                                                 {0, 0, 0, 0, 0, 0, 0, 0},
                                                 {0, 0, 0, 0, 0, 0, 0, 0}},
                                                {{0, 0, 0, 0, 0, 0, 0, 0},
                                                 {0, 0, 2, 1, 0, 0, 0, 0},
                                                 {0, 2, 1, 0, -1, 0, 0, 0},
                                                 {0, 1, 0, -1, -2, 0, 0, 0},
                                                 {0, 0, -1, -2, 0, 0, 0, 0},
                                                 {0, 0, 0, 0, 0, 0, 0, 0}},
                                                {{0, 0, 0, 3, 0, 0, 0, 0},
                                                 {0, 0, 0, 3, 0, 0, 0, 0},
                                                 {0, 0, 0, 0, 1, 0, -3, 0},
                                                 {0, 0, 0, 0, 0, -1, -3, 0},
                                                 {0, 0, 0, 0, 0, 0, 0, 0},
                                                 {0, 0, 0, 0, 0, 0, 0, 0}},
                                                {{0, 0, 0, 0, 0, 0, 0, 0},
                                                 {0, 0, 0, 0, 0, 0, 0, 0},
                                                 {0, 0, 0, 0, 0, 0, 0, 0},
                                                 {2, 2, 2, 2, 2, 2, -6, -6},
                                                 {0, 0, 1, 0, 0, 0, 0, 0},
                                                 {0, 0, 0, -1, 0, 0, 0, 0}},
                                                {{0, 0, -2, -3, 0, 0, 0, 0},
                                                 {0, 0, 0, 0, 0, 0, 0, 0},
                                                 {2, 0, 0, 0, 0, -3, 1, 0},
                                                 {3, 0, 0, 0, 0, -2, 0, -1},
                                                 {0, 0, 0, 0, 0, 0, 0, 0},
                                                 {0, 0, 3, 2, 0, 0, 0, 0}},
                                                {{0, 0, 0, 0, 0, 0, 0, 0},
                                                 {0, 0, 0, 0, 0, 0, 0, 0},
                                                 {1, 0, 0, 0, 0, 0, 0, -5},
                                                 {0, -1, 0, 0, 0, 0, 0, -5},
                                                 {0, 0, 5, 0, 0, 0, 0, 0},
                                                 {0, 0, 5, 0, 0, 0, 0, 0}}};

void turn(char table[6][9], int icmd) {
  char tmp[6][9], i, j;
  for (i = 0; i < 6; i++) {
    for (j = 0; j < 8; j++) {
      tmp[i + dy[icmd][i][j]][j + dx[icmd][i][j]] = table[i][j];
    }
    tmp[i][j] = '\\0';
  }
  for (i = 0; i < 6; i++) {
    for (j = 0; j < 8; j++) {
      table[i][j] = tmp[i][j];
    }
    table[i][j] = '\\0';
  }
}

int main(void) {
  int i;
  char c, table[6][9] = {{"..rr...."}, {"..rr...."}, {"ggyybbww"},
                         {"ggyybbww"}, {"..oo...."}, {"..oo...."}};
  while ((c = getchar()) != '\\n') {
    for (i = 0; i < 6; i++) {
      if (cmd[i] == c) {
        turn(table, i);
      }
    }
  }
  for (i = 0; i < 6; i++) {
    printf("%s\\n", table[i]);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>

using namespace std;

char cmd[7] = {"UFRDBL"};

int dy[6][6][8] = {{{0, 0, 0, 1, 0, 0, 0, 0},
                    {0, 0, -1, 0, 0, 0, 0, 0},
                    {0, 0, 0, 0, 0, 0, 0, 0},
                    {0, 0, 0, 0, 0, 0, 0, 0},
                    {0, 0, 0, 0, 0, 0, 0, 0},
                    {0, 0, 0, 0, 0, 0, 0, 0}},
                   {{0, 0, 0, 0, 0, 0, 0, 0},
                    {0, 0, 1, 2, 0, 0, 0, 0},
                    {0, -1, 0, 1, 2, 0, 0, 0},
                    {0, -2, -1, 0, 1, 0, 0, 0},
                    {0, 0, -2, -1, 0, 0, 0, 0},
                    {0, 0, 0, 0, 0, 0, 0, 0}},
                   {{0, 0, 0, 3, 0, 0, 0, 0},
                    {0, 0, 0, 1, 0, 0, 0, 0},
                    {0, 0, 0, -2, 0, 1, 3, 0},
                    {0, 0, 0, -2, -1, 0, 1, 0},
                    {0, 0, 0, -2, 0, 0, 0, 0},
                    {0, 0, 0, -2, 0, 0, 0, 0}},
                   {{0, 0, 0, 0, 0, 0, 0, 0},
                    {0, 0, 0, 0, 0, 0, 0, 0},
                    {0, 0, 0, 0, 0, 0, 0, 0},
                    {0, 0, 0, 0, 0, 0, 0, 0},
                    {0, 0, 0, 1, 0, 0, 0, 0},
                    {0, 0, -1, 0, 0, 0, 0, 0}},
                   {{0, 0, 3, 2, 0, 0, 0, 0},
                    {0, 0, 0, 0, 0, 0, 0, 0},
                    {3, 0, 0, 0, 0, -2, 0, 1},
                    {2, 0, 0, 0, 0, -3, -1, 0},
                    {0, 0, 0, 0, 0, 0, 0, 0},
                    {0, 0, -2, -3, 0, 0, 0, 0}},
                   {{0, 0, 2, 0, 0, 0, 0, 0},
                    {0, 0, 2, 0, 0, 0, 0, 0},
                    {0, 1, 2, 0, 0, 0, 0, -1},
                    {-1, 0, 2, 0, 0, 0, 0, -3},
                    {0, 0, -1, 0, 0, 0, 0, 0},
                    {0, 0, -3, 0, 0, 0, 0, 0}}};

int dx[6 /*コマンド数*/][6 /*縦*/][8 /*横*/] = {{{0, 0, 1, 0, 0, 0, 0, 0},
                                                 {0, 0, 0, -1, 0, 0, 0, 0},
                                                 {6, 6, -2, -2, -2, -2, -2, -2},
                                                 {0, 0, 0, 0, 0, 0, 0, 0},
                                                 {0, 0, 0, 0, 0, 0, 0, 0},
                                                 {0, 0, 0, 0, 0, 0, 0, 0}},
                                                {{0, 0, 0, 0, 0, 0, 0, 0},
                                                 {0, 0, 2, 1, 0, 0, 0, 0},
                                                 {0, 2, 1, 0, -1, 0, 0, 0},
                                                 {0, 1, 0, -1, -2, 0, 0, 0},
                                                 {0, 0, -1, -2, 0, 0, 0, 0},
                                                 {0, 0, 0, 0, 0, 0, 0, 0}},
                                                {{0, 0, 0, 3, 0, 0, 0, 0},
                                                 {0, 0, 0, 3, 0, 0, 0, 0},
                                                 {0, 0, 0, 0, 1, 0, -3, 0},
                                                 {0, 0, 0, 0, 0, -1, -3, 0},
                                                 {0, 0, 0, 0, 0, 0, 0, 0},
                                                 {0, 0, 0, 0, 0, 0, 0, 0}},
                                                {{0, 0, 0, 0, 0, 0, 0, 0},
                                                 {0, 0, 0, 0, 0, 0, 0, 0},
                                                 {0, 0, 0, 0, 0, 0, 0, 0},
                                                 {2, 2, 2, 2, 2, 2, -6, -6},
                                                 {0, 0, 1, 0, 0, 0, 0, 0},
                                                 {0, 0, 0, -1, 0, 0, 0, 0}},
                                                {{0, 0, -2, -3, 0, 0, 0, 0},
                                                 {0, 0, 0, 0, 0, 0, 0, 0},
                                                 {2, 0, 0, 0, 0, -3, 1, 0},
                                                 {3, 0, 0, 0, 0, -2, 0, -1},
                                                 {0, 0, 0, 0, 0, 0, 0, 0},
                                                 {0, 0, 3, 2, 0, 0, 0, 0}},
                                                {{0, 0, 0, 0, 0, 0, 0, 0},
                                                 {0, 0, 0, 0, 0, 0, 0, 0},
                                                 {1, 0, 0, 0, 0, 0, 0, -5},
                                                 {0, -1, 0, 0, 0, 0, 0, -5},
                                                 {0, 0, 5, 0, 0, 0, 0, 0},
                                                 {0, 0, 5, 0, 0, 0, 0, 0}}};

void turn(char table[6][9], int icmd) {
  char tmp[6][9], i, j;
  for (i = 0; i < 6; i++) {
    for (j = 0; j < 8; j++) {
      tmp[i + dy[icmd][i][j]][j + dx[icmd][i][j]] = table[i][j];
    }
    tmp[i][j] = '\\0';
  }
  for (i = 0; i < 6; i++) {
    for (j = 0; j < 8; j++) {
      table[i][j] = tmp[i][j];
    }
    table[i][j] = '\\0';
  }
}

int main(void) {
  int i;
  char c, table[6][9] = {{"..rr...."}, {"..rr...."}, {"ggyybbww"},
                         {"ggyybbww"}, {"..oo...."}, {"..oo...."}};
  while ((c = getchar()) != '\\n') {
    for (i = 0; i < 6; i++) {
      if (cmd[i] == c) {
        turn(table, i);
      }
    }
  }
  for (i = 0; i < 6; i++) {
    printf("%s\\n", table[i]);
  }
  while (1)
    ;
  return 0;
}