text
stringlengths
291
465k
### 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; import java.util.ArrayList; public class Main { Scanner sc = new Scanner(System.in); public void run(){ while(true){ int h = sc.nextInt(); if(h == 0) break; calc(h); } } public void calc(int h){ int[][] board = new int[h][5]; for(int i = h-1; i >= 0; i--){ for(int j = 0; j < 5; j++){ board[i][j] = sc.nextInt(); } } int ans = 0; while(true){ int sum = 0; for(int i = 0; i < h; i++){ int count = 0; int start = -1; int before = 0; for(int j = 0; j < 5; j++){ if(board[i][j] == 0){ count = 0; start = -1; before = 0; } else if(board[i][j] == before){ count += 1; if(j == 4 && count >= 3){ sum += before*count; for(int k = start; k < start+count; k++){ board[i][k] = 0; } } } else { if(count >= 3){ sum += before*count; for(int k = start; k < start+count; k++){ board[i][k] = 0; } } count = 1; start = j; before = board[i][j]; } } } if(sum == 0) break; else ans += sum; for(int j = 0; j < 5; j++){ int counter = 0; for(int i = 0; i < h; i++){ if(board[i][j] == 0){ counter++; } else if(counter > 0){ board[i-counter][j] = board[i][j]; board[i][j] = 0; } } } } System.out.println(ans); } public static void main(String[] args){ new Main().run(); } } ```
### 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 # http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1193&lang=jp import sys read = sys.stdin.read readline = sys.stdin.readline readlines = sys.stdin.readlines def main(): ans = [] while True: H = int(readline()) if H == 0: break c = [[int(i) for i in readline().split()] for _ in range(H)] seq = [[1] for _ in range(H)] ans_tmp = [0] score = 0 while True: # 消す for i, line in enumerate(c): for j in range(1,5): if line[j] == line[j-1]: seq[i][-1] += 1 else: seq[i].append(1) acc = -1 for s in seq[i]: acc += s if s >= 3: score += s * line[acc] for j in range(acc - s + 1, acc + 1): line[j] = 0 seq[i] = [1] for i in reversed(range(H)): for j in range(5): if c[i][j] == 0: for h in reversed(range(i)): if c[h][j] != 0: c[h][j], c[i][j] = c[i][j], c[h][j] break ans_tmp.append(score) if ans_tmp[-1] == ans_tmp[-2]: ans.append(ans_tmp[-1]) break print("\n".join(map(str, ans))) if __name__ == "__main__": main() ```
### 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 import sys input = sys.stdin.readline def cal(board): H = len(board) gain = 0 tmp = [1]*5 for row in board: for i in range(1, 5): tmp[i] = tmp[i-1] * (row[i-1] == row[i]) + 1 for i in range(4, -1, -1): if tmp[i] >= 3: gain += row[i] * tmp[i] for j in range(i, i-tmp[i], -1): row[j] = 0 break for col in range(5): i = H-1 for row in board[::-1]: if row[col]: board[i][col] = row[col] i -= 1 for i in range(i, -1, -1): board[i][col] = 0 return gain while True: H = int(input()) if H == 0: break board = [] for _ in [0]*H: *line, = map(int, input().split()) board.append(line) score = 0 while True: gain = cal(board) if gain == 0: break score += gain print(score) ```
### 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 main(){ while(1){ int H; cin>> H; if(!H) break; int a[H][5]; for(int i=0; i<H; i++){ for(int j=0; j<5; j++){ cin>> a[i][j]; } } int f=1, s=0; while(f){ f=0; int _s=0; for(int i=0; i<H; i++){ for(int j=0; j+2<5; j++){ if(a[i][j]!=0&&a[i][j]==a[i][j+1]&&a[i][j+1]==a[i][j+2]){ f++; int d=a[i][j]; for(; j<5&&a[i][j]==d; j++){ a[i][j]=0; s+=d; } } } } for(int j=0; j<5; j++){ for(int i=0; i+1<H; i++){ if(a[i+1][j]==0){ for(int k=i+1; k-1>=0; k--){ swap(a[k][j], a[k-1][j]); } } } } } cout<< s<< endl; } 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: exit() board = [[0]*5 for _ in range(H+1)] for i in range(1,H+1): board[i] = list(map(int, input().split())) board = board[::-1] change = True score = 0 while change==True: change = False for i in range(H-1,-1,-1): if len(set(board[i]))==1 and board[i][0]>0: score += sum(board[i]) change = True for j in range(i,H): board[j] = board[j+1] elif len(set(board[i][:4]))==1 and board[i][:4][0]>0: score += sum(board[i][:4]) change = True for j in range(i,H): board[j][:4] = board[j+1][:4] elif len(set(board[i][1:]))==1 and board[i][1:][0]>0: score += sum(board[i][1:]) change = True for j in range(i,H): board[j][1:] = board[j+1][1:] elif len(set(board[i][:3]))==1 and board[i][:3][0]>0: score += sum(board[i][:3]) change = True for j in range(i,H): board[j][:3] = board[j+1][:3] elif len(set(board[i][1:4]))==1 and board[i][1:4][0]>0: score += sum(board[i][1:4]) change = True for j in range(i,H): board[j][1:4] = board[j+1][1:4] elif len(set(board[i][2:]))==1 and board[i][2:][0]>0: score += sum(board[i][2:]) change = True for j in range(i,H): board[j][2:] = board[j+1][2:] print(score) ```
### 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; for(;cin>>h,h;) { int board[h][5]; for(int i=0;i<h;i++) for(int j=0;j<5;j++) cin>>board[i][j]; int ans=0; while(true) { int add=0; for(int i=0;i<h;i++) { for(int j=0;j<3;j++) if(board[i][j]==board[i][j+1]&& board[i][j+1]==board[i][j+2] ) { int jj = j; while(jj < 5 &&board[i][j]==board[i][jj]) jj++; for(int x=j;x<jj;x++) { add+=board[i][x]; board[i][x]=0; } } } ans+=add; if(add==0) break; for(int i=h-1;i>0;i--) { for(int j=0;j<5;j++) { if(board[i][j]==0) { for(int ii=i-1;ii>=0;ii--) if(board[ii][j]!=0) { board[i][j]=board[ii][j]; board[ii][j]=0; break; } } } } /* cerr<<endl; for(int i=0;i<h;i++,cerr<<endl) for(int j=0;j<5;j++) cerr<<board[i][j]<<" "; cerr<<endl; */ } cout<<ans<<endl; } } ```
### 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<bits/stdc++.h> using namespace std; int main(){ int H; while(cin>>H,H){ int W = 5; int fi[20][20]; for(int i = 0;i < H;i++){ for(int j = 0;j < W;j++){ cin>>fi[i][j]; } } int sc = 0; int D = 10; int p = 0; while(D--){ bool er[20][20] = {}; for(int i = 0;i < H;i++){ for(int j = 1;j < W - 1;j++){ if(fi[i][j-1] == fi[i][j] && fi[i][j] == fi[i][j+1]){ er[i][j] = true; er[i][j-1] = true; er[i][j+1] = true; } } for(int j = 0;j < W;j++){ if(er[i][j]){ p += fi[i][j]; fi[i][j] = 0; } } } int h = H+5; while(h--){ for(int i = H - 1;i > 0;i--){ for(int j = 0;j < W;j++){ if(fi[i][j] == 0){ swap(fi[i][j],fi[i-1][j]); } } } }/* cout<<"***"<<endl; for(int i = 0;i < H;i++){ for(int j = 0;j < W;j++){ cout<<fi[i][j]; } cout<<endl; }*/ } cout<<p<<endl; } } ```
### 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<bits/stdc++.h> #define rep(i,n)for(int i=0;i<n;i++) using namespace std; int main() { int h; while (cin >> h, h) { vector<int>V[5]; rep(i, h) { rep(j, 5) { int d; cin >> d; V[j].push_back(d); } } rep(i, 5)reverse(V[i].begin(), V[i].end()); int ans = 0;bool update; do { update = 0; 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 = 1; 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); cout<<ans<<endl; } } ```
### Prompt Please create a solution in JAVA 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 //http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1148 import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); // int TC = Integer.parseInt(sc.next(), 10); while (true) { int R = sc.nextInt(); if (R == 0) break; int[][] puzzle = new int[R][5]; for (int r = 0; r < R; r++) { for (int c = 0; c < 5; c++) { puzzle[r][c] = sc.nextInt(); } } int totalScore = 0; while (true) { boolean foundMatch = false; int tempScore = getTempScore(puzzle, R); if (tempScore > 0) { foundMatch = true; totalScore += tempScore; settle(puzzle, R); } if (!foundMatch) break; } System.out.println(totalScore); } } public static int getTempScore(int[][] puzzle, int R) { int tempScore = 0; for (int r = 0; r < R; r++) { int a0 = puzzle[r][0]; int a1 = puzzle[r][1]; int a2 = puzzle[r][2]; int a3 = puzzle[r][3]; int a4 = puzzle[r][4]; if (a0 > 0 && a0 == a1 && a0 == a2 && a0 == a3 && a0 == a4) { tempScore += a0 * 5; puzzle[r][0] = 0; puzzle[r][1] = 0; puzzle[r][2] = 0; puzzle[r][3] = 0; puzzle[r][4] = 0; } else if (a0 > 0 && a0 == a1 && a0 == a2 && a0 == a3 && a0 != a4) { tempScore += a0 * 4; puzzle[r][0] = 0; puzzle[r][1] = 0; puzzle[r][2] = 0; puzzle[r][3] = 0; } else if (a1 > 0 && a0 != a1 && a1 == a2 && a1 == a3 && a1 == a4) { tempScore += a1 * 4; puzzle[r][1] = 0; puzzle[r][2] = 0; puzzle[r][3] = 0; puzzle[r][4] = 0; } else if (a0 > 0 && a0 == a1 && a0 == a2 && a0 != a3) { tempScore += a0 * 3; puzzle[r][0] = 0; puzzle[r][1] = 0; puzzle[r][2] = 0; } else if (a1 > 0 && a0 != a1 && a1 == a2 && a1 == a3 && a1 != a4) { tempScore += a1 * 3; puzzle[r][1] = 0; puzzle[r][2] = 0; puzzle[r][3] = 0; } else if (a4 > 0 && a4 != a1 && a4 == a2 && a4 == a3) { tempScore += a4 * 3; puzzle[r][2] = 0; puzzle[r][3] = 0; puzzle[r][4] = 0; } } return tempScore; } public static void settle(int[][] puzzle, int R) { for (int c = 0; c < 5; c++) { while (true) { boolean again = false; for (int r = 0; r < R-1; r++) { if (puzzle[r][c] > 0 && puzzle[r+1][c] == 0) { int tmp = puzzle[r][c]; puzzle[r][c] = 0; puzzle[r+1][c] = tmp; again = true; } } if (!again) break; } } } } ```
### 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 main(n): f = [list(map(int, input().split())) for i in range(n)] d = True ans = 0 while d: d = False for i in range(n): l = [] for k in range(3): if f[i][k] == 0: continue if f[i][k] == f[i][k + 1] == f[i][k + 2]: l.append(k) l.append(k + 1) l.append(k + 2) if l == []: continue l = list(set(l)) b = f[i][l[0]] d = True for k in l: ans += b f[i][k] = 0 if d: for i in range(n-1,-1,-1): for k in range(5): x = i while f[x][k]: if x < n - 1: if f[x + 1][k] == 0: f[x+1][k] = f[x][k] f[x][k] = 0 x += 1 continue break print(ans) while 1: n = int(input()) if n == 0: break main(n) ```
### 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 <string> #include <vector> #include <algorithm> #include <utility> #include <cstring> using namespace std; #define rep(i,n) for(int i=0; i<n; i++) int h, point; int f[11][6]; bool check() { bool res = false; for(int y=h-1; y>=0; y--) { for(int x=2; x<5; x++) { if(f[y][x]!=0 && f[y][x-2]==f[y][x] && f[y][x-1]==f[y][x]) { res = true; int dx=1; while(x+dx<5 && f[y][x]==f[y][x+dx]) dx++; point += f[y][x] * (3+dx-1); for(int ex=x-2; ex<x+dx; ex++) { f[y][ex] = 0; } } } } return res; } void fall() { int py[5] = {}; rep(y, h) { rep(x, 5) { f[py[x]][x] = f[y][x]; if(f[y][x]>0) { py[x]++; } } } rep(x, 5) { for(int y=py[x]; y<h; y++) { f[y][x] = 0; } } } int main() { while(cin >> h, h) { point = 0; memset( f, 0, sizeof(f) ); rep(y, h) { rep(x, 5) { int k; cin >> k; f[h-y-1][x] = k; } } while(check()) { fall(); } cout << point << endl; } return 0; } ```
### Prompt Create a solution in Java 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 ```java import java.util.*; import java.io.*; import java.math.BigDecimal; import java.awt.geom.*; import static java.util.Arrays.*; import static java.lang.Math.*; public class Main{ static final Reader sc = new Reader(); static final PrintWriter out = new PrintWriter(System.out,false); public static void main(String[] args) throws Exception { while(true){ int h = sc.nextInt(); if(h==0){ break; } ArrayList<Integer> list1 = new ArrayList<Integer>(); ArrayList<Integer> list2 = new ArrayList<Integer>(); ArrayList<Integer> list3 = new ArrayList<Integer>(); ArrayList<Integer> list4 = new ArrayList<Integer>(); ArrayList<Integer> list5 = new ArrayList<Integer>(); for(int i=0;i<=h;i++){ list1.add(-1); list2.add(-2); list3.add(-3); list4.add(-4); list5.add(-5); } for(int i=0;i<h;i++){ list1.set(h-i-1,sc.nextInt()); list2.set(h-i-1,sc.nextInt()); list3.set(h-i-1,sc.nextInt()); list4.set(h-i-1,sc.nextInt()); list5.set(h-i-1,sc.nextInt()); } int ans = 0; while(true){ boolean judge = false; for(int i=h-1;i>=0;i--){ int a; int b; int c; int d; int e; if(i<list1.size()){ a = list1.get(i); } else{ a = -1; } if(i<list2.size()){ b = list2.get(i); } else{ b = -2; } if(i<list3.size()){ c = list3.get(i); } else{ c = -3; } if(i<list4.size()){ d = list4.get(i); } else{ d = -4; } if(i<list5.size()){ e = list5.get(i); } else{ e = -5; } if(a==b && b==c){ if(c==d && d==e){ ans += 2*d; list4.remove(i); list5.remove(i); } else if(c==d){ ans += d; list4.remove(i); } ans += 3*a; list1.remove(i); list2.remove(i); list3.remove(i); judge = true; } else if(b==c && c==d){ if(d==e){ ans += e; list5.remove(i); } ans += 3*b; list2.remove(i); list3.remove(i); list4.remove(i); judge = true; } else if(c==d && d==e){ ans += 3*c; list3.remove(i); list4.remove(i); list5.remove(i); judge = true; } } if(!judge){ break; } } out.println(ans); out.flush(); } sc.close(); out.close(); } static void trace(Object... o) { System.out.println(Arrays.deepToString(o));} } class Reader { private final InputStream sc; private final byte[] buf = new byte[1024]; private int ptr = 0; private int buflen = 0; public Reader() { this(System.in);} public Reader(InputStream source) { this.sc = source;} private boolean hasNextByte() { if (ptr < buflen) return true; ptr = 0; try{ buflen = sc .read(buf); }catch (IOException e) {e.printStackTrace();} if (buflen <= 0) return false; return true; } private int readByte() { if (hasNextByte()) return buf[ptr++]; else return -1;} private boolean isPrintableChar(int c) { return 33 <= c && c <= 126;} private void skip() { while(hasNextByte() && !isPrintableChar(buf[ptr])) ptr++;} public boolean hasNext() {skip(); return hasNextByte();} public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); boolean minus = false; long num = readByte(); if(num == '-'){ num = 0; minus = true; }else if (num < '0' || '9' < num){ throw new NumberFormatException(); }else{ num -= '0'; } while(true){ int b = readByte(); if('0' <= b && b <= '9') num = num * 10 + (b - '0'); else if(b == -1 || !isPrintableChar(b)) return minus ? -num : num; else throw new NoSuchElementException(); } } public int nextInt() { long num = nextLong(); if (num < Integer.MIN_VALUE || Integer.MAX_VALUE < num) throw new NumberFormatException(); return (int)num; } public double nextDouble() { return Double.parseDouble(next()); } public char nextChar() { if (!hasNext()) throw new NoSuchElementException(); return (char)readByte(); } public String nextLine() { while (hasNextByte() && (buf[ptr] == '\n' || buf[ptr] == '\r')) ptr++; if (!hasNextByte()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (b != '\n' && b != '\r') { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public int[] nextIntArray(int n) { int[] res = new int[n]; for (int i=0; i<n; i++) res[i] = nextInt(); return res; } public char[] nextCharArray(int n) { char[] res = new char[n]; for (int i=0; i<n; i++) res[i] = nextChar(); return res; } public void close() {try{ sc.close();}catch(IOException e){ e.printStackTrace();}}; } ```
### 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 <stack> using namespace std; void drop(vector<vector<int> > &field, int h){ for(int i = 0; i < h; ++i){ for(int j = h-1; j > 0; --j){ for(int k = 0; k < 5; ++k){ if(field[j][k] == 0){ swap(field[j-1][k], field[j][k]); } } } } } int erase(vector<vector<int> > &field, int h){ int ret = 0; for(int i = 0; i < h; ++i){ for(int j = 0; j < 3; ++j){ // search int len = 1; for(int k = j+1; k < 5; ++k){ if(field[i][j] != field[i][k]){ break; } ++len; } if(len >= 3){ ret += field[i][j] * len; for(int k = j; k < j + len; ++k){ field[i][k] = 0; } } } } return ret; } int main(){ int h; while(true){ cin >> h; if(h == 0){ break; } vector<vector<int> > field(h, vector<int>(5)); for(int i = 0; i < h; ++i){ for(int j = 0; j < 5; ++j){ cin >> field[i][j]; } } int ans = 0; while(true){ int point = erase(field, h); if(point == 0){ break; } ans += point; drop(field, h); } cout << ans << endl; } } ```
### 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 <cstdio> #include <vector> #include <queue> #include <cmath> #include <algorithm> #include <functional> using namespace std; #define rep(i,n) for(int i=0;i<(n);++i) #define rep1(i,n) for(int i=1;i<=(n);++i) #define all(c) (c).begin(),(c).end() #define pb push_back #define fs first #define sc second #define show(x) cout << #x << " = " << x << endl int main(){ while(true){ int H; cin>>H; if(H==0) break; int a[10][5]; rep(i,H) rep(j,5) cin>>a[H-1-i][j]; int ans=0; while(true){ bool update=false; rep(i,H){ if(a[i][2]==-1) continue; int l=2,r=2; while(l>0){ if(a[i][l-1]==a[i][2]) l--; else break; } while(r<4){ if(a[i][r+1]==a[i][2]) r++; else break; } if(r-l+1>=3){ update=true; ans+=a[i][2]*(r-l+1); for(int j=l;j<=r;j++) a[i][j]=-1; } } if(!update) break; rep(i,5){ vector<int> vc; rep(j,H){ if(a[j][i]!=-1) vc.pb(a[j][i]); } rep(j,vc.size()) a[j][i]=vc[j]; for(int j=vc.size();j<H;j++) a[j][i]=-1; } } cout<<ans<<endl; } } ```
### 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 while 1: n=int(input()) if n==0:break A=[list(map(int,input().split())) for _ in range(n)] ans=0 while 1: score=ans for i in range(n): cnt,num=0,A[i][0] for j in range(5): if A[i][j]==num:cnt +=1 else: if cnt>=3: ans +=num*cnt for k in range(cnt):A[i][j-k-1]=0 num=A[i][j] cnt=1 if j==4 and cnt>=3: ans +=num*cnt for k in range(cnt):A[i][j-k]=0 B=[[-1 for _ in range(5)] for _ in range(n)] for i in range(5): cnt=n-1 for j in range(n)[::-1]: if A[j][i]!=0: B[j][i]=cnt cnt -=1 C=[[0 for _ in range(5)] for _ in range(n)] for i in range(5): for j in range(n): if B[j][i]!=-1: C[B[j][i]][i]=A[j][i] A=C if score==ans:break print(ans) ```
### 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 h; int field[15][5]; int changed[15][5]; int main(){ while(1){ cin >> h; if(h==0)break; int ans=0; for(int i=0;i<h;i++){ for(int j=0;j<5;j++){ cin >> field[i][j]; } } while(1){ bool ok=true; for(int i=0;i<h;i++){ for(int j=0;j<3;j++){//起点として見るのは3番目までで良い if(field[i][j]==field[i][j+1]&&field[i][j]>0){ int cnt=0; int copy=field[i][j]; while(copy==field[i][j]&&j<5){ cnt++; j++; } if(cnt>2){ j-=cnt; for(int k=j;k<5;k++){ if(copy==field[i][k]){ changed[i][k]=1; } else break; } j+=cnt; } j--; } } } for(int i=0;i<h;i++){ for(int j=0;j<5;j++){ if(changed[i][j]==1){ ok=false; ans+=field[i][j]; for(int k=i;k>0;k--){ field[k][j]=(field[k-1][j]>0?field[k-1][j]:0); } field[0][j]=0; } } } for(int i=0;i<h;i++){ for(int j=0;j<5;j++){ changed[i][j]=0; } } if(ok)break; } cout << ans << endl; } return 0; } ```
### 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.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) throws IOException { List<Integer> list1 = new ArrayList<Integer>(); List<Integer> list2 = new ArrayList<Integer>(); List<Integer> list3 = new ArrayList<Integer>(); List<Integer> list4 = new ArrayList<Integer>(); List<Integer> list5 = new ArrayList<Integer>(); BufferedReader reader = new BufferedReader((new InputStreamReader(System.in))); while(true){ int n = Integer.parseInt(reader.readLine()); if(n==0){ break; } list1 = new ArrayList<Integer>(); list2 = new ArrayList<Integer>(); list3 = new ArrayList<Integer>(); list4 = new ArrayList<Integer>(); list5 = new ArrayList<Integer>(); for(int i=0; i<n; i++){ String[] line = reader.readLine().split(" "); list1.add(0, Integer.parseInt(line[0])); list2.add(0, Integer.parseInt(line[1])); list3.add(0, Integer.parseInt(line[2])); list4.add(0, Integer.parseInt(line[3])); list5.add(0, Integer.parseInt(line[4])); } int score = 0; boolean change = true; while(change) { change = false; for (int i = 0; i < n; i++) { if (list1.get(i) == list2.get(i) && list2.get(i) == list3.get(i) && list2.get(i) != null) { int k = list1.get(i); list1.set(i, null); list2.set(i, null); list3.set(i, null); score += 3 * k; if (list4.get(i) == k) { list4.set(i, null); score += k; if (list5.get(i) == k) { list5.set(i, null); score += k; } } change = true; } else if (list2.get(i) == list3.get(i) && list3.get(i) == list4.get(i) && list3.get(i) != null) { int k = list2.get(i); list2.set(i, null); list3.set(i, null); list4.set(i, null); score += 3 * k; if (list5.get(i) == k) { list5.set(i, null); score += k; } change = true; } else if (list3.get(i) == list4.get(i) && list5.get(i) == list4.get(i) && list3.get(i) != null) { int k = list3.get(i); list3.set(i, null); list4.set(i, null); list5.set(i, null); score += 3 * k; change = true; } } if(change){ list1 = slide(list1); list2 = slide(list2); list3 = slide(list3); list4 = slide(list4); list5 = slide(list5); } else { System.out.println(score); break; } } } } public static List<Integer> slide(List<Integer> list) { for(int i=0; i<list.size(); i++){ if(list.get(list.size()-i-1) == null){ for(int j=list.size()-i-1; j<list.size()-1; j++){ list.set(j, list.get(j+1)); } list.set(list.size() - 1, null); } } return list; } } ```
### 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.*; public class Main { private static final Scanner scn = new Scanner(System.in); private static int WIDTH = 5; private static int VANISH = 3; public static void main(String[] args) { int height; while((height = scn.nextInt()) > 0) { Puzzle puzzle = new Main().new Puzzle(height); puzzle.proc(); System.out.println(puzzle.showScore()); } } class Puzzle { int score; int[][] numbers; Puzzle(int height) { score = 0; numbers = new int[height][WIDTH]; for(int i = 0; i < height; i++) { for(int j = 0; j < WIDTH; j++) { numbers[i][j] = scn.nextInt(); } } } int showScore() { return score; } void proc() { int lines; while((lines = vanishPhase()) > 0) { dropPhase(lines); } } private int vanishPhase() { int lines = 0; for(int i = 0; i < numbers.length; i++) { for(int j = 0; j <= WIDTH - VANISH; j++) { int base = numbers[i][j]; if(base > 0) { int count = 1; while(j + count < WIDTH && base == numbers[i][j + count]) { count++; } if(count >= VANISH) { score += base * count; while(count-- > 0) { numbers[i][j + count] = 0; } lines++; break; } } } } return lines; } private void dropPhase(int lines) { while(lines-- > 0) { for(int i = ~-numbers.length; i > 0; i--) { for(int j = 0; j < WIDTH; j++) { if(numbers[i][j] == 0) { numbers[i][j] ^= numbers[~-i][j]; numbers[~-i][j] ^= numbers[i][j]; numbers[i][j] ^= numbers[~-i][j]; } } } } } } } ```
### 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() { int h; while (cin >> h && h != 0) { int map[10 + 1][5]; for (int j = 0; j < 5; j++) { map[0][j] = -1; } for (int i = 1; i <= h; i++) { for (int j = 0; j < 5; j++) { cin >> map[i][j]; } } int ans = 0; while (true) { vector<int> heightIndex; for (int i = 1; i <= h; i++) { if (map[i][2] == -1)continue; int left = 2; int right = 2; int value = map[i][2]; while (left - 1>= 0 && map[i][left - 1] == value)left--; while (right + 1 < 5 && map[i][right + 1] == value)right++; int count = (right - left) + 1; if (count >= 3) { for (int k = left; k < right + 1; k++) { map[i][k] = -1; } ans += value * ((right - left) + 1); heightIndex.push_back(i); } } if (heightIndex.size() == 0)break; for (int i = h; i >= 1; i--) { for (int j = 0; j < 5; j++) { int k = 0; while (k + i <= h && map[i + k][j] == -1) { map[i + k][j] = map[i + k - 1][j]; map[i + k - 1][j] = -1; k++; } } } } cout << ans <<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 <iostream> using namespace std; int main() { while(true){ 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(true){ 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){ // 0~2が同じ 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){ // 1~3が同じ 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){ // 2~4が同じ 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 "bits/stdc++.h" using namespace std; typedef vector<int> vi; typedef pair<int,int> pii; typedef long long ll; #define rep(i,n) for(ll i=0;i<(ll)(n);i++) #define all(a) (a).begin(),(a).end() #define pb push_back #ifdef Debug #define dump(x) cerr << #x << " = " << (x) << endl #else #define dump(x) #endif int data[30][30]; int h; int dell(int data[30][30]){ int ret=0; rep(i,h){ rep(j,3){ int num=1; rep(k,5){ if(j+k>=5)break; if( data[i][j+k]!=-1 && data[i][j+k]==data[i][j+k+1])num++; else break; } if(num>=3){ rep( k,num ){ data[i][k+j]=-1; } ret++; } } } return ret; } void fall(int data[30][30]){ rep(k,h){ for(int i=h-1;i>=1;i--){ rep(j,5){ if(data[i][j]==-1)swap(data[i][j],data[i-1][j]); } } } } int main(){ while(cin>>h){ if(h==0)break; int bef=0; rep(i,h) rep(j,5){ cin>>data[i][j]; bef+=data[i][j]; } while(1){ if( dell(data)==0 )break; fall(data); } int af=0; rep(i,h)rep(j,5)if(data[i][j]!=-1)af+=data[i][j]; cout<<bef-af<<endl; } } ```
### 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 from itertools import zip_longest while True: n = int(input()) if not n: break field = [list(map(int, input().split())) for _ in range(n)] field.reverse() score = 0 while True: no_more_disappear = True for row in field: for i, stone in enumerate(row[:3]): if stone is None: continue cnt = 1 for stone2 in row[i + 1:]: if stone != stone2: break cnt += 1 if cnt >= 3: row[i:i + cnt] = [None] * cnt score += stone * cnt no_more_disappear = False if no_more_disappear: break fieldT = map(lambda row: filter(None, row), zip(*field)) field = list(map(list, zip_longest(*fieldT))) print(score) ```
### 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 class Puzzle(): def __init__(self, mat, N): self.mat = mat self.h = N self.score = 0 def calc(self): tmp_score = self.score for i in range(self.h): for j in range(3): if self.mat[i][j] > 0: # 5,2 for k in range(5, 2, -1): if j+k <= 7 and k-j >= 3: s = set(self.mat[i][j:k]) if len(s) == 1 and -1 not in s: tmp_score += list(s)[0] * (k-j) for l in range(j, k): self.mat[i][l] = -1 break if self.score >= tmp_score: return False else: self.score = tmp_score return True def flatten(self): pivot_mat = [] for i in range(5): row = [] for j in range(self.h): if self.mat[j][i] != -1: row.append(self.mat[j][i]) short = self.h - len(row) if short > 0: row.reverse() for k in range(short): row.append(-1) row.reverse() pivot_mat.append(row) mat = [ [-1]*5 for i in range(self.h) ] for i in range(self.h): for j in range(5): mat[i][j] = pivot_mat[j][i] self.mat = mat while True: h = int(input()) if h==0: exit() mat = [] for _ in range(h): mat.append(list(map(int,input().split()))) puzzle = Puzzle(mat, h) while True: if puzzle.calc(): puzzle.flatten() else: break print(puzzle.score) ```
### 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 rep(i,n) for(int i=0;i<n;i++) #define REP(i,n) for(int i=1;i<=n;i++) typedef long long ll; int main(){ int H; cin >> H; while(H!=0){ int puzzle[12][5]; rep(i,H){ rep(j,5){ cin >> puzzle[i][j]; } } bool allign = true; int ans = 0; while (allign){ //まず0のところを落とす。 for (int i = H-2; i >= 0; --i) { rep(j,5){ int temp = i; if(puzzle[i][j] != 0){ while(puzzle[temp+1][j] == 0){ puzzle[temp+1][j] = puzzle[temp][j]; puzzle[temp][j] = 0; temp++; if(temp == H-1) break; } } } } allign = false; rep(i,H){ int count = 1; int id = 0; rep(j,4){ if(puzzle[i][j] == 0) { if(count < 3){ count = 1; continue; }else{ continue; } } if(puzzle[i][j] == puzzle[i][j+1]){ count++; id = j+1; } if(puzzle[i][j] != puzzle[i][j+1]){ if(count < 3){ count = 1; }else{ break; } } } if(count >= 3){ allign = true; for (int j = id - count + 1; j <= id ; ++j) { ans += puzzle[i][j]; puzzle[i][j] = 0; } } } } cout << ans << endl; cin >> H; } 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 <algorithm> #include <vector> using namespace std; int main() { int H; while (cin >> H, H) { vector<vector<int>> mp(H, vector<int>(5)); for (auto &a : mp) { for (auto &b : a) { cin >> b; } } int score = 0; bool down = true; while (down) { down = false; for (int h = H - 1; h >= 0; h--) { int prev = -1, cnt = 0; for (int w = 0; w < 5; w++) { if (prev == mp[h][w]) { cnt++; } else { if (cnt >= 3) { for (int ww = w - 1; cnt; ww--, cnt--) { score += mp[h][ww]; mp[h][ww] = 0; } } prev = mp[h][w]; cnt = 1; } } if (cnt >= 3) { for (int ww = 4; cnt; ww--, cnt--) { score += mp[h][ww]; mp[h][ww] = 0; } } } for (int w = 0; w < 5; w++) { vector<int> rem; for (int h = H - 1; h >= 0; h--) { if (mp[h][w] != 0) { rem.push_back(mp[h][w]); } } rem.resize(H, 0); reverse(rem.begin(), rem.end()); for (int h = H - 1; h >= 0; h--) { if (mp[h][w] != rem[h]) { down = true; } mp[h][w] = rem[h]; } } } cout << score << endl; } return 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.util.Arrays; import java.util.Scanner; public class Main { Scanner sc; void run() { for (; ; ) { int H = ni(); if ( H == 0 ) { break; } int[][] field = new int[ H ][ 5 ]; for ( int i = 0; i < H; ++i ) { for ( int j = 0; j < 5; ++j ) { field[ i ][ j ] = ni(); } } int ans = 0; for (; ; ) { int sum = 0; for ( int i = 0; i < H; ++i ) { for ( int j = 0; j < 3; ++j ) { int v = field[ i ][ j ]; boolean del = true; for ( int k = 1; k < 3; ++k ) { del &= field[ i ][ j + k ] == v; } if ( del ) { for ( int k = 0; j + k < 5; ++k ) { if ( field[ i ][ j + k ] != v ) { //debug("here", i, j + k, v, field[i][j+k]); break; } sum += v; field[ i ][ j + k ] = 0; for ( int l = i; 0 < l; --l ) { field[ l ][ j + k ] = field[ l - 1 ][ j + k ]; } field[ 0 ][ j + k ] = 0; } break; } } } //for ( int[] v : field ) debug(v); //debug(); if ( sum == 0 ) { break; } ans += sum; // debug(sum); } System.out.println(ans); } } Main() { sc = new Scanner(System.in); } int ni() { return sc.nextInt(); } public static void main(String[] args) { new Main().run(); } void debug(Object... os) { System.err.println(Arrays.deepToString(os)); } } ```
### 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; #define FOR( i, m, n ) for( int (i) = (m); (i) < (n); (i)++ ) #define REP( i, n ) FOR( i, 0, n ) #define ALL( a ) (a).begin(), (a).end() int main() { int n; array<int, 5> tmp; while( 1 ) { cin >> n; if( n == 0 ) break; vector<array<int, 5>> p( n ); REP( i, n ) { REP( j, 5 ) { cin >> tmp[j]; } p[i] = tmp; } int m = 0, score = 0; while( 1 ) { FOR( i, m, n ) { int cnt = 0; REP( j, 5 ) { int tmp = p[i][j]; if( j != 4 && p[i][j + 1] == tmp ) { cnt++; } else { if( cnt >= 2 ) { score += ( cnt + 1 ) * p[i][j]; REP( k, cnt + 1 ) { if( i == 0 ) { p[i][j - k] = 0; } else { REP( l, i ) { p[i - l][j - k] = p[i - l - 1][j - k]; } p[0][j - k] = 0; } } } cnt = 0; } } } m++; if( m == n ) break; } cout << score << 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<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 h,arr[10][5]; bool erase[10][5]; void compute(){ bool update = true; int score = 0; while( update ){ memset(erase,false,sizeof(erase)); update = false; for(int y=h-1;y>=0;y--){ for(int x=0;x<3;x++){ if( arr[y][x] == 0 ) continue; int cnt = 1; REP(x2,x+1,5) { if( arr[y][x] != arr[y][x2] ) break; ++cnt; } if( cnt >= 3 ) { update = true; REP(x2,x,5) { if( arr[y][x] != arr[y][x2] ) break; erase[y][x2] = true; } } } } rep(y,h) rep(x,5) if( erase[y][x] ) { score += arr[y][x]; arr[y][x] = 0; } for(int y=h-1;y>=0;y--){ for(int x=0;x<5;x++){ if( arr[y][x] == 0 ) { int ny = y; while( 1 ){ ny -= 1; if( ny < 0 ) break; if( arr[ny][x] ) { swap(arr[y][x],arr[ny][x]); break; } } } } } } cout << score << endl; } int main(){ while( cin >> h, h ){ rep(i,h) rep(j,5) cin >> arr[i][j]; compute(); } return 0; } ```
### Prompt Construct a Java 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 ```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; int height = H; boolean hasNext = true; while (hasNext) { hasNext = false; // chain for (int i = 0; i < height; i++) { int left = W, right = -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)) { left = Math.min(left, j - 1); right = Math.max(right, j + 1); stone = board[j].get(i); hasNext = true; } } if (right - left >= 2) point += stone * (right - left + 1); for (int j = left; j <= right; j++) board[j].set(i, 0); } // drop down for (int i = 0; i < W; i++) { board[i].removeIf(x -> x == 0); height = Math.max(height, board[i].size()); } for (int i = 0; i < W; i++) { while (board[i].size() < height) board[i].add(0); } } System.out.println(point); } //end while } //end main } ```
### 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 <vector> #include <iostream> using namespace std; int main(){ int h; while(cin >> h,h){ vector<int> mp(6*h); for(int i=0;i<h;i++){ cin >> mp[6*i] >> mp[6*i+1] >> mp[6*i+2] >> mp[6*i+3] >> mp[6*i+4]; mp[6*i + 5] = -1; } bool del = true; int ct =0; while(del){ del = false; int ketsu = 0; int point = 0; for(int i=0;i<6*h-2;i++){ if(mp[i] == mp[i+1] && mp[i+1] == mp[i+2] && mp[i] !=0){ if(ketsu == 0){ ketsu = 3; point = mp[i]; }else{ ketsu++; } }else if(ketsu > 0){ ct += point * ketsu; del = true; for(int ki = 0;ki<ketsu;ki++){ mp[i + 1 - ki] = 0; } ketsu = 0;//init point = 0; } } /* for(int i = 0;i<6*h;i++){ cout << mp[i] << " "; if(i%6 == 0 && i>0)cout << endl; } cout << endl;*/ if(del){//fall for(int i=0;i<5;i++){//line number int fall_ct = 0; for(int j=h-1;j>=0;j--){//direction is up if(mp[j*6 + i] == 0){ fall_ct++; }else if(fall_ct > 0){ mp[(j + fall_ct)*6 + i ] = mp[j * 6 + i]; mp[j*6 + i ] = 0; } } } } /* for(int i = 0;i<6*h;i++){ cout << mp[i] << " "; if(i%6 == 0 && i>0)cout << endl; } cout << endl;*/ } cout << ct << 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<iostream> using namespace std; #define T t[i][j] #define r(i,h) for(int i=0;i<h;i++) int main(){ int h,a; while(cin>>h,h){ a=0; int t[200][5]={{0},{0}}; r(i,h)r(j,5)cin>>T; while(1){ bool f=1; r(i,h){ int c=0,q=-1; r(j,3){ if(T==t[i][j+1]&&T==t[i][j+2]&&T!=0){ c++; if(q==-1)q=j; } } if(c!=0){a+=t[i][q]*(2+c),f=0;r(j,2+c)t[i][q+j]=0;} } if(f)break; for(int i=h-2;i>=0;i--){ r(j,5){ int x=i; while(true){ if(x==h-1||t[x+1][j]!=0)break; t[x+1][j]=t[x][j]; t[x][j]=0; x++; } } } } cout<<a<<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 # http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1193&lang=jp import sys while True: n = int(input()) if n == 0: sys.exit() puzzle = [[] for _ in range(5)] # puzzle input for i in range(n): for j, stone in enumerate(list(map(int, input().split()))): puzzle[j].insert(0, stone) # puzzle[j].append(stone) tmp_score = 10 ans = 0 while tmp_score != 0: tmp_score = 0 for i in range(n - 1, -1, -1): #for i in range(0, n): flg = False for left in range(3): for right in range(4, 1 + left, -1): tmp_list = [] for idx in range(left, right+1): try: tmp_list.append(puzzle[idx][i]) except: tmp_list.append(0) if len(set(tmp_list)) == 1 and not (0 in tmp_list): tmp_score += sum(tmp_list) flg = True # puzzle削除 for idx in range(left, right+1): try: puzzle[idx].pop(i) except: pass break if flg: break ans += tmp_score print(ans) ```
### 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.*; public class Main { static class Graph0n { private ArrayList<Node0n> dt = new ArrayList<>(); Graph0n(int sz){ for(int i=0; i<sz; i++){ Node0n node1 = new Node0n(); dt.add(node1); } } public void add(int vn, int val){ dt.get(vn).add(val); } public int get(int vn, int index){ return dt.get(vn).get(index); } public ArrayList<Integer> get(int vn){ return dt.get(vn).getAll(); } public int sizeOf(int vn){ return dt.get(vn).size(); } public void clear(){ for(int i=0; i<dt.size(); i++){ dt.get(i).clear(); } } } static class Node0n { //重みなし無向グラフの頂点 private ArrayList<Integer> next_vs = new ArrayList<>(); public void add(int val){ next_vs.add(val); } public int get(int ad){ return next_vs.get(ad); } public ArrayList<Integer> getAll(){ return next_vs; } public int size(){ return next_vs.size(); } public void clear(){ next_vs.clear(); } } static class Edge { int from=-1, v2=-1; long weight; public Edge(int vn, long w){ this.v2 = vn; this.weight = w; } public Edge(int cm, int vn, long w){ this.from = cm; this.v2 = vn; this.weight = w; } } static class Edge2 { int v2; long cost1,cost2; public Edge2(int vn, long w1, long w2){ this.v2 = vn; this.cost1 = w1; this.cost2 = w2; } } static class Comparator_Edge implements Comparator<Edge>{ public int compare(Edge a, Edge b){ if(a.weight>b.weight) return 1; else if(a.weight<b.weight) return -1; else return 0; } } public static void main(String[] args) throws Exception { FastScanner sc = new FastScanner(); PrintWriter out = new PrintWriter(System.out); while(true){ int h = sc.nexI(); if(h==0) break; int[][] dt = new int[h][5]; for(int i=0; i<h; i++){ for(int j=0; j<5; j++){ dt[i][j] = sc.nexI(); } } int score = 0; boolean disapper_happened = true; while(disapper_happened){ disapper_happened = false; for(int i=0; i<h; i++){ if(same3(dt[i][0],dt[i][1],dt[i][2]) && (dt[i][0]>0)){ if(dt[i][3] == dt[i][0]){ if(dt[i][4] == dt[i][0]){ score += dt[i][0]; dt[i][4]=0; //0-4 } //0-3 score += dt[i][0]; dt[i][3]=0; } score += (dt[i][0]*3); dt[i][0]=0; dt[i][1]=0; dt[i][2]=0; disapper_happened=true; }else if(same3(dt[i][1],dt[i][2],dt[i][3]) && (dt[i][1]>0)){ if(dt[i][4] == dt[i][1]){ score += dt[i][1]; dt[i][4]=0; } score += (dt[i][1]*3); dt[i][1]=0; dt[i][2]=0; dt[i][3]=0; disapper_happened=true; }else if(same3(dt[i][2],dt[i][3],dt[i][4]) && (dt[i][2]>0)){ score += (dt[i][2]*3); dt[i][2]=0; dt[i][3]=0; dt[i][4]=0; disapper_happened=true; } } for(int i=0; i<5; i++){ List<Integer> falls = new ArrayList<>(); for(int j=h-1; j>=0; j--){ if(dt[j][i] > 0){ falls.add(dt[j][i]); dt[j][i]=0; } } for(int j=h-1; j>=(h-falls.size()); j--){ dt[j][i] = falls.get(h-j-1); } } } out.println(score); } out.flush(); } private static int INF = (int)1e8; private static long INFL = (long)1e17; private static long e97 = (long)1e9 + 7; private static int abs(int a){ return (a>=0) ? a: -a; } private static long abs(long a){ return (a>=0) ? a: -a; } private static double abs(double a){ return (a>=0) ? a: -a; } private static int min(int a, int b){ return (a>b) ? b : a; } private static long min(long a, long b){ return (a>b) ? b : a; } private static double min(double a, double b){ return (a>b) ? b : a; } private static int max(int a, int b){ return (a>b) ? a : b; } private static long max(long a, long b){ return (a>b) ? a : b; } private static double max(double a, double b){ return (a>b) ? a : b; } private static int minN(int... ins){ int min = ins[0]; for(int i=1; i<ins.length; i++){ if(ins[i] < min) min = ins[i]; } return min; } private static int maxN(int... ins){ int max = ins[0]; for(int i=1; i<ins.length; i++){ if(ins[i] > max) max = ins[i]; } return max; } private static long minN(long... ins){ long min = ins[0]; for(int i=1; i<ins.length; i++){ if(ins[i] < min) min = ins[i]; } return min; } private static long maxN(long... ins){ long max = ins[0]; for(int i=1; i<ins.length; i++){ if(ins[i] > max) max = ins[i]; } return max; } private static int minExAd(int[] dt, int ad){ int min=Integer.MAX_VALUE; for(int i=0; i<dt.length; i++){ if((i != ad) && (dt[i] < min)) min = dt[i]; } return min; } private static long minExAd(long[] dt, int ad){ long min=Long.MAX_VALUE; for(int i=0; i<dt.length; i++){ if((i != ad) && (dt[i] < min)) min = dt[i]; } return min; } private static int minExVal(int[] dt, int ex_val){ int min=Integer.MAX_VALUE; for(int i=0; i<dt.length; i++){ if((dt[i] != ex_val) && (dt[i] < min)) min = dt[i]; } return min; } private static long minExVal(long[] dt, long ex_val){ long min=Long.MAX_VALUE; for(int i=0; i<dt.length; i++){ if((dt[i] != ex_val) && (dt[i] < min)) min = dt[i]; } return min; } private static boolean same3(long a, long b, long c){ if(a!=b) return false; if(b!=c) return false; if(c!=a) return false; return true; } private static boolean dif3(long a, long b, long c){ if(a==b) return false; if(b==c) return false; if(c==a) return false; return true; } private static double hypod(double a, double b){ return Math.sqrt(a*a+b*b); } private static int factorial(int n) { int ans=1; for(int i=n; i>0; i--){ ans*=i; } return ans; } private static long gcd(long m, long n) { if(m < n) return gcd(n, m); if(n == 0) return m; return gcd(n, m % n); } private static boolean is_prime(long a){ if(a==1) return false; for(int i=2; i<=Math.sqrt(a); i++){ if(a%i == 0) return false; } return true; } private static long modinv(long a, long m) { //a|m, >1に注意 long b = m, u = 1, v = 0; while (b>0) { long t = a / b; long pe = a%b; a=b; b=pe; pe= u-t*v; u=v; v=pe; } u %= m; if (u < 0) u += m; return u; } private static long[] fac10E97 = null; private static long[] finv10E97 = null; private static void Cinit(int max_sz, long p){ fac10E97 = new long[max_sz+1]; finv10E97 = new long[max_sz+1]; fac10E97[0]=1; finv10E97[0]=1; for(int i=1; i<=max_sz; i++){ fac10E97[i] = (fac10E97[i-1]*i) % p; } finv10E97[max_sz] = modinv(fac10E97[max_sz], p); for(int i=max_sz; i>1; i--){ finv10E97[i-1] = (finv10E97[i]*i) % p; } } private static long C10e97(int n, int k, long p){ long ans = fac10E97[n]; ans *= finv10E97[k]; ans %= p; ans *= finv10E97[n-k]; ans %=p; if(ans<0) return ans+p; else return ans; } private static int pow2(int in){ return in*in; } private static long pow2(long in){ return in*in; } private static double pow2(double in){ return in*in; } private static int getDigit2(long num){ long cf = 1; int d=0; while(num >= cf){ d++; cf = 1<<d; } return d; //numはd桁の数で、2^dより小さい } private static int getDigit10(long num){ long cf = 1; int d=0; while(num >= cf){ d++; cf*=10; } return d; //numはd桁の数で、10^dより小さい } private static boolean isINF(int in){ if(((long)in*20)>INF) return true; else return false; } private static boolean isINFL(long in){ if((in*10000)>INFL) return true; else return false; } private static long pow10E97(long ob, long soeji, long p){ if(ob==0) return 0; if(soeji==0) return 1; if(soeji==2) return (ob*ob)%p; int d = getDigit2(soeji); long[] ob_pow_2pow = new long[d]; ob_pow_2pow[0] = ob; for(int i=1; i<d; i++){ ob_pow_2pow[i] = (ob_pow_2pow[i-1]*ob_pow_2pow[i-1])%p; } long ans=1; for(int i=d-1; i>=0; i--){ if(soeji >= (long)(1<<i)){ soeji -= (long)(1<<i); ans = (ans*ob_pow_2pow[i])%p; } } return ans; } private static int flag(int pos){ return (1<<pos); } private static boolean isFlaged(int bit, int pos){ if((bit&(1<<pos)) > 0) return true; else return false; } private static int deflag(int bit, int pos){ return bit&~(1<<pos); } private static void showflag(int bit){ for(int i=0; i<getDigit2(bit); i++){ if(isFlaged(bit,i)) System.out.print("O"); else System.out.print("."); } } public static int biSearch(int[] dt, int target){ int left=0, right=dt.length-1; int mid=-1; while(left<=right){ mid = (right+left)/2; if(dt[mid] == target) return mid; if(dt[mid] < target) left=mid+1; else right=mid-1; } return -1; } public static int biSearchMax(long[] dt, long target){ int left=-1, right=dt.length; int mid=-1; while((right-left)>1){ mid = left + (right-left)/2; if(dt[mid] < target) left=mid; else right=mid; } return left;//target未満の最大のaddress } public static int biSearchMaxAL(ArrayList<Integer> dt, long target){ int left=-1, right=dt.size(); int mid=-1; while((right-left)>1){ mid = left + (right-left)/2; if(dt.get(mid) < target) left=mid; else right=mid; } return left;//target未満の最大のaddress } private static int get_root_uf(int[] parent, int index){ if(parent[index] == index) return index; int root = get_root_uf(parent, parent[index]); parent[index] = root; return root; } private static boolean is_same_uf(int[] parent, int x, int y){ if(get_root_uf(parent,x) == get_root_uf(parent,y)) return true; else return false; } private static void unite_uf(int[] parent, int receiver, int attacker){ parent[get_root_uf(parent,attacker)] = get_root_uf(parent, receiver); } private static int areaSum_half_open(int[][] acum_plus1, int bY_notInclude, int aY_include, int bX_notInclude, int aX_include){ int ans = acum_plus1[aY_include+1][aX_include+1]; ans -= acum_plus1[aY_include+1][bX_notInclude+1]; ans -= acum_plus1[bY_notInclude+1][aX_include+1]; ans += acum_plus1[bY_notInclude+1][bX_notInclude+1]; return ans; } private static long areaSum_close(long[][] acum_plus1, int bY_Include, int aY_include, int bX_Include, int aX_include){ long ans = acum_plus1[aY_include+1][aX_include+1]; ans -= acum_plus1[aY_include+1][bX_Include]; ans -= acum_plus1[bY_Include][aX_include+1]; ans += acum_plus1[bY_Include][bX_Include]; return ans; } private static void nextPerm(ArrayList<Integer> former){ int pivot=-1; int ln = former.size(); for(int i = ln; i>1; i--){ if(former.get(i-2) < former.get(i-1)){ pivot = i-2; break; } } if(pivot==-1){ former = null; return; } int pivot_fig = former.get(pivot); int min_pos=pivot+1; int min=former.get(min_pos); for(int i=ln-1; i > pivot+1; i--){ if((former.get(i) < min)&&(former.get(i) > pivot_fig)){ min_pos=i; min=former.get(min_pos); } } Collections.swap(former, pivot, min_pos); Collections.sort(former.subList(pivot+1, ln)); } private static ArrayList<boolean[]> combi_enum(int ln, boolean[] former, int now_pos, int rest){ ArrayList<boolean[]> ans = new ArrayList<>(); boolean[] ans1 = new boolean[former.length+1]; for(int i=0; i<former.length; i++){ ans1[i] = former[i]; } if(ln==(now_pos+1)){ switch(rest){ case 0: ans1[former.length] = false; ans.add(ans1); break; case 1: ans1[former.length] = true; ans.add(ans1); break; } }else{ if(rest == 0){ ans1[former.length] = false; ans.addAll(combi_enum(ln, ans1, now_pos+1, rest)); }else{ ans1[former.length] = false; ans.addAll(combi_enum(ln, ans1.clone(), now_pos+1, rest)); ans1[former.length] = true; ans.addAll(combi_enum(ln, ans1.clone(), now_pos+1, rest-1)); } } return ans; } private static int dfs2(boolean[][] state, int x, int y, int depth){ //薄氷渡り int[] tmpltX = {1,-1,0,0}; int[] tmpltY = {0,0,1,-1}; int newX = -1, newY = -1; state[x][y] = false; depth++; int maxDunder_this_brunch = depth, tmpD=0; for(int i=0; i < 4; i++){ newX = x+tmpltX[i]; newY = y+tmpltY[i]; if((newX >= 0) && (newY >= 0) && (newX < state.length) && (newY < state[0].length) && state[newX][newY]) tmpD = dfs2(state, newX, newY, depth); if(tmpD > maxDunder_this_brunch) maxDunder_this_brunch=tmpD; } state[x][y] = true; return maxDunder_this_brunch; } static void show_b2(boolean[][] dt, int lit_x, int lit_y){ PrintWriter out = new PrintWriter(System.out); for(int i=0; i<dt[0].length; i++){ for(int j=0; j<dt.length; j++){ if((i==lit_y) && (j==lit_x)) out.print("X"); else if(dt[j][i]) out.print("O"); else out.print("."); } out.println(); } out.flush(); } static void show_L2(long[][] dt, String cmnt){ PrintWriter out = new PrintWriter(System.out); for(int i=0; i<dt[0].length; i++){ for(int j=0; j<dt.length; j++){ out.print(dt[j][i]+","); } out.println("<-"+cmnt+":"+i); } out.flush(); } static void disp_que(ArrayDeque<Integer> dt){ //上手くいかなかった時用 int a=0; while(dt.size()>0){ a=dt.removeLast(); System.out.print(a); } System.out.println("\n"); } static void disp_list(List dt){ //上手くいかなかった時用 for(int i=0; i<dt.size(); i++){ System.out.print(dt.get(i)+","); } System.out.println("\n"); } private static void fill(boolean[] ob, boolean res){ for(int i=0; i<ob.length; i++){ ob[i] = res; } } private static void fill(int[] ob, int res){ for(int i=0; i<ob.length; i++){ ob[i] = res; } } private static void fill(long[] ob, long res){ for(int i=0; i<ob.length; i++){ ob[i] = res; } } private static void fill(char[] ob, char res){ for(int i=0; i<ob.length; i++){ ob[i] = res; } } private static void fill(boolean[][] ob, boolean res){ for(int i=0; i<ob.length; i++){ for(int j=0; j<ob[0].length; j++){ ob[i][j] = res; } } } private static void fill(int[][] ob, int res){ for(int i=0; i<ob.length; i++){ for(int j=0; j<ob[0].length; j++){ ob[i][j] = res; } } } private static void fill(long[][] ob, long res){ for(int i=0; i<ob.length; i++){ for(int j=0; j<ob[0].length; j++){ ob[i][j] = res; } } } private static void fill(char[][] ob, char res){ for(int i=0; i<ob.length; i++){ for(int j=0; j<ob[0].length; j++){ ob[i][j] = res; } } } private static void fill(int[][][] ob, int res){ for(int i=0; i<ob.length; i++){ for(int j=0; j<ob[0].length; j++){ for(int k=0; k<ob[0][0].length; k++){ ob[i][j][k] = res; } } } } private static void fill(long[][][][] ob, long res){ for(int i=0; i<ob.length; i++){ for(int j=0; j<ob[0].length; j++){ for(int k=0; k<ob[0][0].length; k++){ for(int l=0; l<ob[0][0][0].length; l++){ ob[i][j][k][l]=res; } } } } } static class FastScanner { private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte() { if (ptr < buflen) { return true; }else{ ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;} private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;} public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();} public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nexL() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while(true){ if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; }else if(b == -1 || !isPrintableChar(b) || b == ':'){ return minus ? -n : n; }else{ throw new NumberFormatException(); } b = readByte(); } } public int nexI() { long nl = nexL(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nexD() { return Double.parseDouble(next());} public void ni(long[] array2){ for(int i=0; i<array2.length; i++){ array2[i] = nexL(); } return; } public void ni(int[] array2){ for(int i=0; i<array2.length; i++){ array2[i] = nexI(); } return; } } } ```
### 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 def move(board): # 空きを埋める関数 board = [list(x) for x in zip(*board)] for i in range(len(board)): row = board[i] row = [i for i in row if i!=0] n = len(board[i]) - len(row) row.extend([0]*n) board[i] = row board = [list(x) for x in zip(*board)] return board ls_ans = [] while(1): ans = 0 H = int(input()) if H==0: break board = [] for h in range(H): C = list(map(int,input().split())) board.append(C) board = board[::-1] for t in range(H): for h in range(H): C = board[h] changed = set() old_c = '' oldold_c = '' streak = 0 for i,c in enumerate(C): if c == old_c and c == oldold_c: if streak == 0: streak += 3*c C[i] = C[i-1] = C[i-2] = 0 else: streak += c C[i] = 0 else: ans += streak streak = 0 oldold_c = old_c old_c = c ans += streak board[h] = C board = move(board) ls_ans.append(ans) for ans in ls_ans: print (ans) ```
### 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 <bits/stdc++.h> using namespace std; const int MAX_H=21; int H,ans,ok,rev; vector<vector<int>> stones(MAX_H,vector<int>(5)); vector<int> seq(5); int main(){ cin.tie(0); ios::sync_with_stdio(false); while(cin >> H,H){ for (int i=0;i<H;++i) for (int j=0;j<5;++j) cin >> stones[H-1-i][j]; ans=0; while(1){ ok=0; for (int i=0;i<H;++i){ for (int j=0;j<5;++j) seq[j]=0; seq[2]=(stones[i][2]>0); rev=0; for (int j=1;j<=2;++j){ if (seq[1+j]&&stones[i][2+j]==stones[i][2]) seq[2+j]=1; if (seq[3-j]&&stones[i][2-j]==stones[i][2]) seq[2-j]=1; } for (int j=0;j<5;++j) rev+=seq[j]; if (rev>=3){ ok=1; ans+=rev*stones[i][2]; for (int j=0;j<5;++j){ if (seq[j]) stones[i][j]=0; } } } for (int i=0;i<H;++i){ for (int j=0;j<5;++j){ if (!stones[i][j]){ for (int k=i+1;k<H;++k){ if (stones[k][j]){ stones[i][j]=stones[k][j]; stones[k][j]=0; break; } } } } } if (!ok) break; } cout << ans << '\n'; } } ```
### 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 import sys import itertools # import numpy as np import time import math import heapq from collections import defaultdict sys.setrecursionlimit(10 ** 7) INF = 10 ** 9 + 7 read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines # map(int, input().split()) while True: H = int(input()) if H == 0: break B = [0] * H for i in range(H): B[i] = list(map(int, input().split())) W = len(B[i]) score = 0 while True: # erase step for i in range(H): start = 0 while start < W: count = 1 while start + count < W and B[i][start] == B[i][start + count]: count += 1 if count >= 3: score += B[i][start] * count for j in range(start, start + count): B[i][j] = 0 start += count # drop step updated = False for i in range(H - 1, -1, -1): for j in range(W - 1, -1, -1): if B[i][j] != 0: continue new_col = [] for k in range(i, -1, -1): if B[k][j] == 0: continue new_col.append(B[k][j]) l = 0 for k in range(i, -1, -1): if l >= len(new_col): B[k][j] = 0 else: B[k][j] = new_col[l] updated = True l += 1 if not updated: break print(score) ```
### 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 itertools import zip_longest while True: n = int(input()) if not n: break field = [list(map(int, input().split())) for _ in range(n)] field.reverse() score = 0 while True: no_more_disappear = True for y, row in enumerate(field): for i, stone in enumerate(row[:3]): if stone is None: continue cnt = 1 for stone2 in row[i + 1:]: if stone != stone2: break cnt += 1 if cnt >= 3: row[i:i + cnt] = [None] * cnt score += stone * cnt no_more_disappear = False if no_more_disappear: break fieldT = map(lambda row: filter(None, row), zip(*field)) field = list(map(list, zip_longest(*fieldT))) print(score) ```
### 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 = input() if h == 0: break M = [map(int, raw_input().split()) for i in xrange(h)] ans = 0 update = 1 while update: update = 0 for i in xrange(h): cnt = 0; prev = None for j in xrange(5): if prev != M[i][j]: if cnt >= 3: if prev is not None: for k in xrange(cnt): M[i][j - k - 1] = None ans += cnt * prev update = 1 cnt = 1 else: cnt += 1 prev = M[i][j] if cnt >= 3: if prev is not None: for k in xrange(cnt): M[i][5 - k - 1] = None ans += cnt * prev update = 1 for j in xrange(5): cur = h-1 for i in xrange(h-1, -1, -1): if M[i][j] is not None: if i < cur: M[cur][j] = M[i][j] M[i][j] = None cur -= 1 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 "bits/stdc++.h" using namespace std; int main() { int H,pazzle[10][5]; while(scanf("%d",&H),H) { for(int i=0;i<H;i++) { for(int j=0;j<5;j++) { scanf("%d",&pazzle[i][j]); } } int ans=0; while(1) { bool ok=0; for(int i=0;i<H;i++) { int len=1,add=0; for(int j=1;j<5;j++) { if(pazzle[i][j]==pazzle[i][j-1]) { len++; }else { if(len>=3) { add=pazzle[i][j-1]*len; for(int k=0;k<len;k++) pazzle[i][j-1-k]=0; } len=1; } } if(len>=3) { add=pazzle[i][4]*len; for(int k=0;k<len;k++) pazzle[i][4-k]=0; } if(add>0) ans+=add,ok=1; } if(!ok) break; for(int j=0;j<5;j++) { int pos=H-1; for(int i=H-1;i>=0;i--) { if(pazzle[i][j]>0) pazzle[pos--][j]=pazzle[i][j]; } for(int i=pos;i>=0;i--) pazzle[i][j]=0; } } printf("%d\n",ans); } } ```
### 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.Arrays; import java.util.Scanner; public class Main { Scanner sc; Main() { sc = new Scanner(System.in); } int ni() { return sc.nextInt(); } public static void main(String[] args) { new Main().run(); } class Point { int x, y; } int[][] ofs = { {-1, 0}, {0, 1}, {1, 0}, {0, -1} }; int INF = 1 << 28; void run() { for (; ; ) { int h = ni(); if (h == 0) { break; } int[][] field = new int[h][5]; for (int i = 0; i < h; ++i) { for (int j = 0; j < 5; ++j) { field[i][j] = ni(); } } int sum = 0; for (; ; ) { boolean flag = false; for (int i = h - 1; 0 <= i; --i) { for (int j = 0; j < 5; ++j) { int val = field[i][j]; if (val == 0) { continue; } int cnt = 1; for (int k = j + 1; k < 5; ++k) { if (val == field[i][k]) { ++cnt; } else { break; } } if (cnt >= 3) { flag |= true; for (int k = j; k < 5; ++k) { if (val == field[i][k]) { sum += field[i][k]; field[i][k] = 0; } else { break; } } } } } if (!flag) { break; } for (int i = h - 1; 0 <= i; --i) { for (int j = 0; j < 5; ++j) { for (int k = i; k + 1 < h; ++k) { if (field[k + 1][j] != 0) { break; } field[k + 1][j] = field[k][j]; field[k][j] = 0; } } } } System.out.println(sum); } } void debug(Object... os) { System.err.println(Arrays.deepToString(os)); } } ```
### Prompt Your challenge is to write a python 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 ```python while True: w = 5 h = input() if h == 0: break a = [] for i in range(h): a.append(map(int, raw_input().split())) flag = True ans = 0 while flag: flag = False for i in range(h - 1, -1, -1): cnts = [1 for _ in range(w)] for j in range(w - 1): if a[i][j] != -1 and a[i][j] == a[i][j + 1]: cnts[j + 1] += cnts[j] for j in range(w - 1, -1, -1): if a[i][j] != -1 and cnts[j] >= 3: ans += a[i][j] * cnts[j] for k in range(cnts[j]): a[i][j - k] = -1 flag = True flag2 = True while flag2: flag2 = False for i in range(h - 1, 0, -1): for j in range(w): if a[i][j] == -1 and a[i - 1][j] != -1: (a[i][j], a[i - 1][j]) = (a[i - 1][j], a[i][j]) flag2 = True """ for i in range(h): for j in range(w): print a[i][j], print print """ 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<bits/stdc++.h> #define int long long #define for0(i, n) for(int i = 0; i < (n); i++) #define for1(i, n) for(int i = 1; i <= (n);i++) using namespace std; int n; void solve() { int a[15][7], sum = 0; for1(i, n)for1(j, 5) { cin >> a[i][j]; sum += a[i][j]; } bool b = 1; while (b) { b = 0; for1(i, n) { if (a[i][2] == a[i][3] && a[i][3] == a[i][4] && a[i][3] != 0) { b = 1; if (a[i][1] == a[i][3])a[i][1] = 0; if (a[i][5] == a[i][3])a[i][5] = 0; a[i][2] = 0; a[i][3] = 0; a[i][4] = 0; } else if (a[i][1] == a[i][2] && a[i][2] == a[i][3] && a[i][3] != 0) { b = 1; a[i][1] = 0; a[i][2] = 0; a[i][3] = 0; } else if (a[i][5] == a[i][4] && a[i][4] == a[i][3] && a[i][3] != 0) { b = 1; a[i][5] = 0; a[i][4] = 0; a[i][3] = 0; } } for1(j, 5) { vector<int>v; for1(i, n)if (a[i][j])v.push_back(a[i][j]); for1(i, n)a[i][j] = (n - v.size() >= i ? 0 : v[i - n + v.size() - 1]); } } for1(i, n)for1(j, 5)sum -= a[i][j]; cout << sum << endl; } signed main() { while (cin >> n, n)solve(); } ```
### 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 <cstring> #include <iostream> #include <algorithm> using namespace std; int M[11][5]; int main() { ios::sync_with_stdio(false); while(true) { int H; cin >> H; if(H == 0) break; memset(M, 0, sizeof(M)); for(int i = H - 1; i >= 0; --i) { for(int j = 0; j < 5; ++j) { cin >> M[i][j]; } } int ans = 0; while(true) { /* for(int i = H - 1; i >= 0; --i) { for(int j = 0; j < 5; ++j) printf("%d", M[i][j]); printf("\n"); } */ bool update = false; for(int i = 0; i < H; ++i) { for(int j = 0; j < 5; ++j) { if(M[i][j] == 0) continue; int s = 0; for(int k = j; k < 5; ++k) { if(M[i][k] == M[i][j]) ++s; else break; } if(s >= 3) { ans += s * M[i][j]; for(int x = 0; x < s; ++x) { M[i][j + x] = 0; } update = true; } } } for(int j = 0; j < 5; ++j) { while(true) { bool update2 = false; for(int i = 0; i < H; ++i) { if(M[i][j] == 0 && M[i + 1][j] != 0) { M[i][j] = M[i + 1][j]; M[i + 1][j] = 0; update2 = true; } } if(!update2) break; } } if(!update) break; } 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 <bits/stdc++.h> using namespace std; int main() { long long int N; while( cin >> N, N ) { long long int ans = 0; vector<vector<long long int>>d( N + 1, vector<long long int>( 5 ) ); for( size_t i = 1; i <= N; i++ ) { for( size_t j = 0; j < 5; j++ ) { cin >> d[i][j]; ans += d[i][j]; } } bool f = true; while( f ) { f = false; for( size_t i = 1; i <= N; i++ ) { if( d[i][2] ) { int now = d[i][2]; int count = 1; int j = 3, k = 1; while( j < 5 && now == d[i][j] ) j++; while( k >= 0 && now == d[i][k] ) k--; if( j - k >3 ) { f = true; for( size_t l = k + 1; l < j; l++ ) { d[i][l] = 0; } } } } for( int i = N; i >= 1; i-- ) { for( size_t j = 0; j < 5; j++ ) { if( !d[i][j] ) { int k = i; while( k > 0 && !d[k][j] ) k--; if( k != 0 ) swap( d[i][j], d[k][j] ); } } } } for( size_t i = 0; i <= N; i++ ) { for( size_t j = 0; j < 5; j++ ) { ans -= d[i][j]; } } 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; using vi=vector<int>; using vvi=vector<vi>; using vs=vector<string>; using msi=map<string,int>; using mii=map<int,int>; using pii=pair<int,int>; using vlai=valarray<int>; using ll=long long; #define rep(i,n) for(int i=0;i<n;i++) #define range(i,s,n) for(int i=s;i<n;i++) #define all(a) a.begin(),a.end() #define rall(a) a.rbegin(),a.rend() #define fs first #define sc second int main(){ int n; while(cin>>n,n){ vvi v(n,vi(5)); rep(i,n)rep(j,5)cin>>v[i][j]; int ans=0; bool isC=true; while(isC){ isC=false; rep(i,n){ for(vi p : vvi{{0,5},{0,4},{1,5},{0,3},{1,4},{2,5}}){ if(v[i][p[0]]!=0 && count(v[i].begin()+p[0], v[i].begin()+p[1], v[i][p[0]]) == p[1]-p[0]){ isC=true; ans+=(p[1]-p[0])*v[i][p[0]]; fill(v[i].begin()+p[0], v[i].begin()+p[1], 0); break; } } } rep(lp,n){ for(int i=n-1;i>=1;i--)rep(j,5){ if(v[i][j]==0) swap(v[i-1][j], v[i][j]); } } //rep(i,n){rep(j,5)cout<<v[i][j];cout<<endl;} } cout<<ans<<endl; } return 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 <iostream> #include <cstdio> #include <cstdlib> using namespace std; int main() { int H,p,c,f,l,b; //p:point, c:check, f:flag, l:length int s[50]; while(cin >> H){ if(H==0){return 0;} p=0; b=-1; for(int i=0; i<50; i++){ s[i]=0;} for(int i=H-1; i>=0; i--){ for(int j=0 ; j<5; j++){ cin >> s[i*5+j]; } } while(true){ if(b==p){break;} b=p; for(int i=H-1; i>=0; i--){ c=s[i*5+0]; l=1; f=0; for(int j=1 ; j<5; j++){ if(c==s[i*5+j]){l++;} else if(l>=3){break;} else{ f=j; l=1; c=s[i*5+j];} } if(l>=3){ for(int j=f ; j<l+f; j++){ p+=s[i*5+j]; s[i*5+j]=0; } } } for(int k=0; k<H-1; k++){ for(int i=H-1; i>0; i--){ for(int j=0 ; j<5; j++){ if(s[(i-1)*5+j]==0){ s[(i-1)*5+j]=s[i*5+j]; s[i*5+j]=0; } } } } } cout << p << 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 <iostream> #include <vector> #include <cstdio> using namespace std; int main() { int H; cin >> H; if (!H) { return 0; } vector< vector<int> > b(H, vector<int>(6)); for (int i = 0; i < H; i++) { cin >> b[i][0] >> b[i][1] >> b[i][2] >> b[i][3] >> b[i][4]; b[i][5] = 0; } int ans = 0; int score = -1; while (score) { score = 0; for (int i = 0; i < H; i++) { int prev = b[i][0]; int index = 0; int length = 1; for (int j = 1; j <= 5; j++) { if (b[i][j] == prev) { length++; } else { if (length >= 3) { score += prev * length; for (int k = index; k < index + length; k++) { b[i][k] = 0; } } prev = b[i][j]; index = j; length = 1; } } } vector< vector<int> > t(H, vector<int>(6, 0)); for (int j = 0; j < 5; j++) { for (int i = H, k = H - 1; i--;) { if (b[i][j]) { t[k][j] = b[i][j]; k--; } } } ans += score; b = t; } cout << ans << endl; return main(); } ```
### Prompt Construct a java 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 ```java import java.util.Scanner; public class Main { public static int check(int[][] stone, int line) { int point = 0, flag, i, j; for (i = 0; i < 3; i++) { flag = 1; for (j = i + 1; j < 5; j++) { if (stone[i][line] == stone[j][line]) { flag++; } else { break; } } if (flag >= 3) { point = stone[i][line] * flag; for (j = 0; j < flag; j++) { stone[i + j][line] = 0; } break; } } return point; } public static void fall(int[][] stone, int n) { int i, j,k; for (i = 0; i < 5; i++) { for (j = 0; j < n; j++) { if (stone[i][j] == 0) { if (j + 1 == n) { stone[i][j] = 0; } else { for(k=j;k<n;k++) { if(stone[i][k]>0) { stone[i][j]=stone[i][k]; stone[i][k]=0; break; } } } } } } } public static void show(int[][] stone, int n) { for (int i = n - 1; i >= 0; i--) { if(i==4) { System.out.println("----------"); } System.out.printf("%d %d %d %d %d\n", stone[0][i], stone[1][i], stone[2][i], stone[3][i], stone[4][i]); } System.out.println(); } public static void main(String[] args) { Scanner scan = new Scanner(System.in); while (true) { int point = 0; int n = scan.nextInt(); if (n == 0) { break; } int[][] stone = new int[5][n]; for (int i = n - 1; i >= 0; i--) { for (int j = 0; j < 5; j++) { stone[j][i] = scan.nextInt(); } } while (true) { int add = 0; for (int i = 0; i < n; i++) { if (i < n) { add += check(stone, i); } } if (add == 0) { break; } else { point += add; fall(stone, n); } } System.out.println(point); } scan.close(); } } ```
### 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> #define REP(i, a, n) for(int i = ((int) a); i < ((int) n); i++) using namespace std; int H, A[10][5]; bool v[10][5]; int main(void) { while(cin >> H, H) { REP(i, 0, H) REP(j, 0, 5) cin >> A[H - i - 1][j]; int ans = 0; bool cont = true; while(cont) { cont = false; REP(i, 0, H) REP(j, 0, 5) v[i][j] = false; REP(i, 0, H) REP(j, 0, 3) { if(A[i][j] != 0 && A[i][j] == A[i][j + 1] && A[i][j] == A[i][j + 2]) { v[i][j] = v[i][j + 1] = v[i][j + 2] = true; cont = true; } } REP(i, 0, H) REP(j, 0, 5) if(v[i][j]) ans += A[i][j]; REP(j, 0, 5) REP(i, 0, H) { if(v[i][j]) { REP(k, i + 1, H) { A[k - 1][j] = A[k][j]; v[k - 1][j] = v[k][j]; } A[H - 1][j] = 0; v[H - 1][j] = false; i--; } } } 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.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Comparator; import java.util.Deque; import java.util.LinkedList; public class Main { public static int[][] map; public static int w; public static int h; public static void main(String[] args) throws NumberFormatException, IOException { ContestScanner in = new ContestScanner(); w = 5; while(true){ h = in.nextInt(); if(h==0) break; int score = 0; map = new int[h][w]; for(int i=0; i<h; i++){ for(int j=0; j<w; j++){ map[i][j] = in.nextInt(); } } int sc; while((sc = calcScore()) > 0){ score += sc; nextState(); } System.out.println(score); } } public static int calcScore(){ // ?????????????????¢????¨???????????¶?????????????0?????????????????? int res = 0; for(int i=0; i<h; i++){ int cnt = 1; for(int j=1; j<=w; j++){ if(j != w && map[i][j] == map[i][j-1]){ cnt++; continue; } if(cnt < 3){ cnt = 1; continue; } int num = map[i][j-1]; if(num == 0){ cnt = 0; continue; } int id = j-1; while(id >= 0 && map[i][id] == num){ res += num; map[i][id] = 0; id--; } cnt = 0; } } return res; } public static void nextState(){ // 0??????????¶??????????????????¨??? int[][] nmap = new int[h][w]; for(int i=0; i<w; i++){ Deque<Integer> qu = new LinkedList<Integer>(); for(int j=h-1; j>=0; j--){ if(map[j][i] > 0) qu.add(map[j][i]); } int id = h-1; while(!qu.isEmpty()){ nmap[id--][i] = qu.pollFirst(); } } map = nmap; } public static String dump(int[][] map){ StringBuilder sb = new StringBuilder(); for(int i=0; i<h; i++){ for(int j=0; j<w; j++){ sb.append(map[i][j]+" "); } sb.append("\n"); } return sb.toString(); } } class Node{ int id; int sink; ArrayList<Node> edge = new ArrayList<Node>(); public Node(int id) { this.id = id; } public void createEdge(Node node) { edge.add(node); } } class MyComp implements Comparator<int[]> { final int idx; public MyComp(int idx){ this.idx = idx; } public int compare(int[] a, int[] b) { return a[idx] - b[idx]; } } class Reverse implements Comparator<Integer> { public int compare(Integer arg0, Integer arg1) { return arg1 - arg0; } } class ContestWriter { private PrintWriter out; public ContestWriter(String filename) throws IOException { out = new PrintWriter(new BufferedWriter(new FileWriter(filename))); } public ContestWriter() throws IOException { out = new PrintWriter(System.out); } public void println(String str) { out.println(str); } public void println(Object obj) { out.println(obj); } public void print(String str) { out.print(str); } public void print(Object obj) { out.print(obj); } public void close() { out.close(); } } class ContestScanner { private BufferedReader reader; private String[] line; private int idx; public ContestScanner() throws FileNotFoundException { reader = new BufferedReader(new InputStreamReader(System.in)); } public ContestScanner(String filename) throws FileNotFoundException { reader = new BufferedReader(new InputStreamReader(new FileInputStream( filename))); } public String nextToken() throws IOException { if (line == null || line.length <= idx) { line = reader.readLine().trim().split(" "); idx = 0; } return line[idx++]; } public String readLine() throws IOException{ return reader.readLine(); } public long nextLong() throws IOException, NumberFormatException { return Long.parseLong(nextToken()); } public int nextInt() throws NumberFormatException, IOException { return (int) nextLong(); } public double nextDouble() throws NumberFormatException, IOException { return Double.parseDouble(nextToken()); } } ```
### 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; int main(){ int h; while(cin>>h,h){ int a[10][5],ans=0; for(int i=0;i<h;i++) for(int j=0;j<5;j++)cin>>a[i][j]; bool f=1; while(f){ f=0; for(int i=0;i<h;i++){ int c=1,p=0; for(int j=1;j<5;j++) if(a[i][p]==a[i][j])c++; else if(c<3)c=1,p=j; else break; if(c>=3){ ans+=c*a[i][p]; for(int k=0;k<c;k++)a[i][p+k]=0; } } for(int i=0;i<5;i++) for(int j=1;j<h;j++) for(int k=j;k>0;k--) if(!a[k][i]&&a[k-1][i])swap(a[k][i],a[k-1][i]),f=1; } cout<<ans<<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 <cstdio> #define rep(i,n) for(int i=0;i<(n);i++) using namespace std; int main(){ for(int h,w=5;scanf("%d",&h),h;){ int B[10][5]; rep(i,h) rep(j,w) scanf("%d",B[i]+j); int ans=0; while(1){ bool changed=false; // vanish for(int i=h-1;i>=0;i--){ int cont=1; for(int j=1;j<=w;j++){ if(j==w || B[i][j-1]!=B[i][j]){ if(cont>=3 && B[i][j-1]!=0){ ans+=B[i][j-1]*cont; rep(k,cont) B[i][j-k-1]=0; changed=true; } cont=1; } else cont++; } } // fall rep(j,w){ rep(_,h) rep(i,h-1) { if(B[i][j]!=0 && B[i+1][j]==0) B[i+1][j]=B[i][j], B[i][j]=0; } } if(!changed) break; } printf("%d\n",ans); } 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 <vector> #include <algorithm> using namespace std; int main(){ int H; //?????????????????¶????????\??? while (cin >> H && H>0) { vector< vector<int> > c(5,vector<int>(H)); int sum = 0; for (int i=H-1; i>=0; i--){ for (int j=0; j<5; j++){ cin >> c[j][i]; } } //????????°??????3?????\????¨???????????????´???????????°????¨??????????0????????? int flg = 1; //?¶?????????¨???flg == 1 while (flg) { flg = 0; for (int i=H-1; i>=0; i--){ for (int j=0; j<3; j++) { int cnt = 1; //?¨???????????????° if (c[j][i] != 0) { int flag = 1; for (int k=j+1; k<5 && flag; k++) { //????¨?????????????????????? if (c[j][i] == c[k][i]) cnt++; if (c[j][i] != c[k][i] || k == 4) { //?????????????????£??????????????????????????? flag = 0; if (cnt >= 3) { //3?????\???????????§?????¨??? sum += c[j][i] * cnt; //???????????? for (int l=j; l<=j+cnt-1; l++) { //0??????????????? c[l][i] = 0; } flg = 1; } } } } } } for (int j=0; j<5; j++) { vector<int>::iterator itr = remove(c[j].begin(), c[j].end(), 0); for (; itr != c[j].end(); itr++) *itr = 0; } } //???????????????????????????0????????£??????flg==0??¨?????????????????±??? cout << sum << 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 <string> #include <cstdio> #include <vector> #include <algorithm> #include <map> #include <stack> #include <cmath> using namespace std; int H, score; int board[10][5]; void printBoard(){ cout << endl; for(int i = 0; i < H; i++){ for(int j = 0; j < 5; j++) cout << board[i][j] << " "; cout << endl; } } bool checkBoard(){ bool flag = false; for(int i = 0; i < H; i++){ int prev = board[i][0], head = 0, tail = 1; int ans_head = 0, ans_tail = 1; for(int j = 1; j < 5; j++){ if(board[i][j] == prev && prev != 0) tail++; else{ prev = board[i][j]; head = j; tail = head + 1; } if(tail - head > ans_tail - ans_head){ ans_head = head; ans_tail = tail; } } if(ans_tail - ans_head < 3) continue; score += board[i][ans_head] * (ans_tail - ans_head); flag = true; for(int j = i; j >= 1; j--){ for(int s = ans_head; s < ans_tail; s++) board[j][s] = board[j-1][s]; } for(int j = ans_head; j < ans_tail; j++) board[0][j] = 0; } return flag; } int main(void) { while(cin >> H && H != 0){ score = 0; for(int i = 0; i < H; i++){ for(int j = 0; j < 5; j++) cin >> board[i][j]; } while(checkBoard()); cout << score << 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 <bits/stdc++.h> using namespace std; #define REP(i,s,n) for(int i=s;i<n;++i) #define rep(i,n) REP(i,0,n) #define SORT(c) sort((c).begin(),(c).end()) #define IINF INT_MAX #define LLINF LLONG_MAX #define DEBUG false typedef long long ll; typedef pair<int, int> ii; int main() { int h; while(cin >> h , h){ vector<vector<int>> mp(h, vector<int>(5)); for(int i = h-1; i >= 0; i--) rep(j, 5) cin >> mp[i][j]; int ans = 0; bool f = true; while(f){ f = false; rep(i, h){ int cnt = 1; REP(j, 1, 5){ if(mp[i][j] == mp[i][j - 1] && mp[i][j] > 0) cnt++; else{ if(cnt >= 3){ rep(k, cnt){ ans += mp[i][j-1-k]; mp[i][j-1-k] = -1; f = true; } } cnt = 1; } } if(cnt >= 3){ rep(k, cnt){ ans += mp[i][4-k]; mp[i][4-k] = -1; f = true; } } } rep(i, h){ rep(j, 5){ if(mp[i][j] == -1){ int tmp = 0; for(int k = 1; i + k < h; k++){ if(mp[i+k][j] != -1){ tmp = mp[i+k][j]; mp[i+k][j] = -1; break; } mp[i+k][j] = -1; } mp[i][j] = tmp; } } } } 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,h|h!=0){ string temp; int board[h][6]; for (int i=0;i<h;i++){ cin>>board[i][0]>>board[i][1]>>board[i][2]>>board[i][3]>>board[i][4]; } int num,count; int check=0; int score=0; while (1){ check=score; for (int i=h-2;i>=0;i--){ for (int j=0;j<5;j++){ for (int k=h-1;k>i;k--){ if (board[k][j]==0){ board[k][j]=board[i][j]; board[i][j]=0; break; } } } } for (int i=0;i<h;i++){ count=1; num=0; for (int j=0;j<5;j++){ if (board[i][j]==num){ count++; } else{ if (count>2) break; count=1; } num=board[i][j]; } if (count<=2) num=0; score=score+num*count; int ii=0; for (int j=0;j<5;j++){ if ((ii==count-1||j==4||board[i][j+1]==num)&&board[i][j]==num){ board[i][j]=0; ii++; } if (ii==count) break; } } /* for (int i=0;i<h;i++){ for (int j=0;j<5;j++){ cout<<board[i][j]<<" "; } cout<<endl; }*/ if (check==score){ break; } // cout<<" "<<score<<endl; } cout<<score<<endl; } } ```
### Prompt Construct a Java 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 ```java import java.io.FileNotFoundException; import java.util.Scanner; public class Main { int[][] f; int h; public static void main(String[] args) throws FileNotFoundException { new Main().run(); } private void run() { Scanner sc = new Scanner( System.in ); while(true){ h = sc.nextInt(); f = new int [h][5]; if(h==0) return; for(int i=0;i<h;i++){ for(int j=0;j<5;j++){ f[i][j] = sc.nextInt(); } } int score = 0; while(true){ while(true){ int delScore = delete(); if(delScore==-1) break; score += delScore; } int cntDown = down2(); if(cntDown==0) break; } System.out.println(score); } } private int down2() { int res = 0; int id = h - 1; for (int i = 0; i < 5; i++) { id = h - 1; for (int j = h - 1; 0 <= j; j--) { if (f[j][i] != -1) { f[id][i] = f[j][i]; if (id != j) { res++; } id--; } } for (int j = id; j >= 0; j--) { f[j][i] = -1; } } return res; } private int delete() { for(int i=0;i<h;i++){ for(int j=0;j<5;j++){ if(f[i][j]==-1) continue; int start = j; int goal = j; while(true){ if(goal+1>=5) break; if(f[i][start]==f[i][goal+1]){ goal++; } else{ break; } } if(goal-start+1>=3){ int element = f[i][start]; for(int k=start;k<=goal;k++){ f[i][k] = -1; } return (goal-start+1)*element; } } } return -1; } } ```
### 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 sys,queue,math sys.setrecursionlimit(10**7) input = sys.stdin.readline INF = 10**18 MOD = 10**9 + 7 LI = lambda : [int(x) for x in input().split()] _LI = lambda : [int(x)-1 for x in input().split()] while True: N = int(input()) if N == 0 : break puyo = [[int(x) for x in input().split()] + [0] for _ in range(N)] p = 0 update = True while update: update = False for i in range(N): cl = puyo[i][0] cnt = cl > 0 for j in range(1,6): if puyo[i][j] == cl and cl > 0: cnt += 1 else: if cnt >= 3: p += cl * cnt for k in range(cnt): puyo[i][j-k-1] = 0 update = True cl = puyo[i][j] cnt = cl > 0 for i in range(N-1,0,-1): for j in range(5): if puyo[i][j] > 0: continue x = i while x > 0: x -= 1 if puyo[x][j] > 0: puyo[i][j] = puyo[x][j] puyo[x][j] = 0 break print (p) ```
### 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<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); } } for(int i=0;i<h;i++){ for(int j=0;j<5;j++){ 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 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 # coding: utf-8 while 1: h=int(input()) if h==0: break field=[] for i in range(h): field.insert(0,list(map(int,input().split()))) field[0]+=[0] ans=0 f=True while f: f=False for i in range(h): now=-1 count=0 for j in range(6): if now!=field[i][j] and count>=3: for k in range(count): field[i][j-1-k]=0 ans+=now*count count=0 f=True if now!=0 else f elif now!=field[i][j]: count=0 now=field[i][j] count+=1 for i in range(h-1): for j in range(5): if field[i][j]==0: for k in range(i+1,h): if field[k][j]!=0: field[i][j],field[k][j]=field[k][j],field[i][j] break print(ans) ```
### 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 #!usr/bin/env python3 import sys import math import string import collections import fractions import random from operator import itemgetter import itertools from collections import deque import copy import heapq import bisect MOD = 10 ** 9 + 7 INF = 10 ** 18 input = lambda: sys.stdin.readline().strip() sys.setrecursionlimit(10 ** 8) H = int(input()) while H != 0: table = [list(map(int, input().split())) for _ in range(H)] prev_ans = -1 ans = 0 while ans != prev_ans: prev_ans = ans for i in range(H): for j in range(3): if table[i][j] != -1: if table[i][j] == table[i][j + 1] == table[i][j + 2]: for l in range(j + 2, 5): if table[i][j] != table[i][l]: ans += (l - j) * table[i][j] for k in range(j, l): table[i][k] = -1 break elif l == 4: ans += (l - j + 1) * table[i][j] for k in range(j, 5): table[i][k] = -1 break for i in range(H - 2, -1, -1): for j in range(5): if table[i][j] != -1: if table[i + 1][j] == -1: for l in range(i + 1, H): if table[l][j] != -1: table[l - 1][j] = table[i][j] for k in range(i, l - 1): table[k][j] = -1 break elif l == H - 1: table[l][j] = table[i][j] table[i][j] = -1 break print(ans) H = int(input()) ```
### 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 <cstdio> #include <vector> #include <algorithm> using namespace std; int main(){ int h; while(scanf("%d", &h), h){ vector<vector<int> > v(5, vector<int>(h)); for(int i = h - 1; i >= 0; --i) for(int j = 0; j < 5; ++j){ scanf("%d", &v[j][i]); } int s = 0; bool upd = true; while(upd){ upd = false; for(int i = 0; i < h; ++i){ bool rm[5] = {}; for(int j = 0; j < 3; ++j){ if(v[j][i] && v[j][i] == v[j + 1][i] && v[j][i] == v[j + 2][i]){ rm[j] = rm[j + 1] = rm[j + 2] = true; upd = true; } } for(int j = 0; j < 5; ++j){ if(rm[j]){ s += v[j][i]; v[j][i] = 0; } } } for(int j = 0; j < 5; ++j){ fill(remove(v[j].begin(), v[j].end(), 0), v[j].end(), 0); } } printf("%d\n", s); } } ```
### 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; using ll = long long; #define REP(i,n) for(ll i=0;i<(n);i++) #define ALL(x) x.begin(),x.end() #define REPR(i,n) for(ll i=(n)-1;i>=0;i--) #define FOR(i,m,n) for(ll i=(m);i<(n);i++) #define int long long int H; int A[20][10]; void solve() { int H;cin>>H; int W = 5; if(H==0)exit(0); REP(i,H){ REP(j,5){ cin>>A[i][j]; } } bool found = true; int ans = 0; while(found){ found = false; REP(i,H){ REP(l,W){ int r = l; if(A[i][l]==-1)continue; while(A[i][r]==A[i][l]){ r++; if(r==W)break; } if(r-l>=3){ found = true; FOR(j,l,r){ ans+=A[i][j]; A[i][j]=-1; } } } } REP(_,H+1){ REPR(i,H-1){ REP(j,W){ if(A[i+1][j]==-1){ A[i+1][j]=A[i][j]; A[i][j]=-1; } } } } } cout<<ans<<endl; } signed main() { while (true)solve(); } ```
### 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 public class Main{ public void run(java.io.InputStream in, java.io.PrintStream out){ java.util.Scanner sc = new java.util.Scanner(in); /*answer*/ int[][] f; int count; int i, j, k, l, score, h, tmp; for(;;){ h = sc.nextInt(); if(h == 0)break; f = new int[h][5]; for(i = 0;i < h;i++)for(j = 0;j < 5;j++)f[i][j] = sc.nextInt(); score = 0; for(k = 0;k < h;k++){ for(i = 0;i < h;i++){ count = 0; for(j = 0;j < 5;j++)if(f[i][j] == f[i][2] && f[i][2] != 0)count++; if(count == 5){ score += 5 * f[i][2]; for(j = 0;j < 5;j++)f[i][j] = 0; }else if(count >= 3){ if(f[i][2] != f[i][0] && f[i][2] == f[i][1] && count == 4){ score += 4 * f[i][2]; for(j = 1;j < 5;j++)f[i][j] = 0; }else if(f[i][2] != f[i][4] && f[i][2] == f[i][1] && count == 4){ score += 4 * f[i][2]; for(j = 0;j < 4;j++)f[i][j] = 0; }else if(f[i][2] == f[i][3] && f[i][2] == f[i][4]){ score += 3 * f[i][2]; for(j = 2;j < 5;j++)f[i][j] = 0; }else if(f[i][2] == f[i][1] && f[i][2] == f[i][3]){ score += 3 * f[i][2]; for(j = 1;j < 4;j++)f[i][j] = 0; }else if(f[i][2] == f[i][0] && f[i][2] == f[i][1]){ score += 3 * f[i][2]; for(j = 0;j < 3;j++)f[i][j] = 0; } } } for(j = 0;j < 5;j++){ for(i = 1;i < h;i++){ if(f[i][j] == 0){ tmp = f[i][j]; for(l = i;l > 0;l--)f[l][j] = f[l - 1][j]; f[l][j] = 0; } } } } out.println(score); } sc.close(); } public static void main(String[] args){ (new Main()).run(System.in, System.out); } } ```
### 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.awt.geom.Point2D; import java.util.LinkedList; import java.util.Scanner; public class Main { static Scanner in = new Scanner(System.in); final double EPS=1.0e-8; public static void main(String[] args) { new Main(); } public Main() { new B().doIt(); } class B{ int h; int[][] field; int point; void doIt(){ while(true){ h = in.nextInt(); if(h == 0)return; field = new int[h][5]; for(int i=0;i<h;i++)for(int s=0;s<5;s++)field[i][s] = in.nextInt(); point = 0; play(); } } void play(){ while(true){ int plusPoint = deleteStone(); if(plusPoint == 0)break; point += plusPoint; fixField(); // print(); } System.out.println(point); } void fixField(){ for(int x=0;x<5;x++){//watch tate for(int y=h-1;y>=0;y--)if(field[y][x] < 0){ for(int dy = y-1;dy >= 0; dy--)if(field[dy][x] > 0){ int hoge = field[dy][x]; field[dy][x] = field[y][x]; field[y][x] = hoge; break; } } } } int deleteStone(){ int p = 0; for(int i=0;i<h;i++)p += deleteLine(i); return p; } int deleteLine(int y){ int count = 0; int targetNumber = field[y][2]; if(targetNumber == -1)return 0; if(field[y][1] == targetNumber && field[y][3] == targetNumber &&field[y][0] == targetNumber && field[y][4] == targetNumber){ count=5; field[y][0] = field[y][1] = field[y][2] = field[y][3] = field[y][4] = -1; }else if(field[y][0] == targetNumber && field[y][1] == targetNumber && field[y][3] == targetNumber ){ count=4; field[y][0] = field[y][1] = field[y][2] = field[y][3] = -1; }else if(field[y][1] == targetNumber && field[y][3] == targetNumber && field[y][4] == targetNumber){ count=4; field[y][1] = field[y][2] = field[y][3] = field[y][4] = -1; }else if(field[y][1] == targetNumber && field[y][3] == targetNumber ){ count=3; field[y][1] = field[y][2] = field[y][3] = -1; }else if(field[y][1] == targetNumber &&field[y][0] == targetNumber){ count=3; field[y][0] = field[y][1] = field[y][2] = -1; }else if(field[y][3] == targetNumber && field[y][4] == targetNumber){ count=3; field[y][2] = field[y][3] = field[y][4] = -1; } return count * targetNumber; } void print(){ System.out.println(); for(int i=0;i<h;i++){ for(int s=0;s<5;s++)System.out.print(field[i][s]+" "); System.out.println(); } } } } ```
### 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> #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; } void print(){ for(int i=h-1;i>=0;i--){ for(int j=0;j<5;j++)if(g[i][j]<0)cout << "*";else cout << g[i][j]; cout << endl; } } 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); } } for(int i=0;i<h;i++){ for(int j=0;j<5;j++){ 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 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 h; while (cin>>h,h|h!=0){ string temp; int board[h][6]; for (int i=0;i<h;i++){ cin>>board[i][0]>>board[i][1]>>board[i][2]>>board[i][3]>>board[i][4]; } int num,count; int check=0; int score=0; while (1){ check=score; for (int i=h-2;i>=0;i--){ for (int j=0;j<5;j++){ for (int k=h-1;k>i;k--){ if (board[k][j]==0){ board[k][j]=board[i][j]; board[i][j]=0; break; } } } } for (int i=0;i<h;i++){ count=1; num=0; for (int j=0;j<5;j++){ if (board[i][j]==num){ count++; } else{ if (count>2) break; count=1; } num=board[i][j]; } if (count<=2) num=0; score=score+num*count; int ii=0; for (int j=0;j<5;j++){ if ((ii==count-1||j==4||board[i][j+1]==num)&&board[i][j]==num){ board[i][j]=0; ii++; } if (ii==count) break; } } if (check==score){ break; } } cout<<score<<endl; } } ```
### 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 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 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> #include <string> #include <vector> #include <cmath> #include <algorithm> #include <cstdlib> #include <ctime> #include <cstdio> #include <functional> #include <set> #include <sstream> using namespace std; #define MP make_pair long gcd(long a, long b){ if(a%b==0) return b; else return gcd(b,a%b); } long lcm(long a, long b){ return a*b/gcd(a,b); } int main(){ int h; while(cin>>h, h){ vector< vector<int> > map(h , vector<int>(5)); for(int i=0;i<h;i++){ for(int j=0;j<5;j++){ cin>>map[i][j]; } } int res=0; while(true){ bool flag=true; for(int i=0;i<h;i++){ int num=map[i][0],len=1,pos=0; for(int j=1;j<5;j++){ if(map[i][j]==num) len++; else if(len>=3) break; else{ num=map[i][j]; pos=j; len=1;} } if(len>=3 && num!=0){ flag=false; res+=num*len; for(int j=0;j<len;j++) map[i][pos+j]=0; } } for(int i=h-2;i>=0;i--){ for(int j=0;j<5;j++){ int pos=i; while(pos+1<h && map[pos+1][j]==0) pos++; swap(map[i][j],map[pos][j]); } } if(flag) break; } cout<<res<<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; 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]; int cnt = 1; if(col==0) continue; while(j+cnt<5 && col==board[i][j+cnt]){ cnt++; } if(cnt<3) continue; for(int k=0; k<cnt; k++){ board[i][j+k] = 0; } point += col*cnt; flag = true; } } //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 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: break arr = [[-1 for i in range(h)], [-1 for i in range(h)], [-1 for i in range(h)], [-1 for i in range(h)], [-1 for i in range(h)]] midlen = h for i in range(h - 1, -1, -1): inp = list(map(int, input().split())) for j, e in enumerate(inp): arr[j][i] = e score = 0 za = True while za: za = False for i, e in list(enumerate(arr[2]))[::-1]: for j in range(3): ge = 0 for h in range(j, 5): if len(arr[h]) > i: if arr[h][i] == e: ge += 1 continue break if ge > 2: za = True for h in range(j, j + ge): score += arr[h].pop(i) print(score) ```
### 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 <cstdio> #include <cstdint> #include <vector> #include <queue> int nya(std::vector<std::vector<int>>& a) { size_t h = a.size(); int res = 0; for (size_t i = 0; i < h; ++i) { size_t l = 2; size_t r = 2; if (a[i][1] == a[i][2]) { l = 1; if (a[i][0] == a[i][1]) l = 0; } if (a[i][3] == a[i][2]) { r = 3; if (a[i][4] == a[i][3]) r = 4; } if (r-l+1 < 3) continue; res += a[i][2] * (r-l+1); for (size_t j = l; j <= r; ++j) a[i][j] = 0; } for (size_t j = 0; j < 5; ++j) { std::queue<int> q; for (size_t i = h; i--;) { if (a[i][j]) q.push(a[i][j]); a[i][j] = 0; } size_t i = h-1; while (!q.empty()) { int t = q.front(); q.pop(); a[i--][j] = t; } } return res; } int testcase_ends() { size_t n; scanf("%zu", &n); if (n == 0) return 1; std::vector<std::vector<int>> a(n, std::vector<int>(5)); for (auto& ai: a) for (auto& aij: ai) scanf("%d", &aij); int res = 0; while (true) { int kieta = nya(a); if (!kieta) break; res += kieta; } printf("%d\n", res); return 0; } int main() { while (!testcase_ends()) {} } ```
### Prompt Please create a solution in java 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 { public static int h, w; public static int[][] map, checkMap; public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { h = sc.nextInt(); if (h == 0) { break; } w = 5; map = new int[h][5]; checkMap = new int[h][5]; for (int i = 0; i < h; i++) { for (int j = 0; j < 5; j++) { map[i][j] = sc.nextInt(); } } System.out.println(point()); } } public static int point() { int p = 0; while (true) { arrayDump(map); for (int i = 0; i < h; i++) { checkLine(i); } arrayDump(checkMap); int res = calc(); drop(); arrayDump(map); p += res; if (res == 0) { break; } } return p; } public static void drop() { for (int i = 0; i < h; i++) { for (int j = h - 2; j >= 0; j--) { for (int k = 0; k < w; k++) { if (map[j + 1][k] == 0) { map[j + 1][k] = map[j][k]; map[j][k] = 0; } } } } } public static int calc() { int p = 0; for (int i = 0; i < map.length; i++) { for (int j = 0; j < map[0].length; j++) { if (checkMap[i][j] == 1) { p += map[i][j]; checkMap[i][j] = 0; map[i][j] = 0; } } } return p; } public static void checkLine(int i) { int chain = 0; int current = 0; for (int j = 0; j < w; j++) { if (map[i][j] != current) { if (chain >= 3) { for (int rj = 0; rj < chain; rj++) { checkMap[i][j - rj - 1] = 1; } } current = map[i][j]; chain = 1; continue; } chain ++; } if (chain >= 3) { for (int rj = 0; rj < chain; rj++) { checkMap[i][w - rj - 1] = 1; } } } public static void arrayDump(int[][] list) { return; // for (int i = 0; i < list.length; i++) { // for (int j = 0; j < list[0].length; j++) { // System.out.print(list[i][j] + ", "); // } // System.out.println(); // } // System.out.println(); } } ```
### 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> #include <cstring> #include <algorithm> #define rep(i,a) for(int i=0;i<(a);++i) #define rrep(i,a) for(int i=(a)-1;i>=0;--i) using namespace std; int main() { int n; while(cin >> n,n){ vector<vector<int> > v(5,vector<int>(n)); rrep(i,n) rep(j,5) cin >> v[j][i]; int sum=0,lsum; do{ lsum = sum; rep(i,n){ bool rm[5]; memset(rm,false,sizeof(rm)); rep(j,3){ if(v[j][i] && v[j][i] == v[j+1][i] && v[j+1][i] == v[j+2][i]){ rm[j]=rm[j+1]=rm[j+2]=true; } } rep(j,5){ if(rm[j]){ sum+=v[j][i]; v[j][i]=0; } } } rep(i,5){ fill(remove(v[i].begin(),v[i].end(),0),v[i].end(),0); } }while(lsum!=sum); 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 mp[10][5]; int main() { while (true) { int h; cin >> h; if (h == 0) break; for (int i = 0; i < 10; i++) for (int j = 0; j < 5; j++) mp[i][j] = 0; for (int i = h - 1; i >= 0; i--) for (int j = 0; j < 5; j++) cin >> mp[i][j]; bool found = true; int score = 0; while (found) { found = false; for (int i = 0; i < 10; i++) { int bg = 0; for (int j = 1; j < 5; j++) { if (mp[i][j] != mp[i][bg]) { if (j - bg >= 3 && mp[i][bg] != 0) { found = true; for (int k = bg; k < j; k++) { score += mp[i][k]; mp[i][k] = 0; } } bg = j; } } if (bg <= 2 && mp[i][bg] != 0) { found = true; for (int k = bg; k < 5; k++) { score += mp[i][k]; mp[i][k] = 0; } } } for (int j = 0; j < 5; j++) { int t[10], p = 0; for (int i = 0; i < 10; i++) t[i] = 0; for (int i = 0; i < 10; i++) if (mp[i][j] != 0) t[p++] = mp[i][j]; for (int i = 0; i < 10; i++) mp[i][j] = t[i]; } } 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<iostream> using namespace std; int main(){ while(1){ int h; cin >> h; if(h==0) break; int pt = 0; int a[h+1][5]; for(int i = 1; i <= h; i++){ for(int j = 0; j < 5; j++){ cin >> a[i][j]; } } bool flg = true; while(flg){ flg = false; for(int i = 1; i <= h; i++){ for(int j = 0; j < 3; j++){ int count = 1; for(int k = j + 1; k < 5; k++){ if(a[i][j] > 0 && a[i][j] == a[i][k]){ count += 1; }else{ break; } } if(count > 2){ flg = true; pt += a[i][j] * count; for(int k = j; k < j + count; k++){ for(int l = i; l > 0; l--){ a[l][k] = a[l - 1][k]; } } break; } } } } cout << pt << 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> #include<string> #include<algorithm> #include<vector> #include<cmath> #include<set> using namespace std; int H,c,ct=0,tmpct=0; vector<int> b; int ren=0; void solve(){ bool roop = true; while(roop){ for(int i=0;i<H*6-2;i++){ if(b[i] == b[i+1] && b[i+1] == b[i+2] && b[i] != 0){ if(ren == 0){tmpct += b[i]*3;ren++; }else{ tmpct += b[i]; ren++; } }else if(ren>0){ for(int j=i-ren;j<i+2;j++) b[j]=0; ren = 0; } } for(int i=0;i<5;i++){ c=0; for(int j=0;j<H;j++) if(b[(H-j-1)*6 + i]==0)c++; else if(c>0){ b[(H-j+c-1)*6 + i] = b[(H-j-1)*6 + i]; b[(H-j-1)*6 + i] = 0; } } /* for(int i=0;i<H;i++){ for(int j=0;j<6;j++){ cout << b[(i)*6 + j] << " "; } cout << "\n"; } cout << "tmpct" << tmpct << "\n"; */ if(tmpct > 0){ct += tmpct; tmpct = 0;} else break; } } int main(){ while(true){ cin >> H; if(H == 0)break; b.resize(H*6); for(int i=0;i<H;i++){ cin >> b[i*6] >> b[i*6+1] >> b[i*6+2] >> b[i*6+3] >> b[i*6+4]; b[i*6+5]=-1; } solve(); b.clear(); cout << ct << endl; ct = 0; } 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> #define REP(i, a, n) for(int i = (a); i < (n); i++) using namespace std; int H, S[10][5], v[10][5]; int main(void) { while(cin >> H, H) { REP(i, 0, H) REP(j, 0, 5) cin >> S[i][j]; int p = 0, q; while(1) { q = 0; REP(i, 0, H) REP(j, 0, 5) v[i][j] = 0; REP(i, 0, H) REP(j, 0, 3) { if(S[i][j] == 0) continue; int d = 1; for(; j + d < 5 && S[i][j + d] == S[i][j]; d++); if(d >= 3) { REP(k, j, j + d) { q += S[i][k]; S[i][k] = 0; } } } if(q == 0) break; p += q; bool b = true; while(b) { b = false; REP(i, 0, H - 1) REP(j, 0, 5) { if(S[i][j] != 0 && S[i + 1][j] == 0) { S[i + 1][j] = S[i][j]; S[i][j] = 0; b = true; } } } } cout << p << endl; } return 0; } ```
### 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 class Game: def __init__(self, mp, height): self.mp = mp self.height = height self.score = 0 self.erase_flag = False def erase(self): for y in range(self.height): line = self.mp[y] new_line, add_score = self.fix(line) self.mp[y] = new_line self.score += add_score if add_score != 0:self.erase_flag = True def fix(self, line): add_score = 0 new_line = line for length in range(5, 2, -1): for left in range(5 - length + 1): if all(map(lambda x:x==line[left], line[left:left+length])): add_score = line[left] * length new_line = line[:left] + [0] * length + line[left+length:] return new_line, add_score return new_line, add_score def fall(self): for x in range(5): col = [] for y in range(self.height): if self.mp[y][x] != 0: col.append(self.mp[y][x]) col = [0] * (self.height - len(col)) + col for y in range(self.height): self.mp[y][x] = col[y] def run(self): while True: self.erase_flag = False self.erase() if not self.erase_flag:break self.fall() def print_state(self): print(*self.mp, sep="\n") print(self.score) while True: h = int(input()) if h == 0:break mp = [list(map(int, input().split())) for _ in range(h)] game = Game(mp, h) game.run() print(game.score) ```
### 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 <queue> #include <string> #include <cstring> #include <map> #include <algorithm> #define REP(i,n) for(int i=0;i<(int)(n);i++) #define ALL(v) v.begin(),v.end() #define pb push_back using namespace std; const int INF = 1000000000; int main() { while(1){ int h; cin>>h; if(!h)break; vector<vector<int>> t(h,vector<int>(5)); REP(i,h)REP(j,5)cin>>t[i][j]; int pt=0; while(1){ bool ch=false; REP(i,h){ int len=1; REP(j,4){ if(t[i][j]==t[i][j+1]){ len++; }else{ if(len >= 3 && t[i][j]!=0){ pt+=len*(t[i][j]); REP(k,len){ t[i][j-k]=0; } ch=true; } len=1; } } if(len >= 3 && t[i][4]!=0){ pt+=len*(t[i][4]); REP(k,len){ t[i][4-k]=0; } ch=true; } } if(!ch)break; REP(i,5){ for(int j=h-1;j>=0;--j){ for(int k=j+1;k<h;++k){ if(t[k][i]==0){ swap(t[k][i],t[k-1][i]); } } } } } cout<<pt<<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; int main(){ int h,ans; int map[10][7]; bool b; while(1){ cin>>h; if(h==0) break; b=true; ans=0; for(int i=0;i<10;i++){ for(int j=0;j<7;j++){ map[i][j]=0; } } for(int i=0;i<h;i++){ for(int j=0;j<5;j++){ cin>>map[i][j]; } } while(b){ /*cout<<endl;*/ b=false; for(int i=0;i<h;i++){ for(int j=0;j<3;j++){ if(map[i][j]==0) continue; if(map[i][j]==map[i][j+1] && map[i][j]==map[i][j+2]){ b=true; if(map[i][j]==map[i][j+3]){ if(map[i][j]==map[i][j+4]){ for(int k=j;k<j+5;k++){ ans+=map[i][k]; map[i][k]=0; } } else for(int k=j;k<j+4;k++){ ans+=map[i][k]; map[i][k]=0; } } else for(int k=j;k<j+3;k++){ ans+=map[i][k]; map[i][k]=0; } } } } for(int i=0;i<h;i++){ for(int j=0;j<5;j++){ if(map[i][j]==0){ for(int k=i;k>=0;k--){ if(k==0) map[k][j]=0; else map[k][j]=map[k-1][j]; } } } } /*for(int i=0;i<h;i++){ for(int j=0;j<5;j++){ cout<<map[i][j]<<" "; } cout<<endl; } cout<<ans<<endl;*/ } cout<<ans<<endl; } return 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 <iostream> #include <string> #include <vector> using namespace std; int main(){ ios_base::sync_with_stdio(false); while(true){ int h, w = 5; cin >> h; if(h == 0){ break; } vector<vector<int>> field(h, vector<int>(w)); for(int i = 0; i < h; ++i){ for(int j = 0; j < w; ++j){ cin >> field[i][j]; } } int score = 0; while(true){ bool modified = false; for(int i = 0; i < h; ++i){ for(int j = 0; j < w; ++j){ if(field[i][j] < 0){ continue; } int k = j; while(k < w && field[i][j] == field[i][k]){ ++k; } if(k - j < 3){ continue; } for(--k; k >= j; --k){ score += field[i][k]; field[i][k] = -1; modified = true; } } } if(!modified){ break; } for(int j = 0; j < w; ++j){ int k = h - 1; for(int i = h - 1; i >= 0; --i){ if(field[i][j] >= 0){ field[k--][j] = field[i][j]; } } for(; k >= 0; --k){ field[k][j] = -1; } } } 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<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 < 100; foo++){ for(int q = 0; q < 30; 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 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.Arrays; import java.util.Scanner; public class Main { public static final int LINES = 5; public static void main(String[] args) { final Scanner sc = new Scanner(System.in); while (true) { final int H = sc.nextInt(); if(H == 0){ break; } int[][] map = new int[H][LINES]; for(int i = H - 1; i >= 0; i--){ for(int j = 0; j < LINES; j++){ final int val = sc.nextInt(); map[i][j] = val; } } int score = 0; while(true){ boolean updated = false; /* System.out.println("----------------------------------------"); for(int i = 0; i < H; i++){ for(int j = 0; j < LINES; j++){ System.out.print(map[i][j] + " "); } System.out.println(); } */ for(int i = 0; i < H; i++){ for(int j = 0; j < LINES; j++){ if(map[i][j] == 0){ continue; } int end = j + 1; while(end < LINES && map[i][end] == map[i][j]){ end++; } final int size = end - j; //System.out.println(i + " " + j + " " + end + " " + size); if(size >= 3){ for(int k = j; k < end; k++){ score += map[i][k]; map[i][k] = 0; updated = true; } } } } if(!updated){ break; } for(int i = 0; i < H; i++){ for(int j = 0; j < LINES; j++){ if(map[i][j] == 0){ boolean ok = false; for(int k = i + 1; k < H; k++){ if(map[k][j] != 0){ ok = true; break; } } if(!ok){ break; } for(int k = i + 1; k < H; k++){ map[k - 1][j] = map[k][j]; } map[H - 1][j] = 0; j--; } } } } System.out.println(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.Scanner; public class Main{ static Scanner s = new Scanner(System.in); static int score; static int[][] puzzle; public static void main(String[] args) { while (true){ int H = s.nextInt(); if(H==0)break; score=0; puzzle = new int[H][5]; for(int i=0;i<H;i++){ for(int j=0;j<5;j++){ puzzle[i][j]=s.nextInt(); } } while (true) { boolean flag = vanish(); if(!flag) break; move(); } System.out.println(score); } } static boolean vanish(){ boolean flag=false; for(int i=0;i<puzzle.length;i++){ int nowNum=puzzle[i][0]; int count=1; for(int j=1;j<5;j++){ if(nowNum!=0 && nowNum==puzzle[i][j]){ count++; } else{ if(count>=3){ puzzle[i]=erase(puzzle[i],j-1,count,nowNum); flag=true; break; }else{ count=1; nowNum=puzzle[i][j]; } } if(j==4 && count>=3){ puzzle[i]=erase(puzzle[i],j,count,nowNum); flag=true; } } } return flag; } static int[] erase(int[] puzzleLine,int num,int count,int point){ for(int i=num;i>num-count;i--){ puzzleLine[i]=0; } score+=point*count; return puzzleLine; } static void move(){ for(int j=0;j<5;j++){ int temp=puzzle.length-1; for(int i=puzzle.length-1;i>=0;i--){ if(puzzle[i][j]!=0){ puzzle[temp][j]=puzzle[i][j]; temp--; } } for(int i=temp;i>=0;i--){ puzzle[i][j]=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> #include <cstring> using namespace std; #define H 10 int map[10][5]; int calc_and_remove() { int res = [&]{ int res = 0; for(int i = 0; i < H; i++) { for(int j = 0; j < 5; j++) { int left = j; int right = left; int prev = map[i][left]; while(right < 5 && map[i][right] && map[i][right] == prev) right++; if (right - left >= 3) { for(int k = left; k < right; k++) { res += map[i][k]; map[i][k] = 0; } } } } return res; }(); for(int i = H-1; i >= 0; i--) { for(int j = 0; j < 5; j++) { int cnt = 0; while (map[i][j] == 0 && cnt < 5) { for(int k = i; k >= 0; k--) { if (k-1 == -1) { map[k][j] = 0; } else { map[k][j] = map[k-1][j]; } } cnt ++; } } } return res; } int main() { int h; while(cin >> h, h) { memset(map, 0, H*5*sizeof(int)); for(int i = 0; i < h; i++) for(int j = 0; j < 5; j++) cin >> map[i+(10-h)][j]; int res = 0; for(int k = 0; k < 20; k++) { res += calc_and_remove(); } cout << res << endl; } 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 // Chain Disappearance Puzzle import java.util.Arrays; import java.util.Scanner; public class Main { static int[][] field; static int h; public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (true) { h = sc.nextInt(); if (h == 0) break; field = new int[h][5]; for (int i = 0; i < h; i++) for (int j = 0; j < 5; j++) field[i][j] = sc.nextInt(); System.out.println(disappear()); } sc.close(); } static int disappear() { int result = 0; boolean[][] bfield = new boolean[h][5]; boolean canDelete = true; while (canDelete) { canDelete = false; for (int i = 0; i < h; i++) { Arrays.fill(bfield[i], false); for (int j = 0; j < 3; j++) { if (field[i][j] != 0 && field[i][j] == field[i][j + 1] && field[i][j] == field[i][j + 2]) { bfield[i][j] = true; bfield[i][j + 1] = true; bfield[i][j + 2] = true; canDelete = true; } } for (int j = 0; j < 5; j++) if (bfield[i][j]) { result += field[i][j]; field[i][j] = 0; } } for (int i = 0; i < 5; i++) { for (int t = h; t > 0; t--) { for (int j = 1; j < t; j++) { if (field[j][i] == 0) { int temp = field[j][i]; field[j][i] = field[j - 1][i]; field[j - 1][i] = temp; } } if (field[t - 1][i] == 0) break; } } } return result; } } ```
### 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() { int n; while (scanf("%d", &n) && n){ int blk[16][8]; int ch[16][8]; for (int i = 0; i < n; i++){ for (int j = 0; j < 5; j++){ scanf("%d", &blk[i][j]); } } int tot = 0; bool upd; do { upd = 0; memset(ch, 0, sizeof(ch)); for (int i = 0; i < n; i++){ for (int j = 0; j < 3; j++){ if (!blk[i][j]) continue; if (blk[i][j] == blk[i][j + 1] && blk[i][j] == blk[i][j + 2]){ ch[i][j] = ch[i][j + 1] = ch[i][j + 2] = 1; upd = true; } } } int btm[8] = {0}; for (int i = n - 1; i >= 0; i--){ for (int j = 0; j < 5; j++){ tot += blk[i][j] * ch[i][j]; if (!ch[i][j]) blk[n - ++btm[j]][j] = blk[i][j]; } } for (int i = 0; i < 5; i++){ for (int j = 0; j < n - btm[i]; j++) blk[j][i] = 0; } } while (upd); printf("%d\n", tot); } 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 <cstdio> #include <cstring> #include <climits> using namespace std; int h,map[10][5]; int main(){ while(scanf("%d",&h),h){ int ans=0; for(int i=0;i<10;i++) for(int j=0;j<5;j++) if(i<h) scanf("%d",&map[h-i-1][j]); else map[i][j]=0; bool boo=true; while(boo){ boo=false; for(int z=0;z<10;z++){ for(int i=0;i<h;i++){ for(int j=0;j<5;j++) if(map[i][j]==0){ for(int k=i+1;k<h;k++){ map[k-1][j]=map[k][j]; map[k][j]=0; } } } } /* for(int i=0;i<h;i++){ for(int j=0;j<5;j++){ printf("%d ",map[i][j]); } printf("\n"); } printf("\n"); */ for(int i=0;i<h;i++){ int a=-1,b=0,t=0; for(int j=0;j<5;j++){ if(map[i][j]==a){ t++; }else if(t<3){ a=map[i][j]; b=j; t=1; }else break; } if(t>=3){ if(a==0)continue; boo=true; ans+=a*t; //printf("%d %d %d\n%d\n",a,b,t,ans); for(int j=0;j<t;j++) map[i][b+j]=0; } } } printf("%d\n",ans); } 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<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 dela[H][W]; void calc_del(){ rep(i,H)rep(j,W)dela[i][j]=0; rep(i,h){ rep(j,W-2){ int num = board[i][j]; bool hit = true; rep(k,3){ if(board[i][j+k]!=num)hit=false; } if(hit){ rep(k,3)dela[i][j+k]=1; } } } } int del(){ int ans = 0; rep(i,H){ rep(j,W){ if(dela[i][j]==1){ ans += board[i][j]; board[i][j]=0; } } } return ans; } void tume(){ rep(i,h){ for(int i=h-1;i>=1;i--){ rep(j,W){ if(board[i][j]==0){ swap(board[i][j], board[i-1][j]); } } } } } void print(){ rep(i,h){ rep(j,W){ printf("%d ",board[i][j]); }puts(""); } } void solve(){ int ans= 0; while(1){ //print();puts(""); calc_del(); int val = del(); ans += val; if(val==0)break; tume(); } cout<<ans<<endl; } int main(){ while(input())solve(); } ```
### 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); while(true){ int h = sc.nextInt(); if(h==0) break; Integer[][] map = new Integer[5][h]; for(int i=0; i<h; i++){ for(int j=0; j<5; j++){ map[j][i] = sc.nextInt(); } } System.out.println(chainDisappearance(map)); } } public static int chainDisappearance(Integer[][] map){ int score = 0; boolean flag = false; for(int i=map[0].length-1; i>=0; i--){ if(map[2][i]==0) break; if(map[2][i]==map[1][i]){ if(map[2][i]==map[3][i]){ score += map[2][i]*3; flag = true; if(map[2][i]==map[0][i]){ score += map[2][i]; map[0][i] = 0; } if(map[2][i]==map[4][i]){ score += map[2][i]; map[4][i] = 0; } map[1][i] = 0; map[2][i] = 0; map[3][i] = 0; }else{ if(map[2][i]==map[0][i]){ score += map[2][i]*3; map[2][i] = 0; map[1][i] = 0; map[0][i] = 0; flag = true; } } }else{ if(map[2][i]==map[3][i] && map[2][i]==map[4][i]){ score += map[2][i]*3; map[2][i] = 0; map[3][i] = 0; map[4][i] = 0; flag = true; } } } if(flag){ for(int i=0; i<5; i++){ Arrays.sort(map[i], new Comparator<Integer>(){ public int compare(Integer a, Integer b){ int temp = 0; if(a==0 && b>0) temp = -1; return temp; } }); } score += chainDisappearance(map); } return score; } } ```
### 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<string> #include<cstring> #include<algorithm> #define rep(i,n) for(int i=0;i<(n);i++) using namespace std; int field[10][6]; int height[5]; int H; int del(){ int score=0; rep(y,H){ int seq=0; int prev=0; rep(x,6){ if(prev==field[y][x]){ seq++; }else{ if(3<=seq){ for(int i=1;i<=seq;i++){ score+=field[y][x-i]; field[y][x-i]=0; } } seq=1; prev=field[y][x]; } } } return score; } void fall(){ memset(height,0,sizeof(height)); rep(y,H){ rep(x,5){ if(field[y][x]==0)continue; if(y!=height[x]){ field[height[x]][x]=field[y][x]; field[y][x]=0; } height[x]++; } } } int main(){ while(cin>>H&&H){ for(int y=H-1;y>=0;y--) for(int x=0;x<5;x++)cin>>field[y][x]; int ans=0,t; while(t=del()){ ans+=t; fall(); } cout<<ans<<endl; } return 0; } ```
### 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 solve(): cnt = -1 while 1: cnt += 1 H = int(input()) if H==0: break puzzle = [list(map(int, input().split())) for _ in range(H)] ans = 0 while True: flg = True for i, row in enumerate(puzzle): if len(set(row))>=4: continue streak = [-1, 1] for j, c in enumerate(row): if c==-1: streak[0] = -1 streak[1] = 1 continue if c==streak[0]: streak[1]+=1 if streak[1]==3: flg = False ans += c*3 puzzle[i][j-2:j+1] = [-1]*3 elif streak[1]>=4: ans += c puzzle[i][j] = -1 else: streak[0] = c streak[1] = 1 for i, row in enumerate(puzzle[::-1]): if i==H-1: break for j, c in enumerate(row): if c==-1: for h in range(H-i-1): if puzzle[(H-i-1)-h-1][j]!=-1: puzzle[-i-1][j] = puzzle[(H-i-1)-h-1][j] puzzle[(H-i-1)-h-1][j] = -1 break if flg: print(ans) break solve() ```
### 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.FileInputStream; import java.io.PrintStream; import java.util.ArrayList; import java.util.Scanner; public class Main { static int h; static int[][] map; private static int calc() { int score = 0; // 消す for(int y = 0;y < h;y++) { // 消えるのは6通りしか無いので列挙 if(map[y][0] == map[y][1] && map[y][1] == map[y][2] && map[y][2] == map[y][3] && map[y][3] == map[y][4]) { score += map[y][0] * 5; map[y][0] = map[y][1] = map[y][2] = map[y][3] = map[y][4] = 0; } else if(map[y][0] == map[y][1] && map[y][1] == map[y][2] && map[y][2] == map[y][3]) { score += map[y][0] * 4; map[y][0] = map[y][1] = map[y][2] = map[y][3] = 0; } else if(map[y][1] == map[y][2] && map[y][2] == map[y][3] && map[y][3] == map[y][4]) { score += map[y][1] * 4; map[y][1] = map[y][2] = map[y][3] = map[y][4] = 0; } else if(map[y][0] == map[y][1] && map[y][1] == map[y][2]) { score += map[y][0] * 3; map[y][0] = map[y][1] = map[y][2] = 0; } else if(map[y][1] == map[y][2] && map[y][2] == map[y][3]) { score += map[y][1] * 3; map[y][1] = map[y][2] = map[y][3] = 0; } else if(map[y][2] == map[y][3] && map[y][3] == map[y][4]) { score += map[y][2] * 3; map[y][2] = map[y][3] = map[y][4] = 0; } } // 落とす ArrayList<Integer> stock; for(int x = 0;x < 5;x++) { stock = new ArrayList<Integer>(); for(int y = 0;y < h;y++) { if(map[y][x] != 0) stock.add(new Integer(map[y][x])); } for(int y = 0;y < h;y++) { if(y < stock.size()) map[y][x] = stock.get(y).intValue(); else map[y][x] = 0; } } return score; } private static void start() { int score = 0; while(true) { int dscore = 0; dscore = calc(); if(dscore == 0) break; score += dscore; } System.out.println(score); } public static void main(String[] args) { Scanner sca = new Scanner(System.in); while(true) { h = sca.nextInt(); if(h == 0) break; map = new int[h][5]; for(int y = h-1;y >= 0;y--) for(int x = 0;x < 5;x++) map[y][x] = sca.nextInt(); start(); } } } ```
### 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 # -*- coding: utf-8 -*- from collections import Counter def inpl(): return list(map(int, input().split())) N = int(input()) while N: B = [inpl() for _ in range(N)] ans = -1 tmp = 1 while tmp: ans += tmp tmp = 0 erase = [set() for _ in range(5)] for i in range(N): for j in range(1, 4): if B[i][j-1] == B[i][j] == B[i][j+1]: for k in range(j-1, j+2): erase[k].add(i) B2 = list(map(list, zip(*B))) for j in range(5): for i in sorted(erase[j], reverse=True): tmp += B[i][j] del B2[j][i] B2[j] = [0]*len(erase[j]) + B2[j] B = list(map(list, zip(*B2))) print(ans) N = int(input()) ```
### 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; #define HMAX 10 int main(){ int H; int puzz[HMAX][5]; bool lf,ff; int cnt,bef; int score; while(1){ cin>>H; if(H==0) break; for(int i=0;i<H;i++){ for(int j=0;j<5;j++){ cin>>puzz[i][j]; } } score=0; while(1){ lf=false; for(int i=0;i<H;i++){ bef=puzz[i][0]; cnt=1; for(int j=1;j<6;j++){ if(j!=5&&bef==puzz[i][j]) cnt++; else if(cnt<3||bef==-1){ cnt=1; bef=puzz[i][j]; } else{ for(int k=j-cnt;k<j;k++){ puzz[i][k]=-1; } score+=bef*cnt; lf=true; break; } } } if(!lf) break; while(1){ ff=false; for(int i=0;i<H-1;i++){ for(int j=0;j<5;j++){ if(puzz[i+1][j]==-1&&puzz[i][j]!=-1){ puzz[i+1][j]=puzz[i][j]; puzz[i][j]=-1; ff=true; } } } if(!ff) break; } } cout<<score<<endl; } } ```
### 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 def fall(S,H): for h in range(H-1): for i in range(5): if S[h][i]=="0": S[h][i] = S[h+1][i] S[h+1][i] = "0" return S while True: H = int(input()) if H==0: break S = [list(input().split()) for i in range(H)] S = S[::-1] ans = 0 k = 1 while k!=0: k = 0 for h in range(H): s = S[h] for l in range(3): for r in range(5,2+l,-1): if s[l]=="0": continue if s[l:r].count(s[l])==(r-l): k = 1 ans += int(s[l])*(r-l) S[h][l:r] = ["0"]*(r-l) for i in range(H): S = fall(S,H) print(ans) ```
### Prompt Create a solution in Python 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 ```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 j in range(H+1)] 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][0] += 1 tmpBoard[i-1-h][tmpBoard[i-1-h][0]] = Board[i-1-h][j] con_num = 1 total_score += tmp_score Board = tmpBoard print total_score ```
### 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; int main() { int h; while (scanf("%d", &h), h){ vector<vector<int> > v; v.resize(h); for (int i = 0; i < h; i++){ v[i].resize(5); } for (int i = 0; i < h; i++){ for (int j = 0; j < 5; j++){ scanf("%d", &v[i][j]); } } int sum = 0; bool nico = true; while (nico){ nico = false; for (int i = 0; i < h; i++){ int flag = 0; int pos = 0; for (int j = 1; j < 5; j++){ if (v[i][j - 1] == v[i][j] && v[i][j] != 0){ flag++; } else if (flag > 1){ break; } else { flag = 0; pos = j; } } if (flag > 1){ for (int j = pos; j <= pos + flag; j++){ sum += v[i][j]; v[i][j] = 0; } nico = true; } } for (int i = h - 1; i > 0; i--){ for (int j = 0; j < 5; j++){ if (v[i][j] == 0){ int k = i - 1; while (v[k][j] == 0 && k > 0){ k--; } v[i][j] = v[k][j]; v[k][j] = 0; } } } } printf("%d\n", sum); } 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> #include <vector> using namespace std; int main(){ int H, h, b, e; while (cin >> H, H){ int C[H][5], ans = 0; for (int i = 0; i < H; ++i){ for (int j = 0; j < 5; ++j){ cin >> C[H-1-i][j]; } } bool f = true; vector< vector<int> > V; while (f){ f = false; for (int i = 0; i < H; ++i){ b = 0; e = 0; for (int j = 1; j < 5; ++j){ if (C[i][j] && C[i][j-1] == C[i][j]){ e = j; }else{ if (e - b > 1){ f = true; V.push_back({i,b,e}); } b = j; e = j; } } if (e - b > 1){ f = true; V.push_back({i,b,e}); } } while (!V.empty()){ h = V.back()[0]; b = V.back()[1]; e = V.back()[2]; V.pop_back(); ans += C[h][b]*(e-b+1); for (int i = h; i < H; ++i){ for (int j = b; j <= e; ++j){ if (i+1 < H){ C[i][j] = C[i+1][j]; }else{ C[i][j] = 0; } } } } } cout << ans << endl; } return 0; } ```