alexjercan/bugnet huggingface dataset:
Problem p02384 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
#define lp(i, a, b) for (int i = a; i < b; i++)
#define rp(i, n) for (int i = 0; i < n; i++)
using namespace std;
int main() {
  int dice[7], cdice[7];
  lp(i, 1, 7) {
    cin >> dice[i];
    cdice[i] = dice[i];
  }
  int n, x, y;
  cin >> n;
  rp(i, n) {
    cin >> x >> y;
    lp(j, 1, 7) { dice[j] = cdice[j]; }
    int f = 0;
    while (1) {
      if ((dice[2] == x || dice[5] == x) && (dice[1] == y || dice[6] == y)) {
        int a = dice[1], b = dice[2], c = dice[3], d = dice[4], e = dice[5],
            f = dice[6];
        dice[1] = a;
        dice[2] = d;
        dice[3] = b;
        dice[4] = e;
        dice[5] = c;
        dice[6] = f;
      }

      if (dice[1] == x && dice[2] == y)
        break;
      while (1) {
        if (dice[1] == x || dice[2] == x || dice[5] == x)
          break;
        int a = dice[1], b = dice[2], c = dice[3], d = dice[4], e = dice[5],
            f = dice[6];
        dice[1] = d;
        dice[2] = b;
        dice[3] = a;
        dice[4] = f;
        dice[5] = e;
        dice[6] = c;
        // cout<<1<<endl;
      }
      while (1) {
        if (dice[2] == y)
          break;
        int a = dice[1], b = dice[2], c = dice[3], d = dice[4], e = dice[5],
            f = dice[6];
        dice[1] = a;
        dice[2] = d;
        dice[3] = b;
        dice[4] = e;
        dice[5] = c;
        dice[6] = f;
        // cout<<1<<endl;
      }
    }
    cout << dice[3] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#define lp(i, a, b) for (int i = a; i < b; i++)
#define rp(i, n) for (int i = 0; i < n; i++)
using namespace std;
int main() {
  int dice[7], cdice[7];
  lp(i, 1, 7) {
    cin >> dice[i];
    cdice[i] = dice[i];
  }
  int n, x, y;
  cin >> n;
  rp(i, n) {
    cin >> x >> y;
    lp(j, 1, 7) { dice[j] = cdice[j]; }
    while (1) {
      if (dice[1] == x)
        break;
      int a = dice[1], b = dice[2], c = dice[3], d = dice[4], e = dice[5],
          f = dice[6];
      dice[1] = d;
      dice[2] = b;
      dice[3] = a;
      dice[4] = f;
      dice[5] = e;
      dice[6] = c;
      // cout<<1<<endl;
    }
    while (1) {
      if (dice[2] == y)
        break;
      int a = dice[1], b = dice[2], c = dice[3], d = dice[4], e = dice[5],
          f = dice[6];
      dice[1] = a;
      dice[2] = d;
      dice[3] = b;
      dice[4] = e;
      dice[5] = c;
      dice[6] = f;
      // cout<<1<<endl;
    }
    cout << dice[3] << endl;
  }
  return 0;
}
Problem p02384 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

struct Dice {
  int f[7];
  int n[100];
  int right[7][7] = {{0, 0, 0, 0, 0, 0, 0}, {0, 0, 3, 5, 2, 4, 0},
                     {0, 4, 0, 1, 6, 0, 3}, {0, 2, 6, 0, 0, 1, 5},
                     {0, 5, 1, 0, 0, 6, 2}, {0, 3, 0, 6, 1, 0, 4},
                     {0, 0, 4, 2, 5, 3, 0}};

  void move(char dir) {
    int sw;
    switch (dir) {
    case 'E':
      sw = f[1];
      f[1] = f[4];
      f[4] = f[6];
      f[6] = f[3];
      f[3] = sw;
      break;
    case 'N':
      sw = f[1];
      f[1] = f[2];
      f[2] = f[6];
      f[6] = f[5];
      f[5] = sw;
      break;
    case 'S':
      sw = f[1];
      f[1] = f[5];
      f[5] = f[6];
      f[6] = f[2];
      f[2] = sw;
      break;
    case 'W':
      sw = f[1];
      f[1] = f[3];
      f[3] = f[6];
      f[6] = f[4];
      f[4] = sw;
      break;
    }
  }
  int getright(int up, int front) { return f[right[up][front]]; }
};

int main() {
  Dice dice;
  for (int i = 1; i <= 6; ++i) {
    cin >> dice.f[i];
    dice.n[dice.f[i]] = i;
  }
  int n;
  cin >> n;
  int up, front;
  for (int i = 0; i < n; ++i) {
    cin >> up;
    cin >> front;
    cout << dice.getright(dice.n[up], dice.n[front]) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

struct Dice {
  int f[7];
  int right[7][7] = {{0, 0, 0, 0, 0, 0, 0}, {0, 0, 3, 5, 4, 2, 0},
                     {0, 4, 0, 1, 6, 0, 3}, {0, 2, 6, 0, 0, 1, 5},
                     {0, 5, 1, 0, 0, 6, 2}, {0, 3, 0, 6, 1, 0, 4},
                     {0, 0, 4, 2, 5, 3, 0}};

  void move(char dir) {
    int sw;
    switch (dir) {
    case 'E':
      sw = f[1];
      f[1] = f[4];
      f[4] = f[6];
      f[6] = f[3];
      f[3] = sw;
      break;
    case 'N':
      sw = f[1];
      f[1] = f[2];
      f[2] = f[6];
      f[6] = f[5];
      f[5] = sw;
      break;
    case 'S':
      sw = f[1];
      f[1] = f[5];
      f[5] = f[6];
      f[6] = f[2];
      f[2] = sw;
      break;
    case 'W':
      sw = f[1];
      f[1] = f[3];
      f[3] = f[6];
      f[6] = f[4];
      f[4] = sw;
      break;
    }
  }
  int getright(int up, int front) { return f[right[up][front]]; }
};

int main() {
  Dice dice;
  for (int i = 1; i <= 6; ++i) {
    cin >> dice.f[i];
  }
  int n;
  cin >> n;
  int up, front;
  for (int i = 0; i < n; ++i) {
    cin >> up;
    cin >> front;
    cout << dice.getright(up, front) << endl;
  }
  return 0;
}
Problem p02384 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
using namespace std;

int main() {
  int n = 6;
  int dat[n];
  for (int i = 0; i < n; i++) {
    cin >> dat[i];
  }

  int tbl[][7] = {{4, 2, 3, 5, 4, 0}, {1, 4, 6, 3, 1, 0}, {1, 2, 6, 5, 1, 0},
                  {1, 5, 6, 2, 1, 0}, {1, 3, 6, 4, 1, 0}, {5, 3, 2, 4, 5, 0}};

  int tblx = sizeof(tbl) / sizeof(tbl[0]);
  int tbly = sizeof(tbl[0]) / sizeof(tbl[0][0]);

  for (int i = 0; i < tblx; i++) {
    for (int j = 0; j < tbly; j++) {
      if (j == tbly - 1) {
        continue;
      }
      tbl[i][j] = dat[tbl[i][j] - 1];
    }
  }

  int q, a, b, right;
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> a >> b;
    for (int j = 0; j < tblx; j++) {
      for (int k = 0; k < tbly; k++) {
        if (tbl[j][k] == a and tbl[j][k + 1] == b) {

          if (false) {
            for (int l = 0; l < tbly; l++) {
              cout << tbl[j][l] << ",";
            }
            cout << ":" << a << "/" << b << "/";
          }
          cout << dat[j] << endl;
        }
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>
using namespace std;

int main() {
  int n = 6;
  int d[n];
  for (int i = 0; i < n; i++) {
    cin >> d[i];
  }

  int tbl[][7] = {{4, 2, 3, 5, 4, 0}, {1, 4, 6, 3, 1, 0}, {1, 2, 6, 5, 1, 0},
                  {1, 5, 2, 6, 1, 0}, {1, 3, 6, 4, 1, 0}, {5, 3, 2, 4, 5, 0}};

  int q, a, b, right;
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> a >> b;
    for (int j = 0; j < sizeof(tbl[j]); j++) {
      for (int k = 0; k < sizeof(tbl[j][k]); k++) {
        if (tbl[j][k] == a and tbl[j][k + 1] == b) {
          cout << j + 1 << endl;
        }
      }
    }
  }
  return 0;
}
Problem p02384 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>

class Dice {
public:
  int a[7]; //-,up,flont,right,left,back,down
  void roll(char d);
  void rotate(void);
};

void Dice::rotate(void) {
  int tmp;
  tmp = a[2];
  a[2] = a[3];
  a[3] = a[5];
  a[5] = a[4];
  a[4] = tmp;
}

void Dice::roll(char d) {
  int tmp;
  if (d == 'N') {
    tmp = a[1];
    a[1] = a[2];
    a[2] = a[6];
    a[6] = a[5];
    a[5] = tmp;
  } else if (d == 'S') {
    tmp = a[1];
    a[1] = a[5];
    a[5] = a[6];
    a[6] = a[2];
    a[2] = tmp;
  } else if (d == 'W') {
    tmp = a[1];
    a[1] = a[3];
    a[3] = a[6];
    a[6] = a[4];
    a[4] = tmp;
  } else if (d == 'E') {
    tmp = a[1];
    a[1] = a[4];
    a[4] = a[6];
    a[6] = a[3];
    a[3] = tmp;
  }
}

int main(void) {
  int i, n;
  Dice dice;

  for (i = 1; i < 7; i++) {
    std::cin >> dice.a[i];
  }
  std::cin >> n;

  int u, f;
  bool b;
  for (i = 0; i < n; i++) {
    std::cin >> u >> f;
    b = 0;
    while (dice.a[1] != u) {
      if (b) {
        dice.roll('N');
      } else {
        if (dice.a[3] == u || dice.a[4] == u)
          dice.roll('W');
        b = 1;
      }
    }
    while (dice.a[2] != f) {
      dice.rotate();
    }
    std::cout << dice.a[3] << std::endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <string>

class Dice {
public:
  int a[7]; //-,up,flont,right,left,back,down
  void roll(char d);
  void rotate(void);
};

void Dice::rotate(void) {
  int tmp;
  tmp = a[2];
  a[2] = a[3];
  a[3] = a[5];
  a[5] = a[4];
  a[4] = tmp;
}

void Dice::roll(char d) {
  int tmp;
  if (d == 'N') {
    tmp = a[1];
    a[1] = a[2];
    a[2] = a[6];
    a[6] = a[5];
    a[5] = tmp;
  } else if (d == 'S') {
    tmp = a[1];
    a[1] = a[5];
    a[5] = a[6];
    a[6] = a[2];
    a[2] = tmp;
  } else if (d == 'W') {
    tmp = a[1];
    a[1] = a[3];
    a[3] = a[6];
    a[6] = a[4];
    a[4] = tmp;
  } else if (d == 'E') {
    tmp = a[1];
    a[1] = a[4];
    a[4] = a[6];
    a[6] = a[3];
    a[3] = tmp;
  }
}

int main(void) {
  int i, n;
  Dice dice;

  for (i = 1; i < 7; i++) {
    std::cin >> dice.a[i];
  }
  std::cin >> n;

  int c, u, f;
  for (i = 0; i < n; i++) {
    std::cin >> u >> f;
    c = 0;
    while (dice.a[1] != u) {
      if (c % 2 == 0) {
        dice.roll('N');
      } else {
        dice.roll('W');
      }
    }
    while (dice.a[2] != f) {
      dice.rotate();
    }
    std::cout << dice.a[3] << std::endl;
  }
  return 0;
}
Problem p02384 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

int faces[1 + 6];

void roll(int f) {
  int tmp = faces[f];
  faces[f] = faces[1];
  faces[1] = faces[7 - f];
  faces[7 - f] = faces[6];
  faces[6] = tmp;
}

int main() {
  int input[1 + 6];
  for (int i = 1; i <= 6; i++) {
    cin >> input[i];
  }

  int n, top, front, right, topNum;
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 1; j <= 6; j++) {
      faces[j] = input[j];
    }
    cin >> top >> front;
    for (int j = 1; j <= 6; j++) {
      if (top == faces[j]) {
        top = j;
        continue;
      }
      if (front == faces[j]) {
        front = j;
        continue;
      }
    }

    topNum = faces[top];
    switch (front) {
    case 6:
      break;
    case 1:
      roll(top);
      roll(top);
      break;
    default:
      roll(front);
      break;
    }

    for (int i = 1; i <= 6; i++) {
      if (topNum == faces[i]) {
        top = i;
        break;
      }
    }

    switch (top) {
    case 2:
      right = 3;
      break;
    case 3:
      right = 5;
      break;
    case 5:
      right = 4;
      break;
    case 4:
      right = 2;
      break;
    }

    cout << faces[right] << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

int faces[1 + 6];

void roll(int f) {
  int tmp = faces[f];
  faces[f] = faces[1];
  faces[1] = faces[7 - f];
  faces[7 - f] = faces[6];
  faces[6] = tmp;
}

int main() {
  int input[1 + 6];
  for (int i = 1; i <= 6; i++) {
    cin >> input[i];
  }

  int n, top, front, right, topNum;
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 1; j <= 6; j++) {
      faces[j] = input[j];
    }
    cin >> top >> front;
    for (int j = 1; j <= 6; j++) {
      if (top == faces[j]) {
        top = j;
        break;
      }
      if (front == faces[j]) {
        front = j;
      }
    }

    topNum = faces[top];
    switch (front) {
    case 6:
      break;
    case 1:
      roll(top);
      roll(top);
      break;
    default:
      roll(front);
      break;
    }

    for (int i = 1; i <= 6; i++) {
      if (topNum == faces[i]) {
        top = i;
        break;
      }
    }

    switch (top) {
    case 2:
      right = 3;
      break;
    case 3:
      right = 5;
      break;
    case 5:
      right = 4;
      break;
    case 4:
      right = 2;
      break;
    }

    cout << faces[right] << endl;
  }

  return 0;
}
Problem p02384 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#include <string.h>
typedef struct {
  char direction[100];
  int num1, num2, num3, num4, num5, num6;
} DICE_t;
int main() {
  DICE_t a;
  scanf("%d %d %d %d %d %d", &a.num1, &a.num2, &a.num3, &a.num4, &a.num5,
        &a.num6);
  int n, x, y, dummy;
  scanf("%d", &n);
  for (int times = 0; times < n; times++) {
    int turn = 0;
    scanf("%d %d", &x, &y);
    while (1) {
      if (a.num1 != x) {
        strcpy(a.direction, (turn % 2 == 0 ? "N" : "E"));
      } else if (a.num1 == x && a.num2 != y) {
        strcpy(a.direction, "SEN");
      } else if (a.num1 == x && a.num2 == y)
        break;
      for (int i = 0; a.direction[i] != 0; i++) {
        if (a.direction[i] == 'N') {
          dummy = a.num5;
          a.num5 = a.num1;
          a.num1 = a.num2;
          a.num2 = a.num6;
          a.num6 = dummy;
        } else if (a.direction[i] == 'S') {
          dummy = a.num6;
          a.num6 = a.num2;
          a.num2 = a.num1;
          a.num1 = a.num5;
          a.num5 = dummy;
        } else if (a.direction[i] == 'E') {
          dummy = a.num4;
          a.num4 = a.num6;
          a.num6 = a.num3;
          a.num3 = a.num1;
          a.num1 = dummy;
        } else if (a.direction[i] == 'W') {
          dummy = a.num1;
          a.num1 = a.num3;
          a.num3 = a.num6;
          a.num6 = a.num4;
          a.num4 = dummy;
        }
      }
      turn++;
    }
    printf("%d\\n", a.num3);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
#include <string.h>
typedef struct {
  char direction[100];
  int num1, num2, num3, num4, num5, num6;
} DICE_t;
int main() {
  DICE_t a;
  scanf("%d %d %d %d %d %d", &a.num1, &a.num2, &a.num3, &a.num4, &a.num5,
        &a.num6);
  int n, x, y, dummy;
  scanf("%d", &n);
  for (int times = 0; times < n; times++) {
    while (1) {
      scanf("%d %d", &x, &y);
      if (a.num1 != x) {
        strcpy(a.direction, "N");
        if (a.num1 != x) {
          strcat(a.direction, "E");
        }
      } else if (a.num1 == x && a.num2 != y) {
        strcpy(a.direction, "SEN");
      } else if (a.num1 == x && a.num2 == y)
        break;
      for (int i = 0; a.direction[i] != 0; i++) {
        if (a.direction[i] == 'N') {
          dummy = a.num5;
          a.num5 = a.num1;
          a.num1 = a.num2;
          a.num2 = a.num6;
          a.num6 = dummy;
        } else if (a.direction[i] == 'S') {
          dummy = a.num6;
          a.num6 = a.num2;
          a.num2 = a.num1;
          a.num1 = a.num5;
          a.num5 = dummy;
        } else if (a.direction[i] == 'E') {
          dummy = a.num4;
          a.num4 = a.num6;
          a.num6 = a.num3;
          a.num3 = a.num1;
          a.num1 = dummy;
        } else if (a.direction[i] == 'W') {
          dummy = a.num1;
          a.num1 = a.num3;
          a.num3 = a.num6;
          a.num6 = a.num4;
          a.num4 = dummy;
        }
      }
    }
    printf("%d\\n", a.num3);
  }

  return 0;
}
Problem p02384 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

void dice_roll(int *dice, char i) {
  if (i == 'W') {
    int tmp = dice[0];
    dice[0] = dice[1];
    dice[1] = dice[5];
    dice[5] = dice[4];
    dice[4] = tmp;
  }
  if (i == 'S') {
    int tmp = dice[0];
    dice[0] = dice[4];
    dice[4] = dice[5];
    dice[5] = dice[1];
    dice[1] = tmp;
  }
  if (i == 'D') {
    int tmp = dice[0];
    dice[0] = dice[3];
    dice[3] = dice[5];
    dice[5] = dice[2];
    dice[2] = tmp;
  }
  if (i == 'A') {
    int tmp = dice[0];
    dice[0] = dice[2];
    dice[2] = dice[5];
    dice[5] = dice[3];
    dice[3] = tmp;
  }
  if (i == 'd') {
    int tmp = dice[1];
    dice[1] = dice[2];
    dice[2] = dice[4];
    dice[4] = dice[3];
    dice[3] = tmp;
  }
}

int main() {
  /*
          0
  3	1	2	4
          5
  */
  int dice[6];
  for (int i = 0; i < 6; i++)
    cin >> dice[i];
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int u, f;
    cin >> u >> f;
    int cnt = 0;
    while (u != dice[0]) {
      dice_roll(dice, 'W');
      cnt++;
      if (cnt == 4)
        break;
    }
    if (cnt == 4) {
      cnt = 0;
      while (u != dice[0]) {
        dice_roll(dice, 'D');
      }
      while (f != dice[1]) {
        dice_roll(dice, 'd');
      }
      cout << dice[2] << endl;
    } else {
      cnt = 0;
      while (f != dice[1]) {
        dice_roll(dice, 'd');
      }
      cout << dice[2] << endl;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

void dice_roll(int *dice, char i) {
  if (i == 'W') {
    int tmp = dice[0];
    dice[0] = dice[1];
    dice[1] = dice[5];
    dice[5] = dice[4];
    dice[4] = tmp;
  }
  if (i == 'S') {
    int tmp = dice[0];
    dice[0] = dice[4];
    dice[4] = dice[5];
    dice[5] = dice[1];
    dice[1] = tmp;
  }
  if (i == 'D') {
    int tmp = dice[0];
    dice[0] = dice[3];
    dice[3] = dice[5];
    dice[5] = dice[2];
    dice[2] = tmp;
  }
  if (i == 'A') {
    int tmp = dice[0];
    dice[0] = dice[2];
    dice[2] = dice[5];
    dice[5] = dice[3];
    dice[3] = tmp;
  }
  if (i == 'd') {
    int tmp = dice[1];
    dice[1] = dice[2];
    dice[2] = dice[4];
    dice[4] = dice[3];
    dice[3] = tmp;
  }
}

int main() {
  /*
          0
  3       1       2       4
          5
  */
  int dice[6];
  for (int i = 0; i < 6; i++)
    cin >> dice[i];
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int u, f;
    cin >> u >> f;
    int cnt = 0;
    while (u != dice[0]) {
      dice_roll(dice, 'W');
      cnt++;
      if (cnt == 4)
        break;
    }
    if (cnt == 4) {
      cnt = 0;
      while (f != dice[1]) {
        dice_roll(dice, 'd');
      }
      while (u != dice[0]) {
        dice_roll(dice, 'D');
      }
      cout << dice[2] << endl;
    } else {
      cnt = 0;
      while (f != dice[1]) {
        dice_roll(dice, 'd');
      }
      cout << dice[2] << endl;
    }
  }
  return 0;
}
Problem p02384 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>

using namespace std;

struct dice {
  int q1, q2, q3, q4, q5, q6;
};

void mN(dice &d) {
  int tmp = d.q1;
  d.q1 = d.q2;
  d.q2 = d.q6;
  d.q6 = d.q5;
  d.q5 = tmp;
}

void mE(dice &d) {
  int tmp = d.q1;
  d.q1 = d.q4;
  d.q4 = d.q6;
  d.q6 = d.q3;
  d.q3 = tmp;
}
void mW(dice &d) {
  int tmp = d.q1;
  d.q1 = d.q3;
  d.q3 = d.q6;
  d.q6 = d.q4;
  d.q4 = tmp;
}
void mS(dice &d) {
  int tmp = d.q1;
  d.q1 = d.q5;
  d.q5 = d.q6;
  d.q6 = d.q2;
  d.q2 = tmp;
}

void spin(dice &d) {
  int tmp = d.q2;
  d.q2 = d.q3;
  d.q3 = d.q5;
  d.q5 = d.q4;
  d.q4 = tmp;
}

int main() {
  dice z;
  int q;

  scanf(" %d %d %d %d %d %d", &z.q1, &z.q2, &z.q3, &z.q4, &z.q5, &z.q6);
  scanf(" %d", &q);

  for (int i = 0; i < q; ++i) {
    int a, b;
    dice x = z;

    scanf(" %d %d", &a, &b);

    if (a == x.q3 || a == x.q4)
      mE(x);
    while (a != x.q1)
      mN(x);
    while (b != x.q2)
      spin(x);

    printf("%d\\n", x.q3);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;

struct dice {
  int q1, q2, q3, q4, q5, q6;
};

void mN(dice &d) {
  int tmp = d.q1;
  d.q1 = d.q2;
  d.q2 = d.q6;
  d.q6 = d.q5;
  d.q5 = tmp;
}

void mE(dice &d) {
  int tmp = d.q1;
  d.q1 = d.q4;
  d.q4 = d.q6;
  d.q6 = d.q3;
  d.q3 = tmp;
}
void mW(dice &d) {
  int tmp = d.q1;
  d.q1 = d.q3;
  d.q3 = d.q6;
  d.q6 = d.q4;
  d.q4 = tmp;
}
void mS(dice &d) {
  int tmp = d.q1;
  d.q1 = d.q5;
  d.q5 = d.q6;
  d.q6 = d.q2;
  d.q2 = tmp;
}

void spin(dice &d) {
  int tmp = d.q2;
  d.q2 = d.q3;
  d.q3 = d.q5;
  d.q5 = d.q4;
  d.q4 = tmp;
}

int main() {
  dice z;
  int q;

  scanf(" %d %d %d %d %d %d", &z.q1, &z.q2, &z.q3, &z.q4, &z.q5, &z.q6);
  scanf(" %d", &q);

  for (int i = 0; i < q; ++i) {
    int a, b;
    dice x = z;

    scanf(" %d %d", &a, &b);

    if (a == 3 || a == 4)
      mE(x);
    while (a != x.q1)
      mN(x);
    while (b != x.q2)
      spin(x);

    printf("%d\\n", x.q3);
  }

  return 0;
}
Problem p02384 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

class dice {

public:
  int _1, _2, _3, _4, _5, _6, sus;

  void moveN() {
    sus = _1;
    _1 = _2;
    _2 = _6;
    _6 = _5;
    _5 = sus;
  }

  void moveE() {
    sus = _1;
    _1 = _4;
    _4 = _6;
    _6 = _3;
    _3 = sus;
  }

  void moveW() {
    sus = _1;
    _1 = _3;
    _3 = _6;
    _6 = _4;
    _4 = sus;
  }

  void moveS() {
    sus = _1;
    _1 = _5;
    _5 = _6;
    _6 = _2;
    _2 = sus;
  }

  void spin() {
    sus = _2;
    _2 = _3;
    _3 = _5;
    _5 = _4;
    _4 = sus;
  }
};

int main() {

  dice d1;

  int dir_t, i, T, F;

  cin >> d1._1 >> d1._2 >> d1._3 >> d1._4 >> d1._5 >> d1._6;
  cin >> dir_t;

  for (i = 0; i < dir_t; i++) {

    cin >> T >> F;

    if (d1._3 == T || d1._4 == T)
      d1.moveE();
    while (d1._1 != T)
      d1.moveN();
    while (d1._2 != F)
      d1.spin();

    cout << d1._3 << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

class dice {

public:
  int _1, _2, _3, _4, _5, _6, sus;

  void move() {
    sus = _1;
    _1 = _2;
    _2 = _6;
    _6 = _5;
    _5 = sus;
  }

  void spin() {
    sus = _2;
    _2 = _3;
    _3 = _5;
    _5 = _4;
    _4 = sus;
  }
};

int main() {

  dice d1;

  int dir_t, i, T, F;

  cin >> d1._1 >> d1._2 >> d1._3 >> d1._4 >> d1._5 >> d1._6;
  cin >> dir_t;

  for (i = 0; i < dir_t; i++) {

    cin >> T >> F;

    while (d1._1 != T)
      d1.move();

    while (d1._2 != F)
      d1.spin();

    cout << d1._3 << endl;
  }

  return 0;
}
Problem p02384 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

int dice(int a, int b) {
  int count = 0;
  string tpat[3];
  tpat[0] = "12651";
  tpat[1] = "23542";
  tpat[2] = "13641";
  int ret[3];
  ret[0] = 3;
  ret[1] = 1;
  ret[2] = 5;
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 4; j++) {
      if (tpat[i][j] - '0' == a && tpat[i][j + 1] - '0' == b) {
        return ret[i];
      }
    }
  }
  tpat[0] = "15621";
  tpat[1] = "24532";
  tpat[2] = "14631";
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 4; j++) {
      if (tpat[i][j] - '0' == a && tpat[i][j + 1] - '0' == b) {
        return 7 - ret[i];
      }
    }
  }
}

int main(int argc, char const *argv[]) {
  int d[6];
  for (int i = 0; i < 6; i++) {
    cin >> d[i];
  }
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    int a1, b1;
    for (int i = 0; i < 6; i++) {
      if (d[i] == a)
        a1 = i;
      if (d[i] == b)
        b1 = i;
    }
    cout << d[dice(a1 + 1, b1 + 1) - 1] << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

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

using namespace std;

int dice(int a, int b) {
  int count = 0;
  string tpat[3];
  tpat[0] = "12651";
  tpat[1] = "23542";
  tpat[2] = "13641";
  int ret[3];
  ret[0] = 3;
  ret[1] = 1;
  ret[2] = 5;
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 4; j++) {
      if (tpat[i][j] - '0' == a && tpat[i][j + 1] - '0' == b) {
        return ret[i];
      }
    }
  }
  tpat[0] = "15621";
  tpat[1] = "24532";
  tpat[2] = "14631";
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 4; j++) {
      if (tpat[i][j] - '0' == a && tpat[i][j + 1] - '0' == b) {
        return 7 - ret[i];
      }
    }
  }
}

int main(int argc, char const *argv[]) {
  int d[6];
  for (int i = 0; i < 6; i++) {
    cin >> d[i];
  }
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    cout << d[dice(a, b) - 1] << endl;
  }
  return 0;
}