fork download
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <string>
  5. #include <algorithm>
  6. using namespace std;
  7.  
  8. #define rep(i, n) reps(i, 0, n)
  9. #define reps(i, m, n) for (int i = m; i < int(n); ++i)
  10.  
  11. int h;
  12. int cell[10][5];
  13.  
  14. int score;
  15. #define DEBUG 0
  16.  
  17. void fall(void) {
  18. rep(i, 5) {
  19. vector<int> ls;
  20. rep(j,h) {
  21. if(cell[j][i])
  22. ls.push_back(cell[j][i]);
  23. cell[j][i]=0;
  24. }
  25. int s = ls.size();
  26. reps(j,h-s,h) {
  27. cell[j][i] = ls[j+s-h];
  28. }
  29. }
  30. }
  31.  
  32. bool next(void) {
  33. bool mod=false;
  34. rep(i,h) {
  35. bool mi = false;
  36. rep(j,3) {
  37. if(mi)break;
  38. int c = cell[i][j];
  39. if(c==0)continue;
  40. int t=0;
  41. for(; t<5-j;++t) {
  42. if(cell[i][j+t] != c) {
  43. break;
  44. }
  45. }
  46. if(t>=3) { //delete
  47. mi = true;
  48. score += t * c;
  49. rep(k,t) {
  50. cell[i][j+k] = 0;
  51. }
  52. }
  53. }
  54. if(mi) mod=true;
  55. }
  56. return mod;
  57. }
  58.  
  59. int main()
  60. {
  61. while(1) {
  62. cin>>h;
  63. if(h==0)break;
  64. rep(i,h) {
  65. rep(j,5) {
  66. cin >> cell[i][j];
  67. }
  68. }
  69. score = 0;
  70. while(next()){
  71. if(DEBUG) {
  72. rep(i,h) {
  73. rep(j,5) {
  74. cout << cell[i][j] << " ";
  75. }
  76. }
  77. cout << endl;
  78. }
  79. fall();}
  80. cout << score << endl;
  81. }
  82. }
  83.  
Success #stdin #stdout 0s 3476KB
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