text
stringlengths
291
465k
### Prompt Please formulate a Cpp solution to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include<iostream> #include<algorithm> using namespace std; int map[20][5]; int h,ans; void DFS(){ bool move=false; for(int i=0;i<h;i++){ int cnt=0,set=0; for(int j=0;j<5;j++){ if(map[i][j]!=set){ if(cnt>=3&&set!=0){ for(int k=0;k<cnt;k++){ ans+=map[i][j-k-1]; map[i][j-k-1]=0; } //cout<<"$"<<set<<" "<<cnt<<endl; move=true; } cnt=1; set=map[i][j]; } else cnt++; } if(cnt>=3&&set!=0){ for(int k=0;k<cnt;k++){ ans+=map[i][4-k]; map[i][4-k]=0; } //cout<<"$"<<set<<" "<<cnt<<endl; move=true; } } for(int k=0;k<h;k++){ for(int j=0;j<5;j++){ for(int i=h-1;i>0;i--){ if(map[i-1][j]==0){ int tmp=map[i][j]; map[i][j]=0; map[i-1][j]=tmp; } } } } if(move)DFS(); } int main(){ while(true){ cin>>h; if(h==0)break; for(int i=0;i<h;i++){ for(int j=0;j<5;j++){ cin>>map[h-i-1][j]; } } ans=0; DFS(); cout<<ans<<endl; } return 0; } ```
### Prompt Please provide a java coded solution to the problem described below: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```java import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class Main { private static Scanner in = new Scanner(System.in); private static PrintWriter out = new PrintWriter(System.out); private static BigInteger MOD = BigInteger.valueOf((long) 1e9 + 7); public static void main(String[] args) { solve(); out.flush(); } private static void solve() { while (true) { int h = in.nextInt(); if (h == 0) break; int[][] a = new int[h][5]; for (int i=0; i < h; i++) { for (int j=0; j < 5; j++) { a[i][j] = in.nextInt(); } } int score = 0; while (true) { // System.out.println(Arrays.deepToString(a)); int scoreBefore = score; for (int i = 0; i < h; i++) { int cont = 1; int before = a[i][0]; for (int j = 1; j < 5; j++) { if (before == a[i][j]) cont++; else { if (cont >= 3) { score += before * cont; remove(a, i, j - cont, j); } cont = 1; before = a[i][j]; } } if (cont >= 3) { score += before * cont; remove(a, i, 5 - cont, 5); } } // drop down blocks TODO drop(a); if (scoreBefore == score) break; } out.println(score); } } private static void remove(int[][] a, int i, int from, int to) { for (int j=from; j < to; j++) { a[i][j] = 0; } } private static void drop(int[][] a) { List<Integer> temp = new ArrayList<>(); for (int j=0; j < a[0].length; j++) { for (int i=a.length-1; i >= 0; i--) { if (a[i][j] != 0) temp.add(a[i][j]); } int in = 0; for (int i=a.length-1; i >= 0; i--) { if (in < temp.size()) a[i][j] = temp.get(in); else a[i][j] = 0; in++; } temp.clear(); } } } ```
### Prompt Create a solution in Cpp for the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include <iostream> #include <vector> using namespace std; #define W 5 int main(){ while(true){ int h; cin >> h; if(h==0)break; vector<vector<int> > map(h,vector<int>(W,0)); for(int i=0;i<h;i++) for(int j=0;j<W;j++){ cin >> map[i][j]; } bool flag=true; int ans=0; do{ flag=false; for(int i=0;i<h;i++) for(int j=0;j<W;j++){ if(map[i][j]==-1)continue; int seg; for(seg=1;j+seg<W;seg++){ if(map[i][j]!=map[i][j+seg])break; } if(seg>=3){ for(int k=0;k<seg;k++){ flag=true; ans += map[i][j+k]; map[i][j+k] = -1; } } } for(int k=0;k<h;k++){ for(int i=1;i<h;i++){ for(int j=0;j<W;j++){ if(map[i][j]==-1){ int tmp = map[i][j]; map[i][j]=map[i-1][j]; map[i-1][j]=tmp; } } } } }while(flag); cout << ans << endl; } return 0; } ```
### Prompt Create a solution in Cpp for the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #define NDEBUG 1 #include <iostream> #include <cstdio> #include <vector> #include <algorithm> #include <cassert> using namespace std; #define loop(i,a,b) for(int i=(a);i<int(b);i++) #define rep(i,b) loop(i,0,b) typedef long long ll; int main(){ int n; while(cin >> n && n){ vector<vector<int>> g(5,vector<int>(n)); rep(i,n)rep(j,5) cin >> g[j][i]; rep(i,5) reverse(begin(g[i]), end(g[i])); // rep(i,5)rep(j,5) cin >> g[i][j]; // rep(i,5) reverse(begin(g[i]), end(g[i])); // rep(i,5)rep(j,i) swap(g[i][j], g[j][i]); int ans = 0, add = -1; while(add){ add = 0; rep(i,5){ rep(j,g[i].size()){ int ni = i; if(g[i][j] == -1) continue; while(ni < 5 && j < (int)g[ni].size() && g[ni][j]==g[i][j]) ni++; if(ni - i < 3) continue; add += g[i][j] * (ni-i); // printf(" %d %d %d\n", j,i,ni); loop(k,i,ni) g[k][j] = -1; } } for(auto & r:g) r.erase(remove(begin(r), end(r),-1), r.end()); ans += add; assert(add > 0); } cout << ans << endl; } } ```
### Prompt Construct a cpp code solution to the problem outlined: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include<iostream> #include<map> #include<string> #include<vector> #include<algorithm> #include<queue> #include<map> using namespace std; int main() { int a[10][5]; int b; while (cin >> b, b) { for (int c = 0; c < b; c++) { for (int d = 0; d < 5; d++) { scanf("%d", &a[c][d]); } } bool S; int sum = 0; do { S = false; bool k[10][5]{}; for (int c = 0; c < b; c++) { for (int d = 0; d <= 2; d++) { if (a[c][d] == a[c][d + 1]&&a[c][d] == a[c][d + 2] && a[c][d] != 0) { k[c][d] = k[c][d + 1] = k[c][d + 2] = true; S = true; } } } for (int c = 0; c < 5; c++) { for (int d = 0; d < b; d++) { if (k[d][c]) { sum += a[d][c]; for (int e = d; e > 0; e--) { a[e][c] = a[e - 1][c]; } a[0][c] = 0; } } } } while (S); cout << sum << endl; } } ```
### Prompt Please create a solution in cpp to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include<cstdio> #include<iostream> #include<algorithm> #include<vector> #include<string> using namespace std; #define reps(i,f,n) for(int i=f;i<int(n);i++) #define rep(i,n) reps(i,0,n) #define pb push_back const int H = 11; const int W = 5; int h; int board[H][W]; bool input(){ cin>>h; if(h==0)return false; rep(i,H)rep(j,W)board[i][j]=0; rep(i,h){ rep(j,W)cin>>board[i][j]; } return true; } int check[H][W]; void docheck(){ rep(i,H)rep(j,W)check[i][j]=0; rep(i,h){ rep(j,W-2){ bool hit = true; rep(k,3){ if(board[i][j+k] != board[i][j])hit=false; } if(hit){ rep(k,3)check[i][j+k]=1; } } } } int del(){ int ans= 0; rep(i,h){ rep(j,W){ if(check[i][j]==1){ ans += board[i][j]; board[i][j]=0; } } } return ans; } void tsume(){ rep(i,h){ reps(j,1,h){ rep(k,W){ if(board[j][k]==0){ swap(board[j-1][k], board[j][k]); } } } } } void print(){ rep(i,h){ rep(j,W)printf("%d ",board[i][j]);puts(""); } } void solve(){ int ans = 0; while(1){ docheck(); int val = del(); ans += val; if(val==0)break; tsume(); //print();puts(""); } cout<<ans<<endl; } int main(){ while(input())solve(); } ```
### Prompt Your challenge is to write a JAVA solution to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```java import java.util.Arrays; import java.util.Scanner; public class Main { static Scanner sc = new Scanner(System.in); static int H; static int[][] f; static boolean[][] vanish; public static void main(String[] args) { while (true) { H = sc.nextInt(); if (H == 0) break; f = new int[H][5]; vanish = new boolean[H][5]; for (int i = 0; i < H; ++i) { for (int j = 0; j < 5; ++j) { f[i][j] = sc.nextInt(); } } int sum = 0; while (true) { int add = erase(); if (add == 0) break; sum += add; fall(); } System.out.println(sum); } } static int erase() { for (int i = 0; i < H; ++i) { for (int j = 0; j < 3; ++j) { if (f[i][j] != 0 && f[i][j] == f[i][j + 1] && f[i][j] == f[i][j + 2]) { vanish[i][j] = vanish[i][j + 1] = vanish[i][j + 2] = true; } } } int ret = 0; for (int i = 0; i < H; ++i) { for (int j = 0; j < 5; ++j) { if (vanish[i][j]) { vanish[i][j] = false; ret += f[i][j]; f[i][j] = 0; } } } return ret; } static void fall() { for (int i = 0; i < 5; ++i) { int to = H - 1; for (int j = H - 1; j >= 0; --j) { if (f[j][i] == 0) continue; f[to][i] = f[j][i]; --to; } while (to >= 0) { f[to][i] = 0; --to; } } } } ```
### Prompt Please provide a JAVA coded solution to the problem described below: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```java import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int h = sc.nextInt(); while(h != 0){ int[][] s = new int[h + 1][5]; for(int i = 1; i <= h; i++){ for(int j = 0; j < 5; j++){ s[i][j] = sc.nextInt(); } } int res = 0; boolean flg = true; while(flg){ flg = false; for(int i = 1; i <= h; i++){ for(int j = 0; j < 3; j++){ int cnt = 1; for(int k = j + 1; k < 5; k++){ if(s[i][j] > 0 && s[i][j] == s[i][k]){ cnt = cnt + 1; }else{ break; } } if(cnt > 2){ flg = true; res += s[i][j] * cnt; for(int k = j; k < j + cnt; k++){ for(int l = i; l > 0; l--){ s[l][k] = s[l - 1][k]; } } break; } } } } System.out.println(res); h = sc.nextInt(); } sc.close(); } } ```
### Prompt In CPP, your task is to solve the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; int main(){ while(1){ int h; cin >> h; if(h==0) break; vector<vector<int> > board(h, vector<int>(5, 0)); for(int i=0; i<h; i++){ for(int j=0; j<5; j++){ cin >> board[i][j]; } } int point=0; bool flag = true; while(flag){ flag = false; //disappear for(int i=0; i<h; i++){ for(int j=0; j<3; j++){ int col = board[i][j]; if(col==0) continue; if(board[i][j+1]==col && board[i][j+2]==col){ flag =true; point += col*3; board[i][j] = board[i][j+1] = board[i][j+2] = 0; if(j<=1 && board[i][j+3]==col){ point += col; board[i][j+3] = 0; }else{ continue; } if(j==0 && board[i][j+4]==col){ point += col; board[i][j+4] = 0; } } } } //fall for(int i=0; i<5; i++){ for(int j=0; j<h; j++){ for(int k=h-1; k>j; k--){ if(board[k][i]==0){ swap(board[k-1][i], board[k][i]); } } } } } cout << point << endl; } return 0; } ```
### Prompt In CPP, your task is to solve the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include<bits/stdc++.h> #define REP(i,s,n) for(int i=s ; i < n ; i++) #define rep(i,n) REP(i,0,n) using namespace std; int field[10][5]; int cnt; void Connect_Num(int y,int x){ cnt++; if( x+1 < 5 && field[y][x] == field[y][x+1]){ Connect_Num(y,x+1); } } void Del(int y,int x,int n){ rep(i,n){ for(int j=y ; j > 0 ; j--){ field[j][x+i]=field[j-1][x+i]; } field[0][x+i]=0; } } int main(){ int h; while(1){ cin >>h; if(h==0)break; rep(i,h){ rep(j,5){ cin >>field[i][j]; } } int score=0; while(1){ bool flag=true; rep(i,h){ rep(j,5){ if(field[i][j] != 0){ cnt=0; Connect_Num(i,j); if(cnt >=3){ score+=cnt*field[i][j]; Del(i,j,cnt); j+=cnt; flag=false; } } } } if(flag)break; } cout <<score<<endl; } return 0; } ```
### Prompt In cpp, your task is to solve the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include <stdio.h> #include <cmath> #include <algorithm> #include <stack> #include <queue> #include <vector> using namespace std; void func(int H){ int table[H][5]; for(int i = 0; i < H; i++){ for(int k = 0; k < 5; k++)scanf("%d",&table[i][k]); } bool FLG; int count = 0; while(true){ FLG = false; for(int i = 0; i < H; i++){ for(int k = 0,p = 0; k <= 2; k++){ if(table[i][k] == -1)continue; for(p = 1; k+p < 5 && table[i][k+p] == table[i][k]; p++); if(p >= 3){ for(int a = k; a < k+p; a++){ count += table[i][a]; table[i][a] = -1; FLG = true; } break; } } } for(int col = 0; col < 5; col++){ for(int row = H-2,i = 0; row >= 0; row--){ if(table[row][col] != -1){ for(i = row+1; i < H && table[i][col] == -1; i++); if(i > row+1){ table[i-1][col] = table[row][col]; table[row][col] = -1; } } } } if(!FLG)break; } printf("%d\n",count); } int main(){ int H; while(true){ scanf("%d",&H); if(H == 0)break; func(H); } return 0; } ```
### Prompt Construct a Cpp code solution to the problem outlined: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include <bits/stdc++.h> using namespace std; #define REP(i,n) for(int i=(0); i<n; ++i) double eps = 1e-10; const int INF = 1e+9; typedef pair<int, int> P; typedef pair<int, P> pp; const int MAX_N = 105000; int H; int maze[16][8]; int search(int x, int y, int c){ int cnt = 0; while(1){ if(x >= 5 || maze[y][x] != c) break; x += 1; cnt++; } if(cnt >= 3) return cnt; else return 0; } int main() { while(cin >> H && H){ for(int i = 0; i < 16; ++i) for(int j = 0; j < 5; ++j) maze[i][j] = 0; for(int i = 0; i < H; ++i) for(int j = 0; j < 5; ++j) scanf("%d", &maze[i][j]); int ans = 0; while(1){ bool next = false; for(int y = 0; y < H; ++y){ int cnt; for(int x = 0; x < 3; ++x){ if(maze[y][x] == 0) continue; if(cnt = search(x, y, maze[y][x])){ ans += maze[y][x] * cnt; for(int i = x; i < x + cnt; ++i){ maze[y][i] = 0; } next = true; break; } } } if(!next) break; while(1){ bool changed = false; for(int y = H - 1; y > 0; --y){ for(int x = 0; x < 5; ++x){ if(maze[y][x] == 0 && maze[y - 1][x]){ swap(maze[y][x], maze[y - 1][x]); changed = true; } } } if(!changed) break; } } cout << ans << endl; } return 0; } ```
### Prompt Construct a cpp code solution to the problem outlined: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include<iostream> #include<algorithm> using namespace std; #define HMAX 10 #define WMAX 5 int main(){ int H,W; int ban[HMAX][WMAX]; while(1){ int sum1=0; cin>>H; if(H==0) break; W=5; for(int i=0;i<H;i++){ for(int j=0;j<W;j++){ cin>>ban[i][j]; sum1+=ban[i][j]; } } while(1){ bool ff=false; for(int i=0;i<H;i++){ for(int j=0;j<W;j++){ for(int l=W;l>=3;l--){ bool f=true; int base=ban[i][j]; if(ban[i][j]==-1) f=false; for(int k=0;k<l;k++){ if(j+k>=W) f=false; else if(ban[i][j+k]!=base) f=false; } if(f){ for(int k=0;k<l;k++){ ban[i][j+k]=-1; } ff=true; } } } } while(1){ bool f=false; for(int i=1;i<H;i++){ for(int j=0;j<W;j++){ if(ban[i][j]==-1&&ban[i-1][j]!=-1){ swap(ban[i][j],ban[i-1][j]); f=true; ff=true; } } } if(!f) break; } if(!ff) break; } int sum2=0; for(int i=0;i<H;i++){ for(int j=0;j<W;j++){ if(ban[i][j]!=-1) sum2+=ban[i][j]; } } cout<<sum1-sum2<<endl; } } ```
### Prompt Your challenge is to write a PYTHON3 solution to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```python3 from collections import deque ans_list = [] def solve(): H = int(input()) if H == 0: return "end" grid = [list(map(int,input().split())) for _ in range(H)] def erase(i): res = 0 a = grid[i] dq = deque() for j in range(5): if not dq: dq.append(a[j]) elif dq[-1] == a[j] and a[j] != 0: dq.append(a[j]) else: if len(dq) >= 3: res += dq[-1] * len(dq) # 消したところを0に書き換える for nj in range(j-len(dq),j): grid[i][nj] = 0 dq.clear() dq.append(a[j]) if len(dq) >= 3: res += dq[-1] * len(dq) # 消したところを0に書き換える for nj in range(j+1-len(dq),j+1): grid[i][nj] = 0 return res def drop(j): b = [grid[i][j] for i in range(H)] dq = deque() for i in range(H)[::-1]: # 下から詰めていく 左から取り出して下から詰める if b[i] != 0: dq.append(b[i]) for ni in range(H)[::-1]: if dq: num = dq.popleft() else: num = 0 grid[ni][j] = num score = 0 while True: res = 0 for i in range(H): res += erase(i) score += res if res == 0: break for j in range(5): drop(j) return score while True: ans = solve() if ans == "end": break ans_list.append(ans) for ans in ans_list: print(ans) ```
### Prompt Please create a solution in CPP to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include <iostream> using namespace std; int h, w = 5; int t[10][5]; int checkAndDelete(int x, int y){ int tx = x; while(tx < w && t[y][tx] == t[y][x]) tx++; if(tx - x < 3) return 0; int res = 0; for(int j = x; j < tx; j++){ res += t[y][j]; t[y][j] = 0; } return res; } void burokkuOtosu(){ for(int i = h - 1; i >= 0; i--){ for(int j = 0; j < w; j++){ if(t[i][j] == 0) continue; int ti; for(ti = i + 1; ti < h; ti++){ if(t[ti][j] != 0) break; } ti--; if(i != ti){ t[ti][j] = t[i][j]; t[i][j] = 0; } } } } void solve(){ int res = 0; while(true){ bool changeFlg = false; for(int i = h - 1; i >= 0; i--){ for(int j = 0; j < 3; j++){ if(t[i][j] == 0) continue; int tmp = checkAndDelete(j, i); res += tmp; if(tmp){ changeFlg = true; break; } } } burokkuOtosu(); if(!changeFlg) break; } cout << res << endl; } int main(){ while(cin >> h, h){ for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++){ cin >> t[i][j]; } } solve(); } } ```
### Prompt Your challenge is to write a JAVA solution to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```java import java.util.Scanner; public class Main{ static int[][] p; //static int h; //static int score; //static boolean end; static int check(int h, int score) { for(int i = 1; i <= 9; i++) { String c = String.valueOf(i); String s5 = c + c + c + c + c; String s4 = c + c + c + c; String s3 = c + c + c; for(int j = 0; j < h; j++) { String t = ""; for(int k = 0; k < 5; k++) { t += String.valueOf(p[j][k]); } if(t.indexOf(s5) != -1) { score += i * 5; p[j][0] = p[j][1] = p[j][1] = p[j][3] = p[j][4] = 0; }else if(t.indexOf(s4) != -1) { score += i * 4; int in = t.indexOf(s4); p[j][in] = p[j][in+1] = p[j][in+2] = p[j][in+3] = 0; }else if(t.indexOf(s3) != -1) { score += i * 3; int in = t.indexOf(s3); p[j][in] = p[j][in+1] = p[j][in+2] = 0; } } } // for(int i = 0; i < h; i++) { // for(int j = 0; j < 5; j++) { // System.out.print(p[i][j] + " "); // } // System.out.println(); // } return score; } static void down(int h) { for(int j = 0; j < 5; j++) { String s = ""; for(int i = 0; i < h; i++) { s += String.valueOf(p[i][j]); p[i][j] = 0; } s = s.replaceAll("0", ""); for(int i = 0; i < s.length(); i++) { p[i][j] = Integer.parseInt(s.substring(i, i+1)); } } } // static void swap(int a, int b) { // int temp = a; // a = b; // b = temp; // } public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(true) { int h = sc.nextInt(); if(h == 0) break; p = new int[h][5]; for(int i = h-1; i >= 0; i--) { for(int j = 0; j < 5; j++) { p[i][j] = sc.nextInt(); } } int score = 0; while(true) { int oldscore = score; score = check(h, score); //System.out.println(score); if(score == oldscore) { System.out.println(score); break; } down(h); // for(int i = 0; i < h; i++) { // for(int j = 0; j < 5; j++) { // System.out.print(p[i][j] + " "); // } // System.out.println(); // } } } } } ```
### Prompt Please provide a CPP coded solution to the problem described below: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include<iostream> using namespace std; int main(){ int h,ans; while(cin>>h,h){ ans=0; int t[200][5]={{0},{0}}; for(int i=0;i<h;i++)for(int j=0;j<5;j++)cin>>t[i][j]; while(1){ bool f=1; for(int i=0;i<h;i++){ int c=0,fi=-1; for(int j=0;j<3;j++){ if(t[i][j]==t[i][j+1]&&t[i][j]==t[i][j+2]&&t[i][j]!=0){ c++; if(fi==-1)fi=j; } } if(c!=0)ans+=t[i][fi]*(2+c),f=0; if(c!=0)for(int j=0;j<2+c;j++)t[i][fi+j]=0; } if(f)break; for(int i=h-2;i>=0;i--){ for(int j=0;j<5;j++){ int ti=i; while(true){ if(ti==h-1||t[ti+1][j]!=0)break; t[ti+1][j]=t[ti][j]; t[ti][j]=0; ti++; } } } } cout<<ans<<endl; } return 0; } ```
### Prompt Please create a solution in python3 to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```python3 def calc(puzzle,H,score): flag=False for i in range(H-1,-1,-1): stones=[] for j in range(5): now=[j,puzzle[j][i]] if (len(stones)==0 or stones[-1][1]==now[1]) and now[1]!=0: stones.append(now) elif len(stones)<3: stones=[now] else: break if len(stones)>2: flag=True score+=stones[0][1]*len(stones) for stone in stones: puzzle[stone[0]].pop(i) puzzle[stone[0]].append(0) if flag==True: return calc(puzzle,H,score) else: return score if __name__=="__main__": while True: score=0 H=int(input()) if H==0: break puzzle=[[] for _ in range(5)] for i in range(H): temp=list(map(int,input().split())) for j in range(5): puzzle[j].insert(0,temp[j]) score=calc(puzzle,H,score) print(score) ```
### Prompt Your task is to create a Java solution to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```java import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int[] one = new int[10]; int[] two = new int[10]; int[] the = new int[10]; int[] fou = new int[10]; int[] fiv = new int[10]; int reg2,sumpoint; for(;;){ sumpoint=0; Arrays.fill(one, 0); Arrays.fill(two, 0); Arrays.fill(the, 0); Arrays.fill(fou, 0); Arrays.fill(fiv, 0); int n=sc.nextInt(); if(n==0){ return; } for(int i=0;i<n;i++){ one[9-i]=sc.nextInt(); two[9-i]=sc.nextInt(); the[9-i]=sc.nextInt(); fou[9-i]=sc.nextInt(); fiv[9-i]=sc.nextInt(); } shift(one); shift(two); shift(the); shift(fou); shift(fiv); for(;;){ reg2=check(one,two,the,fou,fiv); if(reg2>0){ shift(one); shift(two); shift(the); shift(fou); shift(fiv); sumpoint+=reg2; }else{ System.out.println(sumpoint); break; } } } } static int check(int[] one,int[] two,int[] the,int[] fou,int[] fiv){ int point; int reg; int sum=0; for(int i=0;i<10;i++){ point=0; reg=0; if(two[i]==the[i]&&fou[i]==the[i]){ point=3; if(one[i]==the[i]){ point++; reg+=1; } if(fiv[i]==the[i]){ point++; reg+=2; } if(reg==0){ point*=the[i]; two[i]=0; the[i]=0; fou[i]=0; }else if(reg==1){ point*=the[i]; one[i]=0; two[i]=0; the[i]=0; fou[i]=0; }else if(reg==2){ point*=the[i]; two[i]=0; the[i]=0; fou[i]=0; fiv[i]=0; }else if(reg==3){ point*=the[i]; one[i]=0;; two[i]=0;; the[i]=0;; fou[i]=0;; fiv[i]=0; } }else if(two[i]==the[i]){ if(one[i]==the[i]){ point=3*the[i]; one[i]=0; two[i]=0; the[i]=0; } }else if(fou[i]==the[i]){ if(fiv[i]==the[i]){ point=3*the[i]; the[i]=0; fou[i]=0; fiv[i]=0; } } sum+=point; } return sum; } static void shift(int[] list){ for(int i=1;i<10;i++){ int j=0; while(i-j-1>=0&&list[i-j-1]==0){ j++; } if(j!=0){ list[i-j]=list[i]; list[i]=0; } } } } ```
### Prompt Please provide a cpp coded solution to the problem described below: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { vector<vector<int>> board; vector<vector<bool>> check; vector<int> count; int H,ans=0;bool c=true;scanf("%d",&H); while(H!=0) { ans=0;c=true; board.resize(0);check.resize(0);board.resize(H);check.resize(H); for(int i=0;i<H;i++){board[i].resize(5);check[i].resize(5,false);} for(int i=0;i<H;i++)for(int j=0;j<5;j++)scanf("%d ",&board[i][j]); while(c) { c=false; for(int i=0;i<H;i++) { count.resize(0);count.resize(5,1); for(int j=0;j<5;j++){if(check[i][j]){count[j]=0;continue;}if(j==0)continue;if(board[i][j-1]==board[i][j])count[j]+=count[j-1];} for(int j=4;j>-1;j--)if(count[j]>2&&!check[i][j]){ans+=count[j]*board[i][j];for(int k=j;k>j-count[j];k--)check[i][k]=true;c=true;break;} } for(int j=0;j<5;j++) { int stack=H-1; for(int i=H-1;i>-1;i--)if(!check[i][j]){board[stack][j]=board[i][j];check[i][j]=true;check[stack][j]=false;stack--;} } } printf("%d\n",ans); scanf("%d",&H); } } ```
### Prompt Please formulate a Java solution to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```java import java.util.*; import java.io.*; import java.awt.geom.*; import java.math.*; public class Main { static final Scanner in = new Scanner(System.in); static final PrintWriter out = new PrintWriter(System.out,false); static boolean debug = false; static boolean solve() { int h = in.nextInt(); if (h == 0) return false; int[][] t = new int[h][5]; for (int i=0; i<h; i++) { for (int j=0; j<5; j++) { t[i][j] = in.nextInt(); } } int ans = 0; while (true) { boolean f = false; for (int i=0; i<h; i++) { for (int j=0; j<3; j++) { if (t[i][j] == -1) continue; int pos = j; while (pos < 5 && t[i][j] == t[i][pos]) pos++; if (pos - j >= 3) { for (int k=j; k<pos; k++) { ans += t[i][k]; t[i][k] = -1; } f = true; } } } for (int i=h-1; i>0; i--) { for (int j=0; j<5; j++) { if (t[i][j] > 0) continue; int pos = i - 1; while (pos > 0 && t[pos][j] < 0) pos--; t[i][j] = t[pos][j]; t[pos][j] = -1; } } if (!f) break; } out.println(ans); return true; } public static void main(String[] args) { debug = args.length > 0; long start = System.nanoTime(); while (solve()); out.flush(); long end = System.nanoTime(); dump((end - start) / 1000000 + " ms"); in.close(); out.close(); } static void dump(Object... o) { if (debug) System.err.println(Arrays.deepToString(o)); } } ```
### Prompt Your task is to create a CPP solution to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include <iostream> #include <algorithm> using namespace std; int h; int w=5; int tbl[10][10]; int main(){ while(cin>>h,h){ for(int i=0;i<h;++i) for(int j=0;j<w;++j) cin >>tbl[h-i-1][j]; int score=0; while(true){ bool dis = false; for(int i=0;i<h;++i){ int c = 1; int cur = tbl[i][0]; for(int j=1;j<=w;++j){ if(tbl[i][j]==cur){ ++c; } else{ if(c>=3 && cur){ dis=true; score+=cur*c; for(int k=0;k<c;++k) tbl[i][j-k-1]=0; break; } cur = tbl[i][j]; c=1; } } } if(!dis) break; for(int j=0;j<w;++j){ int tmp[10]={}; int p=0; for(int i=0;i<h;++i) if(tbl[i][j]!=0) tmp[p++]=tbl[i][j]; for(int i=0;i<h;++i) tbl[i][j]=tmp[i]; } } cout << score << endl; } return 0; } ```
### Prompt Create a solution in cpp for the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include <iostream> #include <algorithm> using namespace std; int main(void){ while(1){ int h; cin>>h; if(!h) break; int ans=0, table[10][5]; for(int i=0; i<h; i++){ for(int j=0; j<5; j++) cin >>table[i][j];{ } } int hoge = 0; while(1){ for(int i=0; i<h; i++){ int count=0; bool b[5]; fill(b, b+5, false); if(table[i][2] == 0) continue; if(table[i][2] == table[i][1]){ b[1]=true; count++; if(table[i][2] == table[i][0]){ b[0]=true; count++; } } if(table[i][2] == table[i][3]){ b[3]=true; count++; if(table[i][2] ==table[i][4]){ b[4]=true; count++; } } if(count>=2){ b[2] = true; count++; ans += (table[i][2]*count); for(int j=0; j<5; j++){ if(b[j]==true){ table[i][j]=0; } } } }//endfor if(hoge == ans) break; hoge = ans; for(int i=0; i<h; i++){ for(int j=0; j<5; j++){ if(table[i][j] == 0){ for(int k=i; k>0; k--){ swap(table[k][j], table[k-1][j]); } } } } }//endwhile cout << ans << endl; }//endwhile return 0; } ```
### Prompt Please provide a Java coded solution to the problem described below: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```java import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class Main { MyScanner sc = new MyScanner(); Scanner sc2 = new Scanner(System.in); final int MOD = 1000000007; int[] dx = { 1, 0, 0, -1 }; int[] dy = { 0, 1, -1, 0 }; void run() { for (;;) { int H = sc.nextInt(); if (H == 0) { return; } int[][] input = new int[5][H]; for (int i = 0; i < H; i++) { for (int j = 0; j < 5; j++) { input[j][i] = sc.nextInt(); } } @SuppressWarnings("unchecked") ArrayList<Integer>[] list = new ArrayList[5]; for (int i = 0; i < 5; i++) { list[i] = new ArrayList<Integer>(); } for (int i = 0; i < 5; i++) { for (int j = 0; j < H; j++) { list[i].add(input[i][H - 1 - j]); } } int score = 0; boolean go = true; while (go) { go = false; for (int i = H; i >= 0; i--) { for (int j = 0; j < 5; j++) { int start = j; int cnt = 0; while (j + 1 < 5 && list[j].size() > i && list[j + 1].size() > i && list[j].get(i) == list[j + 1].get(i)) { j++; cnt++; } if (cnt >= 2) { int add = list[start].get(i) * (cnt + 1); score += add; for (int k = start; k <= start + cnt; k++) { list[k].remove(i); } go = true; } } } } System.out.println(score); } } public static void main(String[] args) { new Main().run(); } void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } void debug2(int[][] array) { for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++) { System.out.print(array[i][j]); } System.out.println(); } } class MyScanner { int nextInt() { try { int c = System.in.read(); while (c != '-' && (c < '0' || '9' < c)) c = System.in.read(); if (c == '-') return -nextInt(); int res = 0; do { res *= 10; res += c - '0'; c = System.in.read(); } while ('0' <= c && c <= '9'); return res; } catch (Exception e) { return -1; } } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } String next() { try { StringBuilder res = new StringBuilder(""); int c = System.in.read(); while (Character.isWhitespace(c)) c = System.in.read(); do { res.append((char) c); } while (!Character.isWhitespace(c = System.in.read())); return res.toString(); } catch (Exception e) { return null; } } } } ```
### Prompt Develop a solution in cpp to the problem described below: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include <iostream> #include <vector> using namespace std; int main() { while( true ) { long long int h; cin >> h; if ( h == 0 ) break; vector< vector< long long int > > v; for ( long long int y = 0; y < h; y++ ) { vector< long long int > w; w.push_back( 0 ); for ( long long int x = 0; x < 5; x++ ) { long long int in; cin >> in; w.push_back( in ); } w.push_back( 0 ); v.push_back( w ); } long long int ans = 0; while( true ) { bool f = false; for ( long long int y = 0; y < h; y++ ) { long long int cnt = 0; long long int num = 0; for ( long long int x = 0; x < 7; x++ ) { if ( v[y][x] != num ) { if ( cnt >= 3 && num != 0 ) { ans += cnt * num; f = true; for ( long long int k = 0; k < cnt; k++ ) { v[y][x-k-1] = 0; } } cnt = 1; num = v[y][x]; }else { cnt++; } } } if ( f == false ) { cout << ans << endl; break; } for ( long long int x = 1; x <= 5; x++ ) { long long int m = -1; for ( long long int y = h - 1; y >= 0; y-- ) { if ( v[y][x] == 0 && m == -1 ) { m = y; } if ( v[y][x] != 0 && m != -1 ) { v[m][x] = v[y][x]; v[y][x] = 0; m--; } } } } } return 0; } ```
### Prompt Your task is to create a Cpp solution to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include<iostream> using namespace std; int H,t[50][6]; int main(){ while(1){ cin>>H; if(H==0)break; for(int i=H-1;i>=0;i--){ for(int j=0;j<5;j++)cin>>t[i][j]; } int ans=0; while(1){ int tmp=ans; for(int i=0;i<H;i++){ int key=t[i][0],cnt=1; t[i][5]=-1; for(int j=1;j<6;j++){ if(key==t[i][j])cnt++; else{ if(cnt>=3&&key!=0) for(int k=0;k<cnt;k++){ ans+=key; t[i][j-k-1]=0; } cnt=1; key=t[i][j]; } } } if(tmp==ans)break; for(int j=0;j<5;j++){ int I=0; for(int i=0;i<H;i++){ if(t[i][j]==0)continue; t[I][j]=t[i][j]; I++; } while(I<H)t[I++][j]=0; } } cout<<ans<<endl; } } ```
### Prompt Generate a PYTHON3 solution to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```python3 bi = 10 while True: H = int(input()) if H == 0: break a = [] for i in range(H): A = list(input().split()) a.append(A) ans = 0 rep = True while rep: b = [[False] * 5 for i in range(H)] rep = False for i in range(H): for j in range(5): if j - 2 >= 0 and a[i][j-2] == a[i][j-1] and a[i][j-1] == a[i][j]: b[i][j] = True if j - 1 >= 0 and j + 1 < 5 and a[i][j-1] == a[i][j] and a[i][j+1] == a[i][j]: b[i][j] = True if j + 2 < 5 and a[i][j+2] == a[i][j+1] and a[i][j+1] == a[i][j]: b[i][j] = True for i in range(H): for j in range(5): if b[i][j]: rep = True #print (a[i][j]) ans += int(a[i][j]) a[i][j] = bi bi += 0.1 rep2 = True while rep2: rep2 = False for i in range(H-1): i = H-2-i for j in range(5): if int(a[i][j]) <= 9 and int(a[i+1][j]) > 9: a[i+1][j] = a[i][j] a[i][j] = bi bi += 0.1 rep2 = True #print (a) print (ans) ```
### Prompt Please formulate a cpp solution to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include <bits/stdc++.h> using namespace std; void solve(int h){ int a[h][5]; for(int i=0; i<h; ++i) for(int j=0; j<5; ++j) cin >> a[i][j]; int ans = 0; while(1){ /* for(int i=0; i<h; ++i) for(int j=0; j<5; ++j) cout << a[i][j] << (j==4?"\n":" "); */ bool f = false; for(int i=0; i<h; ++i){ if(a[i][2] == 0) continue; int l = 0, r = 0; if(a[i][1] == a[i][2]){ l = 1; if(a[i][0] == a[i][1]) l = 2; } if(a[i][3] == a[i][2]){ r = 1; if(a[i][4] == a[i][3]) r = 2; } if(l+r+1 >= 3){ ans += a[i][2] * (l+r+1); for(int j=0; j<r; ++j) a[i][2+j+1] = 0; for(int j=0; j<l; ++j) a[i][2-j-1] = 0; a[i][2] = 0; f = true; } } for(int i=0; i<5; ++i) for(int j=h-1; j>0; --j){ int t = 0; while(j-t >= 0 && a[j-t][i] == 0) ++t; if(t > 0 && j-t >= 0){ a[j][i] = a[j-t][i]; a[j-t][i] = 0; } } if(!f) break; } cout << ans << "\n"; } int main(){ // cin.tie(0); // ios::sync_with_stdio(false); while(1){ int h; cin >> h; if(h == 0) return 0; solve(h); } } ```
### Prompt Please formulate a Cpp solution to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include <iostream> using namespace std; int main(){ int H; cin >> H; while (H){ int B[10][5] = { 0 }, score = 0; for (int i = 0; i < H; i++){ cin >> B[i][0] >> B[i][1] >> B[i][2] >> B[i][3] >> B[i][4]; } int flag = 1; while (flag){ flag = 0; for (int i = 0; i < H; i++){ int connection = 0, j; for (j = 1; j < 5; j++){ if (B[i][j] && B[i][j - 1] == B[i][j]){ connection++; } else{ if (connection >= 2){ break; } connection = 0; } } if (connection >= 2){ score += B[i][j - 1] * (connection + 1); for (int k = j - 1; k >= j - connection - 1; k--){ B[i][k] = 0; } flag = 1; } } for (int i = 0; i < 5; i++){ for (int j = H - 1; j > 0; j--){ for (int k = H - 1; k > 0; k--){ if (!B[k][i]){ B[k][i] = B[k - 1][i]; B[k - 1][i] = 0; } } } } } cout << score << endl; cin >> H; } return 0; } ```
### Prompt In PYTHON3, your task is to solve the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```python3 ans_list = [] while True: h = int(input()) if not h: break stone = [list(map(int, input().split())) for i in range(h)] ans = 0 while True: key = 1 for i in range(h): del_list = [] for j in range(len(stone[0]) - 2): if stone[i][j:j + 3] == [stone[i][j], stone[i][j], stone[i][j]] and stone[i][j] != 0: key = 0 del_list.append(j) for j in del_list: ans += sum(stone[i][j:j + 3]) stone[i][j:j + 3] = [0, 0, 0] key2 = 1 ij = 0 while key2: import copy last_stone = copy.deepcopy(stone) key2 = 0 for i in range(h - 1, -1, -1): for j in range(len(stone[i])): if stone[i][j] == 0: if i != 0: key2 = 1 stone[i][j] = stone[i - 1][j] stone[i - 1][j] = 0 ij += 1 if last_stone == stone: key2 = 0 if key: break ans_list.append(ans) for i in ans_list: print(i) ```
### Prompt Please create a solution in CPP to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include <iostream> using namespace std; int a[10][5]; int check(int i,int j,int res){ if(a[i][j] == 0)return 0; if(j == 4)return res; if(a[i][j] != a[i][j+1])return res; if(a[i][j] == a[i][j+1])return check(i,j+1,res+1); } int main(){ int n,x,m; int count; int score; while(1){ cin >> n; if(n == 0)break; for(int i = 0;i < 10;i++){ for(int j = 0;j < 5;j++){ a[i][j] = 0; } } m = 0; for(int i = 10 - n;i < 10;i++){ for(int j = 0;j < 5;j++){ cin >> a[i][j]; } m++; } score = 0; while(1){ /* for(int i = 0;i < 10;i++){ for(int j = 0;j < 5;j++){ cout << a[i][j] <<" "; } cout << endl; } */ count = 0; m = 0; for(int i = 9;m < n;i--){ for(int j = 0;j < 5;j++){ x = check(i,j,1); if(x > 2){ count++; score += a[i][j] * x; for(int k = j;k < j + x;k++){ a[i][k] = 0; } break; } } m++; } //cout << count <<endl; if(count == 0)break; for(int k = 0;k < count;k++){ for(int j = 0;j < 5;j++){ m = 0; for(int i = 9;m < n;i--){ if(a[i][j] == 0)swap(a[i][j],a[i-1][j]); m++; } } } } cout << score <<endl; } return 0; } ```
### Prompt In Java, your task is to solve the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```java import java.util.Scanner; public class Main { public static void main(String[] args) { int score; Scanner sc = new Scanner(System.in); while(true) { int h=sc.nextInt(); if(h==0) break; int[][] map = new int[h][5]; for(int j=0;j<h;++j) { for(int i=0;i<5;++i) { map[j][i] = sc.nextInt(); } } score=0; while(true) { int cs = check(h,map); if(cs==0) { System.out.println(score); break; } score+=cs; map=down(h,map); } } } public static int check(int h, int[][] map) { int len,score=0; for(int j=0;j<h;++j) { for(int i=0;i<=2;++i) { for(len=1;len+i<=4;++len) { if(map[j][i] != map[j][i+len]) break; } if(len<=2) continue; score += map[j][i]*len; for(int z=0;z<len;++z) { map[j][i+z]=0; } } } return score; } public static int[][] down(int h, int[][] map) { for(int i=0;i<5;++i) { for(int min=1;min<h;++min) { for(int j=h-1;j>=min;--j) { if(map[j][i]==0) { map[j][i]=map[j-1][i]; map[j-1][i]=0; } } } } return map; } } ```
### Prompt Generate a cpp solution to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include<iostream> #include<vector> #include<string> using namespace std; #define rep(i,n) for(int i=0;i<n;i++) int main(){ int h, tmp; while (cin >> h, h) { vector<vector<int>>v(h, vector<int>()); rep(i, h) { rep(j, 5) { cin >> tmp; v[i].push_back(tmp); } } bool flag = true; int ns = 0; while (flag) { flag = false; //?¶???? rep(i, h) { rep(j, 5) { tmp = 0; while (true) { if (v[i][j] == 0 || (j + tmp + 1) == 5)break; if (v[i][j] == v[i][j + tmp + 1])tmp++; else break; } if (tmp >= 2) { ns += v[i][j] * (tmp + 1); rep(k, tmp + 1)v[i][j + k] = 0; flag = true; } } } //?????¨??? bool cont = true; while (cont) { cont = false; for (int i = h - 1; i > 0; i--) { rep(j, 5) { if (v[i][j] == 0 && v[i - 1][j]) { v[i][j] = v[i - 1][j]; v[i - 1][j] = 0; cont = true; } } } } } cout << ns << endl; } return 0; } ```
### Prompt Please formulate a CPP solution to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include<bits/stdc++.h> using namespace std; #define inf 1e9 #define ll long long #define ull unsigned long long #define M 1000000007 #define P pair<int,int> #define PLL pair<ll,ll> #define FOR(i,m,n) for(int i=(int)m;i<(int)n;i++) #define RFOR(i,m,n) for(int i=(int)m;i>=(int)n;i--) #define rep(i,n) FOR(i,0,n) #define rrep(i,n) RFOR(i,n,0) #define all(a) a.begin(),a.end() const int vx[4] = {0,1,0,-1}; const int vy[4] = {1,0,-1,0}; #define PI 3.14159265 void f(int n){ int a[100][100]={}; rep(i,n){ rep(j,5){ cin>>a[n-1-i][j]; } } ll ans = 0; bool flag = true; while(flag){ flag = false; rep(i,n){ rep(j,3){ if(a[i][j]==0) continue; int b = a[i][j]; int c = 0; FOR(k,j,6){ if(b==a[i][k]) c++; else break; } if(c>=3){ ans += c*b; while(b==a[i][j]){ a[i][j]=0; j++; flag = true; } } } } rep(i,5){ rep(j,n){ if(a[j][i]==0){ int k = j; while(a[k][i]==0&&k<n) k++; a[j][i] = a[k][i]; a[k][i] = 0; } } } } cout<<ans<<endl; } int main(){ int n; while(1){ cin>>n; if(n==0) break; f(n); } } ```
### Prompt Please formulate a PYTHON3 solution to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```python3 def puyo_one(A): ans = 0 now = 1 for i in range(1, 5): if A[i] == A[i - 1]: now += 1 else: if now >= 3: ans += now * A[i - 1] now = 1 return ans def puyo(A):#A:配列 p = 0#消せるものがなくなったらp=1とし、ansを返す ans = 0 while p == 0: this = 0 #ぷよを消して加点 for i in range(len(A)): now = 1 for j in range(1, 5): if (A[i][j] == A[i][j - 1]) and (A[i][j] != 0): now += 1 else: if now >= 3: this = 1 ans += A[i][j - 1] * now for k in range(j - now, j): A[i][k] = 0 now = 1 if now >= 3: this = 1 ans += A[i][j - 1] * now for k in range(j - now + 1, j + 1): A[i][k] = 0 #print(A, this, ans) #一回も消してない場合 if this == 0: p = 1 #残ったぷよを落とす #落とすところ N = len(A) for i in range(1, N + 1): for j in range(5): if (A[N - i][j] == 0) and (i != N): k = 1 while (A[N - i - k][j] == 0) and (k + i < N): k += 1 A[N - i][j] = A[N - i - k][j] A[N - i - k][j] = 0 #一つもない行がある時、その行ごと消す q = 0 while q == 0: if [0, 0, 0, 0, 0] in A: A.pop(A.index([0, 0, 0, 0, 0])) else: q = 1 #print(A) return ans l = 0 while l == 0: H = int(input()) if H == 0: quit() else: A = [0] * H for i in range(H): A[i] = list(map(int, input().split())) #print(A) print(puyo(A)) ```
### Prompt Develop a solution in Python3 to the problem described below: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```python3 while True: H = int(input()) if H == 0: exit() L = [input().split() for _ in range(H)] L1 = list(reversed([l[0] for l in L])) L2 = list(reversed([l[1] for l in L])) L3 = list(reversed([l[2] for l in L])) L4 = list(reversed([l[3] for l in L])) L5 = list(reversed([l[4] for l in L])) ans = 0 while True: changed = False for i in range(H): if L1[i] != 'X' and L1[i] == L2[i] == L3[i] == L4[i] == L5[i]: changed = True ans += 5*int(L1[i]) L1[i] = 'X' L2[i] = 'X' L3[i] = 'X' L4[i] = 'X' L5[i] = 'X' elif L1[i] != 'X' and L1[i] == L2[i] == L3[i] == L4[i]: changed = True ans += 4*int(L1[i]) L1[i] = 'X' L2[i] = 'X' L3[i] = 'X' L4[i] = 'X' elif L2[i] != 'X' and L2[i] == L3[i] == L4[i] == L5[i]: changed = True ans += 4*int(L2[i]) L2[i] = 'X' L3[i] = 'X' L4[i] = 'X' L5[i] = 'X' elif L1[i] != 'X' and L1[i] == L2[i] == L3[i]: changed = True ans += 3*int(L1[i]) L1[i] = 'X' L2[i] = 'X' L3[i] = 'X' elif L2[i] != 'X' and L2[i] == L3[i] == L4[i]: changed = True ans += 3*int(L2[i]) L2[i] = 'X' L3[i] = 'X' L4[i] = 'X' elif L3[i] != 'X' and L3[i] == L4[i] == L5[i]: changed = True ans += 3*int(L3[i]) L3[i] = 'X' L4[i] = 'X' L5[i] = 'X' if not changed: break L1 = list(filter(lambda x: x!='X',L1)) + ['X']*L1.count('X') L2 = list(filter(lambda x: x!='X',L2)) + ['X']*L2.count('X') L3 = list(filter(lambda x: x!='X',L3)) + ['X']*L3.count('X') L4 = list(filter(lambda x: x!='X',L4)) + ['X']*L4.count('X') L5 = list(filter(lambda x: x!='X',L5)) + ['X']*L5.count('X') print(ans) ```
### Prompt Please provide a Cpp coded solution to the problem described below: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include<cstdio> #include<iostream> #include<algorithm> #include<string> #include<cstring> using namespace std; #define rep(i,a,b) for(int (i)=(a);(i)<(b);(i)++) #define H_MAX 10 #define W 5 int H,score; int pazzle[H_MAX][W]; void disp(){ rep(i, 0, H){ rep(j, 0, W)cout << pazzle[i][j]; cout << endl; } } bool calc(void){ //disp(); int prev = score; rep(i, 0, H){ bool check[W] = { false }; int num = 0; int cnt = 0; rep(j, 1, W - 1) if (pazzle[i][j] == pazzle[i][j - 1] && pazzle[i][j] == pazzle[i][j + 1]){ cnt++; num = pazzle[i][j]; rep(k, j - 1, j + 2)check[k] = true; } score += (cnt + 2)*num; rep(j, 0, W)if (check[j])pazzle[i][j] = 0; } if (prev != score)return true; else return false; } void clear(){ //disp(); rep(i, -H+1, 0) rep(j, 0, W)if (!pazzle[-i][j]){ rep(k, i + 1, 1)if (pazzle[-k][j]){ int temp = pazzle[-i][j]; pazzle[-i][j] = pazzle[-k][j]; pazzle[-k][j] = temp; break; } } //disp(); } int main(void){ while (true){ cin >> H; if (!H)return(0); score = 0; rep(i, 0, H)rep(j, 0, W)cin >> pazzle[i][j]; while (calc())clear(); cout << score << endl; } } ```
### Prompt Generate a Cpp solution to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main(){ while (1){ int H; cin >> H; if (H == 0){ break; } vector<vector<int>> A(H, vector<int>(7, -1)); for (int i = 0; i < H; i++){ for (int j = 1; j <= 5; j++){ cin >> A[i][j]; } } int ans = 0; while (1){ bool update = false; for (int i = 0; i < H; i++){ if (A[i][3] != 0){ int L = 3; while (A[i][L] == A[i][3]){ L--; } int R = 3; while (A[i][R] == A[i][3]){ R++; } if (R - L >= 4){ for (int j = L + 1; j <= R - 1; j++){ ans += A[i][j]; A[i][j] = 0; update = true; } } } } if (!update){ break; } for (int i = 1; i <= 5; i++){ for (int j = H - 2; j >= 0; j--){ if (A[j][i] != 0){ int p = j; while (A[p + 1][i] == 0){ swap(A[p][i], A[p + 1][i]); p++; if (p == H - 1){ break; } } } } } } cout << ans << endl; } } ```
### Prompt Develop a solution in CPP to the problem described below: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include <iostream> #include <algorithm> #include <string> #include <vector> using namespace std; vector<int> c[5]; int erase(int h) { int ct = 1,n = 0,old_n = 0,max_ct = 1,i2,n2; for(int i = 0;i < 5;i++) { old_n = n; if((int)c[i].size() > h) n = c[i][h]; else n = 0; if(old_n == n) ct++; else ct = 1; if(ct > max_ct) { max_ct = ct; i2 = i; n2 = n; } } if(max_ct >= 3 && n2 != 0) { for(int i = i2 - max_ct + 1;i <= i2;i++) c[i][h] = 0; return max_ct * n2; } return 0; } int main() { int h; while(cin >> h && h != 0) { for(int i = 0;i < 5;i++) { c[i].resize(h,0); } for(int i = 0;i < h;i++) { cin >> c[0][h - i - 1] >> c[1][h - i - 1] >> c[2][h - i - 1] >> c[3][h - i - 1] >> c[4][h - i - 1]; } int score = 0,old_score = -1; while(score != old_score) { old_score = score; for(int i = 0;i < h;i++) score += erase(i); for(int i = 0;i < 5;i++) c[i].erase(remove(c[i].begin(),c[i].end(),0),c[i].end()); } cout << score << endl; } } ```
### Prompt In Cpp, your task is to solve the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include <iostream> #include <vector> #include <cstring> #include <string> #include <algorithm> #include <iomanip> using namespace std; int h; int b[10][5]; int main() { cin.tie(0); ios::sync_with_stdio(false); while(cin >> h, h) { memset(b, 0, sizeof b); for(int y = 0; y < h; y++) { for(int x = 0; x < 5; x++) { cin >> b[y][x]; } } int ans = 0; bool flag = true; while(flag) { flag = false; for(int y = 0; y < h; y++) { for(int len = 5; len >= 3; len--) { for(int s = 0; s + len <= 5; s++) { if(b[y][s] == 0) continue; bool same = true; for(int x = s; x < s + len - 1; x++) { same &= b[y][x] == b[y][x + 1]; } if(same) { flag = true; ans += b[y][s] * len; for(int x = s; x < s + len; x++) { b[y][x] = 0; } } } } } for(int y = h - 1; y >= 1; y--) { for(int x = 0; x < 5; x++) { if(b[y][x] == 0) { int ny = y; while(ny >= 0 && b[ny][x] == 0) ny--; if(ny >= 0) { swap(b[ny][x], b[y][x]); } } } } } cout << ans << endl; } } ```
### Prompt Develop a solution in python3 to the problem described below: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```python3 from inspect import currentframe def debug_print(s): # print(s) return def debug_key(*args): names = {id(v): k for k, v in currentframe().f_back.f_locals.items()} debug_print(', '.join(names.get(id(arg), '???')+' = '+repr(arg) for arg in args)) def solve(H, stone): debug_print("\n-----solve-----") debug_key(stone) ans = -1 tmp = 1 while tmp: ans += tmp tmp = 0 erase = [set() for _ in range(5)] for i in range(H): for j in range(1, 4): if stone[i][j - 1] == stone[i][j] == stone[i][j + 1]: for k in range(j - 1, j + 2): erase[k].add(i) stone2 = list(map(list, zip(*stone))) for j in range(5): for i in sorted(erase[j], reverse=True): tmp += stone[i][j] del stone2[j][i] stone2[j] = [0] * len(erase[j]) + stone2[j] stone = list(map(list, zip(*stone2))) print(ans) return if __name__ == '__main__': while True: H_input = int(input()) if H_input == 0: break stone_input = [list(map(int, input().split())) for _ in range(H_input)] solve(H_input, stone_input) ```
### Prompt Please create a solution in PYTHON to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```python while 1: H = int(raw_input()) if H == 0: break Board = [[0 for j in range(H+1)] for i in range(6)] for j in range(H): inputs = map(int,raw_input().split(" ")) for i in range(5): Board[i][H-j] = inputs[i] total_score = 0 tmp_score = -1 while tmp_score != 0: tmp_score = 0 tmpBoard = [[0] for i in range(6)] for j in range(1,H+1): con_num = 1 for i in range(1,6): if Board[i][j] == Board[i-1][j]: con_num += 1 else : if con_num >= 3: tmp_score += con_num * Board[i-1][j] else : for h in range(con_num): tmpBoard[i-1-h].append(Board[i-1-h][j]) tmpBoard[i-1-h][0] += 1 con_num = 1 total_score += tmp_score Board = [line + [0 for j in range(H-line[0])] for line in tmpBoard] print total_score ```
### Prompt Your challenge is to write a PYTHON3 solution to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```python3 while True: H = int(input()) if H == 0: break squares = [list(map(int, input().split())) for _ in range(H)] score = 0 while True: deleted = False for i in range(H): for j in range(5, 2, -1): for k in range(5 - j + 1): deletable = True first_num = squares[i][k] if first_num == 0: continue for num in squares[i][k + 1:k + j]: if first_num != num: deletable = False break if deletable: score += first_num * j squares[i][k:k + j] = [0] * j deleted = True # print(squares) if deleted: while True: not_falled = True # 下からやっていった方がいい。また、0行目はやる必要なし for i in range(H - 1, 0, -1): for k, num in enumerate(squares[i]): if num == 0 and squares[i - 1][k] != 0: squares[i - 1][k], squares[i][k] = squares[i][k], squares[i - 1][k] not_falled = False if not_falled: break else: break print(score) # print(squares) ```
### Prompt Your task is to create a cpp solution to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include <cstdio> using namespace std; int main(){ const int MAX_H = 10; int H, board[MAX_H][5]; int l, res, sub; bool deleted; while(true){ scanf("%d", &H); if(H == 0) break; for(int i = 0; i < H; i++){ for(int j = 0; j < 5; j++){ scanf("%d", &board[i][j]); } } deleted = true; res = 0; while(deleted){ deleted = false; for(int i = 0; i < H; i++){ for(int j = 0; j < 5;){ l = j; while(j < 5 && board[i][l] == board[i][j]) j++; if(j - l >= 3 && board[i][l] != 0){ res += board[i][l] * (j - l); for(int k = l; k < j; k++) board[i][k] = 0; deleted = true; } } } for(int j = 0; j < 5; j++){ for(int i = 0; i < H; i++){ if(board[i][j] == 0){ for(int k = i; k - 1 >= 0; k--){ sub = board[k][j]; board[k][j] = board[k - 1][j]; board[k - 1][j] = sub; } } } } } printf("%d\n", res); } return 0; } ```
### Prompt Construct a Cpp code solution to the problem outlined: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include <iostream> #include <algorithm> #include <queue> #include <cstring> using namespace std; int h; int map[11][6]; int main(){ int num = 1; while(cin>>h){ if(h == 0) break; // cout<<"--------------------"<<num<<"-----------------------"<<endl; //num++; memset(map,0,sizeof(map)); for(int i = 10-h+1;i<11;i++){ for(int j = 0;j<5;j++){ cin>>map[i][j]; } } int point = 0; bool update = true; /* for(int i = 0;i<10;i++){ for(int j = 0;j<5;j++){ cout<<map[i][j]<<"\t"; } cout<<endl; } */ while(update){ update = false; for(int i = 10-h+1;i <= 10;i++){ int tmp = 1; for(int j = 1;j<6;j++){ if(j != 5 && map[i][j] == 0) continue; if(map[i][j] == map[i][j-1]){ tmp++; }else if(tmp >= 3){ point += tmp * map[i][j-1]; update = true; for(int k = j-tmp;k<j;k++){ for(int l = i;l >= 0;l--){ map[l][k] = map[l-1][k]; } } tmp = 0; }else{ tmp = 1; } } } /* cout<<"--------------------------"<<endl; for(int i = 0;i<10;i++){ for(int j = 0;j<5;j++){ cout<<map[i][j]<<"\t"; } cout<<endl; } */ } cout<<point<<endl; } return 0; } ```
### Prompt Develop a solution in Cpp to the problem described below: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include<bits/stdc++.h> using namespace std; #define rep(i,n) repi(i,0,n) #define repi(i,a,b) for(int i=(int)(a);i<(int)(b);++i) #define pb push_back int H; int W= 5; int S[10][10]; int B[10][10]; int main() { while(true){ cin >> H; if(H==0)return 0; int ans = 0; rep(i,H) { rep(j,W) { cin>>S[i][j]; } } while(1) { bool flag=true; memset(B,0,sizeof(B)); /*rep(i,H) { rep(j,W) { printf("%d ",S[i][j]); } }*/ rep(i,H) { repi(j,2,W) { if(S[i][j]!=-1 && S[i][j]==S[i][j-1] && S[i][j-1]==S[i][j-2]) { B[i][j]=B[i][j-1]=B[i][j-2]=1; flag=false; } } } //printf("%d",flag); if(flag)break; rep(k,H) { //int k = H-i-1; rep(j,W) { if(B[k][j]) { ans += S[k][j]; int p = k; while(true) { if(p==0||S[p-1][j]==-1) { S[p][j]=-1; break; } else { S[p][j]=S[p-1][j]; p--; } } } } } } printf("%d\n",ans); } } ```
### Prompt Please formulate a Python3 solution to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```python3 from collections import Counter while 1: H = int(input()) if H == 0: exit() A = [[] for _ in range(5)] for i in range(H): tmp = list(map(int, input().split())) for j in range(5): A[j].append(tmp[j]) for j in range(5): A[j] = A[j][::-1] ans = 0 while 1: tmp = ans N = min([len(a) for a in A]) for j in range(N): l = Counter([A[i][j] for i in range(5)]) for k in range(10): if l[k] == 5: tmp += 5*k for i in range(5): A[i][j] = 'x' break elif A[0][j]==A[1][j]==A[2][j]==A[3][j]==k: tmp += 4*k for i in range(4): A[i][j] = 'x' break elif A[1][j]==A[2][j]==A[3][j]==A[4][j]==k: tmp += 4*k for i in range(1,5): A[i][j] = 'x' break elif A[0][j]==A[1][j]==A[2][j]==k: tmp += 3*k for i in range(3): A[i][j] = 'x' break elif A[1][j]==A[2][j]==A[3][j]==k: tmp += 3*k for i in range(1,4): A[i][j] = 'x' break elif A[2][j]==A[3][j]==A[4][j]==k: tmp += 3*k for i in range(2,5): A[i][j] = 'x' break if tmp == ans: break else: ans = tmp B = [[] for _ in range(5)] for i in range(5): for a in A[i]: if a != 'x': B[i].append(a) A = B print(ans) ```
### Prompt Develop a solution in Java to the problem described below: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```java import java.util.Scanner; public class Main { static int sum; // 消した石の数値の合計 public static void main(String[] args) { final int ROW = 5; // 列の数 final int MINREMOVE = 3; // 消すのに必要な個数 Scanner sc = new Scanner(System.in); while (true) { int H = sc.nextInt(); if (H == 0) { break; } int[][] field = new int[H][ROW+1]; for (int i = 0; i < H; i++) { for (int j = 0; j < ROW; j++) { field[i][j] = sc.nextInt(); } field[i][ROW] = 0; } sum = 0; //printField(field); //System.out.println(); while (removeField(field, MINREMOVE)) { //printField(field); //System.out.println(); fallField(field); //printField(field); //System.out.println(); } System.out.println(sum); } } public static boolean removeField(int[][] field, int n) { boolean isRemove = false; // 1回以上消したかどうか for (int i = 0; i < field.length; i++) { int num = 0; // num連続 for (int j = 0; j < field[i].length; j++) { if (j == 0) { num = 1; } else { //System.out.println(i + " " + j); if (field[i][j-1] == field[i][j] && field[i][j] != 0) { num++; } else { if (num >= n) { removeStone(field, j - num, i, num); isRemove = true; } num = 1; } } } } return isRemove; } public static void removeStone(int[][] field, int x, int y, int n) { for (int i = 0; i < n; i++) { sum += field[y][x+i]; field[y][x+i] = 0; } } public static void fallField(int[][] field) { // 下から2段目以上にある石を落とす for (int i = field.length - 2; i >= 0; i--) { for (int j = 0; j < field[i].length; j++) { if (field[i][j] != 0) { int x = j; int y = i; while (y < field.length - 1&& field[y+1][x] == 0) { field[y+1][x] = field[y][x]; field[y][x] = 0; y++; } } } } } public static void printField(int[][] field) { for (int i = 0; i < field.length; i++) { for (int j = 0; j < field[i].length; j++) { System.out.print(field[i][j] + " "); } System.out.println(); } } } ```
### Prompt In cpp, your task is to solve the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include<iostream> using namespace std; int main(){ while(1){ int H; cin>>H; if(H==0) break; int S[10][5]; for(int y=0;y<H;y++) for(int x=0;x<5;x++) cin>>S[y][x]; int sco=0; while(1){ bool flg=false; for(int y=0;y<H;y++){ int m=S[y][2]; if(m==0) continue; if(S[y][0]==m&&S[y][1]==m){ sco+=m*3;S[y][0]=S[y][1]=S[y][2]=0;flg=true; if(S[y][3]==m){ sco+=m;S[y][3]=0; if(S[y][4]==m){ sco+=m;S[y][4]=0; } } } else if(S[y][1]==m&&S[y][3]==m){ sco+=m*3;S[y][1]=S[y][2]=S[y][3]=0;flg=true; if(S[y][4]==m){ sco+=m;S[y][4]=0; } } else if(S[y][3]==m&&S[y][4]==m){ sco+=m*3;S[y][2]=S[y][3]=S[y][4]=0;flg=true; } } if(!flg) break; for(int x=0;x<5;x++){ for(int y=H-1;y>=0;y--){ if(S[y][x]!=0) continue; for(int y2=y-1;y2>=0;y2--){ if(S[y2][x]!=0){ S[y][x]=S[y2][x];S[y2][x]=0;break; } } } } } cout<<sco<<endl; } return 0; } ```
### Prompt Generate a CPP solution to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include<iostream> #include<vector> #include<algorithm> #include<string> using namespace std; int h,g[11][11]; bool fall(int x,int y){ if(g[y][x]==0)return false; bool fg=false; int tmp=g[y][x]; g[y][x]=0; while(y>0 && g[y-1][x]==0)y--,fg=true; g[y][x]=tmp; return fg; } int remove(int x,int y){ if(g[y][x]==0)return 0; int res=g[y][x],cnt=0; while(g[y][x]==g[y][x+cnt])cnt++; if(cnt<3)return 0; for(int i=0;i<cnt;i++)g[y][x+i]=0; return res*cnt; } int solve(){ bool fg; int res=0; while(true){ fg=false; for(int i=0;i<h;i++){ for(int j=0;j<5;j++){ res+=remove(j,i); fg|=fall(j,i); } } if(!fg)return res; } } int main(void){ while(cin >> h,h){ for(int i=0;i<11;i++) for(int j=0;j<11;j++)g[i][j]=0; for(int i=h-1;i>=0;i--){ for(int j=0;j<5;j++)cin >> g[i][j]; } cout << solve() << endl; } return 0; } ```
### Prompt Your challenge is to write a python3 solution to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```python3 from functools import lru_cache digits = list(map(lambda x: str(x),range(1,10))) @lru_cache(maxsize=None) def check(s,n): return ' '.join([s]*n) def down(x): x = list(x[::-1]) while '0' in x: x.remove('0') x.append('#') return x def main(n): data = '\n'.join(map(lambda x: input(), range(n))) score = 0 while True: removed = False for d in digits: if check(d, 3) in data: for i in range(5, 2, -1): if check(d, i) in data: score += int(d) * (data.count(check(d, i))*i) data = data.replace(check(d, i), check('0', i)) removed = True if removed == False: break data = zip(*map(lambda x: x.split(), data.split('\n'))) data = map(lambda x: down(x), data) data = list(map(lambda x: ' '.join(x),zip(*data))) data = '\n'.join(data[::-1]) print(score) while True: n = int(input()) if n == 0: break main(n) ```
### Prompt Please provide a PYTHON3 coded solution to the problem described below: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```python3 H = int(input()) while H: Ali = [list(map(int,input().split())) for _ in range(H)] score = 0 while True: sc = score for i in range(H): Row = Ali[i] #5つ if Row[0] == Row[1] == Row[2] == Row[3] == Row[4]: score += Row[0]*5 Ali[i][0] = Ali[i][1] = Ali[i][2] = Ali[i][3] = Ali[i][4] = 0 #4つ if Row[0] == Row[1] == Row[2] == Row[3]: score += Row[0]*4 Ali[i][0] = Ali[i][1] = Ali[i][2] = Ali[i][3] = 0 if Row[1] == Row[2] == Row[3] == Row[4]: score += Row[1]*4 Ali[i][1] = Ali[i][2] = Ali[i][3] = Ali[i][4] = 0 #3つ if Row[0] == Row[1] == Row[2]: score += Row[0]*3 Ali[i][0] = Ali[i][1] = Ali[i][2] = 0 if Row[1] == Row[2] == Row[3]: score += Row[1]*3 Ali[i][1] = Ali[i][2] = Ali[i][3] = 0 if Row[2] == Row[3] == Row[4]: score += Row[2]*3 Ali[i][2] = Ali[i][3] = Ali[i][4] = 0 if sc == score: break #落としていく処理 #下から上に見ていく for i in range(1,H+1): for j in range(0,5): if Ali[-i][j] == 0: for k in range(i+1,H+1): if Ali[-k][j] != 0: Ali[-i][j] = Ali[-k][j] Ali[-k][j] = 0 break print(score) H = int(input()) ```
### Prompt Please provide a java coded solution to the problem described below: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```java import java.util.Scanner; public class Main { class Field { int[][] ary; int len; int point; public Field(int[][] ary) { this.ary = ary; this.len = ary.length; } boolean update() { boolean changed = erase(); fall(); return changed; } boolean erase() { int buf = -1; boolean changed = false; for(int i=len-1; i>=0; i--) { int cnt = 0; for(int j=0; j<=5; j++) { if(j == 5) { //テヲツ慊ォテ・ツーツセ if(cnt >= 3 && buf != 0) { backerase(i,j,cnt); changed=true; } } else if(ary[i][j] != buf) { if(cnt >= 3 && buf != 0) { backerase(i,j,cnt); changed=true; } buf = ary[i][j]; cnt = 1; } else { cnt++; } } } return changed; } void backerase(int i, int j, int cnt) { int pt = cnt * ary[i][j-1]; while(cnt != 0) { ary[i][j-cnt] = 0; cnt--; } point += pt; } boolean fall() { boolean falled = false; for(int i=len-1; i>=0; i--) { for(int j=0; j<5; j++) { if(ary[i][j] == 0) { if(fallcolumn(i,j)) falled = true; } } } return falled; } boolean fallcolumn(int y, int j) { for(int i=y; i>=0; i--) { if(ary[i][j] != 0) { ary[y][j] = ary[i][j]; ary[i][j] = 0; return true; } } return false; } } public Main() { Scanner scanner = new Scanner(System.in); while(scanner.hasNext()) { int h = scanner.nextInt(); if(h == 0) break; int[][] ary = new int[h][5]; for(int i=0;i<h;i++) { for(int j=0; j<5; j++) { ary[i][j] = scanner.nextInt(); } } Field f = new Field(ary); while(f.update()){} System.out.println(f.point); } scanner.close(); } public static void main(String[] args) { new Main(); } } ```
### Prompt Generate a CPP solution to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include <bits/stdc++.h> using namespace std; #define rep(i, n) for(int i = 0; i < int(n) ;++i) int n; int in[100][5]; int ans; void solve(){ rep(i, n) rep(j, 5) cin >> in[n - i - 1][j]; ans = 0; while(true){ bool end = true; rep(i, n){ rep(j, 5){ if(in[i][j] == 0) continue; int k = j; while(k < 5){ if(in[i][j] == in[i][k]) ++k; else break; } if(k - j >= 3){ end = false; ans += (k - j) * in[i][j]; for(int l = j; l < k; ++l) in[i][l] = 0; } } } if(end) break; while(true){ bool end = true; for(int i = 0; i + 1 < n; ++i){ rep(j, 5){ if(in[i][j]) continue; if(in[i + 1][j]){ end = false; swap(in[i + 1][j], in[i][j]); } } } if(end) break; } } cout << ans << endl; } int main(int argc, char *argv[]) { while(cin >> n && n) solve(); return 0; } ```
### Prompt Your task is to create a java solution to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```java import java.util.Scanner; public class Main { /** * @param args */ public static void main(String[] args) { Scanner sc=new Scanner(System.in); while(true){ int h = sc.nextInt(); if(h==0)break; int[][] f = new int[h][5]; for (int i = 0; i < f.length; i++) { for (int j = 0; j < 5; j++) { f[f.length-1-i][j]=sc.nextInt(); } } System.out.println(calc(f)); } } static int calc(int[][] f){ int res = 0; while(true){ boolean[][] checked = new boolean[f.length][5]; int point = 0; for (int i = 0; i < f.length; i++) { for (int j = 0; j < 3; j++) { for (int j2 = 1; j2 <= 9; j2++) { boolean flg =true; for (int k = 0; k < 3; k++) { if(f[i][j+k]!=j2){ flg=false; } } if(flg){ for (int k = 0; k < 3; k++) { checked[i][j+k]=true; } } } } } for (int i = 0; i < checked.length; i++) { for (int j = 0; j < 5; j++) { if(checked[i][j]){ point+=f[i][j]; f[i][j]=0; } } } if(point==0)break; res+=point; fall(f); } return res; } static void fall(int[][] f){ for (int i = 0; i < f.length; i++) { for (int j = 0; j < 5; j++) { while(f[i][j]==0 && above(f,i,j)){ for (int j2 = i; j2 < f.length-1; j2++) { f[j2][j] = f[j2+1][j]; } f[f.length-1][j]=0; } } } } static boolean above(int[][]f,int y,int x){ for (int i = y+1; i < f.length; i++) { if(f[i][x]>0)return true; } return false; } } ```
### Prompt Please provide a java coded solution to the problem described below: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```java import java.util.Scanner; public class Main { static int H; static int[][] stone=new int[11][6]; static int search() { int len; int point=0; for(int row=0; row<H; row++) { for(int col=0; col<=2; col++) { for(len=1; col+len<=4; len++) { if(stone[row][col]!=stone[row][col+len]) { break; } } if(len<=2) { continue; } point+=stone[row][col]*len; for(int i=0; i<len; i++) { stone[row][col+i]=0;//消滅させる石 } } } return point;//その状態のpoint } static void change() { for(int col=0; col<5; col++) { for(int min=1; min<H; min++) { for(int row=H-1; row>=min; row--) {//落とせるところまで落とす if(stone[row][col]==0) { stone[row][col]=stone[row-1][col];//下に落とす stone[row-1][col]=0;//一つ上は消去 } } } } } public static void main(String[] args) { try(Scanner sc = new Scanner(System.in)){ while(sc.hasNext()) { H=sc.nextInt(); if(H==0) break; for(int i=0; i<H; i++) { for(int j=0; j<5; j++) { stone[i][j]=sc.nextInt(); } } int sum=0; int cont; while(true) { cont=search(); if(cont==0) { System.out.println(sum); break; } sum+=cont; change();//移動 } } } } } ```
### Prompt In Cpp, your task is to solve the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include <iostream> #include <algorithm> #include <vector> #include <string> using namespace std; int sum = 0; void vanish(int h,int stone[10][6]) { for (int i = 1; i < h; ++i) { for (int j = 0; j < 5; ++j) { if (stone[i][j] == 0) { for (int k = 0; k < i; ++k) stone[i - k][j] = stone[i - (k + 1)][j]; stone[0][j] = 0; } } } } void puzzle(int h,int stone[10][6]) { int num = 1, flag = 0; int w; for (int i = 0; i < h; ++i) { for (int j = 0; j < 5; ++j) { if (stone[i][j] == stone[i][j + 1] && stone[i][j] != 0) { if (num == 1) w = j; num++; } else if (num >= 3) { sum += num * (stone[i][w]); for (int k = 0; k < num; ++k) stone[i][w + k] = 0; num = 1; flag++; } else num = 1; } } if (flag != 0) { vanish(h, stone); puzzle(h, stone); } } int main() { int h; while (cin >> h && h != 0) { int stone[10][6] = {}; for (int i = 0; i < h; ++i) for (int j = 0; j < 5; ++j) cin >> stone[i][j]; puzzle(h, stone); cout << sum << endl; sum = 0; } return 0; } ```
### Prompt Please create a solution in CPP to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; /** Problem1193 : Chain Disappearance Puzzle **/ int main() { int H; while (cin>>H, H) { vector<vector<int> > board(H, vector<int>(5)); int ans=0; for (int y=0; y<H; y++) { for (int x=0; x<5; x++) { cin>>board[y][x]; } } while (true) { bool update=false; // erase for (int y=0; y<H; y++) { for (int x=0; x<5; x++) { int count = 1; if (board[y][x]<0) continue; for (int tx=x+1; tx<5; tx++) { if (board[y][tx] == board[y][tx-1]) count++; else break; } if (count>=3) { update = true; ans += count*board[y][x]; for (int i=0; i<count; i++) { board[y][x+i] = -1; } } } } // drop for (int x=0; x<5; x++) { for (int y=H-2; y>=0; y--) { if (board[y][x]>=0) { int ty=y; while (ty+1<H && board[ty+1][x]<0 && board[ty][x]>=0) { swap(board[ty+1][x], board[ty][x]); ty++; } } } } if (!update) break; } cout << ans << endl; } } ```
### Prompt Develop a solution in CPP to the problem described below: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include<bits/stdc++.h> using namespace std; int main(){ while(1){ int h; cin>>h; if(h==0){break;} int a[20][10]={}; for(int i=5;i<h+5;i++){ for(int t=0;t<5;t++){ cin>>a[i][t]; } } int count=0; bool j=true; while(j){ j=false; for(int i=5;i<h+5;i++){ for(int t=0;t<3;t++){ for(int x=t+1;t<5;x++){ if(a[i][t]!=a[i][x] || a[i][x]==0){break;} if(x-t>=2 && a[i][x+1]!=a[i][x]){ j=true; for(int y=t;y<=x;y++){count+=a[i][y]; a[i][y]=0;} } } } } bool k=true; while(k){ k=false; for(int i=h+4;i>=5;i--){ for(int t=0;t<5;t++){ if(a[i][t]==0 && a[i-1][t]!=0){k=true; a[i][t]=a[i-1][t]; a[i-1][t]=0;} } } } } cout<<count<<endl; } return 0; } ```
### Prompt Please formulate a cpp solution to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include "bits/stdc++.h" #define rep(i,n) for(int i = 0; i < (n); ++i) using namespace std; typedef long long int ll; typedef pair<int, int> P; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } void see(vector<vector<int>> &a){ rep(i,a.size()){ rep(j,5) cout << a[i][j] << " "; cout << endl; } cout << endl; } int main(){ cin.tie(0); ios::sync_with_stdio(false); while(true){ int h; cin >> h; if(h == 0) break; vector<vector<int>> a(h, vector<int>(5, 0)); rep(i,h)rep(j,5) cin >> a[i][j]; bool changed = true; int ans = 0; while(changed){ //see(a); changed = false; rep(i,h){ vector<bool> used(5, false); rep(j,3){ if(a[i][j] == 0) continue; if(a[i][j] == a[i][j+1] && a[i][j+1] == a[i][j+2]){ used[j] = true; used[j+1] = true; used[j+2] = true; } } rep(j,5){ if(used[j]){ ans += a[i][j]; a[i][j] = 0; changed = true; } } } rep(j,5){ for(int i = h-1; i > 0; i--){ if(a[i][j] == 0){ int cur = i-1; while(cur > 0 && a[cur][j] == 0) cur--; a[i][j] = a[cur][j]; a[cur][j] = 0; } } } } cout << ans << endl; } return 0; } ```
### Prompt Please create a solution in Cpp to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include <iostream> using namespace std; int main(){ int h; while(cin>>h){ if(h==0) break; int a[h][5]; for(int i=0;i<h;i++){ for(int j=0;j<5;j++){ cin>>a[i][j]; } } int now,ct=0,tp=0,p=1; while(p>0){ p=0; for(int i=0;i<h;i++){ ct=0; now=0; for(int j=0;j<5;j++){ if(a[i][j]==now){ ct++; } else if(a[i][j]!=now&&ct>=2){ for(int k=0;k<j;k++){ if(a[i][k]==now){ a[i][k]=0; p=p+now; } } now=a[i][j]; ct=0; } else{ ct=0; now=a[i][j]; } } if(ct>=2){ for(int j=4-ct;j<=4;j++){ a[i][j]=0; p=p+now; } } } tp=tp+p; for(int i=0;i<=h-1;i++){ for(int j=0;j<5;j++){ if(a[i][j]==0){ for(int k=i-1;k>=0;k--){ a[k+1][j]=a[k][j]; } a[0][j]=0; } } } } cout<<tp<<endl; } return 0; } ```
### Prompt Please create a solution in cpp to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include <iostream> #include <string> #include <vector> #include <cmath> #include <algorithm> #include <cstdlib> #include <ctime> #include <cstdio> #include <functional> #include <set> #include <queue> #include <cctype> #include <climits> #include <stack> #include <unordered_map> using namespace std; int main(){ int h; int st[10][5]; bool next; while(cin>>h,h){ next=true; for(int i=0;i<h;i++){ for(int j=0;j<5;j++) scanf("%d",&st[i][j]); } int res=0; while(next){ next=false; for(int i=0;i<h;i++){ for(int left=0;left+2<5;left++){ int right=left; while(right+1<5 && st[i][right+1]==st[i][left]) right++; //cout<<left<<" "<<right<<endl; if(right-left+1>=3){ res+=(right-left+1)*st[i][left]; for(int j=left;j<=right;j++) st[i][j]=0; } left=right; } } for(int i=h-2;i>=0;i--){ for(int j=0;j<5;j++){ if(st[i][j]==0) continue; int pos=i; while(pos+1<h && st[pos+1][j]==0){ next=true; swap(st[pos][j],st[pos+1][j]); pos++; } } } } cout<<res<<endl; } return 0; } ```
### Prompt Your task is to create a Cpp solution to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include <bits/stdc++.h> #define REP(i,n,N) for(int i=n;i<N;i++) #define RREP(i,N,n) for(int i=N;i>=n;i++) #define CH(n,a,b) (a)<=(n)&&(n)<(b) #define p(s) cout<<(s)<<endl typedef long long ll; using namespace std; int main() { int H; while(cin>>H,H){ int field[12][5]; bool flag[12][5]; int ans=0; REP(j,0,5) field[0][j]=-1; REP(i,1,H+1) REP(j,0,5) cin>>field[i][j]; bool ok=true; while(ok){ REP(i,0,12) REP(j,0,5) flag[i][j]=false; ok=false; REP(i,1,H+1){ int prev=0,tmp=1; REP(j,0,5){ if(field[i][j]==-1){ prev=-1; tmp=1; continue; } if(field[i][j]==prev){ tmp++; if(j==4&&tmp>2){ ok=true; ans+=prev*tmp; for(int k=4;k>j-tmp;k--) flag[i][k]=true; break; } } else{ if(tmp<3) { prev=field[i][j]; tmp=1; } else{ ok=true; ans+=prev*tmp; for(int k=j-1;k>=j-tmp;k--) flag[i][k]=true; break; } } } } REP(i,1,H+1){ REP(j,0,5){ if(flag[i][j]) { for(int k=i;(k>0&&field[k][j]!=-1);k--)field[k][j]=field[k-1][j]; } } } } p(ans); } return 0; } ```
### Prompt Your challenge is to write a CPP solution to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include <iostream> using namespace std; int table[10][5]; int H,W; int destroy(){ int done[10][5]={}; for(int i = 0 ; i < H ; i++){ for(int j = 0 ; j+2 < W ; j++){ if( table[i][j] == table[i][j+1] && table[i][j+2] == table[i][j] ) done[i][j] = done[i][j+1] = done[i][j+2] = 1; } } int ans = 0; for(int i = 0 ; i < H ; i++){ for(int j = 0 ; j < W ; j++){ if( done[i][j] ){ ans += table[i][j]; table[i][j] = 0; } } } for(int k = 0 ; k < H ; k++) for(int i = 0; i+1 < H ; i++){ for(int j = 0 ; j < W ; j++){ if( table[i+1][j] == 0 ){ table[i+1][j] = table[i][j]; table[i][j] = 0; } } } return ans; } int main(){ while( cin >> H && H ){ W = 5; for(int i = 0 ; i < H ; i++) for(int j = 0 ; j < W ; j++) cin >> table[i][j]; int r; int ans = 0; while( r=destroy() ){ ans += r; } cout << ans << endl; } } ```
### Prompt Generate a Cpp solution to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include<stdio.h> int main(){ int a[11][5]; int h,i,j,k,c,po,score,sw,sw2; while(1){ score=0; sw=0; sw2=0; po=0; c=0; scanf("%d",&h); if(h==0)break; for(i=0;i<h;i++){ for(j=0;j<5;j++){ scanf("%d",&a[i][j]); } } // printf("check0\n"); while(1){ sw=score; //del for(i=0;i<h;i++){ for(j=0;j<3;j++){ c=1; po=a[i][j]; for(k=1;j+k<5;k++){ if(a[i][j+k]==po){ c++; } else break; } if(c>=3){ for(k=0;k<c;k++){ score+=a[i][j+k]; a[i][j+k]=0; } } } } if(sw==score)break; //del end // printf("check1\n"); //fall while(1){ sw2=0; if(h!=0){ for(i=h-2;i>=0;i--){ for(j=0;j<5;j++){ if(a[i][j]!=0&&a[i+1][j]==0){ a[i+1][j]=a[i][j]; a[i][j]=0; sw2=1; } } } } if(sw2==0)break; } //fall end // printf("check2\n"); } printf("%d\n",score); } return 0; } ```
### Prompt In cpp, your task is to solve the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include<bits/stdc++.h> #define rep(i,n)for(int i=0;i<n;i++) using namespace std; int main() { int h; while (scanf("%d", &h), h) { vector<int>V[5]; rep(i, h) { rep(j, 5) { int d; scanf("%d", &d); V[j].push_back(d); } } rep(i, 5)reverse(V[i].begin(), V[i].end()); int ans = 0; bool update; do { update = false; rep(i, h) { rep(j, 3) { int k; for (k = 0; j + k<5 && V[j + k].size()>i&&V[j][i] == V[j + k][i]; k++); if (k >= 3) { update = true; ans += V[j][i]*k; rep(t, k)V[j + t][i] = 0; } } } rep(i, 5) { rep(j, V[i].size()) { if (!V[i][j]) { V[i].erase(V[i].begin() + j); j--; } } } } while (update); printf("%d\n", ans); } } ```
### Prompt Please create a solution in Python3 to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```python3 def CDP(h,w,k): q=[] ans=0 d=-1 m=0 while True: if d==ans:break d=ans for i in range(h): cnt=0 for j in range(w-1): if a[j][i]==a[j+1][i]:cnt+=1 else: if cnt>=k-1: q.append((j-cnt,i,cnt+1)) cnt=0 if cnt>=k-1: q.append((w-cnt-1,i,cnt+1)) while q: j,i,cnt=q.pop() for c in range(cnt): ans+=a[j+c].pop(i) a[j+c].append(0) return ans while True: h=int(input()) if h==0:break b=[list(i[::-1]) for i in zip(*[input().split() for _ in range(h)])] a=[[int(s) for s in strs] for strs in b] print(CDP(h,5,3)) ```
### Prompt Develop a solution in Java to the problem described below: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```java import java.io.InputStream; import java.io.PrintStream; import java.util.Arrays; import java.util.Scanner; public class Main { InputStream in = System.in; PrintStream out = System.out; private boolean check(int[] arr, int idx) { boolean a = idx >= 2 && arr[idx-2] == arr[idx-1] && arr[idx-1] == arr[idx]; boolean b = idx >=1 && idx<=3 && arr[idx-1] == arr[idx] && arr[idx] == arr[idx+1]; boolean c = idx<=2 && arr[idx] == arr[idx+1] && arr[idx+1] == arr[idx+2]; return a||b||c; } private long solv(int [][] b, int H) { long score = 0; //消す for(int i=0;i<H;i++) { int[] nl = Arrays.copyOf(b[i], b[i].length); for(int j=0;j<=4;j++) { if( check(b[i], j)) { score += b[i][j]; nl[j] = 0; } } b[i] = nl; } if(score == 0) { return 0; } //落とす for(int t=0;t<H;t++) { for(int i=H-1;i>=1;i--) { for(int j=0;j<5;j++) { if(b[i][j] == 0) { b[i][j] = b[i-1][j]; b[i-1][j] = 0; } } } } return score + solv(b,H); } public void _main(String[] args) { Scanner sc = new Scanner(in); while(true) { int H = sc.nextInt(); if(H==0) { break; } int[][] b = new int[H][5]; for(int i=0;i<H;i++) { for(int j=0;j<5;j++) { b[i][j] = sc.nextInt(); } } out.println(solv(b,H)); } sc.close(); } public static void main(String[] args) { new Main()._main(args); } @SuppressWarnings("unused") private String join(String delimiter, int[] a) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < a.length; i++) { sb.append(a[i]); sb.append(delimiter); } return sb.substring(0, Math.max(sb.length() - delimiter.length(), 0)); } @SuppressWarnings("unused") private String join(String delimiter, long[] a) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < a.length; i++) { sb.append(a[i]); sb.append(delimiter); } return sb.substring(0, Math.max(sb.length() - delimiter.length(), 0)); } @SuppressWarnings("unused") private int max(int[] arr) { if(arr == null || arr.length == 0) { throw new IllegalArgumentException(Arrays.toString(arr)); } int candidate = arr[0]; for(int i=1;i<arr.length;i++) { candidate = candidate > arr[i] ? candidate : arr[i]; } return candidate; } @SuppressWarnings("unused") private long max(long[] arr) { if(arr == null || arr.length == 0) { throw new IllegalArgumentException(Arrays.toString(arr)); } long candidate = arr[0]; for(int i=1;i<arr.length;i++) { candidate = candidate > arr[i] ? candidate : arr[i]; } return candidate; } @SuppressWarnings("unused") private int min(int[] arr) { if(arr == null || arr.length == 0) { throw new IllegalArgumentException(Arrays.toString(arr)); } int candidate = arr[0]; for(int i=1;i<arr.length;i++) { candidate = candidate < arr[i] ? candidate : arr[i]; } return candidate; } @SuppressWarnings("unused") private long min(long[] arr) { if(arr == null || arr.length == 0) { throw new IllegalArgumentException(Arrays.toString(arr)); } long candidate = arr[0]; for(int i=1;i<arr.length;i++) { candidate = candidate < arr[i] ? candidate : arr[i]; } return candidate; } @SuppressWarnings("unused") private int[] nextIntArray(Scanner sc, int n) { int[] result = new int[n]; for (int i = 0; i < n; i++) { result[i] = sc.nextInt(); } return result; } @SuppressWarnings("unused") private long[] nextLongArray(Scanner sc, int n) { long[] result = new long[n]; for (int i = 0; i < n; i++) { result[i] = sc.nextLong(); } return result; } @SuppressWarnings("unused") private int[] nextIntArray1origin(Scanner sc, int n) { int[] result = new int[n + 1]; for (int i = 1; i <= n; i++) { result[i] = sc.nextInt(); } return result; } } ```
### Prompt Construct a cpp code solution to the problem outlined: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include<iostream> #include<vector> #include<algorithm> #include<string> #include<math.h> #include<vector> using namespace std; int main(){ int H; while(1){int X[11][11]={}; cin>>H;if(H==0)break; int sum=0; for(int i=H-1;i>=0;i--) for(int j=0;j<5;j++) cin>>X[i][j]; bool f=false; while(1){ int Y=sum; for(int i=H-1;i>=0;i--){int p=0,r=1; for(int j=0;j<5;j++) { if(p!=X[i][j]){ p=X[i][j];r=1; } else if(p==X[i][j]){ r++; if(r==3){sum+=3*p;//cout<<p<<X[i][j]; X[i][j-2]=0;X[i][j-1]=0;X[i][j]=0; } if(r>3){X[i][j]=0;sum+=p;} } } } for(int j=0;j<5;j++) for(int i=0;i<H;i++) { if(X[i][j]==0){ for(int I=i+1;I<H;I++) if(X[I][j]>0){X[i][j]=X[I][j];X[I][j]=0;break; } } } /* for(int i=H-1;i>=0;i--){cout<<endl; for(int j=0;j<5;j++)cout<<X[i][j]; } */ if(Y==sum)break; } cout<<sum<<endl; } return 0; } ```
### Prompt Construct a cpp code solution to the problem outlined: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include<bits/stdc++.h> using namespace std; int H; const int W = 5; int fie[11][11]; int res; bool del(){ for(int i=0;i<H;i++){ for(int j=0;j<W;j++){ int f = fie[j][i]; if( f && j+3<=W){ int cnt = 0; for(int k=j; k < W && fie[k][i] == f; k++ ) cnt++; if( cnt > 2 ){ for(int k=j; k < W && fie[k][i] == f; k++ ){ res += fie[k][i]; fie[k][i] = 0; } return true; } } } } return false; } bool down(){ bool f = false; for(int i=H-1;i>-1;i--){ for(int j=0;j<W;j++){ if( fie[j][i] == 0 ){ fie[j][i] = fie[j][i-1]; fie[j][i-1] = 0; if( fie[j][i] != 0 ) f= true; } } } return f; } void view(){ cout << "view" << endl; for(int i=0;i<H;i++){ for(int j=0;j<W;j++){ cout << fie[j][i] << " "; } cout << endl; } } void sim(){ while( del() ){ while(del()); while(down()); } } int main(){ while(cin >> H && H){ for(int i=0;i<H;i++){ for(int j=0;j<W;j++) cin >> fie[j][i]; } res = 0; sim(); cout << res << endl; } } ```
### Prompt In cpp, your task is to solve the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include <bits/stdc++.h> using namespace std; #define for_(i,a,b) for(int i=(a);i<(b);++i) #define for_rev(i,a,b) for(int i=(a);i>=(b);--i) #define minit(a, b) memset(a, b, sizeof(a)) int H, board[11][6]; const int NIL = -1; bool fall(int x, int y) { if (board[y][x] == NIL) return false; if (board[y-1][x] == NIL) { board[y-1][x] = board[y][x]; board[y][x] = NIL; return true; } return false; } void erase(int x, int y, int len) { for_(xx,x,x+len) board[y][xx] = NIL; } int vanish(int y) { int x = 0, cur = board[y][0], len = 1; while (x <= 2) { if (cur != NIL) { while (cur == board[y][x+len]) ++len; } if (len >= 3) break; x += len; cur = board[y][x]; len = 1; } if (len < 3) return 0; erase(x, y, len); return cur * len; } void solve() { int score = 0; while (1) { int add = 0; for_(y,0,H) add += vanish(y); for_(y,1,H) { for_(x,0,5) { if (fall(x, y)) { y = 0; break; } } } if (add == 0) break; score += add; } cout << score << endl; } int main() { while (cin >> H, H) { minit(board, NIL); for_rev(y,H-1,0) for_(x,0,5) cin >> board[y][x]; solve(); } } ```
### Prompt Construct a CPP code solution to the problem outlined: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include<cstdio> #include<cstring> int fie[10][10]; int cnt_h[10][10]; int bomb[10][10]; int next[10]; int h; void fall(int x){ memset(next,0,sizeof(next)); int cnt=h-1; for(int i=h-1;i>=0;i--){ if(fie[x][i]!=0)next[i]=cnt,cnt--; } for(int i=h-1;i>=0;i--){ if(fie[x][i]!=0)fie[x][next[i]]=fie[x][i]; } for(int i=0;i<=cnt;i++)fie[x][i]=0; } void bomber(){ for(int i=0;i<h;i++){ for(int j=0;j<5;j++){ if(bomb[j][i]==1)fie[j][i]=0; } } for(int i=0;i<5;i++)fall(i); } int main(void){ while(1){ scanf("%d",&h); if(h==0)break; memset(fie,0,sizeof(fie)); int all=0; for(int i=0;i<h;i++){ for(int j=0;j<5;j++){ scanf("%d",&fie[j][i]); all+=fie[j][i]; } } int flag=1,cnt=1; while(flag){ flag=0; memset(cnt_h,0,sizeof(cnt_h)); memset(bomb,0,sizeof(bomb)); for(int i=0;i<h;i++){ cnt=1; for(int j=1;j<5;j++){ if(fie[j][i]==fie[j-1][i] && fie[j][i]!=0)cnt++; else cnt=1; cnt_h[j][i]=cnt; } } for(int i=0;i<h;i++){ for(int j=2;j<5;j++){ if(cnt_h[j][i]==3){ bomb[j-2][i]=bomb[j-1][i]=bomb[j][i]=1; flag=1; } if(cnt_h[j][i]>3)bomb[j][i]=1; } } if(flag)bomber(); } int rest=0; for(int i=0;i<5;i++){ for(int j=0;j<h;j++)rest+=fie[i][j]; } printf("%d\n",all-rest); } return 0; } ```
### Prompt Your task is to create a PYTHON3 solution to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```python3 while True: h = int(input()) if h == 0: break else: stones_list = [] for i in range(h): stones = list(map(int, input().split())) stones_list.append(stones) flag = 0 point = 0 while True: for i in range(h): cnt = 1 num = stones_list[i][0] for j in range(1, 5): if num == stones_list[i][j] and num != 0: cnt += 1 if cnt >= 3 and j == 4: point += num * cnt flag = 1 for k in range(5-cnt, 5): stones_list[i][k] = 0 else: if cnt >= 3: point += num * cnt flag = 1 for k in range(j-cnt, j): stones_list[i][k] = 0 num = stones_list[i][j] cnt = 1 for i in range(h-1, -1, -1): for j in range(5): for k in range(i, h-1): if stones_list[k+1][j] == 0: stones_list[k+1][j] = stones_list[k][j] stones_list[k][j] = 0 else: break if flag == 0: print(point) break else: flag = 0 ```
### Prompt Please formulate a CPP solution to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include <iostream> #include <algorithm> #include <vector> using namespace std; int main(){ int h; while(1){ int p=0; int flag=1; int x; cin >> h; if(h == 0) break; vector<vector<int>> A(5); for(int j = h-1; j >= 0; j--){ for(int i = 0; i< 5; i++){ cin >> x; A[i].push_back(x); } } for(int i = 0; i< 5; i++){ reverse(A[i].begin(), A[i].end()); } while(1){ int flag=0; for(int i = 0; i < h; i++){ int count=0; int mem=-1; //一行の処理開始 for(int j=0; j< 5;j++){ if(mem==A[j][i]) count++; else { mem =A[j][i]; count = 0; } if(count > 1) { A[j-2][i] +=10; A[j-1][i] +=10; A[j][i] +=10; if(mem!=0) flag =1; } } for(int k = 0; k < 5 ; k++){ if(A[k][i] >=10){ p += A[k][i]%10; A[k][i]=0; } }//一行の処理終了 } for(int i= 0; i < 5 ; i++){ vector<int> B(A[i]); int counter=0; for(int j = 0 ; j < h ; j++){ if(B[j] !=0){ A[i][counter] = B[j]; counter+=1; } } for(;counter<h; counter++){ A[i][counter] = 0; } } if(flag == 0) break; } cout <<p <<endl; } return 0; } ```
### Prompt Please create a solution in cpp to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; #define rep(i, n) for (int i = 0; i < int(n); ++i) int solve(int H, vector<vector<int>>& b) { int retval = 0; for(;;) { bool update = false; rep(i, H) rep(j, 3) if (b[i][j]) { int k = 0; for (; j + k < 5 && b[i][j + k] == b[i][j]; ++k); if (k < 3) continue; while (0 <= --k) { retval += b[i][j + k]; b[i][j + k] = 0; } update = true; } if (!update) break; for (int i = H - 1; i > 0; --i) rep(j, 5) if (!b[i][j]) { int k = 1; for (; 0 <= i - k && !b[i - k][j]; ++k); if (0 <= i - k) swap(b[i][j], b[i - k][j]); } } return retval; } int main() { for (int H; cin >> H, H;) { vector<vector<int>> board(H, vector<int>(5)); rep(i, H) rep(j, 5) cin >> board[i][j]; cout << solve(H, board) << endl; } return 0; } ```
### Prompt Create a solution in CPP for the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include <iostream> using namespace std; int main(){ int ban[10][5],add,result,did,h,i,j; while(cin>>h,h){ result=0; for(i=0;i<h;i++) for(j=0;j<5;j++) cin>>ban[i][j]; while(1){ did=0; for(i=0;i<h;i++) for(j=0;j<3;j++){ add=0; if(ban[i][j]!=0&&ban[i][j]==ban[i][j+1]&&ban[i][j]==ban[i][j+2]){ if(j+3<5) if(ban[i][j]==ban[i][j+3]){ add++; ban[i][j+3]=0; if(j+4<5) if(ban[i][j]==ban[i][j+4]){ add++; ban[i][j+4]=0; } } add+=3; result+=ban[i][j]*add; ban[i][j]=ban[i][j+1]=ban[i][j+2]=0; did++; } } if(did!=0){ for(int k=0;k<10;k++) for(i=h-1;i!=0;i--) for(j=0;j<5;j++) if(ban[i][j]==0){ ban[i][j]=ban[i-1][j]; ban[i-1][j]=0; } } else break; } cout<<result<<endl; } return 0; } ```
### Prompt Please formulate a CPP solution to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include <iostream> using namespace std; int main() { int H; int stone[10][5]; int i, j, k; int score; while(1) { score = 0; cin >> H; if(H == 0) break; for(i = 0; i < H; i++) { for(j = 0; j < 5; j++) { cin >> stone[i][j]; } } while(1) { //石を消去 int score_temp = 0; for(i = 0; i < H; i++) { for(j = 0; j < 3; j++) { for(k = 1; k + j < 5; k++) { if(stone[i][j] != stone[i][j+k]) { break; } } if(k < 3) { continue; } for(int l = 0; l < k; l++) { score_temp += stone[i][j+l]; stone[i][j+l] = 0; } } } if(score_temp == 0) break; score += score_temp; //石を落下 for(j = 0; j < 5; j++) { for(i = 0; i < H; i++) { for(k = H-1; k > i; k--) { if(stone[k][j] == 0) { stone[k][j] = stone[k-1][j]; stone[k-1][j] = 0; } } } } } cout << score << endl; } return 0; } ```
### Prompt Develop a solution in cpp to the problem described below: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include<stdio.h> #include<algorithm> using namespace std; int b[15][6]; int main(){ int a; while(scanf("%d",&a),a){ for(int i=a-1;i>=0;i--){ for(int j=0;j<5;j++)scanf("%d",&b[i][j]); } bool chg=false; int ret=0; do{ chg=false; for(int i=0;i<a;i++){ int len=0; int now=0; for(int j=0;j<6;j++){ if(now!=b[i][j]){ if(~now&&len>=3){ ret+=len*now; for(int k=j-len;k<j;k++)b[i][k]=-1; chg=true; } now=b[i][j]; len=1; }else len++; } } for(int i=0;i<5;i++){ int at=0; for(int j=0;j<a;j++){ if(~b[j][i]){ b[at++][i]=b[j][i]; } } for(int j=at;j<a;j++)b[j][i]=-1; } // for(int i=a-1;i>=0;i--){ // for(int j=0;j<5;j++)printf("%2d ",b[i][j]); // printf("\n"); // }printf("\n"); }while(chg); printf("%d\n",ret); } } ```
### Prompt Please create a solution in cpp to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include<bits/stdc++.h> using namespace std; int main(){ int H; char mas[10][5]; while( cin >> H, H){ for(int i = 0; i < H; i++){ for(int j = 0; j < 5; j++){ cin >> mas[i][j]; } } int ret = 0; bool changed = true; for(int foo = 0; foo < 7; foo++){ for(int q = 0; q < 7; q++){ for(int i = H - 1; i > 0; i--){ for(int j = 0; j < 5; j++){ if(mas[i][j] == '*'){ swap(mas[i][j],mas[i - 1][j]); //上から持ってくる } } } } for(int i = 0; i < H; i++){ char prev = mas[i][0]; int cnt = 0; for(int j = 0; j < 5; j++){ if(mas[i][j] == '*'){ cnt = 0; continue; } if(mas[i][j] != prev){ prev = mas[i][j]; cnt = 0; } cnt++; if(cnt == 3){ ret += (mas[i][j] - '0') * 3; for(int k = j, l = 0; l < cnt; l++, k--){ mas[i][k] = '*'; } } else if(cnt > 3){ ret += (mas[i][j] - '0'); mas[i][j] = '*'; } } } } cout << ret << endl; } } ```
### Prompt Develop a solution in Cpp to the problem described below: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include <bits/stdc++.h> using namespace std; #define reps(i,j,k) for(int i=j;i<k;++i) #define rep(i,j) reps(i,0,j) int stage[12][12]; int W,H; int check(){ int prev = -1; int cnt = 0; int res = 0; rep(i,H){ rep(j,W){ if(prev != stage[i][j] || prev == 0){ if(cnt >= 3){ res += stage[i][j-1]*cnt; reps(k,j-cnt,j){ stage[i][k] = 0; } } prev = stage[i][j]; cnt = 1; } else{ cnt++; } } if(cnt >= 3){ res += stage[i][W-1]*cnt; reps(k,W-cnt,W){ stage[i][k] = 0; } } cnt = 0; prev = -1; } return res; } void drop(){ rep(i,H-1){ rep(j,W){ int c = i; while(stage[i][j] == 0){ if(c == H)break; reps(k,i+1,H){ stage[k-1][j] = stage[k][j]; } stage[H-1][j] = 0; c++; } } } } int main(){ W = 5; while(scanf("%d",&H),H){ rep(i,12){ rep(j,12){ stage[i][j] = 0; } } rep(i,H){ rep(j,W){ scanf("%d",&stage[H-i-1][j]); } } int score = 0; while(true){ int tmp = check(); /*puts("**************"); rep(i,H){ rep(j,W){ printf("%d",stage[i][j]); } puts(""); }*/ if(tmp == 0)break; score += tmp; drop(); } printf("%d\n",score); } return 0; } ```
### Prompt Please create a solution in Python3 to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```python3 from itertools import groupby def main(): while True: h = int(input()) w = 5 if h == 0: break field = [] for i in range(h): field.append(list(map(int,input().split()))) score = 0 while True: scoreinc = 0 for i in range(h): c = 0 for key, value in groupby(field[i]): newlist = list(value) if len(newlist) >= 3: scoreinc += newlist[0] * len(newlist) newlist = [0 for i in range(len(newlist))] if c == 0: field[i] = newlist else: field[i] += newlist c += 1 if scoreinc == 0: break score += scoreinc for i in range(w): ydata = [] for j in range(h): ydata.append(field[j][i]) ydata = sorted(ydata, key = return0) for j in range(h): field[j][i] = ydata[j] #print(field) print(score) def return0(num): if num == 0: return 0 else: return 1 main() ```
### Prompt Create a solution in Cpp for the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include <bits/stdc++.h> using namespace std; #define rep(i,a,b) for(int i=a;i<b;i++) #define rrep(i,a,b) for(int i=a;i>=b;i--) int main() { cin.tie(0); ios::sync_with_stdio(false); int H; int T[10][5]; while (cin >> H) { if (H == 0) return 0; rep(y, 0, H) rep(x, 0, 5) cin >> T[y][x]; int ans = 0; bool f = true; while (f) { f = false; rep(y, 0, H) { rep(k, 0, 3) { if (T[y][k] < 0) continue; int c = T[y][k]; int kk = k + 1; while (T[y][kk] == c) { kk++; if (kk == 5) break; } int len = kk - k; if (3 <= len) { ans += len * c; //cout << " " << ans << endl; rep(ii, k, kk) T[y][ii] = -1; f = true; } } } rep(x, 0, 5) { vector<int> t; rrep(y, H-1, 0) if (0 <= T[y][x]) t.push_back(T[y][x]); rep(y, 0, H) T[y][x] = -1; rep(i, 0, t.size()) T[H - 1 - i][x] = t[i]; } } cout << ans << endl; } } ```
### Prompt Your task is to create a Cpp solution to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAX = 10; int val[10][5]; int main() { int H; while(cin >> H, H){ for(int i = 0;i < H;i++){ for(int j = 0;j < 5;j++){ cin >> val[i][j]; } } int res = 0; bool flag; do{ flag = false; for(int i = 0;i < H;i++){ int x = val[i][2]; if(x == 0) continue; int l = 2, r = 3; while(l >= 0 && val[i][l] == x) l--; l++; while(r < 5 && val[i][r] == x) r++; if(r - l < 3) continue; flag = true; for(int j = l;j < r;j++){ res += val[i][j]; val[i][j] = 0; } } for(int k = 0;k < 5;k++){ for(int i = H - 1, j = H - 1;i >= 0;i--){ if(val[i][k] > 0){ if(i != j) swap(val[i][k], val[j][k]); j--; } } } } while (flag); cout << res << endl; for(int i = 0;i < H;i++){ for(int j = 0;j < 5;j++){ val[i][j] = 0; } } } } ```
### Prompt Generate a java solution to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```java import java.io.InputStream; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.InputMismatchException; import java.util.NoSuchElementException; import java.math.BigInteger; public class Main{ static PrintWriter out; static InputReader ir; static void solve(){ for(;;){ h=ir.nextInt(); if(h==0) return; a=new int[h][]; for(int i=0;i<h;i++) a[i]=ir.nextIntArray(5); int tot=0; for(;;){ int scr=chain(); if(scr==0) break; tot+=scr; } out.println(tot); } } static int h; static int[][] a; public static int chain(){ int ret=0; for(int i=0;i<h;i++){ for(int j=0;j<5;j++){ if(a[i][j]==0) continue; int temp=j; while(j+1<5&&a[i][j]==a[i][j+1]) j++; if(j-temp+1>=3){ ret+=a[i][j]*(j-temp+1); for(int k=temp;k<=j;k++) a[i][k]=0; } } } for(int i=0;i<5;i++){ int now=h-1; for(int j=h-1;j>=0;j--){ if(a[j][i]>0) a[now--][i]=a[j][i]; } for(int j=now;j>=0;j--){ a[j][i]=0; } } return ret; } public static void main(String[] args) throws Exception{ ir=new InputReader(System.in); out=new PrintWriter(System.out); solve(); out.flush(); } static class InputReader { private InputStream in; private byte[] buffer=new byte[1024]; private int curbuf; private int lenbuf; public InputReader(InputStream in) {this.in=in; this.curbuf=this.lenbuf=0;} public boolean hasNextByte() { if(curbuf>=lenbuf){ curbuf= 0; try{ lenbuf=in.read(buffer); }catch(IOException e) { throw new InputMismatchException(); } if(lenbuf<=0) return false; } return true; } private int readByte(){if(hasNextByte()) return buffer[curbuf++]; else return -1;} private boolean isSpaceChar(int c){return !(c>=33&&c<=126);} private void skip(){while(hasNextByte()&&isSpaceChar(buffer[curbuf])) curbuf++;} public boolean hasNext(){skip(); return hasNextByte();} public String next(){ if(!hasNext()) throw new NoSuchElementException(); StringBuilder sb=new StringBuilder(); int b=readByte(); while(!isSpaceChar(b)){ sb.appendCodePoint(b); b=readByte(); } return sb.toString(); } public int nextInt() { if(!hasNext()) throw new NoSuchElementException(); int c=readByte(); while (isSpaceChar(c)) c=readByte(); boolean minus=false; if (c=='-') { minus=true; c=readByte(); } int res=0; do{ if(c<'0'||c>'9') throw new InputMismatchException(); res=res*10+c-'0'; c=readByte(); }while(!isSpaceChar(c)); return (minus)?-res:res; } public long nextLong() { if(!hasNext()) throw new NoSuchElementException(); int c=readByte(); while (isSpaceChar(c)) c=readByte(); boolean minus=false; if (c=='-') { minus=true; c=readByte(); } long res = 0; do{ if(c<'0'||c>'9') throw new InputMismatchException(); res=res*10+c-'0'; c=readByte(); }while(!isSpaceChar(c)); return (minus)?-res:res; } public double nextDouble(){return Double.parseDouble(next());} public int[] nextIntArray(int n){ int[] a=new int[n]; for(int i=0;i<n;i++) a[i]=nextInt(); return a; } public long[] nextLongArray(int n){ long[] a=new long[n]; for(int i=0;i<n;i++) a[i]=nextLong(); return a; } public char[][] nextCharMap(int n,int m){ char[][] map=new char[n][m]; for(int i=0;i<n;i++) map[i]=next().toCharArray(); return map; } } } ```
### Prompt Construct a python3 code solution to the problem outlined: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```python3 # https://onlinejudge.u-aizu.ac.jp/problems/1193 def delete(M): point = 0 for row in M: for l in range(len(row)-1): for r in range(l + 1, len(row)): if row[l] != row[r]: break if r - l >= 3: point += (r - l) * row[l] for i in range(l, r): row[i] = 0 break return point def drop(M): for c in range(5): nums = [] for i in range(len(M)-1, -1, -1): if M[i][c] != 0: nums.append(M[i][c]) M[i][c] = 0 for i in range(len(nums)): M[len(M)-1-i][c] = nums[i] def solve(h, M): result = 0 while True: point = delete(M) if point == 0: break result += point # print('-------') # for row in M: # print(*row) # print('--drop-----') drop(M) # for row in M: # print(*row) # print('-------') print(result) if __name__ == "__main__": while True: h = int(input()) if h == 0: break M = [list(map(int, input().split())) + [0] for _ in range(h)] solve(h, M) ```
### Prompt Please create a solution in Python3 to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```python3 from collections import defaultdict,deque import sys,heapq,bisect,math,itertools,string,queue,datetime sys.setrecursionlimit(10**8) INF = float('inf') mod = 10**9+7 eps = 10**-7 def inpl(): return list(map(int, input().split())) def inpl_str(): return list(input().split()) def calc(p,x): return p*(100+x)//100 while True: H = int(input()) if H == 0: break else: W = 5 ban = [inpl() for i in range(H)] ans = 0 while True: flag = False for y,[a1,a2,a3,a4,a5] in enumerate(ban): if a1 == a2 and a2 == a3 and a1 != -1: flag = True ban[y][0] = -1 ban[y][1] = -1 ban[y][2] = -1 ans += a1*3 if a3 == a4: ban[y][3] = -1 ans += a4 if a4 == a5: ban[y][4] = -1 ans += a5 elif a2 == a3 and a3 == a4 and a2 != -1: flag = True ban[y][1] = -1 ban[y][2] = -1 ban[y][3] = -1 ans += a2*3 if a4 == a5: ban[y][4] = -1 ans += a5 elif a3 == a4 and a4 == a5 and a3 != -1: flag = True ban[y][2] = -1 ban[y][3] = -1 ban[y][4] = -1 ans += a3*3 for _ in range(H): for y in reversed(range(1,H)): for x in range(W): if ban[y][x] == -1: ban[y][x] = ban[y-1][x] ban[y-1][x] = -1 #for y in ban: # print(y) #print() if not flag: break print(ans) ```
### Prompt Create a solution in CPP for the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include <iostream> #include <vector> using namespace std; int erase(vector<vector<int>> &m) { int sc=0; for(vector<int> &l : m){ for(int x=0;x<3;x++){ int n=l[x]; if( l[x+1]==n && l[x+2]==n){ for(int i=x;i<5;i++){ if( l[i]==n ){ sc+=l[i]; l[i]=0; }else{ break; } } break; } } } return sc; } void fall(vector<vector<int>> &m) { int h=m.size(); for(int x=0;x<5;x++){ int fa=0; for(int y=h-1;y>=0;y--){ if( m[y][x]==0){ fa++; }else{ if( fa>0 ){ swap(m[y][x],m[y+fa][x]); } } } } } int f(vector<vector<int>> &m) { int total=0; while(true){ int sc = erase(m); if(sc==0)break; total+=sc; fall(m); } return total; } int main() { while(true) { int n; cin >> n; if(n==0)break; vector<vector<int>> m; for(int i=0;i<n;i++){ vector<int> l; for(int j=0;j<5;j++){ int x; cin >> x; l.push_back(x); } m.push_back(l); } int sc=f(m); cout << sc << endl; } // your code goes here return 0; } ```
### Prompt Construct a Cpp code solution to the problem outlined: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include<bits/stdc++.h> using namespace std; int main(){ int H; char mas[10][5]; while( cin >> H, H){ for(int i = 0; i < H; i++){ for(int j = 0; j < 5; j++){ cin >> mas[i][j]; } } int ret = 0; bool changed = true; for(int foo = 0; foo < 10; foo++){ for(int q = 0; q < 10; q++){ for(int i = H - 1; i > 0; i--){ for(int j = 0; j < 5; j++){ if(mas[i][j] == '*'){ swap(mas[i][j],mas[i - 1][j]); //上から持ってくる } } } } for(int i = 0; i < H; i++){ char prev = mas[i][0]; int cnt = 0; for(int j = 0; j < 5; j++){ if(mas[i][j] == '*'){ cnt = 0; continue; } if(mas[i][j] != prev){ prev = mas[i][j]; cnt = 0; } cnt++; if(cnt == 3){ ret += (mas[i][j] - '0') * 3; for(int k = j, l = 0; l < cnt; l++, k--){ mas[i][k] = '*'; } } else if(cnt > 3){ ret += (mas[i][j] - '0'); mas[i][j] = '*'; } } } } cout << ret << endl; } } ```
### Prompt Create a solution in cpp for the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include<iostream> using namespace std; int main(){ for(int H;cin>>H,H;){ int g[99][5]={}; for(int i=H-1;i>=0;i--){ for(int j=0;j<5;j++){ cin>>g[i][j]; } } int ans=0; for(;;){ bool f=false; for(int i=0;i<H;i++){ bool d[99][5]={}; for(int j=0;j<=2;j++){ if(g[i][j]&&g[i][j]==g[i][j+1]&&g[i][j+1]==g[i][j+2]){ d[i][j]=d[i][j+1]=d[i][j+2]=true; } } for(int j=0;j<5;j++){ if(d[i][j]){ ans+=g[i][j]; g[i][j]=0; f=true; } } } if(!f)break; for(int i=0;i<5;i++){ for(int j=0,r=0;j<H;j++,r++){ while(r<H&&g[r][i]==0){ r++; } g[j][i]=g[r][i]; } } } cout<<ans<<endl; } } ```
### Prompt Construct a cpp code solution to the problem outlined: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include <iostream> #include <vector> #include <string> #include <algorithm> #include <cmath> #include <cstdio> using namespace std; #define loop(i,a,b) for(int i=(a);i<int(b);i++) #define rep(i,b) loop(i,0,b) #define all(c) c.begin(), c.end() typedef vector<int> vi; typedef vector<vi> vvi; int main(){ int n; while(cin>>n && n){ vvi g(5,vi(n)); rep(i,n)rep(j,5)cin>>g[j][i]; rep(i,5)reverse(all(g[i])); int ans=0; while(1){ int t=0; rep(i,5){ rep(j,g[i].size()){ int ni=i; if(g[i][j]==-1) continue; while(ni < 5 && j < g[ni].size() && g[ni][j]==g[i][j]){ ni++; } if(ni-i<3) continue; for(int ii=i;ii<ni;ii++){ t+=g[ii][j]; g[ii][j] = -1; } } } if(t==0)break; // rep(i,5){ // rep(j,g[i].size()){ // cout << g[i][j] << " "; // } // cout<<endl; // } rep(i,5)g[i].erase(remove(all(g[i]),-1),g[i].end()); ans+=t; } cout << ans << endl; } } ```
### Prompt In cpp, your task is to solve the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include<vector> #include<iostream> #define F(A, B, M) for(int A=B; A<M; A++) using namespace std; int printall(vector<vector<int> > wall, int H){ F(i, 0, H){ F(j, 0, 5){ cout << wall[i][j] << ' '; } cout << endl; } } int main(){ int H; cin >> H; while(H != 0){ vector<vector<int> > wall; vector<int> line; int score=0, tmp; F(i, 0, H){ line.clear(); F(j, 0, 5){ cin >> tmp; line.push_back(tmp); } wall.push_back(line); } F(r, 0, 20){ F(i, 0, H){ int cnt=1; F(j, 1, 5){ if(wall[i][j] == -1)continue; if(wall[i][j] == wall[i][j-1]){ cnt++; if(j == 4 && cnt >= 3){ score += wall[i][j]*cnt; F(k, j-cnt+1, j+1){ wall[i][k] = -1; } } }else if(cnt >= 3){ score += wall[i][j-1]*cnt; F(k, j-cnt, j){ wall[i][k] = -1; } cnt = 1; }else{ cnt = 1; } } } //printall(wall, H); //fall for(int i=H-1; i>=0; i--){ F(j, 0, 5){ if(wall[i][j] == -1){ for(int k=i-1; k>=0; k--){ if(wall[k][j] != -1){ wall[i][j] = wall[k][j]; wall[k][j] = -1; break; } } } } } } cout << score << endl; cin >> H; } } ```
### Prompt Develop a solution in CPP to the problem described below: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include <iostream> #include <cmath> #include <algorithm> #include <vector> using namespace std; using ll = long long; int field[10][5]={}; int sum=0; void game(int h){ int newfield[11][5]={}; for(int y=0;y<h;y++){ newfield[y][0]=field[y][0]; newfield[y][4]=field[y][4]; for(int x=1;x<4;x++){ if(field[y][x-1]==field[y][x]&&field[y][x+1]==field[y][x]){ newfield[y][x-1]-=1000; newfield[y][x]-=1000; newfield[y][x+1]-=1000; } else{ newfield[y][x]+=field[y][x]; } } for(int i=0;i<5;i++){ if(newfield[y][i]<0) newfield[y][i]=0; } } for(int i=0;i<h;i++){ for(int y=h-1;y>=0;y--){ for(int x=0;x<5;x++){ if(newfield[y][x]==0){ for(int Y=y;Y>=0;Y--) if(Y==0) newfield[Y][x]=0; else{ newfield[Y][x]=newfield[Y-1][x]; } } } } } //cout << endl; for(int y=0;y<h;y++){ //cout << endl; for(int x=0;x<5;x++){ field[y][x] = newfield[y][x]; //cout << field[y][x] << ' '; } } } int main(void){ while(1){ int H,sum=0,sum2=0; cin >> H; if(!H) break; for(int y=0;y<H;y++){ for(int x=0;x<5;x++){ cin >> field[y][x]; sum+=field[y][x]; } } for(int i=0;i<H;i++) game(H); for(int i=0;i<H;i++){ for(int j=0;j<5;j++) sum2+=field[i][j]; } cout << sum-sum2 << endl; } return 0; } ```
### Prompt Please formulate a cpp solution to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include<bits/stdc++.h> using namespace std; // macro #define rep(i,n) for(i=0;i<n;i++) #define ll long long #define all(v) v.begin(), v.end() // code starts int main() { int h; cin>>h; while(h!=0) { vector<vector<int>> board(h,vector<int>(5)); int i,j,k; rep(i,h)rep(j,5)cin>>board[i][j]; int score=0; bool update=true; while(update) { update=false; rep(i,h) { int base=board[i][2]; if(base==0)continue; int count=0; int kc=0; int st=0; rep(j,5) { if(board[i][j]==base) { if(kc==0&&j<3)st=j; kc++; count=max(count,kc); } else { kc=0; } } if(count>=3) { update=true; score+=base*count; for(j=st;j<st+count;j++)if(board[i][j]==base) { board[i][j]=0; } } } rep(j,5) { for(i=h-1;i>=0;i--) { if(board[i][j]!=0)continue; for(k=i-1;k>=0;k--) { if(board[k][j]!=0) { swap(board[i][j],board[k][j]); break; } } } } } cout<<score<<endl; cin>>h; } } ```
### Prompt Create a solution in python3 for the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```python3 import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**10 mod = 998244353 def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def pf(s): return print(s, flush=True) def main(): rr = [] while True: h = I() if h == 0: break a = [LI() for _ in range(h)] f = True r = 0 while f: f = False d = [[0]*5 for _ in range(h)] for i in range(h): for j in range(3): if a[i][j] > 0 and a[i][j] == a[i][j+1] and a[i][j] == a[i][j+2]: d[i][j] = 1 d[i][j+1] = 1 d[i][j+2] = 1 f = True for i in range(h): for j in range(5): if d[i][j] == 1: r += a[i][j] a[i][j] = 0 for i in range(h-2,-1,-1): for j in range(5): if a[i][j] <= 0: continue k = i while k < h-1 and a[k+1][j] == 0: a[k+1][j] = a[k][j] a[k][j] = 0 k += 1 rr.append(r) return '\n'.join(map(str, rr)) print(main()) ```
### Prompt Please provide a cpp coded solution to the problem described below: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include <cstdio> #include <algorithm> int main(void) { for( ; ; ){ int h; int m[16][16]; scanf("%d", &h); if(!h) break; for(int i = 0;i < h; i++) for(int j = 0;j < 5; j++) scanf("%d", m[h - i - 1] + j); int ans = 0; for( ; ; ){ /* 水平に並んだブロックを探す */ for(int i = 0;i < h; i++){ int pre = m[i][0], cnt = 1; for(int j = 1;j < 5; j++){ if(pre == m[i][j]) cnt++; else{ if(cnt >= 3){ ans += pre * cnt; for(int k = 1;k <= cnt; k++) m[i][j - k] = 0; } pre = m[i][j]; cnt = 1; } } if(cnt >= 3){ ans += pre * cnt; for(int k = 1;k <= cnt; k++) m[i][5 - k] = 0; } } /* ブロックを落とす */ bool flag = true; // 連鎖が終わってる? for(int i = 1;i < h; i++){ for(int j = 0;j < 5; j++){ if(m[i][j] == 0 || m[i - 1][j] != 0) continue; int k = i - 1; for( ;k - 1 >= 0 && m[k - 1][j] == 0; k--) ; std::swap(m[k][j], m[i][j]); flag = false; } } if(flag) break; } printf("%d\n", ans); } } ```
### Prompt Develop a solution in PYTHON3 to the problem described below: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```python3 def dis(): k=0 for i in range(n): for j in range(3): if l[i][j]==0: continue re=1 for z in range(1,100): if j+z>4:break if l[i][j]==l[i][j+z]: re+=1 else: break if re<3:continue k+=l[i][j]*re for x in range(re): l[i][j+x]=0 return k def dow(): for i in range(n-1,0,-1): for j in range(4,-1,-1): if l[i][j]==0: for z in range(1,i+1): if l[i-z][j]!=0: l[i][j],l[i-z][j]=l[i-z][j],l[i][j] break while True: n=int(input()) if n==0:exit() l=[list(map(int,input().split())) for _ in range(n)] ans=0 k=dis() while k!=0: dow() ans+=k k=dis() print(ans) ```
### Prompt Construct a Python3 code solution to the problem outlined: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```python3 #!usr/bin/env python3 from collections import defaultdict,deque from heapq import heappush, heappop from itertools import permutations import sys import math import bisect def LI(): return [int(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS():return [list(x) for x in sys.stdin.readline().split()] def S(): res = list(sys.stdin.readline()) if res[-1] == "\n": return res[:-1] return res def IR(n): return [I() for i in range(n)] def LIR(n): return [LI() for i in range(n)] def SR(n): return [S() for i in range(n)] def LSR(n): return [LS() for i in range(n)] sys.setrecursionlimit(1000000) mod = 1000000007 def solve(): def check(y,x): res = 0 c = s[x][y] for x in range(x,5): if s[x][y] != c: break res += 1 if res < 3: return 0 else: return res def update(y,x,d): for i in range(x,x+d): s[i][y] = 0 def fall(s): q = [[] for i in range(5)] for x in range(5): for y in range(h): if s[x][y]: q[x].append(s[x][y]) for y in range(h-len(q[x])): q[x].append(0) return q while 1: h = I() if h == 0: break s = LIR(h) s = [[s[-1-y][x] for y in range(h)] for x in range(5)] ans = 0 f = 1 while f: f = 0 for y in range(h): for x in range(3): if s[x][y]: d = check(y,x) ans += s[x][y]*d if d: update(y,x,d) f = 1 if f: s = fall(s) print(ans) return #Solve if __name__ == "__main__": solve() ```
### Prompt Please provide a Java coded solution to the problem described below: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws NumberFormatException, IOException { // TODO 自動生成されたメソッド・スタブ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); while(true){ int h = Integer.parseInt(br.readLine()); if(h == 0){ break; } int matrix[][] = new int[h][5]; for(int i = h - 1; i >= 0; i--){ String[] tmpArray = br.readLine().split(" "); for(int j = 0; j < 5; j++){ matrix[i][j] = Integer.parseInt(tmpArray[j]); } } System.out.println(solve(matrix)); } } static int solve(int[][] matrix){ int score = 0; int h = matrix.length; while(true){ if(matrix[0][0] == 0 && matrix[0][1] == 0 && matrix[0][2] == 0 && matrix[0][3] == 0 && matrix[0][4] == 0 ){ break; } int tmpScore = 0; for(int i = 0; i < h; i++){ int left = 0; int right = 0; for(int j = 0; j <= 2; j++){ left = j; right = j; for(int k = j + 1; k < 5; k++){ if(matrix[i][k] == matrix[i][j]){ right = k; } else { break; } } //連鎖成立 if(right - left >= 2){ break; } } //連鎖処理 if(right - left >= 2){ for(int j = left; j <= right ; j++){ tmpScore += matrix[i][j]; matrix[i][j] = 0; } } } // System.out.println("befor"); // show(matrix); //ドロップ処理 if(h != 1){ for(int j = 0; j < 5; j++){ int target = 0; for(int k = 0; k < h; k++){ if(matrix[k][j] != 0){ matrix[target][j] = matrix[k][j]; // System.out.println("k = "+k+" target = "+target); target++; } } for(int k = target; k < h ; k++){ matrix[k][j] = 0; } } } // System.out.println("after"); // show(matrix); if(tmpScore == 0){ break; } else { score += tmpScore; } } return score; } //for debug static void show (int[][] matrix){ for(int i = matrix.length - 1; i >= 0; i--){ for(int j = 0; j < 5; j++){ System.out.print(matrix[i][j] + " "); } System.out.println(); } System.out.println(); } } ```
### Prompt Your challenge is to write a java solution to the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import static java.lang.Integer.parseInt; /** * Chain Disappearance Puzzle */ public class Main { public static int W = 5; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line; while ((line = br.readLine()) != null && !line.isEmpty()) { int H = parseInt(line); if (H == 0) break; // init @SuppressWarnings("unchecked") List<Integer>[] board = new ArrayList[W]; for (int i = 0; i < W; i++) { board[i] = new ArrayList<>(); } for (int i = 0; i < H; i++) { String[] words = br.readLine().split(" "); for (int j = 0; j < W; j++) { board[j].add(0, parseInt(words[j])); } } // solve int point = 0; boolean hasNext = true; while (hasNext) { hasNext = false; // chain for (int i = 0; i < H; i++) { int min = W, max = -1, stone = 0; for (int j = 1; j <= 3; j++) { if (board[j].get(i) != 0 && board[j].get(i) == board[j - 1].get(i) && board[j].get(i) == board[j + 1].get(i)) { min = Math.min(min, j - 1); max = Math.max(max, j + 1); stone = board[j].get(i); hasNext = true; } } if (max - min >= 2) point += stone * (max - min + 1); for (int j = min; j <= max; j++) board[j].set(i, 0); } // drop down for (int i = 0; i < W; i++) { board[i].removeIf(x -> x == 0); while (board[i].size() < H) board[i].add(0); } } System.out.println(point); } //end while } //end main } ```
### Prompt Create a solution in CPP for the following problem: Chain Disappearance Puzzle We are playing a puzzle. An upright board with H rows by 5 columns of cells, as shown in the figure below, is used in this puzzle. A stone engraved with a digit, one of 1 through 9, is placed in each of the cells. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. If there are stones in the cells above the cell with a disappeared stone, the stones in the above cells will drop down, filling the vacancy. <image> The puzzle proceeds taking the following steps. 1. When three or more stones in horizontally adjacent cells are engraved with the same digit, the stones will disappear. Disappearances of all such groups of stones take place simultaneously. 2. When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled. 3. After the completion of all stone drops, if one or more groups of stones satisfy the disappearance condition, repeat by returning to the step 1. The score of this puzzle is the sum of the digits on the disappeared stones. Write a program that calculates the score of given configurations of stones. <image> Input The input consists of multiple datasets. Each dataset is formed as follows. > Board height H > Stone placement of the row 1 > Stone placement of the row 2 > ... > Stone placement of the row H > The first line specifies the height ( H ) of the puzzle board (1 ≤ H ≤ 10). The remaining H lines give placement of stones on each of the rows from top to bottom. The placements are given by five digits (1 through 9), separated by a space. These digits are engraved on the five stones in the corresponding row, in the same order. The input ends with a line with a single zero. Output For each dataset, output the score in a line. Output lines may not include any characters except the digits expressing the scores. Sample Input 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 Output for the Sample Input 36 38 99 0 72 Example Input 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 Output 36 38 99 0 72 ### Response ```cpp #include <iostream> #include <cmath> #include <cstring> #include <utility> using namespace std; int solve(int x,int p){ return floor(p*(1+x*0.01)); } int main() { int n; int a[6][10]; memset(a,0,sizeof(a)); while(cin >> n && n){ for(int i=0;i<n;i++) for(int j=0;j<5;j++) cin >> a[j][i]; int sum=0; int csum; do{ csum=sum; for(int i=0;i<n;i++){ int current=0; int count=0; int start; for(int j=0;j<6;j++){ if(current != a[j][i]){ if(count>=3){ sum+=count*current; for(int k=1;k<=count;k++){ a[j-k][i]=0; } // cout << "count:" << count << endl; // cout << "current:" << current << endl; } current = a[j][i]; count=1; } else { count++; } } } /* for(int i=0;i<n;i++){ for(int j=0;j<5;j++){ cout << a[j][i] << " "; } cout << endl; }*/ for(int j=0;j<5;j++){ for(int i=n-1;i>0;i--){ if(a[j][i]==0){ for(int k=i-1;k>=0;k--){ if(a[j][k]!=0){ // cout << j << " swap " << i << " " << k << endl; swap(a[j][k],a[j][i]); break; } } } } } }while(csum!=sum); cout << sum << endl; } return 0; } ```