fork download
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <complex>
  4. #include <vector>
  5. #include <utility>
  6. #include <algorithm>
  7. #include <cassert>
  8. #include <queue>
  9. #include <cstdio>
  10. #include <cstdlib>
  11. #include <cstring>
  12. #include <map>
  13. using namespace std;
  14.  
  15. #define rep(i, n) for (int i = 0; i < (int)(n); i++)
  16.  
  17. const int W = 5;
  18. int H;
  19. int fld[20][10];
  20.  
  21. int main() {
  22. for (;;) {
  23. scanf("%d", &H);
  24. if (H == 0) return 0;
  25. rep (y, H) rep (x, W) scanf("%d", &fld[y][x]);
  26.  
  27. int ans = 0;
  28. for (;;) {
  29. int tmp = 0;
  30.  
  31. rep (y, H) {
  32. for (int x1 = 0; x1 < W; ++x1) {
  33. if (fld[y][x1] == -1) continue;
  34.  
  35. int x2;
  36. for (x2 = x1 + 1; x2 < W; ++x2) {
  37. if (fld[y][x1] != fld[y][x2]) break;
  38. }
  39. if (x2 - x1 < 3) continue;
  40.  
  41. tmp += fld[y][x1] * (x2 - x1);
  42. for (int x = x1; x < x2; ++x) fld[y][x] = -1;
  43. }
  44. }
  45.  
  46. rep (x, W) {
  47. int y2 = H - 1;
  48. for (int y = H - 1; y >= 0; --y) {
  49. while (y2 >= 0 && fld[y2][x] == -1) --y2;
  50. if (y2 < 0) continue;
  51.  
  52. fld[y][x] = fld[y2][x];
  53. if (y2 != y) fld[y2][x] = -1;
  54. --y2;
  55. }
  56. }
  57.  
  58. if (tmp == 0) break;
  59. ans += tmp;
  60. }
  61. printf("%d\n", ans);
  62. }
  63. }
  64.  
Success #stdin #stdout 0s 3300KB
stdin
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
stdout
36
38
99
0
72