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


#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i, n) for (i = 0; i < (n); i++)
#define loop(i, a, n) for (i = a; i < (n); i++)
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;

int h, w = 5;
int v[15][5];

int disappearance() {

  int ret = 0;
  int i, j;
  /*
消えるとき
1 2 3 4 5
1 2 3 4
1 2 3
2 3 4
2 3 4 5
3 4 5
   */

  rep(i, h) {
    if (v[i][0] == v[i][1] && v[i][1] == v[i][2] && v[i][2] == v[i][3] &&
        v[i][3] == v[i][4] && v[i][0] != -1) {
      ret += v[i][0] * 5;
      rep(j, 5) v[i][j] = -1;
    }
    if (v[i][0] == v[i][1] && v[i][1] == v[i][2] && v[i][2] == v[i][3] &&
        v[i][3] != v[i][4] && v[i][0] != -1) {
      ret += v[i][0] * 4;
      rep(j, 4) v[i][j] = -1;
    }
    if (v[i][0] == v[i][1] && v[i][1] == v[i][2] && v[i][0] != v[i][3] &&
        v[i][0] != -1) {
      ret += v[i][0] * 3;
      rep(j, 3) v[i][j] = -1;
    }
    if (v[i][0] != v[i][1] && v[i][1] == v[i][2] && v[i][2] == v[i][3] &&
        v[i][3] == v[i][4] && v[i][1] != -1) {
      ret += v[i][1] * 4;
      loop(j, 1, 5) v[i][j] = -1;
    }
    if (v[i][0] != v[i][1] && v[i][1] == v[i][2] && v[i][2] == v[i][3] &&
        v[i][3] != v[i][4] && v[i][1] != -1) {
      ret += v[i][1] * 3;
      loop(j, 1, 4) v[i][j] = -1;
    }
    if (v[i][1] != v[i][4] && v[i][2] == v[i][4] && v[i][3] == v[i][4] &&
        v[i][4] != -1) {
      ret += v[i][2] * 3;
      loop(j, 2, 5) v[i][j] = -1;
    }
  }

  return ret;
}

void drop() {
  int i, j;
  loop(i, 1, h) rep(j, w) if (v[i][j] > 0) {
    int k = i - 1;
    while (k >= 0) {
      if (v[k][j] != -1)
        break;
      k--;
    }
    if (k + 1 != i) {
      v[k + 1][j] = v[i][j];
      v[i][j] = -1;
    }
  }
}

int main(void) {
  int i, j;
  while (1) {
    cin >> h;
    if (h == 0)
      break;
    rep(i, 15) rep(j, 5) v[i][j] = -1;
    rep(i, h) rep(j, w) cin >> v[h - 1 - i][j];

    int ans = 0;

    while (1) {

      int t = disappearance();

      if (t == 0)
        break;

      ans += t;

      drop();
    }
    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:

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

string str;

int main() {
  int h;
  int a[11][5];
  int i, j;
  int k;
  int z = 0;

  while (1) {
    cin >> h;
    // cout<<'H'<<h<<endl;
    z = 0;
    if (h == 0)
      break;
    int sum = 0;
    for (i = 0; i < h; i++)
      scanf("%d %d %d %d %d", &a[i][0], &a[i][1], &a[i][2], &a[i][3], &a[i][4]);

    if (h == 1) {

      for (j = 0; j < 3; j++) {

        if (a[i - 1][j] == a[i - 1][j + 1]) {
          if (a[i - 1][j + 1] == a[i - 1][j + 2]) { // 3??£??\\???

            if (j + 2 == 4) { // 3??£?????¨???
              sum += a[i - 1][j] * 3;
              a[i - 1][j] = a[i - 1][j + 1] = a[i - 1][j + 2] = 0;
              z = 1;
              break;
            } // end3

            if (a[i - 1][j + 2] == a[i - 1][j + 3]) { // 4??£??\\???
              if (j + 3 == 4) {                       // 4??£?????¨???
                sum += a[i - 1][j] * 4;
                a[i - 1][j] = a[i - 1][j + 1] = a[i - 1][j + 2] =
                    a[i - 1][j + 3] = 0;
                z = 1;
                break;
              } // end4

              if (a[i - 1][j + 3] == a[i - 1][j + 4]) { // 5??£?????¨???
                sum += a[i - 1][j] * 5;
                z = 1;
                break;
              } // end5

              sum += a[i - 1][j] * 4;
              ;
              a[i - 1][j] = a[i - 1][j + 1] = a[i - 1][j + 2] =
                  a[i - 1][j + 3] = 0;
              z = 1;
              break;
            } // end4over

            sum += a[i - 1][j] * 3;
            a[i - 1][j] = a[i - 1][j + 1] = a[i - 1][j + 2] = 0;
            z = 1;
            break;
          } // end3over
        }   //?????¢?´¢??????
      }
      z = 0;
      cout << sum << endl;
    } else {

      while (1) {
        //???????¶???????????????????????????????
        for (i = h; i > 0; i--) {
          for (j = 0; j < 3; j++) {

            if (a[i - 1][j] == a[i - 1][j + 1]) {
              if (a[i - 1][j + 1] == a[i - 1][j + 2]) { // 3??£??\\???

                if (j + 2 == 4) { // 3??£?????¨???
                  sum += a[i - 1][j] * 3;
                  a[i - 1][j] = a[i - 1][j + 1] = a[i - 1][j + 2] = 0;
                  z = 1;
                  break;
                } // end3

                if (a[i - 1][j + 2] == a[i - 1][j + 3]) { // 4??£??\\???
                  if (j + 3 == 4) {                       // 4??£?????¨???
                    sum += a[i - 1][j] * 4;
                    a[i - 1][j] = a[i - 1][j + 1] = a[i - 1][j + 2] =
                        a[i - 1][j + 3] = 0;
                    z = 1;
                    break;
                  } // end4

                  if (a[i - 1][j + 3] == a[i - 1][j + 4]) { // 5??£?????¨???
                    sum += a[i - 1][j] * 5;
                    z = 1;
                    break;
                  } // end5

                  sum += a[i - 1][j] * 4;
                  ;
                  a[i - 1][j] = a[i - 1][j + 1] = a[i - 1][j + 2] =
                      a[i - 1][j + 3] = 0;
                  z = 1;
                  break;
                } // end4over

                sum += a[i - 1][j] * 3;
                a[i - 1][j] = a[i - 1][j + 1] = a[i - 1][j + 2] = 0;
                z = 1;
                break;
              } // end3over
            }   //?????¢?´¢??????
          }
        } // h?????¢?´¢??????

        if (z == 1) {
          //?????????????????¨???????\\?
          for (j = 0; j < 5; j++) {
            for (i = h; i > 1; i--) {
              if (a[i - 1][j] == 0) {
                for (k = i - 1; k >= 1; k--)
                  a[k][j] = a[k - 1][j];
              }
            }
          }
          //      printf("finish\\n");
          //???????????¨???????\\???????
        } else {
          cout << sum << endl;
          break;
        }
        z = 0;
      } // while end
    }
  }
}