message
stringlengths
2
19.9k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
322
108k
cluster
float64
15
15
__index_level_0__
int64
644
217k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Valera has a strip infinite in both directions and consisting of cells. The cells are numbered by integers. The cell number 0 has a robot. The robot has instructions — the sequence of moves that he must perform. In one move, the robot moves one cell to the left or one cell to the right, according to instructions. Before the robot starts moving, Valera puts obstacles in some cells of the strip, excluding cell number 0. If the robot should go into the cell with an obstacle according the instructions, it will skip this move. Also Valera indicates the finish cell in which the robot has to be after completing the entire instructions. The finishing cell should be different from the starting one. It is believed that the robot completed the instructions successfully, if during the process of moving he visited the finish cell exactly once — at its last move. Moreover, the latter move cannot be skipped. Let's assume that k is the minimum number of obstacles that Valera must put to make the robot able to complete the entire sequence of instructions successfully and end up in some finishing cell. You need to calculate in how many ways Valera can choose k obstacles and the finishing cell so that the robot is able to complete the instructions successfully. Input The first line contains a sequence of characters without spaces s1s2... sn (1 ≤ n ≤ 106), consisting only of letters "L" and "R". If character si equals "L", then the robot on the i-th move must try to move one cell to the left. If the si-th character equals "R", then the robot on the i-th move must try to move one cell to the right. Output Print a single integer — the required number of ways. It's guaranteed that this number fits into 64-bit signed integer type. Examples Input RR Output 1 Input RRL Output 1 Note In the first sample Valera mustn't add any obstacles and his finishing cell must be cell 2. In the second sample, Valera must add an obstacle in cell number 1, and his finishing cell must be cell number - 1. In this case robot skips the first two moves and on the third move he goes straight from the starting cell to the finishing one. But if Valera doesn't add any obstacles, or adds an obstacle to another cell, then the robot visits the finishing cell more than once. Submitted Solution: ``` def isBounded(sequence, bound): height = 0 minHeight = 0 for c in sequence: height = min(bound, height + c) minHeight = min(height, minHeight) return height <= minHeight s = input() d = {"L": -1, "R": 1} if s[-1] == "R": d = {"R": -1, "L": 1} s = list(map(lambda x: d[x], s[:-1])) if isBounded(s, float("inf")): # I don't have to do nothing print(1) elif not isBounded(s, 0): # I can't do anything print(0) else: fr = 0 to = len(s) while fr + 1 < to: mid = (fr + to) >> 1 if isBounded(s, mid): fr = mid else: to = mid - 1 print(fr + 1) ```
instruction
0
105,598
15
211,196
No
output
1
105,598
15
211,197
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Valera has a strip infinite in both directions and consisting of cells. The cells are numbered by integers. The cell number 0 has a robot. The robot has instructions — the sequence of moves that he must perform. In one move, the robot moves one cell to the left or one cell to the right, according to instructions. Before the robot starts moving, Valera puts obstacles in some cells of the strip, excluding cell number 0. If the robot should go into the cell with an obstacle according the instructions, it will skip this move. Also Valera indicates the finish cell in which the robot has to be after completing the entire instructions. The finishing cell should be different from the starting one. It is believed that the robot completed the instructions successfully, if during the process of moving he visited the finish cell exactly once — at its last move. Moreover, the latter move cannot be skipped. Let's assume that k is the minimum number of obstacles that Valera must put to make the robot able to complete the entire sequence of instructions successfully and end up in some finishing cell. You need to calculate in how many ways Valera can choose k obstacles and the finishing cell so that the robot is able to complete the instructions successfully. Input The first line contains a sequence of characters without spaces s1s2... sn (1 ≤ n ≤ 106), consisting only of letters "L" and "R". If character si equals "L", then the robot on the i-th move must try to move one cell to the left. If the si-th character equals "R", then the robot on the i-th move must try to move one cell to the right. Output Print a single integer — the required number of ways. It's guaranteed that this number fits into 64-bit signed integer type. Examples Input RR Output 1 Input RRL Output 1 Note In the first sample Valera mustn't add any obstacles and his finishing cell must be cell 2. In the second sample, Valera must add an obstacle in cell number 1, and his finishing cell must be cell number - 1. In this case robot skips the first two moves and on the third move he goes straight from the starting cell to the finishing one. But if Valera doesn't add any obstacles, or adds an obstacle to another cell, then the robot visits the finishing cell more than once. Submitted Solution: ``` def isBounded(bound): global sequence height = 0 minHeight = 0 for c in sequence: height = min(bound, height + c) minHeight = min(height, minHeight) return height <= minHeight sequence = input() length = len(sequence) - 1 d = {"L": -1, "R": 1} if sequence[-1] == "R": d = {"R": -1, "L": 1} sequence = map(lambda x: d[x], sequence[:-1]) if isBounded(float("inf")): # I don't have to do nothing print(1) elif not isBounded(0): # I can't do anything print(0) else: fr = 0 to = length while fr + 1 < to: mid = (fr + to) >> 1 if isBounded(mid): fr = mid else: to = mid print(fr + 1) ```
instruction
0
105,599
15
211,198
No
output
1
105,599
15
211,199
Provide tags and a correct Python 3 solution for this coding contest problem. You play a computer game. Your character stands on some level of a multilevel ice cave. In order to move on forward, you need to descend one level lower and the only way to do this is to fall through the ice. The level of the cave where you are is a rectangular square grid of n rows and m columns. Each cell consists either from intact or from cracked ice. From each cell you can move to cells that are side-adjacent with yours (due to some limitations of the game engine you cannot make jumps on the same place, i.e. jump from a cell to itself). If you move to the cell with cracked ice, then your character falls down through it and if you move to the cell with intact ice, then the ice on this cell becomes cracked. Let's number the rows with integers from 1 to n from top to bottom and the columns with integers from 1 to m from left to right. Let's denote a cell on the intersection of the r-th row and the c-th column as (r, c). You are staying in the cell (r1, c1) and this cell is cracked because you've just fallen here from a higher level. You need to fall down through the cell (r2, c2) since the exit to the next level is there. Can you do this? Input The first line contains two integers, n and m (1 ≤ n, m ≤ 500) — the number of rows and columns in the cave description. Each of the next n lines describes the initial state of the level of the cave, each line consists of m characters "." (that is, intact ice) and "X" (cracked ice). The next line contains two integers, r1 and c1 (1 ≤ r1 ≤ n, 1 ≤ c1 ≤ m) — your initial coordinates. It is guaranteed that the description of the cave contains character 'X' in cell (r1, c1), that is, the ice on the starting cell is initially cracked. The next line contains two integers r2 and c2 (1 ≤ r2 ≤ n, 1 ≤ c2 ≤ m) — the coordinates of the cell through which you need to fall. The final cell may coincide with the starting one. Output If you can reach the destination, print 'YES', otherwise print 'NO'. Examples Input 4 6 X...XX ...XX. .X..X. ...... 1 6 2 2 Output YES Input 5 4 .X.. ...X X.X. .... .XX. 5 3 1 1 Output NO Input 4 7 ..X.XX. .XX..X. X...X.. X...... 2 2 1 6 Output YES Note In the first sample test one possible path is: <image> After the first visit of cell (2, 2) the ice on it cracks and when you step there for the second time, your character falls through the ice as intended.
instruction
0
106,590
15
213,180
Tags: dfs and similar Correct Solution: ``` n,m = map(int,input().split()) L = [input() for i in range(n)] Visited = [[0] * m for i in range(n)] start = list(map(int,input().split())) end = list(map(int,input().split())) if abs(end[0] - start[0]) + abs(end[1] - start[1]) == 1 : if (L[end[0] - 1][end[1] - 1] == "X") : print("YES") else : k = 0 if end[0] != 1 : if L[end[0] - 2][end[1] - 1] == "." : k+=1 if end[1] != 1 : if L[end[0] - 1][end[1] - 2] == "." : k+=1 if end[0] != n : if L[end[0]][end[1] - 1] == "." : k+=1 if end[1] != m : if L[end[0] - 1][end[1]] == "." : k+=1 if k >= 1 : print("YES") else : print("NO") exit() def Do(i,j) : q = [[i,j]] f = False while q : d = q[0] if d[0] != 0 : if Visited[d[0] - 1][d[1]] == 0 and L[d[0] - 1][d[1]] == "." : Visited[d[0] - 1][d[1]] = 1 q.append([d[0] - 1,d[1]]) if d[0] != n - 1 : if Visited[d[0] + 1][d[1]] == 0 and L[d[0] + 1][d[1]] == "." : Visited[d[0] + 1][d[1]] = 1 q.append([d[0] + 1,d[1]]) if d[1] != 0 : if Visited[d[0]][d[1] - 1] == 0 and L[d[0]][d[1] - 1] == "." : Visited[d[0]][d[1] - 1] = 1 q.append([d[0],d[1] - 1]) if d[1] != m - 1 : if Visited[d[0]][d[1] + 1] == 0 and L[d[0]][d[1] + 1] == "." : Visited[d[0]][d[1] + 1] = 1 q.append([d[0],d[1] + 1]) del q[0] Do(start[0] - 1,start[1] - 1) if end[0] == start[0] and start[1] == end[1] : Visited[end[0] - 1][end[1] - 1] = 0 if (Visited[end[0] - 1][end[1] - 1]) == 1 : k = 0 if end[0] != 1 : if L[end[0] - 2][end[1] - 1] == "." : k+=1 if end[1] != 1 : if L[end[0] - 1][end[1] - 2] == "." : k+=1 if end[0] != n : if L[end[0]][end[1] - 1] == "." : k+=1 if end[1] != m : if L[end[0] - 1][end[1]] == "." : k+=1 if k >= 2 : print("YES") else : print("NO") else : if L[end[0] - 1][end[1] - 1] == "X" : k = 0 f = False if end[0] != 1 : if L[end[0] - 2][end[1] - 1] == "." : k+=1 if Visited[end[0] - 2][end[1] - 1] == 1 : f = True if end[1] != 1 : if L[end[0] - 1][end[1] - 2] == "." : k+=1 if Visited[end[0] - 1][end[1] - 2] == 1 : f = True if end[0] != n : if L[end[0]][end[1] - 1] == "." : k+=1 if Visited[end[0]][end[1] - 1] == 1 : f = True if end[1] != m : if L[end[0] - 1][end[1]] == "." : k+=1 if Visited[end[0] - 1][end[1]] == 1 : f = True if (k >= 1 and f) : print("YES") else : print("NO") else : print("NO") ```
output
1
106,590
15
213,181
Provide tags and a correct Python 3 solution for this coding contest problem. You play a computer game. Your character stands on some level of a multilevel ice cave. In order to move on forward, you need to descend one level lower and the only way to do this is to fall through the ice. The level of the cave where you are is a rectangular square grid of n rows and m columns. Each cell consists either from intact or from cracked ice. From each cell you can move to cells that are side-adjacent with yours (due to some limitations of the game engine you cannot make jumps on the same place, i.e. jump from a cell to itself). If you move to the cell with cracked ice, then your character falls down through it and if you move to the cell with intact ice, then the ice on this cell becomes cracked. Let's number the rows with integers from 1 to n from top to bottom and the columns with integers from 1 to m from left to right. Let's denote a cell on the intersection of the r-th row and the c-th column as (r, c). You are staying in the cell (r1, c1) and this cell is cracked because you've just fallen here from a higher level. You need to fall down through the cell (r2, c2) since the exit to the next level is there. Can you do this? Input The first line contains two integers, n and m (1 ≤ n, m ≤ 500) — the number of rows and columns in the cave description. Each of the next n lines describes the initial state of the level of the cave, each line consists of m characters "." (that is, intact ice) and "X" (cracked ice). The next line contains two integers, r1 and c1 (1 ≤ r1 ≤ n, 1 ≤ c1 ≤ m) — your initial coordinates. It is guaranteed that the description of the cave contains character 'X' in cell (r1, c1), that is, the ice on the starting cell is initially cracked. The next line contains two integers r2 and c2 (1 ≤ r2 ≤ n, 1 ≤ c2 ≤ m) — the coordinates of the cell through which you need to fall. The final cell may coincide with the starting one. Output If you can reach the destination, print 'YES', otherwise print 'NO'. Examples Input 4 6 X...XX ...XX. .X..X. ...... 1 6 2 2 Output YES Input 5 4 .X.. ...X X.X. .... .XX. 5 3 1 1 Output NO Input 4 7 ..X.XX. .XX..X. X...X.. X...... 2 2 1 6 Output YES Note In the first sample test one possible path is: <image> After the first visit of cell (2, 2) the ice on it cracks and when you step there for the second time, your character falls through the ice as intended.
instruction
0
106,591
15
213,182
Tags: dfs and similar Correct Solution: ``` #file = open('test.txt', 'r') def recurse(): global ans qu = [] qu.append((r1, c1)) count = 1 i = 0 while i < count: r, c = qu[i] if r == r2 and c == c2: ans = True return mas[r][c] = 1 if mas[r + 1][c] == 0: #recurse(r + 1, c) qu.append((r + 1, c)) mas[r + 1][c] = 1 count += 1 if mas[r - 1][c] == 0: #recurse(r - 1, c) qu.append((r - 1, c)) mas[r - 1][c] = 1 count += 1 if mas[r][c + 1] == 0: #recurse(r, c + 1) qu.append((r, c + 1)) mas[r][c + 1] = 1 count += 1 if mas[r][c - 1] == 0: #recurse(r, c - 1) qu.append((r, c - 1)) mas[r][c - 1] = 1 count += 1 i += 1 n, m = [int(x) for x in input().split()] #n, m = [int(x) for x in file.readline().split()] mas = [] mas.append([-1] * (m + 2)) for i in range(1, n + 1): mas.append([0] * (m + 2)) mas.append([-1] * (m + 2)) for i in range(1, n + 1): mas[i][0] = mas[i][m + 1] = -1 for i in range(1, n + 1): s = input() #s = file.readline() for j in range(1, m + 1): if s[j - 1] == 'X': mas[i][j] = -1 r1, c1 = [int(x) for x in input().split()] r2, c2 = [int(x) for x in input().split()] #r1, c1 = [int(x) for x in file.readline().split()] #r2, c2 = [int(x) for x in file.readline().split()] her = 0 if mas[r2 - 1][c2] != -1: her += 1 if mas[r2 + 1][c2] != -1: her += 1 if mas[r2][c2 - 1] != -1: her += 1 if mas[r2][c2 + 1] != -1: her += 1 if r1 == r2 and c1 == c2: if her > 0: print("YES") else: print("NO") elif abs(r1 - r2) + abs(c1 - c2) == 1: if mas[r2][c2] != 0 or her > 0: print("YES") else: print("NO") else: ans = False z = mas[r2][c2] mas[r1][c1] = mas[r2][c2] = 0 recurse() #mas[r2][c2] = z if not ans: print("NO") elif z != 0: print("YES") else: if her > 1: print("YES") else: print("NO") ```
output
1
106,591
15
213,183
Provide tags and a correct Python 3 solution for this coding contest problem. You play a computer game. Your character stands on some level of a multilevel ice cave. In order to move on forward, you need to descend one level lower and the only way to do this is to fall through the ice. The level of the cave where you are is a rectangular square grid of n rows and m columns. Each cell consists either from intact or from cracked ice. From each cell you can move to cells that are side-adjacent with yours (due to some limitations of the game engine you cannot make jumps on the same place, i.e. jump from a cell to itself). If you move to the cell with cracked ice, then your character falls down through it and if you move to the cell with intact ice, then the ice on this cell becomes cracked. Let's number the rows with integers from 1 to n from top to bottom and the columns with integers from 1 to m from left to right. Let's denote a cell on the intersection of the r-th row and the c-th column as (r, c). You are staying in the cell (r1, c1) and this cell is cracked because you've just fallen here from a higher level. You need to fall down through the cell (r2, c2) since the exit to the next level is there. Can you do this? Input The first line contains two integers, n and m (1 ≤ n, m ≤ 500) — the number of rows and columns in the cave description. Each of the next n lines describes the initial state of the level of the cave, each line consists of m characters "." (that is, intact ice) and "X" (cracked ice). The next line contains two integers, r1 and c1 (1 ≤ r1 ≤ n, 1 ≤ c1 ≤ m) — your initial coordinates. It is guaranteed that the description of the cave contains character 'X' in cell (r1, c1), that is, the ice on the starting cell is initially cracked. The next line contains two integers r2 and c2 (1 ≤ r2 ≤ n, 1 ≤ c2 ≤ m) — the coordinates of the cell through which you need to fall. The final cell may coincide with the starting one. Output If you can reach the destination, print 'YES', otherwise print 'NO'. Examples Input 4 6 X...XX ...XX. .X..X. ...... 1 6 2 2 Output YES Input 5 4 .X.. ...X X.X. .... .XX. 5 3 1 1 Output NO Input 4 7 ..X.XX. .XX..X. X...X.. X...... 2 2 1 6 Output YES Note In the first sample test one possible path is: <image> After the first visit of cell (2, 2) the ice on it cracks and when you step there for the second time, your character falls through the ice as intended.
instruction
0
106,592
15
213,184
Tags: dfs and similar Correct Solution: ``` n,m = map(int,input().split()) d = {0:{i:'X' for i in range(m+2)}} for i in range(1,n+1): s = input() d[i]={0:'X'} for j in range(1,m+1): d[i][j]=s[j-1] d[i][m+1]='X' d[n+1]={i:'X' for i in range(m+2)} r1,c1 = map(int,input().split()) r2,c2 = map(int,input().split()) e,p=0,0 if d[r2][c2]=='X': e=1 d[r2][c2]='.' t1,t2=d[r2-1][c2],d[r2+1][c2] t3,t4=d[r2][c2-1],d[r2][c2+1] if t1=='.': p+=1 if t2=='.': p+=1 if t3=='.': p+=1 if t4=='.': p+=1 if r1==r2 and c1==c2: if p==0: print('NO') else: print('YES') from sys import exit exit() queue = [[r1,c1]] a = 0 while queue: q = queue.pop(0) r,c = q[0],q[1] if r==r2 and c==c2: a = 1 break if d[r-1][c]=='.': queue.append([r-1,c]) d[r-1][c]='X' if d[r+1][c]=='.': queue.append([r+1,c]) d[r+1][c]='X' if d[r][c-1]=='.': queue.append([r,c-1]) d[r][c-1]='X' if d[r][c+1]=='.': queue.append([r,c+1]) d[r][c+1]='X' if a==0: print('NO') else: if e==1: print('YES') else: if p>1: print('YES') else: d=0 if r1==r2: if abs(c1-c2)==1: d=1 if c1==c2: if abs(r1-r2)==1: d=1 if d==1 and p>0: print('YES') else: print('NO') ```
output
1
106,592
15
213,185
Provide tags and a correct Python 3 solution for this coding contest problem. You play a computer game. Your character stands on some level of a multilevel ice cave. In order to move on forward, you need to descend one level lower and the only way to do this is to fall through the ice. The level of the cave where you are is a rectangular square grid of n rows and m columns. Each cell consists either from intact or from cracked ice. From each cell you can move to cells that are side-adjacent with yours (due to some limitations of the game engine you cannot make jumps on the same place, i.e. jump from a cell to itself). If you move to the cell with cracked ice, then your character falls down through it and if you move to the cell with intact ice, then the ice on this cell becomes cracked. Let's number the rows with integers from 1 to n from top to bottom and the columns with integers from 1 to m from left to right. Let's denote a cell on the intersection of the r-th row and the c-th column as (r, c). You are staying in the cell (r1, c1) and this cell is cracked because you've just fallen here from a higher level. You need to fall down through the cell (r2, c2) since the exit to the next level is there. Can you do this? Input The first line contains two integers, n and m (1 ≤ n, m ≤ 500) — the number of rows and columns in the cave description. Each of the next n lines describes the initial state of the level of the cave, each line consists of m characters "." (that is, intact ice) and "X" (cracked ice). The next line contains two integers, r1 and c1 (1 ≤ r1 ≤ n, 1 ≤ c1 ≤ m) — your initial coordinates. It is guaranteed that the description of the cave contains character 'X' in cell (r1, c1), that is, the ice on the starting cell is initially cracked. The next line contains two integers r2 and c2 (1 ≤ r2 ≤ n, 1 ≤ c2 ≤ m) — the coordinates of the cell through which you need to fall. The final cell may coincide with the starting one. Output If you can reach the destination, print 'YES', otherwise print 'NO'. Examples Input 4 6 X...XX ...XX. .X..X. ...... 1 6 2 2 Output YES Input 5 4 .X.. ...X X.X. .... .XX. 5 3 1 1 Output NO Input 4 7 ..X.XX. .XX..X. X...X.. X...... 2 2 1 6 Output YES Note In the first sample test one possible path is: <image> After the first visit of cell (2, 2) the ice on it cracks and when you step there for the second time, your character falls through the ice as intended.
instruction
0
106,593
15
213,186
Tags: dfs and similar Correct Solution: ``` n,m=[int(x) for x in input().split()] inp=[input() for i in range(n)] source=[int(x)-1 for x in input().split()] dest=[int(x)-1 for x in input().split()] def valid(x): return True if 0<=x[0]<n and 0<=x[1]<m and inp[x[0]][x[1]]=='.' else False def getn(x): toret=[] y=(x[0]-1,x[1]) if valid(y): toret.append(y) y=(x[0]+1,x[1]) if valid(y): toret.append(y) y=(x[0],x[1]-1) if valid(y): toret.append(y) y=(x[0],x[1]+1) if valid(y): toret.append(y) return toret if source==dest: print('YES' if len(getn(dest)) else 'NO') else: dat=False dis=0 if inp[dest[0]][dest[1]]=='X': inp[dest[0]]=inp[dest[0]][:dest[1]]+'.'+inp[dest[0]][dest[1]+1:] dat=True if tuple(dest) in getn(source): dis=1 vis=[[False]*m for i in range(n)] mystak=[source] while mystak!=[]: x=mystak.pop() if vis[x[0]][x[1]]: continue vis[x[0]][x[1]]=True for i in getn(x): mystak.append(i) # for i in vis: # print(i) print('YES' if vis[dest[0]][dest[1]] and (len(getn(dest))>1-dis or dat) else 'NO') ```
output
1
106,593
15
213,187
Provide tags and a correct Python 3 solution for this coding contest problem. You play a computer game. Your character stands on some level of a multilevel ice cave. In order to move on forward, you need to descend one level lower and the only way to do this is to fall through the ice. The level of the cave where you are is a rectangular square grid of n rows and m columns. Each cell consists either from intact or from cracked ice. From each cell you can move to cells that are side-adjacent with yours (due to some limitations of the game engine you cannot make jumps on the same place, i.e. jump from a cell to itself). If you move to the cell with cracked ice, then your character falls down through it and if you move to the cell with intact ice, then the ice on this cell becomes cracked. Let's number the rows with integers from 1 to n from top to bottom and the columns with integers from 1 to m from left to right. Let's denote a cell on the intersection of the r-th row and the c-th column as (r, c). You are staying in the cell (r1, c1) and this cell is cracked because you've just fallen here from a higher level. You need to fall down through the cell (r2, c2) since the exit to the next level is there. Can you do this? Input The first line contains two integers, n and m (1 ≤ n, m ≤ 500) — the number of rows and columns in the cave description. Each of the next n lines describes the initial state of the level of the cave, each line consists of m characters "." (that is, intact ice) and "X" (cracked ice). The next line contains two integers, r1 and c1 (1 ≤ r1 ≤ n, 1 ≤ c1 ≤ m) — your initial coordinates. It is guaranteed that the description of the cave contains character 'X' in cell (r1, c1), that is, the ice on the starting cell is initially cracked. The next line contains two integers r2 and c2 (1 ≤ r2 ≤ n, 1 ≤ c2 ≤ m) — the coordinates of the cell through which you need to fall. The final cell may coincide with the starting one. Output If you can reach the destination, print 'YES', otherwise print 'NO'. Examples Input 4 6 X...XX ...XX. .X..X. ...... 1 6 2 2 Output YES Input 5 4 .X.. ...X X.X. .... .XX. 5 3 1 1 Output NO Input 4 7 ..X.XX. .XX..X. X...X.. X...... 2 2 1 6 Output YES Note In the first sample test one possible path is: <image> After the first visit of cell (2, 2) the ice on it cracks and when you step there for the second time, your character falls through the ice as intended.
instruction
0
106,594
15
213,188
Tags: dfs and similar Correct Solution: ``` from collections import deque directions = ((1, 0), (0, 1), (-1, 0), (0, -1)) n, m = map(int, input().split()) a = [list(input()) for _ in range(n)] si, sj = map(lambda x:int(x)-1, input().split()) ti, tj = map(lambda x:int(x)-1, input().split()) def is_valid(i, j): return 0 <= i < n and 0 <= j < m def check(i, j): r = 0 for di, dj in directions: ni, nj = i + di, j + dj if is_valid(ni, nj) and a[ni][nj] != 'X' or (ni, nj) == (si, sj): r += 1 return r if (si, sj) == (ti, tj): print('YES' if check(si, sj) >= 1 else 'NO') exit() c = a[ti][tj] a[ti][tj] = '.' vis, q = set(), deque() vis.add((si, sj)) q.append((si, sj)) while q: i, j = q.popleft() for di, dj in directions: ni, nj = i + di, j + dj if is_valid(ni, nj) and a[ni][nj] != 'X' and (ni, nj) not in vis: vis.add((ni, nj)) q.append((ni, nj)) if (ti, tj) not in vis: print('NO') elif c == 'X': print('YES') else: print('YES' if check(ti, tj) >= 2 else 'NO') ```
output
1
106,594
15
213,189
Provide tags and a correct Python 3 solution for this coding contest problem. You play a computer game. Your character stands on some level of a multilevel ice cave. In order to move on forward, you need to descend one level lower and the only way to do this is to fall through the ice. The level of the cave where you are is a rectangular square grid of n rows and m columns. Each cell consists either from intact or from cracked ice. From each cell you can move to cells that are side-adjacent with yours (due to some limitations of the game engine you cannot make jumps on the same place, i.e. jump from a cell to itself). If you move to the cell with cracked ice, then your character falls down through it and if you move to the cell with intact ice, then the ice on this cell becomes cracked. Let's number the rows with integers from 1 to n from top to bottom and the columns with integers from 1 to m from left to right. Let's denote a cell on the intersection of the r-th row and the c-th column as (r, c). You are staying in the cell (r1, c1) and this cell is cracked because you've just fallen here from a higher level. You need to fall down through the cell (r2, c2) since the exit to the next level is there. Can you do this? Input The first line contains two integers, n and m (1 ≤ n, m ≤ 500) — the number of rows and columns in the cave description. Each of the next n lines describes the initial state of the level of the cave, each line consists of m characters "." (that is, intact ice) and "X" (cracked ice). The next line contains two integers, r1 and c1 (1 ≤ r1 ≤ n, 1 ≤ c1 ≤ m) — your initial coordinates. It is guaranteed that the description of the cave contains character 'X' in cell (r1, c1), that is, the ice on the starting cell is initially cracked. The next line contains two integers r2 and c2 (1 ≤ r2 ≤ n, 1 ≤ c2 ≤ m) — the coordinates of the cell through which you need to fall. The final cell may coincide with the starting one. Output If you can reach the destination, print 'YES', otherwise print 'NO'. Examples Input 4 6 X...XX ...XX. .X..X. ...... 1 6 2 2 Output YES Input 5 4 .X.. ...X X.X. .... .XX. 5 3 1 1 Output NO Input 4 7 ..X.XX. .XX..X. X...X.. X...... 2 2 1 6 Output YES Note In the first sample test one possible path is: <image> After the first visit of cell (2, 2) the ice on it cracks and when you step there for the second time, your character falls through the ice as intended.
instruction
0
106,595
15
213,190
Tags: dfs and similar Correct Solution: ``` def pick(D, v): return D[v[0]][v[1]] def getNeighbours(A, x, y, t='.'): neighbours = [] if x > 0 and A[x-1][y] == t: neighbours.append((x-1, y)) if y > 0 and A[x][y-1] == t: neighbours.append((x, y-1)) if x < len(A)-1 and A[x+1][y] == t: neighbours.append((x+1, y)) if y < len(A[0])-1 and A[x][y+1] == t: neighbours.append((x, y+1)) return neighbours def countNeighbours(A, x, y, t='.'): return len(getNeighbours(A, x, y, t)) def hasPath(A, xs, ys, xe, ye): # Pre-set end-location 'dot' A[xe][ye] = '.' D = [[-1 for _ in range(len(A[0]))] for _ in range(len(A))] D[xs][ys] = 0 Q = [(xs, ys)] # BFS while len(Q) > 0: u = Q.pop() for v in getNeighbours(A, *u): if pick(D, v) == -1: Q.insert(0, v) D[v[0]][v[1]] = pick(D, u) + 1 return D[xe][ye] != -1 def solve(A, r1, c1, r2, c2): if r1 == r2 and c1 == c2: if countNeighbours(A, r1, c1) > 0: return True else: return False elif (r2, c2) in getNeighbours(A, r1, c1) or (r2, c2) in getNeighbours(A, r1, c1, 'X'): if A[r2][c2] == 'X': return True else: if countNeighbours(A, r2, c2) > 0: return True else: return False elif A[r2][c2] == '.': if countNeighbours(A, r2, c2) >= 2 and hasPath(A, r1, c1, r2, c2): return True else: return False elif A[r2][c2] == 'X': if countNeighbours(A, r2, c2) > 0 and hasPath(A, r1, c1, r2, c2): return True else: return False def main(): n, m = map(int, input().split()) A = [list(input()) for _ in range(n)] r1, c1 = map(lambda w: int(w)-1, input().split()) r2, c2 = map(lambda w: int(w)-1, input().split()) ans = solve(A, r1, c1, r2, c2) print('YES' if ans else 'NO') main() ```
output
1
106,595
15
213,191
Provide tags and a correct Python 3 solution for this coding contest problem. You play a computer game. Your character stands on some level of a multilevel ice cave. In order to move on forward, you need to descend one level lower and the only way to do this is to fall through the ice. The level of the cave where you are is a rectangular square grid of n rows and m columns. Each cell consists either from intact or from cracked ice. From each cell you can move to cells that are side-adjacent with yours (due to some limitations of the game engine you cannot make jumps on the same place, i.e. jump from a cell to itself). If you move to the cell with cracked ice, then your character falls down through it and if you move to the cell with intact ice, then the ice on this cell becomes cracked. Let's number the rows with integers from 1 to n from top to bottom and the columns with integers from 1 to m from left to right. Let's denote a cell on the intersection of the r-th row and the c-th column as (r, c). You are staying in the cell (r1, c1) and this cell is cracked because you've just fallen here from a higher level. You need to fall down through the cell (r2, c2) since the exit to the next level is there. Can you do this? Input The first line contains two integers, n and m (1 ≤ n, m ≤ 500) — the number of rows and columns in the cave description. Each of the next n lines describes the initial state of the level of the cave, each line consists of m characters "." (that is, intact ice) and "X" (cracked ice). The next line contains two integers, r1 and c1 (1 ≤ r1 ≤ n, 1 ≤ c1 ≤ m) — your initial coordinates. It is guaranteed that the description of the cave contains character 'X' in cell (r1, c1), that is, the ice on the starting cell is initially cracked. The next line contains two integers r2 and c2 (1 ≤ r2 ≤ n, 1 ≤ c2 ≤ m) — the coordinates of the cell through which you need to fall. The final cell may coincide with the starting one. Output If you can reach the destination, print 'YES', otherwise print 'NO'. Examples Input 4 6 X...XX ...XX. .X..X. ...... 1 6 2 2 Output YES Input 5 4 .X.. ...X X.X. .... .XX. 5 3 1 1 Output NO Input 4 7 ..X.XX. .XX..X. X...X.. X...... 2 2 1 6 Output YES Note In the first sample test one possible path is: <image> After the first visit of cell (2, 2) the ice on it cracks and when you step there for the second time, your character falls through the ice as intended.
instruction
0
106,596
15
213,192
Tags: dfs and similar Correct Solution: ``` import sys sys.setrecursionlimit(1000000000) def bfs(x, y): q = [(y, x)] while q: x = q[0][1] y = q[0][0] q.pop(0) if (x-1 > -1) and (g[y][x-1] == '.'): g[y][x-1] = 'X' q.append((y, x-1)) if (x+1 < m) and (g[y][x+1] == '.'): g[y][x+1] = 'X' q.append((y, x+1)) if (y-1 > -1) and (g[y-1][x] == '.'): g[y-1][x] = 'X' q.append((y-1, x)) if (y+1 < n) and (g[y+1][x] == '.'): g[y+1][x] = 'X' q.append((y+1, x)) n, m = map(int, input().split()) g = [['']*m for i in range(n)] for i in range(n): t = input() for j in range(m): g[i][j] = t[j] y, x = map(int, input().split()) b, a = map(int, input().split()) x -= 1 y -= 1 a -= 1 b -= 1 if (x == a) and (y == b): # если начало и конец пути совпадают if ((x-1 > -1) and (g[y][x-1] == '.')) or ((x+1 < m) and (g[y][x+1] == '.')) or ((y-1 > -1) and (g[y-1][x] == '.')) or ((y+1 < n) and (g[y+1][x] == '.')): print('YES') else: print('NO') else: # если финишная клетка треснувшая или у нее есть целые соседи f = False if g[b][a] == '.': f = True one = (a-1 > -1) and (g[b][a-1] == '.') two = (a+1 < m) and (g[b][a+1] == '.') three = (b-1 > -1) and (g[b-1][a] == '.') four = (b+1 < n) and (g[b+1][a] == '.') g[b][a] = '.' bfs(x, y) if g[b][a] == 'X': if f: if one+two+three+four > 1: print('YES') elif (((a == x-1) and (b == y)) or ((a == x+1) and (b == y)) or ((a == x) and (b == y-1)) or ((a == x) and (b == y+1))) and (one+two+three+four == 1): print('YES') else: print('NO') else: print('YES') else: print('NO') ```
output
1
106,596
15
213,193
Provide tags and a correct Python 3 solution for this coding contest problem. You play a computer game. Your character stands on some level of a multilevel ice cave. In order to move on forward, you need to descend one level lower and the only way to do this is to fall through the ice. The level of the cave where you are is a rectangular square grid of n rows and m columns. Each cell consists either from intact or from cracked ice. From each cell you can move to cells that are side-adjacent with yours (due to some limitations of the game engine you cannot make jumps on the same place, i.e. jump from a cell to itself). If you move to the cell with cracked ice, then your character falls down through it and if you move to the cell with intact ice, then the ice on this cell becomes cracked. Let's number the rows with integers from 1 to n from top to bottom and the columns with integers from 1 to m from left to right. Let's denote a cell on the intersection of the r-th row and the c-th column as (r, c). You are staying in the cell (r1, c1) and this cell is cracked because you've just fallen here from a higher level. You need to fall down through the cell (r2, c2) since the exit to the next level is there. Can you do this? Input The first line contains two integers, n and m (1 ≤ n, m ≤ 500) — the number of rows and columns in the cave description. Each of the next n lines describes the initial state of the level of the cave, each line consists of m characters "." (that is, intact ice) and "X" (cracked ice). The next line contains two integers, r1 and c1 (1 ≤ r1 ≤ n, 1 ≤ c1 ≤ m) — your initial coordinates. It is guaranteed that the description of the cave contains character 'X' in cell (r1, c1), that is, the ice on the starting cell is initially cracked. The next line contains two integers r2 and c2 (1 ≤ r2 ≤ n, 1 ≤ c2 ≤ m) — the coordinates of the cell through which you need to fall. The final cell may coincide with the starting one. Output If you can reach the destination, print 'YES', otherwise print 'NO'. Examples Input 4 6 X...XX ...XX. .X..X. ...... 1 6 2 2 Output YES Input 5 4 .X.. ...X X.X. .... .XX. 5 3 1 1 Output NO Input 4 7 ..X.XX. .XX..X. X...X.. X...... 2 2 1 6 Output YES Note In the first sample test one possible path is: <image> After the first visit of cell (2, 2) the ice on it cracks and when you step there for the second time, your character falls through the ice as intended.
instruction
0
106,597
15
213,194
Tags: dfs and similar Correct Solution: ``` def main(): n, m = map(int, input().split()) nm = n * m neigh = [[] for _ in range(nm)] for i in range(0, nm - m, m): for j in range(i, i + m): neigh[j].append(j + m) neigh[j + m].append(j) for i in range(0, nm, m): for j in range(i, i + m - 1): neigh[j].append(j + 1) neigh[j + 1].append(j) field = [c == '.' for _ in range(n) for c in input()] l = [] for _ in (0, 1): i, j = map(int, input().split()) l.append(i * m + j - m - 1) start, stop = l if start == stop: print(("NO", "YES")[any(field[_] for _ in neigh[stop])]) return field[start] = True if field[stop] and sum(field[_] for _ in neigh[stop]) < 2: print("NO") return nxt, field[stop] = (start,), True while nxt: cur, nxt = nxt, set() for u in cur: if u == stop: print("YES") return for v in neigh[u]: if field[v]: field[v] = False nxt.add(v) print("NO") if __name__ == '__main__': main() ```
output
1
106,597
15
213,195
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You play a computer game. Your character stands on some level of a multilevel ice cave. In order to move on forward, you need to descend one level lower and the only way to do this is to fall through the ice. The level of the cave where you are is a rectangular square grid of n rows and m columns. Each cell consists either from intact or from cracked ice. From each cell you can move to cells that are side-adjacent with yours (due to some limitations of the game engine you cannot make jumps on the same place, i.e. jump from a cell to itself). If you move to the cell with cracked ice, then your character falls down through it and if you move to the cell with intact ice, then the ice on this cell becomes cracked. Let's number the rows with integers from 1 to n from top to bottom and the columns with integers from 1 to m from left to right. Let's denote a cell on the intersection of the r-th row and the c-th column as (r, c). You are staying in the cell (r1, c1) and this cell is cracked because you've just fallen here from a higher level. You need to fall down through the cell (r2, c2) since the exit to the next level is there. Can you do this? Input The first line contains two integers, n and m (1 ≤ n, m ≤ 500) — the number of rows and columns in the cave description. Each of the next n lines describes the initial state of the level of the cave, each line consists of m characters "." (that is, intact ice) and "X" (cracked ice). The next line contains two integers, r1 and c1 (1 ≤ r1 ≤ n, 1 ≤ c1 ≤ m) — your initial coordinates. It is guaranteed that the description of the cave contains character 'X' in cell (r1, c1), that is, the ice on the starting cell is initially cracked. The next line contains two integers r2 and c2 (1 ≤ r2 ≤ n, 1 ≤ c2 ≤ m) — the coordinates of the cell through which you need to fall. The final cell may coincide with the starting one. Output If you can reach the destination, print 'YES', otherwise print 'NO'. Examples Input 4 6 X...XX ...XX. .X..X. ...... 1 6 2 2 Output YES Input 5 4 .X.. ...X X.X. .... .XX. 5 3 1 1 Output NO Input 4 7 ..X.XX. .XX..X. X...X.. X...... 2 2 1 6 Output YES Note In the first sample test one possible path is: <image> After the first visit of cell (2, 2) the ice on it cracks and when you step there for the second time, your character falls through the ice as intended. Submitted Solution: ``` #!/usr/bin/python3 from collections import deque def dfs (): while(len(queue)!=0): s=queue.popleft() (i,j)=s for (ni,nj) in [(i-1,j),(i+1,j),(i,j-1),(i,j+1)]: if ni in range(0,n) and nj in range(0,m): fl=data[ni][nj] if(e == (ni,nj)) and fl=="X": return True elif fl == ".": data[ni][nj] = "X" queue.append((ni,nj)) return False [n,m]=list(map(int,(input().split()))) data=list() for i in range(0,n): data.append(list(map(lambda x:x,input()))) [si,sj]=list(map(int,(input().split()))) [ei,ej]=list(map(int,(input().split()))) e=(ei-1,ej-1) queue=deque() queue.append((si-1,sj-1)) print("YES" if dfs() else "NO") ```
instruction
0
106,598
15
213,196
Yes
output
1
106,598
15
213,197
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You play a computer game. Your character stands on some level of a multilevel ice cave. In order to move on forward, you need to descend one level lower and the only way to do this is to fall through the ice. The level of the cave where you are is a rectangular square grid of n rows and m columns. Each cell consists either from intact or from cracked ice. From each cell you can move to cells that are side-adjacent with yours (due to some limitations of the game engine you cannot make jumps on the same place, i.e. jump from a cell to itself). If you move to the cell with cracked ice, then your character falls down through it and if you move to the cell with intact ice, then the ice on this cell becomes cracked. Let's number the rows with integers from 1 to n from top to bottom and the columns with integers from 1 to m from left to right. Let's denote a cell on the intersection of the r-th row and the c-th column as (r, c). You are staying in the cell (r1, c1) and this cell is cracked because you've just fallen here from a higher level. You need to fall down through the cell (r2, c2) since the exit to the next level is there. Can you do this? Input The first line contains two integers, n and m (1 ≤ n, m ≤ 500) — the number of rows and columns in the cave description. Each of the next n lines describes the initial state of the level of the cave, each line consists of m characters "." (that is, intact ice) and "X" (cracked ice). The next line contains two integers, r1 and c1 (1 ≤ r1 ≤ n, 1 ≤ c1 ≤ m) — your initial coordinates. It is guaranteed that the description of the cave contains character 'X' in cell (r1, c1), that is, the ice on the starting cell is initially cracked. The next line contains two integers r2 and c2 (1 ≤ r2 ≤ n, 1 ≤ c2 ≤ m) — the coordinates of the cell through which you need to fall. The final cell may coincide with the starting one. Output If you can reach the destination, print 'YES', otherwise print 'NO'. Examples Input 4 6 X...XX ...XX. .X..X. ...... 1 6 2 2 Output YES Input 5 4 .X.. ...X X.X. .... .XX. 5 3 1 1 Output NO Input 4 7 ..X.XX. .XX..X. X...X.. X...... 2 2 1 6 Output YES Note In the first sample test one possible path is: <image> After the first visit of cell (2, 2) the ice on it cracks and when you step there for the second time, your character falls through the ice as intended. Submitted Solution: ``` ''' https://codeforces.com/contest/540/problem/C Comment ''' import sys, queue def FILE_IO(): sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') '''____________________________________________________________________________________________''' dirt = [(0, -1), (-1, 0), (0, 1), (1, 0)] grp = [] m = n = None def canGo(x, y): if x < 0 or x >= m or y < 0 or y >= n: return False return True def bfs(r1, c1, r2, c2): q = queue.Queue() q.put([r1, c1]) while not q.empty(): ux, uy = q.get() for x, y in dirt: vx, vy = ux + x, uy + y if canGo(vx, vy): if grp[vx][vy] == 'X': if vx == r2 and vy == c2: return 'YES' else: q.put([vx, vy]) grp[vx][vy] = 'X' return 'NO' if __name__ == '__main__': #FILE_IO() m, n = map(int, input().split()) grp = [None]*m for i in range(m): grp[i] = list(input()) r1, c1 = map(int, input().split()) r2, c2 = map(int, input().split()) print(bfs(r1 - 1, c1 - 1, r2 - 1, c2 - 1)) ```
instruction
0
106,599
15
213,198
Yes
output
1
106,599
15
213,199
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You play a computer game. Your character stands on some level of a multilevel ice cave. In order to move on forward, you need to descend one level lower and the only way to do this is to fall through the ice. The level of the cave where you are is a rectangular square grid of n rows and m columns. Each cell consists either from intact or from cracked ice. From each cell you can move to cells that are side-adjacent with yours (due to some limitations of the game engine you cannot make jumps on the same place, i.e. jump from a cell to itself). If you move to the cell with cracked ice, then your character falls down through it and if you move to the cell with intact ice, then the ice on this cell becomes cracked. Let's number the rows with integers from 1 to n from top to bottom and the columns with integers from 1 to m from left to right. Let's denote a cell on the intersection of the r-th row and the c-th column as (r, c). You are staying in the cell (r1, c1) and this cell is cracked because you've just fallen here from a higher level. You need to fall down through the cell (r2, c2) since the exit to the next level is there. Can you do this? Input The first line contains two integers, n and m (1 ≤ n, m ≤ 500) — the number of rows and columns in the cave description. Each of the next n lines describes the initial state of the level of the cave, each line consists of m characters "." (that is, intact ice) and "X" (cracked ice). The next line contains two integers, r1 and c1 (1 ≤ r1 ≤ n, 1 ≤ c1 ≤ m) — your initial coordinates. It is guaranteed that the description of the cave contains character 'X' in cell (r1, c1), that is, the ice on the starting cell is initially cracked. The next line contains two integers r2 and c2 (1 ≤ r2 ≤ n, 1 ≤ c2 ≤ m) — the coordinates of the cell through which you need to fall. The final cell may coincide with the starting one. Output If you can reach the destination, print 'YES', otherwise print 'NO'. Examples Input 4 6 X...XX ...XX. .X..X. ...... 1 6 2 2 Output YES Input 5 4 .X.. ...X X.X. .... .XX. 5 3 1 1 Output NO Input 4 7 ..X.XX. .XX..X. X...X.. X...... 2 2 1 6 Output YES Note In the first sample test one possible path is: <image> After the first visit of cell (2, 2) the ice on it cracks and when you step there for the second time, your character falls through the ice as intended. Submitted Solution: ``` n,m = map(int,input().split()) field = [] for i in range(n): next = list(input()) field.append(next) r1,c1 = map(int,input().split()) r2,c2 = map(int,input().split()) r1-=1 r2-=1 c1-=1 c2-=1 visited = [[0 for _ in range(m+1)] for _ in range(n+1)] for i in range(n): for j in range(m): if field[i][j]=='X': visited[i][j]=1 else: visited[i][j] =0 ans = 'NO' # print([r1,c1],[r2,c2]) def walk(r,c): up =[min(n-1,r+1),c] down =[max(r-1,0),c] left =[r,max(0,c-1)] right = [r,min(m-1,c+1)] ret = [up,down,left,right] return ret queue=[[r1,c1]] while(len(queue)>0): r,c = queue.pop(0) paths = walk(r,c) for next in paths: row, col =next[0],next[1] if not((r==row)&(c==col) or (field[row][col])=='O'): if (row==r2 and col==c2) and (field[row][col]=='X'): print('YES') exit() if visited[row][col]!=1: visited[row][col]+=1 field[row][col]='X' queue.append([row,col]) else: field[row][col] = 'O' print('NO') ```
instruction
0
106,600
15
213,200
Yes
output
1
106,600
15
213,201
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You play a computer game. Your character stands on some level of a multilevel ice cave. In order to move on forward, you need to descend one level lower and the only way to do this is to fall through the ice. The level of the cave where you are is a rectangular square grid of n rows and m columns. Each cell consists either from intact or from cracked ice. From each cell you can move to cells that are side-adjacent with yours (due to some limitations of the game engine you cannot make jumps on the same place, i.e. jump from a cell to itself). If you move to the cell with cracked ice, then your character falls down through it and if you move to the cell with intact ice, then the ice on this cell becomes cracked. Let's number the rows with integers from 1 to n from top to bottom and the columns with integers from 1 to m from left to right. Let's denote a cell on the intersection of the r-th row and the c-th column as (r, c). You are staying in the cell (r1, c1) and this cell is cracked because you've just fallen here from a higher level. You need to fall down through the cell (r2, c2) since the exit to the next level is there. Can you do this? Input The first line contains two integers, n and m (1 ≤ n, m ≤ 500) — the number of rows and columns in the cave description. Each of the next n lines describes the initial state of the level of the cave, each line consists of m characters "." (that is, intact ice) and "X" (cracked ice). The next line contains two integers, r1 and c1 (1 ≤ r1 ≤ n, 1 ≤ c1 ≤ m) — your initial coordinates. It is guaranteed that the description of the cave contains character 'X' in cell (r1, c1), that is, the ice on the starting cell is initially cracked. The next line contains two integers r2 and c2 (1 ≤ r2 ≤ n, 1 ≤ c2 ≤ m) — the coordinates of the cell through which you need to fall. The final cell may coincide with the starting one. Output If you can reach the destination, print 'YES', otherwise print 'NO'. Examples Input 4 6 X...XX ...XX. .X..X. ...... 1 6 2 2 Output YES Input 5 4 .X.. ...X X.X. .... .XX. 5 3 1 1 Output NO Input 4 7 ..X.XX. .XX..X. X...X.. X...... 2 2 1 6 Output YES Note In the first sample test one possible path is: <image> After the first visit of cell (2, 2) the ice on it cracks and when you step there for the second time, your character falls through the ice as intended. Submitted Solution: ``` import sys from queue import Queue n = m = xS = yS = xD = yD = -1 dx = [-1, 1, 0, 0] dy = [0, 0, 1, -1] grid = [] marked = [] def check(x, y): global n, m return -1 < x and x < n and -1 < y and y < m def bfs(grid, xS,yS, xD, yD): q = [None for _ in range(250005)] l = r = 0 q[r] = (xS, yS) r+=1 while l < r: xC, yC = q[l] l+=1 for i in range(4): xN = xC + dx[i] yN = yC + dy[i] if check(xN, yN): if (grid[xN][yN] == 'X' and xN == xD and yN == yD): return True if grid[xN][yN] == '.': grid[xN][yN] = 'X' q[r] = (xN, yN) r+=1 return False n, m = map(int, input().split()) grid = [[] for _ in range(n)] marked = [[False for _ in range(m)] for _ in range(n)] for i in range(n): grid[i] = list(input()) xS, yS = map(lambda x : int(x)-1, input().split()) xD, yD = map(lambda x : int(x) -1, input().split()) if (bfs(grid, xS, yS, xD, yD)): print("YES") else: print("NO") ```
instruction
0
106,601
15
213,202
Yes
output
1
106,601
15
213,203
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You play a computer game. Your character stands on some level of a multilevel ice cave. In order to move on forward, you need to descend one level lower and the only way to do this is to fall through the ice. The level of the cave where you are is a rectangular square grid of n rows and m columns. Each cell consists either from intact or from cracked ice. From each cell you can move to cells that are side-adjacent with yours (due to some limitations of the game engine you cannot make jumps on the same place, i.e. jump from a cell to itself). If you move to the cell with cracked ice, then your character falls down through it and if you move to the cell with intact ice, then the ice on this cell becomes cracked. Let's number the rows with integers from 1 to n from top to bottom and the columns with integers from 1 to m from left to right. Let's denote a cell on the intersection of the r-th row and the c-th column as (r, c). You are staying in the cell (r1, c1) and this cell is cracked because you've just fallen here from a higher level. You need to fall down through the cell (r2, c2) since the exit to the next level is there. Can you do this? Input The first line contains two integers, n and m (1 ≤ n, m ≤ 500) — the number of rows and columns in the cave description. Each of the next n lines describes the initial state of the level of the cave, each line consists of m characters "." (that is, intact ice) and "X" (cracked ice). The next line contains two integers, r1 and c1 (1 ≤ r1 ≤ n, 1 ≤ c1 ≤ m) — your initial coordinates. It is guaranteed that the description of the cave contains character 'X' in cell (r1, c1), that is, the ice on the starting cell is initially cracked. The next line contains two integers r2 and c2 (1 ≤ r2 ≤ n, 1 ≤ c2 ≤ m) — the coordinates of the cell through which you need to fall. The final cell may coincide with the starting one. Output If you can reach the destination, print 'YES', otherwise print 'NO'. Examples Input 4 6 X...XX ...XX. .X..X. ...... 1 6 2 2 Output YES Input 5 4 .X.. ...X X.X. .... .XX. 5 3 1 1 Output NO Input 4 7 ..X.XX. .XX..X. X...X.. X...... 2 2 1 6 Output YES Note In the first sample test one possible path is: <image> After the first visit of cell (2, 2) the ice on it cracks and when you step there for the second time, your character falls through the ice as intended. Submitted Solution: ``` n,m = map(int,input().split()) L = [input() for i in range(n)] Visited = [[0] * m for i in range(n)] start = list(map(int,input().split())) end = list(map(int,input().split())) def Do(i,j) : q = [[i,j]] f = False while q : d = q[0] Visited[d[0]][d[1]] = 1 if d[0] != 0 : if Visited[d[0] - 1][d[1]] == 0 and L[d[0] - 1][d[1]] == "." : Visited[d[0] - 1][d[1]] = 1 q.append([d[0] - 1,d[1]]) if d[0] != n - 1 : if Visited[d[0] + 1][d[1]] == 0 and L[d[0] + 1][d[1]] == "." : Visited[d[0] + 1][d[1]] = 1 q.append([d[0] + 1,d[1]]) if d[1] != 0 : if Visited[d[0]][d[1] - 1] == 0 and L[d[0]][d[1] - 1] == "." : Visited[d[0]][d[1] - 1] = 1 q.append([d[0],d[1] - 1]) if d[1] != m - 1 : if Visited[d[0]][d[1] + 1] == 0 and L[d[0]][d[1] + 1] == "." : Visited[d[0]][d[1] + 1] = 1 q.append([d[0],d[1] + 1]) del q[0] Do(start[0] - 1,start[1] - 1) if end[0]==start[0] and start[1]==end[1] : Visited[end[0] - 1][end[1] - 1]=0 if (Visited[end[0] - 1][end[1] - 1]) == 1 : k = 0 if end[0] != 1 : if L[end[0] - 2][end[1] - 1] == "." : k+=1 if end[1] != 1 : if L[end[0] - 1][end[1] - 2] == "." : k+=1 if end[0] != n : if L[end[0]][end[1] - 1] == "." : k+=1 if end[1] != m : if L[end[0] - 1][end[1]] == "." : k+=1 if k >= 2 : print("YES") else : print("NO") else : if L[end[0] - 1][end[1] - 1] == "X" : k = 0 if end[0] != 1 : if L[end[0] - 2][end[1] - 1] == "." : k+=1 if end[1] != 1 : if L[end[0] - 1][end[1] - 2] == "." : k+=1 if end[0] != n : if L[end[0]][end[1] - 1] == "." : k+=1 if end[1] != m : if L[end[0] - 1][end[1]] == "." : k+=1 if k >= 1 : print("YES") else : print("NO") else : print("NO") ```
instruction
0
106,602
15
213,204
No
output
1
106,602
15
213,205
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You play a computer game. Your character stands on some level of a multilevel ice cave. In order to move on forward, you need to descend one level lower and the only way to do this is to fall through the ice. The level of the cave where you are is a rectangular square grid of n rows and m columns. Each cell consists either from intact or from cracked ice. From each cell you can move to cells that are side-adjacent with yours (due to some limitations of the game engine you cannot make jumps on the same place, i.e. jump from a cell to itself). If you move to the cell with cracked ice, then your character falls down through it and if you move to the cell with intact ice, then the ice on this cell becomes cracked. Let's number the rows with integers from 1 to n from top to bottom and the columns with integers from 1 to m from left to right. Let's denote a cell on the intersection of the r-th row and the c-th column as (r, c). You are staying in the cell (r1, c1) and this cell is cracked because you've just fallen here from a higher level. You need to fall down through the cell (r2, c2) since the exit to the next level is there. Can you do this? Input The first line contains two integers, n and m (1 ≤ n, m ≤ 500) — the number of rows and columns in the cave description. Each of the next n lines describes the initial state of the level of the cave, each line consists of m characters "." (that is, intact ice) and "X" (cracked ice). The next line contains two integers, r1 and c1 (1 ≤ r1 ≤ n, 1 ≤ c1 ≤ m) — your initial coordinates. It is guaranteed that the description of the cave contains character 'X' in cell (r1, c1), that is, the ice on the starting cell is initially cracked. The next line contains two integers r2 and c2 (1 ≤ r2 ≤ n, 1 ≤ c2 ≤ m) — the coordinates of the cell through which you need to fall. The final cell may coincide with the starting one. Output If you can reach the destination, print 'YES', otherwise print 'NO'. Examples Input 4 6 X...XX ...XX. .X..X. ...... 1 6 2 2 Output YES Input 5 4 .X.. ...X X.X. .... .XX. 5 3 1 1 Output NO Input 4 7 ..X.XX. .XX..X. X...X.. X...... 2 2 1 6 Output YES Note In the first sample test one possible path is: <image> After the first visit of cell (2, 2) the ice on it cracks and when you step there for the second time, your character falls through the ice as intended. Submitted Solution: ``` #from os import abort as exit def check(): if b[x2][y2] == 'X': print('YES') exit() s = '' for dx, dy in moves: if 0 <= dx + x2 < n and 0 <= dy + y2 < m: s += b[dx + x2][dy + y2] if s.count('.') >= 2: print('YES') else: print('NO') exit() def dfs(x1, y1): a[x1][y1] = True if x1 ==x2 and y1 == y2: check() for dx, dy in moves: if 0 <= dx + x1 < n and 0 <= dy + y1 < m and (not a[dx + x1][dy + y1] or x1 + dx == x2 and y1 + dy == y2): dfs(dx + x1, dy + y1) moves = [(1, 0), (0, 1), (-1, 0), (0, -1)] n, m = map(int, input().split()) a = [[0] * m for i in range(n)] b = [] for i in range(n): s = input() b.append(s) for j in range(m): if s[j] == 'X': a[i][j] = True else: a[i][j] = False x1, y1 = map(int, input().split()) x2, y2 = map(int, input().split()) x1 -= 1 x2 -= 1 y1 -= 1 y2 -= 1 dfs(x1, y1) print('NO' + '\n') ```
instruction
0
106,603
15
213,206
No
output
1
106,603
15
213,207
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You play a computer game. Your character stands on some level of a multilevel ice cave. In order to move on forward, you need to descend one level lower and the only way to do this is to fall through the ice. The level of the cave where you are is a rectangular square grid of n rows and m columns. Each cell consists either from intact or from cracked ice. From each cell you can move to cells that are side-adjacent with yours (due to some limitations of the game engine you cannot make jumps on the same place, i.e. jump from a cell to itself). If you move to the cell with cracked ice, then your character falls down through it and if you move to the cell with intact ice, then the ice on this cell becomes cracked. Let's number the rows with integers from 1 to n from top to bottom and the columns with integers from 1 to m from left to right. Let's denote a cell on the intersection of the r-th row and the c-th column as (r, c). You are staying in the cell (r1, c1) and this cell is cracked because you've just fallen here from a higher level. You need to fall down through the cell (r2, c2) since the exit to the next level is there. Can you do this? Input The first line contains two integers, n and m (1 ≤ n, m ≤ 500) — the number of rows and columns in the cave description. Each of the next n lines describes the initial state of the level of the cave, each line consists of m characters "." (that is, intact ice) and "X" (cracked ice). The next line contains two integers, r1 and c1 (1 ≤ r1 ≤ n, 1 ≤ c1 ≤ m) — your initial coordinates. It is guaranteed that the description of the cave contains character 'X' in cell (r1, c1), that is, the ice on the starting cell is initially cracked. The next line contains two integers r2 and c2 (1 ≤ r2 ≤ n, 1 ≤ c2 ≤ m) — the coordinates of the cell through which you need to fall. The final cell may coincide with the starting one. Output If you can reach the destination, print 'YES', otherwise print 'NO'. Examples Input 4 6 X...XX ...XX. .X..X. ...... 1 6 2 2 Output YES Input 5 4 .X.. ...X X.X. .... .XX. 5 3 1 1 Output NO Input 4 7 ..X.XX. .XX..X. X...X.. X...... 2 2 1 6 Output YES Note In the first sample test one possible path is: <image> After the first visit of cell (2, 2) the ice on it cracks and when you step there for the second time, your character falls through the ice as intended. Submitted Solution: ``` import queue def BFS(starting_point): q = queue.Queue() visited[starting_point[0]][starting_point[1]] = True X = [-1, 0, 1, 0] Y = [0, 1, 0, -1] q.put(starting_point) while q.qsize() != 0: tmp = q.get() x = tmp[0] y = tmp[1] for i in range (len(X)): if 0 <= x + X[i] < n and 0 <= y + Y[i] < m and visited[x+X[i]][y+Y[i]] == False and a[x + X[i]][y + Y[i]] != '@': q.put((x + X[i], y + Y[i])) if a[x + X[i]][y + Y[i]] == 'X': a[x + X[i]][y + Y[i]] = '@' else: a[x + X[i]][y + Y[i]] = 'X' # a[x + X[i]][y + Y[i]] = 'X' visited[x + X[i]][y + Y[i]] = True if x + X[i] == ending_point[0] and y + Y[i] == ending_point[1]: return n, m = map(int, input().split()) a = [] for i in range(n): s = list(input()) a.append(s) starting_point = list(map(int, input().split())) ending_point = list(map(int, input().split())) starting_point[0] -= 1 starting_point[1] -= 1 ending_point[0] -= 1 ending_point[1] -= 1 visited = [[False] * (m+1) for i in range(n+1)] BFS(starting_point) # for i in range(n): # print(a[i]) if visited[ending_point[0]][ending_point[1]] == False: print("NO") else: if a[ending_point[0] - 1][ending_point[1] - 1] == '@': print("YES") else: X = [0, 1, 0, -1] Y = [1, 0, -1, 0] check = False for i in range(4): x = ending_point[0] y = ending_point[1] if 0 <= x + X[i] < n and 0 <= y + Y[i] < m: if a[x + X[i]][y + Y[i]] == '.': check = True if check == False: print("NO") else: print("YES") ```
instruction
0
106,604
15
213,208
No
output
1
106,604
15
213,209
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You play a computer game. Your character stands on some level of a multilevel ice cave. In order to move on forward, you need to descend one level lower and the only way to do this is to fall through the ice. The level of the cave where you are is a rectangular square grid of n rows and m columns. Each cell consists either from intact or from cracked ice. From each cell you can move to cells that are side-adjacent with yours (due to some limitations of the game engine you cannot make jumps on the same place, i.e. jump from a cell to itself). If you move to the cell with cracked ice, then your character falls down through it and if you move to the cell with intact ice, then the ice on this cell becomes cracked. Let's number the rows with integers from 1 to n from top to bottom and the columns with integers from 1 to m from left to right. Let's denote a cell on the intersection of the r-th row and the c-th column as (r, c). You are staying in the cell (r1, c1) and this cell is cracked because you've just fallen here from a higher level. You need to fall down through the cell (r2, c2) since the exit to the next level is there. Can you do this? Input The first line contains two integers, n and m (1 ≤ n, m ≤ 500) — the number of rows and columns in the cave description. Each of the next n lines describes the initial state of the level of the cave, each line consists of m characters "." (that is, intact ice) and "X" (cracked ice). The next line contains two integers, r1 and c1 (1 ≤ r1 ≤ n, 1 ≤ c1 ≤ m) — your initial coordinates. It is guaranteed that the description of the cave contains character 'X' in cell (r1, c1), that is, the ice on the starting cell is initially cracked. The next line contains two integers r2 and c2 (1 ≤ r2 ≤ n, 1 ≤ c2 ≤ m) — the coordinates of the cell through which you need to fall. The final cell may coincide with the starting one. Output If you can reach the destination, print 'YES', otherwise print 'NO'. Examples Input 4 6 X...XX ...XX. .X..X. ...... 1 6 2 2 Output YES Input 5 4 .X.. ...X X.X. .... .XX. 5 3 1 1 Output NO Input 4 7 ..X.XX. .XX..X. X...X.. X...... 2 2 1 6 Output YES Note In the first sample test one possible path is: <image> After the first visit of cell (2, 2) the ice on it cracks and when you step there for the second time, your character falls through the ice as intended. Submitted Solution: ``` C=1 def main(): n, m = map(int, input().split()) nm = n * m neigh = [[] for _ in range(nm)] for i in range(0, nm - m, m): for j in range(i, i + m): neigh[j].append(j + m) neigh[j + m].append(j) for i in range(0, nm, m): for j in range(i, i + m - 1): neigh[j].append(j + 1) neigh[j + 1].append(j) field = [c == '.' for _ in range(n) for c in input()] l = [] for _ in (0, 1): i, j = map(int, input().split()) l.append(i * m + j - m - 1) start, stop = l if start == stop: print(("NO", "YES")[any(field[_] for _ in neigh[stop])]) return nxt = (start,) flag, field[stop] = field[stop], True while nxt: cur, nxt = nxt, set() for u in cur: field[u] = False if u == stop: print(("NO", "YES")[not flag or any(field[_] for _ in neigh[stop])]) return for v in neigh[u]: if field[v]: nxt.add(v) print("NO") if __name__ == '__main__': main() ```
instruction
0
106,605
15
213,210
No
output
1
106,605
15
213,211
Provide tags and a correct Python 3 solution for this coding contest problem. Galya is playing one-dimensional Sea Battle on a 1 × n grid. In this game a ships are placed on the grid. Each of the ships consists of b consecutive cells. No cell can be part of two ships, however, the ships can touch each other. Galya doesn't know the ships location. She can shoot to some cells and after each shot she is told if that cell was a part of some ship (this case is called "hit") or not (this case is called "miss"). Galya has already made k shots, all of them were misses. Your task is to calculate the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship. It is guaranteed that there is at least one valid ships placement. Input The first line contains four positive integers n, a, b, k (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ n, 0 ≤ k ≤ n - 1) — the length of the grid, the number of ships on the grid, the length of each ship and the number of shots Galya has already made. The second line contains a string of length n, consisting of zeros and ones. If the i-th character is one, Galya has already made a shot to this cell. Otherwise, she hasn't. It is guaranteed that there are exactly k ones in this string. Output In the first line print the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship. In the second line print the cells Galya should shoot at. Each cell should be printed exactly once. You can print the cells in arbitrary order. The cells are numbered from 1 to n, starting from the left. If there are multiple answers, you can print any of them. Examples Input 5 1 2 1 00100 Output 2 4 2 Input 13 3 2 3 1000000010001 Output 2 7 11 Note There is one ship in the first sample. It can be either to the left or to the right from the shot Galya has already made (the "1" character). So, it is necessary to make two shots: one at the left part, and one at the right part.
instruction
0
106,658
15
213,316
Tags: constructive algorithms, greedy, math Correct Solution: ``` from collections import deque n, a, b, k = map(int, input().split()) s = input() start = False cnt = 0 arr = [] idx = -1 for i in range(n): if(s[i] == "0"): if(start): cnt += 1 else: start = True idx = i cnt = 1 else: if(start): start = False arr.append([cnt, idx]) cnt = 0 if(cnt): arr.append([cnt, idx]) divides = deque([]) for i in range(len(arr)): if(arr[i][0] % b == 0): divides.append(arr[i]) else: divides.appendleft(arr[i]) pos = 0 # print(divides) while(a != 1): if(divides[pos][0] >= b): divides[pos][0] -= b divides[pos][1] += b a -= 1 else: pos += 1 ans = 0 at = [] # print(divides) for i in range(len(arr)): if(divides[i][0]): times = int(divides[i][0]//b) ans += times start = divides[i][1] + b - 1 while(times): at.append(start+1) start += b times -= 1 print(ans) print(*at) ```
output
1
106,658
15
213,317
Provide tags and a correct Python 3 solution for this coding contest problem. Galya is playing one-dimensional Sea Battle on a 1 × n grid. In this game a ships are placed on the grid. Each of the ships consists of b consecutive cells. No cell can be part of two ships, however, the ships can touch each other. Galya doesn't know the ships location. She can shoot to some cells and after each shot she is told if that cell was a part of some ship (this case is called "hit") or not (this case is called "miss"). Galya has already made k shots, all of them were misses. Your task is to calculate the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship. It is guaranteed that there is at least one valid ships placement. Input The first line contains four positive integers n, a, b, k (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ n, 0 ≤ k ≤ n - 1) — the length of the grid, the number of ships on the grid, the length of each ship and the number of shots Galya has already made. The second line contains a string of length n, consisting of zeros and ones. If the i-th character is one, Galya has already made a shot to this cell. Otherwise, she hasn't. It is guaranteed that there are exactly k ones in this string. Output In the first line print the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship. In the second line print the cells Galya should shoot at. Each cell should be printed exactly once. You can print the cells in arbitrary order. The cells are numbered from 1 to n, starting from the left. If there are multiple answers, you can print any of them. Examples Input 5 1 2 1 00100 Output 2 4 2 Input 13 3 2 3 1000000010001 Output 2 7 11 Note There is one ship in the first sample. It can be either to the left or to the right from the shot Galya has already made (the "1" character). So, it is necessary to make two shots: one at the left part, and one at the right part.
instruction
0
106,659
15
213,318
Tags: constructive algorithms, greedy, math Correct Solution: ``` import heapq from itertools import groupby def read_int(): return int(input().strip()) def read_ints(): return list(map(int, input().strip().split(' '))) def ilen(ll): return sum(1 for _ in ll) def solve(): """ 13 3 2 3 1000000010001 3 1 000000000 a, b - how many shots do you need to reduce maximum possible number of ships in a segment? 1 1 1 0 0 """ n, a, b, k = read_ints() s = input().strip() segments = [] # (length, start) maximum_count = 0 for start, vals in groupby(enumerate(s), key=lambda p: p[1]): val = next(vals) if val[1] == '0': segments.append((val[0], 1+ilen(vals))) maximum_count += segments[-1][1]//b shots = [] while maximum_count >= a: start, length = segments.pop() if length//b > 0: shots.append(start+b-1) segments.append((start+b, length-b)) maximum_count -= 1 print(len(shots)) print(' '.join(map(lambda shot: str(shot+1), shots))) if __name__ == '__main__': solve() ```
output
1
106,659
15
213,319
Provide tags and a correct Python 3 solution for this coding contest problem. Galya is playing one-dimensional Sea Battle on a 1 × n grid. In this game a ships are placed on the grid. Each of the ships consists of b consecutive cells. No cell can be part of two ships, however, the ships can touch each other. Galya doesn't know the ships location. She can shoot to some cells and after each shot she is told if that cell was a part of some ship (this case is called "hit") or not (this case is called "miss"). Galya has already made k shots, all of them were misses. Your task is to calculate the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship. It is guaranteed that there is at least one valid ships placement. Input The first line contains four positive integers n, a, b, k (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ n, 0 ≤ k ≤ n - 1) — the length of the grid, the number of ships on the grid, the length of each ship and the number of shots Galya has already made. The second line contains a string of length n, consisting of zeros and ones. If the i-th character is one, Galya has already made a shot to this cell. Otherwise, she hasn't. It is guaranteed that there are exactly k ones in this string. Output In the first line print the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship. In the second line print the cells Galya should shoot at. Each cell should be printed exactly once. You can print the cells in arbitrary order. The cells are numbered from 1 to n, starting from the left. If there are multiple answers, you can print any of them. Examples Input 5 1 2 1 00100 Output 2 4 2 Input 13 3 2 3 1000000010001 Output 2 7 11 Note There is one ship in the first sample. It can be either to the left or to the right from the shot Galya has already made (the "1" character). So, it is necessary to make two shots: one at the left part, and one at the right part.
instruction
0
106,660
15
213,320
Tags: constructive algorithms, greedy, math Correct Solution: ``` n, s, b, k = map(int, input().split()) a = list('1' + input() + '1') ans = [] cnt = 0 for i in range(len(a)): if a[i] == '0': cnt += 1 else: cnt = 0 if cnt == b: if s > 1: s -= 1 else: ans.append(i) cnt = 0 print(len(ans)) for i in range(len(ans)): print(ans[i], end=' ') ```
output
1
106,660
15
213,321
Provide tags and a correct Python 3 solution for this coding contest problem. Galya is playing one-dimensional Sea Battle on a 1 × n grid. In this game a ships are placed on the grid. Each of the ships consists of b consecutive cells. No cell can be part of two ships, however, the ships can touch each other. Galya doesn't know the ships location. She can shoot to some cells and after each shot she is told if that cell was a part of some ship (this case is called "hit") or not (this case is called "miss"). Galya has already made k shots, all of them were misses. Your task is to calculate the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship. It is guaranteed that there is at least one valid ships placement. Input The first line contains four positive integers n, a, b, k (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ n, 0 ≤ k ≤ n - 1) — the length of the grid, the number of ships on the grid, the length of each ship and the number of shots Galya has already made. The second line contains a string of length n, consisting of zeros and ones. If the i-th character is one, Galya has already made a shot to this cell. Otherwise, she hasn't. It is guaranteed that there are exactly k ones in this string. Output In the first line print the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship. In the second line print the cells Galya should shoot at. Each cell should be printed exactly once. You can print the cells in arbitrary order. The cells are numbered from 1 to n, starting from the left. If there are multiple answers, you can print any of them. Examples Input 5 1 2 1 00100 Output 2 4 2 Input 13 3 2 3 1000000010001 Output 2 7 11 Note There is one ship in the first sample. It can be either to the left or to the right from the shot Galya has already made (the "1" character). So, it is necessary to make two shots: one at the left part, and one at the right part.
instruction
0
106,661
15
213,322
Tags: constructive algorithms, greedy, math Correct Solution: ``` import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') INF = 10 ** 18 MOD = 10**9+7 Ri = lambda : [int(x) for x in sys.stdin.readline().split()] ri = lambda : sys.stdin.readline().strip() n,a,b,k = Ri() s = ri() cnt = 0 totans = 0; ans = [] for i in range(len(s)): if s[i] == '0': cnt+=1 else: cnt= 0 if cnt == b: if a > 1: a-=1 else: ans.append(i+1) totans+=1 cnt = 0 print(totans) print(*ans) ```
output
1
106,661
15
213,323
Provide tags and a correct Python 3 solution for this coding contest problem. Galya is playing one-dimensional Sea Battle on a 1 × n grid. In this game a ships are placed on the grid. Each of the ships consists of b consecutive cells. No cell can be part of two ships, however, the ships can touch each other. Galya doesn't know the ships location. She can shoot to some cells and after each shot she is told if that cell was a part of some ship (this case is called "hit") or not (this case is called "miss"). Galya has already made k shots, all of them were misses. Your task is to calculate the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship. It is guaranteed that there is at least one valid ships placement. Input The first line contains four positive integers n, a, b, k (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ n, 0 ≤ k ≤ n - 1) — the length of the grid, the number of ships on the grid, the length of each ship and the number of shots Galya has already made. The second line contains a string of length n, consisting of zeros and ones. If the i-th character is one, Galya has already made a shot to this cell. Otherwise, she hasn't. It is guaranteed that there are exactly k ones in this string. Output In the first line print the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship. In the second line print the cells Galya should shoot at. Each cell should be printed exactly once. You can print the cells in arbitrary order. The cells are numbered from 1 to n, starting from the left. If there are multiple answers, you can print any of them. Examples Input 5 1 2 1 00100 Output 2 4 2 Input 13 3 2 3 1000000010001 Output 2 7 11 Note There is one ship in the first sample. It can be either to the left or to the right from the shot Galya has already made (the "1" character). So, it is necessary to make two shots: one at the left part, and one at the right part.
instruction
0
106,662
15
213,324
Tags: constructive algorithms, greedy, math Correct Solution: ``` # Why do we fall ? So we can learn to pick ourselves up. from itertools import groupby n,a,b,k = map(int,input().split()) s = input() sg = [list(g) for s,g in groupby(s)] ll = 0 hits = [] for i in range(0,len(sg)): if sg[i][0] == '0' and len(sg[i]) >= b: for hit in range(b-1,len(sg[i]),b): hits.append(hit+ll+1) ll += len(sg[i]) else: ll += len(sg[i]) # print(hits) hits = hits[a-1:] print(len(hits)) print(*hits) """ 13 3 2 3 1000000010001 15 3 2 3 1000000000010001 """ ```
output
1
106,662
15
213,325
Provide tags and a correct Python 3 solution for this coding contest problem. Galya is playing one-dimensional Sea Battle on a 1 × n grid. In this game a ships are placed on the grid. Each of the ships consists of b consecutive cells. No cell can be part of two ships, however, the ships can touch each other. Galya doesn't know the ships location. She can shoot to some cells and after each shot she is told if that cell was a part of some ship (this case is called "hit") or not (this case is called "miss"). Galya has already made k shots, all of them were misses. Your task is to calculate the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship. It is guaranteed that there is at least one valid ships placement. Input The first line contains four positive integers n, a, b, k (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ n, 0 ≤ k ≤ n - 1) — the length of the grid, the number of ships on the grid, the length of each ship and the number of shots Galya has already made. The second line contains a string of length n, consisting of zeros and ones. If the i-th character is one, Galya has already made a shot to this cell. Otherwise, she hasn't. It is guaranteed that there are exactly k ones in this string. Output In the first line print the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship. In the second line print the cells Galya should shoot at. Each cell should be printed exactly once. You can print the cells in arbitrary order. The cells are numbered from 1 to n, starting from the left. If there are multiple answers, you can print any of them. Examples Input 5 1 2 1 00100 Output 2 4 2 Input 13 3 2 3 1000000010001 Output 2 7 11 Note There is one ship in the first sample. It can be either to the left or to the right from the shot Galya has already made (the "1" character). So, it is necessary to make two shots: one at the left part, and one at the right part.
instruction
0
106,663
15
213,326
Tags: constructive algorithms, greedy, math Correct Solution: ``` # Why do we fall ? So we can learn to pick ourselves up. from itertools import groupby n,a,b,k = map(int,input().split()) s = input() sg = [list(g) for s,g in groupby(s)] ll = 0 hits = [] for i in range(0,len(sg)): if sg[i][0] == '0' and len(sg[i]) >= b: for hit in range(b-1,len(sg[i]),b): hits.append(hit+ll+1) ll += len(sg[i]) else: ll += len(sg[i]) # print(hits) # We remove number of (ships-1) from the total number of hits because we are hitting at every possible location where # where the ship can be placed and since we want to hit AT LEAST ONE SHIP, removing (ships-1) will still hit at least one ship hits = hits[a-1:] print(len(hits)) print(*hits) """ 13 3 2 3 1000000010001 15 3 2 3 1000000000010001 """ ```
output
1
106,663
15
213,327
Provide tags and a correct Python 3 solution for this coding contest problem. Galya is playing one-dimensional Sea Battle on a 1 × n grid. In this game a ships are placed on the grid. Each of the ships consists of b consecutive cells. No cell can be part of two ships, however, the ships can touch each other. Galya doesn't know the ships location. She can shoot to some cells and after each shot she is told if that cell was a part of some ship (this case is called "hit") or not (this case is called "miss"). Galya has already made k shots, all of them were misses. Your task is to calculate the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship. It is guaranteed that there is at least one valid ships placement. Input The first line contains four positive integers n, a, b, k (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ n, 0 ≤ k ≤ n - 1) — the length of the grid, the number of ships on the grid, the length of each ship and the number of shots Galya has already made. The second line contains a string of length n, consisting of zeros and ones. If the i-th character is one, Galya has already made a shot to this cell. Otherwise, she hasn't. It is guaranteed that there are exactly k ones in this string. Output In the first line print the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship. In the second line print the cells Galya should shoot at. Each cell should be printed exactly once. You can print the cells in arbitrary order. The cells are numbered from 1 to n, starting from the left. If there are multiple answers, you can print any of them. Examples Input 5 1 2 1 00100 Output 2 4 2 Input 13 3 2 3 1000000010001 Output 2 7 11 Note There is one ship in the first sample. It can be either to the left or to the right from the shot Galya has already made (the "1" character). So, it is necessary to make two shots: one at the left part, and one at the right part.
instruction
0
106,664
15
213,328
Tags: constructive algorithms, greedy, math Correct Solution: ``` n,a,b,k=map(int,input().split()) A=input() B=A.split('1') C=[] l=1 for i in B: if len(i)>=b: for j in range(b-1,len(i),b): C.append(j+l) l+=len(i)+1 C=C[:len(C)-a+1] print(len(C)) print(' '.join(list(map(str,C)))) ```
output
1
106,664
15
213,329
Provide tags and a correct Python 3 solution for this coding contest problem. Galya is playing one-dimensional Sea Battle on a 1 × n grid. In this game a ships are placed on the grid. Each of the ships consists of b consecutive cells. No cell can be part of two ships, however, the ships can touch each other. Galya doesn't know the ships location. She can shoot to some cells and after each shot she is told if that cell was a part of some ship (this case is called "hit") or not (this case is called "miss"). Galya has already made k shots, all of them were misses. Your task is to calculate the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship. It is guaranteed that there is at least one valid ships placement. Input The first line contains four positive integers n, a, b, k (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ n, 0 ≤ k ≤ n - 1) — the length of the grid, the number of ships on the grid, the length of each ship and the number of shots Galya has already made. The second line contains a string of length n, consisting of zeros and ones. If the i-th character is one, Galya has already made a shot to this cell. Otherwise, she hasn't. It is guaranteed that there are exactly k ones in this string. Output In the first line print the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship. In the second line print the cells Galya should shoot at. Each cell should be printed exactly once. You can print the cells in arbitrary order. The cells are numbered from 1 to n, starting from the left. If there are multiple answers, you can print any of them. Examples Input 5 1 2 1 00100 Output 2 4 2 Input 13 3 2 3 1000000010001 Output 2 7 11 Note There is one ship in the first sample. It can be either to the left or to the right from the shot Galya has already made (the "1" character). So, it is necessary to make two shots: one at the left part, and one at the right part.
instruction
0
106,665
15
213,330
Tags: constructive algorithms, greedy, math Correct Solution: ``` '''input 5 4 1 0 00000 ''' from sys import stdin import collections import math def get_working(string): aux = [] first = None if string[0] == 1: pass else: first = -1 for i in range(len(string)): if string[i] == '1': if first == None: first = i elif first != None: aux.append([first, i, (i - first - 1)]) first = i if first != None: aux.append([first, len(string), (len(string) - first) - 1]) return aux # main starts n, a, b, k = list(map(int, stdin.readline().split())) string = list(stdin.readline().strip()) ans = 0 working = get_working(string) # print(working) current = a flag = 0 for i in working: if flag == 1: break start, end, gap = i j = end - 1 if gap//b > 0: while j > start: if current == 0: flag = 1 break if (j - start)// b > 0: for k in range(j, j - b, -1): string[k] = '2' j -= b current -= 1 else: break ans = [] count = 0 for i in range(len(string)): if string[i] == '0': if i > 0 and string[i - 1] == '0': count += 1 else: count = 1 if count == b: string[i] = 'b' ans.append(i + 1) count = 0 for i in range(len(string)): if string[i] == '2': ans.append(i + 1) break # print(string) print(len(ans)) print(*ans) # print(ans) # for i in range(n): # if string[i] == 'b': # print(i + 1, end = ' ') ```
output
1
106,665
15
213,331
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Galya is playing one-dimensional Sea Battle on a 1 × n grid. In this game a ships are placed on the grid. Each of the ships consists of b consecutive cells. No cell can be part of two ships, however, the ships can touch each other. Galya doesn't know the ships location. She can shoot to some cells and after each shot she is told if that cell was a part of some ship (this case is called "hit") or not (this case is called "miss"). Galya has already made k shots, all of them were misses. Your task is to calculate the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship. It is guaranteed that there is at least one valid ships placement. Input The first line contains four positive integers n, a, b, k (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ n, 0 ≤ k ≤ n - 1) — the length of the grid, the number of ships on the grid, the length of each ship and the number of shots Galya has already made. The second line contains a string of length n, consisting of zeros and ones. If the i-th character is one, Galya has already made a shot to this cell. Otherwise, she hasn't. It is guaranteed that there are exactly k ones in this string. Output In the first line print the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship. In the second line print the cells Galya should shoot at. Each cell should be printed exactly once. You can print the cells in arbitrary order. The cells are numbered from 1 to n, starting from the left. If there are multiple answers, you can print any of them. Examples Input 5 1 2 1 00100 Output 2 4 2 Input 13 3 2 3 1000000010001 Output 2 7 11 Note There is one ship in the first sample. It can be either to the left or to the right from the shot Galya has already made (the "1" character). So, it is necessary to make two shots: one at the left part, and one at the right part. Submitted Solution: ``` n,a,b,k = map(int,input().split()) A=['1'] + list(input()) + ['1'] answer =[] n+= 2 i = 0 while i <= n-2: per = 0 for j in range(i+1, i+b+1): if j > n - 1: break if A[j] == '1': i = j per = 1 break if per == 0: i = j answer.append(i) lens = len(answer) print(lens - a +1) print(' '.join(map(str, answer[0:lens-a+1]))) ```
instruction
0
106,666
15
213,332
Yes
output
1
106,666
15
213,333
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Galya is playing one-dimensional Sea Battle on a 1 × n grid. In this game a ships are placed on the grid. Each of the ships consists of b consecutive cells. No cell can be part of two ships, however, the ships can touch each other. Galya doesn't know the ships location. She can shoot to some cells and after each shot she is told if that cell was a part of some ship (this case is called "hit") or not (this case is called "miss"). Galya has already made k shots, all of them were misses. Your task is to calculate the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship. It is guaranteed that there is at least one valid ships placement. Input The first line contains four positive integers n, a, b, k (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ n, 0 ≤ k ≤ n - 1) — the length of the grid, the number of ships on the grid, the length of each ship and the number of shots Galya has already made. The second line contains a string of length n, consisting of zeros and ones. If the i-th character is one, Galya has already made a shot to this cell. Otherwise, she hasn't. It is guaranteed that there are exactly k ones in this string. Output In the first line print the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship. In the second line print the cells Galya should shoot at. Each cell should be printed exactly once. You can print the cells in arbitrary order. The cells are numbered from 1 to n, starting from the left. If there are multiple answers, you can print any of them. Examples Input 5 1 2 1 00100 Output 2 4 2 Input 13 3 2 3 1000000010001 Output 2 7 11 Note There is one ship in the first sample. It can be either to the left or to the right from the shot Galya has already made (the "1" character). So, it is necessary to make two shots: one at the left part, and one at the right part. Submitted Solution: ``` n,a,b,k=[int(i) for i in input().split()] s=input() l=[] i=0 j=0 while i<len(s): if s[i]=="1": j=0 else : j+=1 if(j%b)==0: l+=[i+1] j=0 i+=1 l=l[a-1:] print(len(l)) print(*l) ```
instruction
0
106,667
15
213,334
Yes
output
1
106,667
15
213,335
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Galya is playing one-dimensional Sea Battle on a 1 × n grid. In this game a ships are placed on the grid. Each of the ships consists of b consecutive cells. No cell can be part of two ships, however, the ships can touch each other. Galya doesn't know the ships location. She can shoot to some cells and after each shot she is told if that cell was a part of some ship (this case is called "hit") or not (this case is called "miss"). Galya has already made k shots, all of them were misses. Your task is to calculate the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship. It is guaranteed that there is at least one valid ships placement. Input The first line contains four positive integers n, a, b, k (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ n, 0 ≤ k ≤ n - 1) — the length of the grid, the number of ships on the grid, the length of each ship and the number of shots Galya has already made. The second line contains a string of length n, consisting of zeros and ones. If the i-th character is one, Galya has already made a shot to this cell. Otherwise, she hasn't. It is guaranteed that there are exactly k ones in this string. Output In the first line print the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship. In the second line print the cells Galya should shoot at. Each cell should be printed exactly once. You can print the cells in arbitrary order. The cells are numbered from 1 to n, starting from the left. If there are multiple answers, you can print any of them. Examples Input 5 1 2 1 00100 Output 2 4 2 Input 13 3 2 3 1000000010001 Output 2 7 11 Note There is one ship in the first sample. It can be either to the left or to the right from the shot Galya has already made (the "1" character). So, it is necessary to make two shots: one at the left part, and one at the right part. Submitted Solution: ``` n,a,b,k = map(int, input().split()) open = 0 must = 0 must_arr = [] for i,c in enumerate(input()): if c == '1': open = 0 else: open += 1 if open == b: open = 0 must += 1 if must >= a: must_arr.append(i+1) must -= a - 1 print(must) print(*must_arr) ```
instruction
0
106,668
15
213,336
Yes
output
1
106,668
15
213,337
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Galya is playing one-dimensional Sea Battle on a 1 × n grid. In this game a ships are placed on the grid. Each of the ships consists of b consecutive cells. No cell can be part of two ships, however, the ships can touch each other. Galya doesn't know the ships location. She can shoot to some cells and after each shot she is told if that cell was a part of some ship (this case is called "hit") or not (this case is called "miss"). Galya has already made k shots, all of them were misses. Your task is to calculate the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship. It is guaranteed that there is at least one valid ships placement. Input The first line contains four positive integers n, a, b, k (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ n, 0 ≤ k ≤ n - 1) — the length of the grid, the number of ships on the grid, the length of each ship and the number of shots Galya has already made. The second line contains a string of length n, consisting of zeros and ones. If the i-th character is one, Galya has already made a shot to this cell. Otherwise, she hasn't. It is guaranteed that there are exactly k ones in this string. Output In the first line print the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship. In the second line print the cells Galya should shoot at. Each cell should be printed exactly once. You can print the cells in arbitrary order. The cells are numbered from 1 to n, starting from the left. If there are multiple answers, you can print any of them. Examples Input 5 1 2 1 00100 Output 2 4 2 Input 13 3 2 3 1000000010001 Output 2 7 11 Note There is one ship in the first sample. It can be either to the left or to the right from the shot Galya has already made (the "1" character). So, it is necessary to make two shots: one at the left part, and one at the right part. Submitted Solution: ``` "Codeforces Round #384 (Div. 2)" "C. Vladik and fractions" # y=int(input()) # a=y # b=a+1 # c=y*b # if y==1: # print(-1) # else: # print(a,b,c) "Technocup 2017 - Elimination Round 2" "D. Sea Battle" n,a,b,k=map(int,input().split()) s=list(input()) n=len(s) lz=[] zeros=[] indexes=[] flage=0 if s[0]=="0": lz.append(0) flage=1 for i in range(1,n): if flage==1 and s[i]=="1": zeros.append(i-1-(lz[-1])+1) lz.append(i-1) flage=0 elif flage==0 and s[i]=="0": lz.append(i) flage=1 if s[-1]=="0": zeros.append(n-1-(lz[-1])+1) lz.append(n-1) min_no_spaces=(a-1)*b spaces_left=n-k l=len(lz) # print(lz) # print(zeros) shotes=0 for i in range(len(zeros)): h=i*2 if min_no_spaces!=0: # print(min_no_spaces) if min_no_spaces>=zeros[i]: min_no_spaces-=(int(zeros[i]/b))*b elif min_no_spaces<zeros[i]: shotes+=int((zeros[i]-min_no_spaces)/b) for j in range(int((zeros[i]-min_no_spaces)/b)): indexes.append(lz[h]+((j+1)*b)) min_no_spaces=0 elif min_no_spaces==0: # print(min_no_spaces) shotes+=int(zeros[i]/b) for j in range(int(zeros[i]/b)): indexes.append(lz[h]+((j+1)*b)) print(shotes) for i in indexes: print(i," ",end="",sep="") ```
instruction
0
106,669
15
213,338
Yes
output
1
106,669
15
213,339
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Galya is playing one-dimensional Sea Battle on a 1 × n grid. In this game a ships are placed on the grid. Each of the ships consists of b consecutive cells. No cell can be part of two ships, however, the ships can touch each other. Galya doesn't know the ships location. She can shoot to some cells and after each shot she is told if that cell was a part of some ship (this case is called "hit") or not (this case is called "miss"). Galya has already made k shots, all of them were misses. Your task is to calculate the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship. It is guaranteed that there is at least one valid ships placement. Input The first line contains four positive integers n, a, b, k (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ n, 0 ≤ k ≤ n - 1) — the length of the grid, the number of ships on the grid, the length of each ship and the number of shots Galya has already made. The second line contains a string of length n, consisting of zeros and ones. If the i-th character is one, Galya has already made a shot to this cell. Otherwise, she hasn't. It is guaranteed that there are exactly k ones in this string. Output In the first line print the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship. In the second line print the cells Galya should shoot at. Each cell should be printed exactly once. You can print the cells in arbitrary order. The cells are numbered from 1 to n, starting from the left. If there are multiple answers, you can print any of them. Examples Input 5 1 2 1 00100 Output 2 4 2 Input 13 3 2 3 1000000010001 Output 2 7 11 Note There is one ship in the first sample. It can be either to the left or to the right from the shot Galya has already made (the "1" character). So, it is necessary to make two shots: one at the left part, and one at the right part. Submitted Solution: ``` '''input 5 1 2 1 00100 ''' from sys import stdin import collections import math def get_working(string): aux = [] first = None if string[0] == 1: pass else: first = -1 for i in range(len(string)): if string[i] == '1': if first == None: first = i elif first != None: aux.append([first, i, (i - first - 1)]) first = i if first != None: aux.append([first, len(string), (len(string) - first) - 1]) return aux # main starts n, a, b, k = list(map(int, stdin.readline().split())) string = list(stdin.readline().strip()) ans = 0 working = get_working(string) # print(working) current = a flag = 0 for i in working: if flag == 1: break start, end, gap = i j = end - 1 if gap//b > 0: while j > start: if current == 0: flag = 1 break if (j - start)// b > 0: for k in range(j, j - b, -1): string[k] = '2' j -= b current -= 1 else: break ans = [] count = 0 for i in range(len(string)): if string[i] == '0': if i > 0 and string[i - 1] == '0': count += 1 else: count = 1 if count == b: string[i] = 'b' ans.append(i + 1) count = 0 for i in range(len(string)): if string[i] == '2': if i > 0 and string[i - 1] == '2': count += 1 else: count = 1 if count == b: string[i] = 'b' ans.append(i + 1) count = 0 break print(string) print(len(ans)) print(*ans) # print(ans) # for i in range(n): # if string[i] == 'b': # print(i + 1, end = ' ') ```
instruction
0
106,670
15
213,340
No
output
1
106,670
15
213,341
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Galya is playing one-dimensional Sea Battle on a 1 × n grid. In this game a ships are placed on the grid. Each of the ships consists of b consecutive cells. No cell can be part of two ships, however, the ships can touch each other. Galya doesn't know the ships location. She can shoot to some cells and after each shot she is told if that cell was a part of some ship (this case is called "hit") or not (this case is called "miss"). Galya has already made k shots, all of them were misses. Your task is to calculate the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship. It is guaranteed that there is at least one valid ships placement. Input The first line contains four positive integers n, a, b, k (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ n, 0 ≤ k ≤ n - 1) — the length of the grid, the number of ships on the grid, the length of each ship and the number of shots Galya has already made. The second line contains a string of length n, consisting of zeros and ones. If the i-th character is one, Galya has already made a shot to this cell. Otherwise, she hasn't. It is guaranteed that there are exactly k ones in this string. Output In the first line print the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship. In the second line print the cells Galya should shoot at. Each cell should be printed exactly once. You can print the cells in arbitrary order. The cells are numbered from 1 to n, starting from the left. If there are multiple answers, you can print any of them. Examples Input 5 1 2 1 00100 Output 2 4 2 Input 13 3 2 3 1000000010001 Output 2 7 11 Note There is one ship in the first sample. It can be either to the left or to the right from the shot Galya has already made (the "1" character). So, it is necessary to make two shots: one at the left part, and one at the right part. Submitted Solution: ``` n, a, b, k = map(int, input().split()) s = input() start = False cnt = 0 arr = [] idx = -1 for i in range(n): if(s[i] == "0"): if(start): cnt += 1 else: start = True idx = i cnt = 1 else: if(start): start = False arr.append([cnt, idx]) cnt = 0 if(cnt): arr.append([cnt, idx]) pos = 0 while(a != 1): if(arr[pos][0] >= b): arr[pos][0] -= b arr[pos][1] += b else: pos += 1 a -= 1 ans = 0 at = [] for i in range(len(arr)): times = int(arr[i][0]//b) ans += times start = arr[i][1] + b - 1 while(times): at.append(start+1) start += b times -= 1 print(ans) print(*at) ```
instruction
0
106,671
15
213,342
No
output
1
106,671
15
213,343
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Galya is playing one-dimensional Sea Battle on a 1 × n grid. In this game a ships are placed on the grid. Each of the ships consists of b consecutive cells. No cell can be part of two ships, however, the ships can touch each other. Galya doesn't know the ships location. She can shoot to some cells and after each shot she is told if that cell was a part of some ship (this case is called "hit") or not (this case is called "miss"). Galya has already made k shots, all of them were misses. Your task is to calculate the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship. It is guaranteed that there is at least one valid ships placement. Input The first line contains four positive integers n, a, b, k (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ n, 0 ≤ k ≤ n - 1) — the length of the grid, the number of ships on the grid, the length of each ship and the number of shots Galya has already made. The second line contains a string of length n, consisting of zeros and ones. If the i-th character is one, Galya has already made a shot to this cell. Otherwise, she hasn't. It is guaranteed that there are exactly k ones in this string. Output In the first line print the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship. In the second line print the cells Galya should shoot at. Each cell should be printed exactly once. You can print the cells in arbitrary order. The cells are numbered from 1 to n, starting from the left. If there are multiple answers, you can print any of them. Examples Input 5 1 2 1 00100 Output 2 4 2 Input 13 3 2 3 1000000010001 Output 2 7 11 Note There is one ship in the first sample. It can be either to the left or to the right from the shot Galya has already made (the "1" character). So, it is necessary to make two shots: one at the left part, and one at the right part. Submitted Solution: ``` '''input 5 1 2 1 00100 ''' from sys import stdin import collections def get_working(string): aux = [] first = None if string[0] == 1: pass else: first = -1 for i in range(len(string)): if string[i] == '1': if first == None: first = i elif first != None: aux.append([first, i, (i - first - 1)]) first = i if first != None: aux.append([first, len(string), (len(string) - first) - 1]) aux = sorted(aux, key = lambda x:x[2]) return aux # main starts n, a, b, k = list(map(int, stdin.readline().split())) string = list(stdin.readline().strip()) ans = 0 working = get_working(string) # print(working) current = a for i in working: start, end, gap = i cap = gap//b mark = start + 1 while mark < end: if mark - start == cap * b: ans += 1 string[mark] = 'b' mark += 1 print(ans) for i in range(n): if string[i] == 'b': print(i + 1, end = ' ') ```
instruction
0
106,672
15
213,344
No
output
1
106,672
15
213,345
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Galya is playing one-dimensional Sea Battle on a 1 × n grid. In this game a ships are placed on the grid. Each of the ships consists of b consecutive cells. No cell can be part of two ships, however, the ships can touch each other. Galya doesn't know the ships location. She can shoot to some cells and after each shot she is told if that cell was a part of some ship (this case is called "hit") or not (this case is called "miss"). Galya has already made k shots, all of them were misses. Your task is to calculate the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship. It is guaranteed that there is at least one valid ships placement. Input The first line contains four positive integers n, a, b, k (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ n, 0 ≤ k ≤ n - 1) — the length of the grid, the number of ships on the grid, the length of each ship and the number of shots Galya has already made. The second line contains a string of length n, consisting of zeros and ones. If the i-th character is one, Galya has already made a shot to this cell. Otherwise, she hasn't. It is guaranteed that there are exactly k ones in this string. Output In the first line print the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship. In the second line print the cells Galya should shoot at. Each cell should be printed exactly once. You can print the cells in arbitrary order. The cells are numbered from 1 to n, starting from the left. If there are multiple answers, you can print any of them. Examples Input 5 1 2 1 00100 Output 2 4 2 Input 13 3 2 3 1000000010001 Output 2 7 11 Note There is one ship in the first sample. It can be either to the left or to the right from the shot Galya has already made (the "1" character). So, it is necessary to make two shots: one at the left part, and one at the right part. Submitted Solution: ``` from collections import deque n, a, b, k = map(int, input().split()) s = input() start = False cnt = 0 arr = [] idx = -1 for i in range(n): if(s[i] == "0"): if(start): cnt += 1 else: start = True idx = i cnt = 1 else: if(start): start = False arr.append([cnt, idx]) cnt = 0 if(cnt): arr.append([cnt, idx]) divides = deque([]) for i in range(len(arr)): if(arr[i][0] % b == 0): divides.appendleft(arr[i]) else: divides.append(arr[i]) pos = 0 while(a != 1): if(divides[pos][0] >= b): divides[pos][0] -= b divides[pos][1] += b else: pos += 1 a -= 1 ans = 0 at = [] for i in range(len(arr)): if(divides[i][0]): times = int(divides[i][0]//b) ans += times start = divides[i][1] + b - 1 while(times): at.append(start+1) start += b times -= 1 print(ans) print(*at) ```
instruction
0
106,673
15
213,346
No
output
1
106,673
15
213,347
Provide tags and a correct Python 3 solution for this coding contest problem. Kilani is playing a game with his friends. This game can be represented as a grid of size n × m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell). The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that. The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends. Input The first line contains three integers n, m and p (1 ≤ n, m ≤ 1000, 1 ≤ p ≤ 9) — the size of the grid and the number of players. The second line contains p integers s_i (1 ≤ s ≤ 10^9) — the speed of the expansion for every player. The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 ≤ x ≤ p) denotes the castle owned by player x. It is guaranteed, that each player has at least one castle on the grid. Output Print p integers — the number of cells controlled by each player after the game ends. Examples Input 3 3 2 1 1 1.. ... ..2 Output 6 3 Input 3 4 4 1 1 1 1 .... #... 1234 Output 1 4 3 3 Note The picture below show the game before it started, the game after the first round and game after the second round in the first example: <image> In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
instruction
0
107,063
15
214,126
Tags: dfs and similar, graphs, implementation, shortest paths Correct Solution: ``` import os import sys from io import BytesIO, IOBase from collections import defaultdict, deque, Counter, OrderedDict import threading from heapq import * dx,dy = [1,-1,0,0],[0,0,1,-1] def main(): n, m, p = map(int,input().split()) s = [*map(int,input().split())] D = [[*input()] for _ in range(n)] S = [[-1]*m for _ in range(n)] ans = [0]*p q=[deque() for _ in range(p)] new_q = [deque() for _ in range(p)] for i in range(n): for j in range(m): if 49<=ord(D[i][j])<=57: x = int(D[i][j]) S[i][j] = x q[x-1].append([i,j,x,0]) ans[x-1] += 1 while 1: flag = False for i in range(p): if len(q[i]): flag = True if not flag: break for i in range(p): while q[i]: x,y,num,move = q[i].popleft() for j in range(4): nx,ny = x+dx[j],y+dy[j] if 0<=nx<n and 0<=ny<m and D[nx][ny] == '.' and S[nx][ny] ==-1 and move < s[num-1]: S[nx][ny] = num ans[num-1] += 1 if move + 1 == s[num-1]: new_q[i].append([nx,ny,num,0]) else:q[i].append([nx,ny,num,move+1]) if len(new_q[i]): q[i] = new_q[i] new_q[i] = deque() print(*ans) BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion if __name__ == "__main__": """sys.setrecursionlimit(400000) threading.stack_size(40960000) thread = threading.Thread(target=main) thread.start()""" main() ```
output
1
107,063
15
214,127
Provide tags and a correct Python 3 solution for this coding contest problem. Kilani is playing a game with his friends. This game can be represented as a grid of size n × m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell). The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that. The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends. Input The first line contains three integers n, m and p (1 ≤ n, m ≤ 1000, 1 ≤ p ≤ 9) — the size of the grid and the number of players. The second line contains p integers s_i (1 ≤ s ≤ 10^9) — the speed of the expansion for every player. The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 ≤ x ≤ p) denotes the castle owned by player x. It is guaranteed, that each player has at least one castle on the grid. Output Print p integers — the number of cells controlled by each player after the game ends. Examples Input 3 3 2 1 1 1.. ... ..2 Output 6 3 Input 3 4 4 1 1 1 1 .... #... 1234 Output 1 4 3 3 Note The picture below show the game before it started, the game after the first round and game after the second round in the first example: <image> In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
instruction
0
107,064
15
214,128
Tags: dfs and similar, graphs, implementation, shortest paths Correct Solution: ``` import sys from collections import deque as dq h,w,P = [int(x) for x in input().split()] S = [int(x) for x in input().split()] board = [] for b in sys.stdin.read(): for c in b: if c=='.': board.append(-1) elif 0<=ord(c)-49<=9: board.append(ord(c)-49) elif c=='#': board.append(-2) new_castles = [dq() for _ in range(P)] for pos in range(h*w): if board[pos]>=0: new_castles[board[pos]].append((pos,0)) Q = dq() player_Q = dq(p for p in range(P) if new_castles[p]) while player_Q: p = player_Q.popleft() Q = new_castles[p] # Do S[p] moves goal = Q[-1][1] + S[p] while Q and Q[0][1] != goal: pos,moves = Q.popleft() y = pos//w x = pos - y*w if 0<x and board[pos-1]==-1: board[pos-1]=p Q.append((pos-1,moves+1)) if x<w-1 and board[pos+1]==-1: board[pos+1]=p Q.append((pos+1,moves+1)) if 0<y and board[pos-w]==-1: board[pos-w]=p Q.append((pos-w,moves+1)) if y<h-1 and board[pos+w]==-1: board[pos+w]=p Q.append((pos+w,moves+1)) if Q: player_Q.append(p) count = [0 for _ in range(P)] for x in board: if x >= 0: count[x] += 1 print(*count) ```
output
1
107,064
15
214,129
Provide tags and a correct Python 3 solution for this coding contest problem. Kilani is playing a game with his friends. This game can be represented as a grid of size n × m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell). The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that. The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends. Input The first line contains three integers n, m and p (1 ≤ n, m ≤ 1000, 1 ≤ p ≤ 9) — the size of the grid and the number of players. The second line contains p integers s_i (1 ≤ s ≤ 10^9) — the speed of the expansion for every player. The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 ≤ x ≤ p) denotes the castle owned by player x. It is guaranteed, that each player has at least one castle on the grid. Output Print p integers — the number of cells controlled by each player after the game ends. Examples Input 3 3 2 1 1 1.. ... ..2 Output 6 3 Input 3 4 4 1 1 1 1 .... #... 1234 Output 1 4 3 3 Note The picture below show the game before it started, the game after the first round and game after the second round in the first example: <image> In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
instruction
0
107,065
15
214,130
Tags: dfs and similar, graphs, implementation, shortest paths Correct Solution: ``` import sys import math from collections import defaultdict,deque import heapq mod=998244353 def check(x,y,n,m): return (0<=x<n and 0<=y<m) n,m,k=map(int,sys.stdin.readline().split()) grid=[] s=list(map(int,sys.stdin.readline().split())) for i in range(n): grid.append(list(sys.stdin.readline()[:-1])) q=deque() dic=defaultdict(deque) for i in range(n): for j in range(m): if grid[i][j]!='.' and grid[i][j]!='#': dic[int(grid[i][j])].append([i,j]) #q.append([int(grid[i][j]),i,j]) q=True dirs=[[0,1],[0,-1],[1,0],[-1,0]] while q: z=True for i in range(k): nq=deque() while dic[i+1]: j=dic[i+1].popleft() #print(j,'j') nq.append(j+[s[i]]) z=False p=i+1 while nq: #print(nq,'nq') x,y,dis=nq.popleft() if dis==0: dic[p].append([x,y]) else: for i,j in dirs: nx,ny=x+i,y+j if check(nx,ny,n,m) and grid[nx][ny]=='.': grid[nx][ny]=p #print(nx,'nx',ny,'ny',dis-1,'dis-1') nq.append([nx,ny,dis-1]) if z: q=False '''for i in range(k): for j in dic[i+1]: q.append([i+1]+j) #print(q,'q') while q: p,curx,cury=q.popleft() #print(p,'p',curx,'curx',cury,'cury',s[p-1]) nq=deque() nq.append([curx,cury,s[p-1]]) if int(grid[curx][cury])==p: while nq: x,y,dis=nq.popleft() #print(x,'x',y,'y',dis,'dis') if dis==0: q.append([p,x,y]) else: for i,j in dirs: nx,ny=x+i,y+j if check(nx,ny,n,m) and grid[nx][ny]=='.': grid[nx][ny]=p #print(nx,'nx',ny,'ny',dis-1,'dis-1') nq.append([nx,ny,dis-1]) for i in range(n): print(grid[i]) print('\n')''' ans=[0 for _ in range(k)] #print(ans,'ans') for i in range(n): for j in range(m): if grid[i][j]!='.' and grid[i][j]!='#': ans[int(grid[i][j])-1]+=1 #print(grid,'grid') print(*ans) ```
output
1
107,065
15
214,131
Provide tags and a correct Python 3 solution for this coding contest problem. Kilani is playing a game with his friends. This game can be represented as a grid of size n × m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell). The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that. The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends. Input The first line contains three integers n, m and p (1 ≤ n, m ≤ 1000, 1 ≤ p ≤ 9) — the size of the grid and the number of players. The second line contains p integers s_i (1 ≤ s ≤ 10^9) — the speed of the expansion for every player. The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 ≤ x ≤ p) denotes the castle owned by player x. It is guaranteed, that each player has at least one castle on the grid. Output Print p integers — the number of cells controlled by each player after the game ends. Examples Input 3 3 2 1 1 1.. ... ..2 Output 6 3 Input 3 4 4 1 1 1 1 .... #... 1234 Output 1 4 3 3 Note The picture below show the game before it started, the game after the first round and game after the second round in the first example: <image> In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
instruction
0
107,066
15
214,132
Tags: dfs and similar, graphs, implementation, shortest paths Correct Solution: ``` from collections import deque n, m, p = [int(v) for v in input().split()] s = [int(v) for v in input().split()] d = {'.': 0, '#': 10} d.update({str(v) : v for v in range(1, p + 1)}) field = [[d[c] for c in input().strip()] for _ in range(n)] ans = [0] * p dists = [[[9999999 for _ in range(m)] for _ in range(n)] for _ in range(p)] frontiers = [deque() for _ in range(p)] for i in range(n): for j in range(m): pp = field[i][j] if 1 <= pp <= 9: frontiers[pp - 1].append((i, j, 0)) ans[pp - 1] += 1 dists[pp - 1][i][j] = 0 off = [(1, 0), (0, 1), (-1, 0), (0, -1)] curr_lim = s[:] def dump(): for line in field: print(line) print() while True: was = False for pp in range(1, p + 1): # dump() while frontiers[pp - 1]: i, j, dist = frontiers[pp - 1].popleft() if field[i][j] not in (0, pp): continue if dist > curr_lim[pp - 1]: frontiers[pp - 1].appendleft((i, j, dist)) break if field[i][j] != pp: field[i][j] = pp ans[pp - 1] += 1 was = True for di, dj in off: ni, nj = i + di, j + dj if 0 <= ni < n and 0 <= nj < m and field[ni][nj] == 0: # print(ni, nj) new_dist = dist + 1 if new_dist < dists[pp - 1][ni][nj]: frontiers[pp - 1].append((ni, nj, new_dist)) dists[pp - 1][ni][nj] = new_dist if was: for i in range(p): curr_lim[i] += s[i] else: break print(' '.join(str(v) for v in ans)) ```
output
1
107,066
15
214,133
Provide tags and a correct Python 3 solution for this coding contest problem. Kilani is playing a game with his friends. This game can be represented as a grid of size n × m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell). The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that. The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends. Input The first line contains three integers n, m and p (1 ≤ n, m ≤ 1000, 1 ≤ p ≤ 9) — the size of the grid and the number of players. The second line contains p integers s_i (1 ≤ s ≤ 10^9) — the speed of the expansion for every player. The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 ≤ x ≤ p) denotes the castle owned by player x. It is guaranteed, that each player has at least one castle on the grid. Output Print p integers — the number of cells controlled by each player after the game ends. Examples Input 3 3 2 1 1 1.. ... ..2 Output 6 3 Input 3 4 4 1 1 1 1 .... #... 1234 Output 1 4 3 3 Note The picture below show the game before it started, the game after the first round and game after the second round in the first example: <image> In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
instruction
0
107,067
15
214,134
Tags: dfs and similar, graphs, implementation, shortest paths Correct Solution: ``` import sys input = sys.stdin.readline import gc, os from collections import deque from os import _exit gc.disable() def safe(i, j): return i<n and j<m and i>=0 and j>=0 and mat[i][j]=='.' def bfs(h): while any(h): for i in range(1,p+1): h[i] = bfs2(i) def bfs2(c): global mat s = l[c-1] q = deque() for i,j in h[c]: q.append((i,j,0)) ans = [] while q: i,j,k = q.popleft() if k>=s: ans.append((i,j)) continue for dx,dy in [(0,1), (0,-1), (1,0), (-1,0)]: x,y = i+dx, j+dy if safe(x,y): mat[x][y]=str(c) q.append((x,y,k+1)) return ans n,m,p = map(int, input().split()) l = list(map(int, input().split())) mat = [list(input()) for i in range(n)] q = [] h = [[] for _ in range(p+1)] for i in range(n): for j in range(m): if mat[i][j] not in '.#': z = int(mat[i][j]) h[z].append((i,j)) bfs(h) count = [0]*p for i in range(n): for j in range(m): if mat[i][j] not in '.#': count[int(mat[i][j]) - 1] += 1 print(*count) sys.stdout.flush() _exit(0) ```
output
1
107,067
15
214,135
Provide tags and a correct Python 3 solution for this coding contest problem. Kilani is playing a game with his friends. This game can be represented as a grid of size n × m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell). The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that. The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends. Input The first line contains three integers n, m and p (1 ≤ n, m ≤ 1000, 1 ≤ p ≤ 9) — the size of the grid and the number of players. The second line contains p integers s_i (1 ≤ s ≤ 10^9) — the speed of the expansion for every player. The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 ≤ x ≤ p) denotes the castle owned by player x. It is guaranteed, that each player has at least one castle on the grid. Output Print p integers — the number of cells controlled by each player after the game ends. Examples Input 3 3 2 1 1 1.. ... ..2 Output 6 3 Input 3 4 4 1 1 1 1 .... #... 1234 Output 1 4 3 3 Note The picture below show the game before it started, the game after the first round and game after the second round in the first example: <image> In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
instruction
0
107,068
15
214,136
Tags: dfs and similar, graphs, implementation, shortest paths Correct Solution: ``` n, m, p=map(int, input().split()) s=list(map(int, input().split())) a=[] front=[set() for i in range(p)] for i in range(n): a.append([(int(0) if ch=='.' else (-1 if ch=='#' else (int(ch) if not front[int(ch)-1].add( (i, j) ) else -99 ))) for j, ch in enumerate(input())]) move=[(-1, 0), (1, 0), (0, -1), (0, 1)] i=0 blocked=[False]*p activeplayers=p i=0 while activeplayers>0: if blocked[i]: i=(i+1)%p continue aset=front[i] mademove=False for gtime in range(s[i]): newset=set() for x,y in aset: for dx, dy in move: if 0<=x+dx<n and 0<=y+dy<m: if a[x+dx][y+dy]==0: newset.add( (x+dx, y+dy) ) a[x+dx][y+dy]=(i+1) mademove=True aset=newset if len(aset)==0: mademove=False break front[i]=aset if not mademove: blocked[i]=True activeplayers=activeplayers-1 i=(i+1)%p res=[0]*p for i in range(n): for j in range(m): if a[i][j]>0: res[int(a[i][j])-1]=res[int(a[i][j])-1]+1 for i in range(p): print(res[i], end=' ') ```
output
1
107,068
15
214,137
Provide tags and a correct Python 3 solution for this coding contest problem. Kilani is playing a game with his friends. This game can be represented as a grid of size n × m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell). The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that. The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends. Input The first line contains three integers n, m and p (1 ≤ n, m ≤ 1000, 1 ≤ p ≤ 9) — the size of the grid and the number of players. The second line contains p integers s_i (1 ≤ s ≤ 10^9) — the speed of the expansion for every player. The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 ≤ x ≤ p) denotes the castle owned by player x. It is guaranteed, that each player has at least one castle on the grid. Output Print p integers — the number of cells controlled by each player after the game ends. Examples Input 3 3 2 1 1 1.. ... ..2 Output 6 3 Input 3 4 4 1 1 1 1 .... #... 1234 Output 1 4 3 3 Note The picture below show the game before it started, the game after the first round and game after the second round in the first example: <image> In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
instruction
0
107,069
15
214,138
Tags: dfs and similar, graphs, implementation, shortest paths Correct Solution: ``` import time def get_frontiers(feild, n, m, p): # print(feild) # print(n, m) frontiers = [[] for i in range(p)] for i in range(n): for j in range(m): ele = feild[i][j] if 1 <= ele <= 9: # print('ele:', ele) frontiers[ele - 1].append((i, j)) return frontiers def go(player_id, frontier, n_turn, feild, n, m): frontier = frontier # print('In go:', player_id, frontier, n_turn) while n_turn and frontier: n_turn -= 1 new_frontier = [] for i, j in frontier: # Down. if i + 1 < n: new_space = feild[i + 1][j] if not new_space: feild[i + 1][j] = player_id new_frontier.append((i + 1, j)) # Up. if i - 1 >= 0: new_space = feild[i - 1][j] if not new_space: feild[i - 1][j] = player_id new_frontier.append((i - 1, j)) # Rigth. if j + 1 < m: new_space = feild[i][j + 1] if not new_space: feild[i][j + 1] = player_id new_frontier.append((i, j + 1)) # Left. if j - 1 >= 0: new_space = feild[i][j - 1] if not new_space: feild[i][j - 1] = player_id new_frontier.append((i, j - 1)) # for d_i, d_j in (-1, 0), (1, 0), (0, 1), (0, -1): # check boarder. # new_i, new_j = i + d_i, j + d_j # if new_i < 0 or new_j < 0 or new_i > n - 1 or new_j > m - 1: # continue # new_space = feild[new_i][new_j] # if new_space == 0: # feild[new_i][new_j] = player_id # new_frontier.append((new_i, new_j)) frontier = new_frontier # print('haha:', frontier) # print('player:', player_id) # for ele in feild: # print(ele) # print('Got new frontier:', frontier) return frontier def solve(speeds, feild, n, m, p): frontiers = get_frontiers(feild, n, m, p) # print('f:', frontiers) hope = set(range(p)) while hope: lost_hope = set() for i in hope: n_turn = speeds[i] frontier = frontiers[i] new_frontier = go(i + 1, frontier, n_turn, feild, n, m) # print('i:', i) # print(new_frontier) if not new_frontier: lost_hope.add(i) frontiers[i] = new_frontier hope -= lost_hope result = get_frontiers(feild, n, m, p) return [len(ele) for ele in result] def test(): n, m, p = 1000, 1000, 9 speeds = [1000000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 1] feild = [[0, -1] * (m // 2) for i in range(n)] for i in range(m): if i % 4 != 1: feild[0][i] = 0 if i % 4 != 3: feild[n - 1][i] = 0 # feild[0][0] = 1 for i in range(9): feild[0][i * 8] = i + 1 # for ele in feild: # print(ele) tick = time.time() result = solve(speeds, feild, n, m, p) tock = time.time() print(' '.join(map(str, result))) print('T:', round(tock - tick, 5)) def main(): d = {str(i): i for i in range(1, 10)} d['.'] = 0 d['#'] = -1 n, m, p = map(int, input().split()) speeds = list(map(int, input().split())) feild = [] for i in range(n): feild.append(list(map(d.get, input()))) # for ele in feild: # print(ele) result = solve(speeds, feild, n, m, p) print(' '.join(map(str, result))) # for ele in feild: # print(ele) if __name__ == "__main__": main() ```
output
1
107,069
15
214,139
Provide tags and a correct Python 3 solution for this coding contest problem. Kilani is playing a game with his friends. This game can be represented as a grid of size n × m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell). The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that. The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends. Input The first line contains three integers n, m and p (1 ≤ n, m ≤ 1000, 1 ≤ p ≤ 9) — the size of the grid and the number of players. The second line contains p integers s_i (1 ≤ s ≤ 10^9) — the speed of the expansion for every player. The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 ≤ x ≤ p) denotes the castle owned by player x. It is guaranteed, that each player has at least one castle on the grid. Output Print p integers — the number of cells controlled by each player after the game ends. Examples Input 3 3 2 1 1 1.. ... ..2 Output 6 3 Input 3 4 4 1 1 1 1 .... #... 1234 Output 1 4 3 3 Note The picture below show the game before it started, the game after the first round and game after the second round in the first example: <image> In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
instruction
0
107,070
15
214,140
Tags: dfs and similar, graphs, implementation, shortest paths Correct Solution: ``` from collections import defaultdict as dd, deque n,m,p = map(int,input().split()) S = [0]+[int(x) for x in input().split()] M = [list(input())+['#'] for i in range(n)] M.append(['#']*m) front = [[], [],[],[],[],[],[],[],[],[]] for i in range(n): for j in range(m): if M[i][j] not in '.#': a = int(M[i][j]) front[a].append((i,j)) M[i][j] = a def expand(p): s = S[p] Q = deque() for i,j in front[p]: Q.append((i,j,0)) new = False nfront = [] while Q: i,j,d = Q.popleft() nfront.append((i,j)) if d >= s: continue for di,dj in [(-1,0), (1,0), (0,1), (0,-1)]: if M[i+di][j+dj] == '.': new = True M[i+di][j+dj] = p Q.append((i+di,j+dj,d+1)) nnfront = [] for i,j in nfront: if M[i-1][j] == '.' or \ M[i+1][j] == '.' or \ M[i][j+1] == '.' or \ M[i][j-1] == '.': nnfront.append((i,j)) front[p] = nnfront return new while any([expand(i) for i in range(1,p+1)]): #for _ in M: # print(*_) pass C = dd(int) for i in range(n): for j in range(m): C[M[i][j]] += 1 print(*(C[i] for i in range(1,p+1))) ```
output
1
107,070
15
214,141
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Kilani is playing a game with his friends. This game can be represented as a grid of size n × m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell). The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that. The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends. Input The first line contains three integers n, m and p (1 ≤ n, m ≤ 1000, 1 ≤ p ≤ 9) — the size of the grid and the number of players. The second line contains p integers s_i (1 ≤ s ≤ 10^9) — the speed of the expansion for every player. The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 ≤ x ≤ p) denotes the castle owned by player x. It is guaranteed, that each player has at least one castle on the grid. Output Print p integers — the number of cells controlled by each player after the game ends. Examples Input 3 3 2 1 1 1.. ... ..2 Output 6 3 Input 3 4 4 1 1 1 1 .... #... 1234 Output 1 4 3 3 Note The picture below show the game before it started, the game after the first round and game after the second round in the first example: <image> In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left. Submitted Solution: ``` # import sys # # f = open('input1.txt', 'r') # # # sys.stdin = f n, m, p = list(map(int, input().split())) s = list(map(int, input().split())) q = [[] for _ in range(p)] # fromnt of each palyer counts = [0] * p field = [] for i in range(n): line = input() field.append([0] * m) for j, c in enumerate(line): if c == '.': field[i][j] = 0 elif c == '#': field[i][j] = -1 else: # player pi = int(c) field[i][j] = pi counts[pi-1] += 1 def get_neibs(i, j): up = (i - 1, j) if i > 0 else None down = (i + 1, j) if i < n - 1 else None left = (i, j - 1) if j > 0 else None right = (i, j + 1) if j < m - 1 else None nbs = [up, down, left, right] return [a for a in nbs if a is not None] def init_bounds(field, q): for i in range(n): for j in range(m): if field[i][j] > 0: index = field[i][j]-1 nbs = get_neibs(i, j) neib_vals = [field[a[0]][a[1]] for a in nbs] if 0 in neib_vals: q[index].append((i, j)) def step_one(index, field, front: list): new_front = [] total_add = 0 for i, j in front: nbs = get_neibs(i, j) for a in nbs: if field[a[0]][a[1]] == 0: # if not yet added field[a[0]][a[1]] = index+1 counts[index] += 1 total_add += 1 new_front.append(a) return new_front, total_add def step(index, field, front, speed): added_len = 0 while speed > 0: front, added_len = step_one(index, field, front) speed -= 1 if added_len == 0: break q[index] = front return front, added_len init_bounds(field, q) while True: progress = 0 added = 0 for i in range(p): _, added = step(i, field, q[i], s[i]) progress += added if progress == 0: break print(" ".join(map(str, counts))) # f.close() ```
instruction
0
107,071
15
214,142
Yes
output
1
107,071
15
214,143
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Kilani is playing a game with his friends. This game can be represented as a grid of size n × m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell). The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that. The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends. Input The first line contains three integers n, m and p (1 ≤ n, m ≤ 1000, 1 ≤ p ≤ 9) — the size of the grid and the number of players. The second line contains p integers s_i (1 ≤ s ≤ 10^9) — the speed of the expansion for every player. The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 ≤ x ≤ p) denotes the castle owned by player x. It is guaranteed, that each player has at least one castle on the grid. Output Print p integers — the number of cells controlled by each player after the game ends. Examples Input 3 3 2 1 1 1.. ... ..2 Output 6 3 Input 3 4 4 1 1 1 1 .... #... 1234 Output 1 4 3 3 Note The picture below show the game before it started, the game after the first round and game after the second round in the first example: <image> In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left. Submitted Solution: ``` import time def get_frontiers(feild, n, m, p): # print(feild) # print(n, m) frontiers = [[] for i in range(p)] for i in range(n): for j in range(m): ele = feild[i][j] if 1 <= ele <= 9: # print('ele:', ele) frontiers[ele - 1].append((i, j)) return frontiers def go(player_id, frontier, n_turn, feild, n, m): frontier = frontier # print('In go:', player_id, frontier, n_turn) while n_turn and frontier: n_turn -= 1 new_frontier = [] for i, j in frontier: # Down. if i + 1 < n: new_space = feild[i + 1][j] if not new_space: feild[i + 1][j] = player_id new_frontier.append((i + 1, j)) # Up. if i - 1 >= 0: new_space = feild[i - 1][j] if not new_space: feild[i - 1][j] = player_id new_frontier.append((i - 1, j)) # Rigth. if j + 1 < m: new_space = feild[i][j + 1] if not new_space: feild[i][j + 1] = player_id new_frontier.append((i, j + 1)) # Left. if j - 1 >= 0: new_space = feild[i][j - 1] if not new_space: feild[i][j - 1] = player_id new_frontier.append((i, j - 1)) # for d_i, d_j in (-1, 0), (1, 0), (0, 1), (0, -1): # check boarder. # new_i, new_j = i + d_i, j + d_j # if new_i < 0 or new_j < 0 or new_i > n - 1 or new_j > m - 1: # continue # new_space = feild[new_i][new_j] # if new_space == 0: # feild[new_i][new_j] = player_id # new_frontier.append((new_i, new_j)) frontier = new_frontier # print('haha:', frontier) # print('player:', player_id) # for ele in feild: # print(ele) # print('Got new frontier:', frontier) return frontier def solve(speeds, feild, n, m, p): frontiers = get_frontiers(feild, n, m, p) # print('f:', frontiers) hope = set(range(p)) while hope: new_hope = set() for i in hope: n_turn = speeds[i] frontier = frontiers[i] new_frontier = go(i + 1, frontier, n_turn, feild, n, m) # print('i:', i) # print(new_frontier) if new_frontier: new_hope.add(i) frontiers[i] = new_frontier hope = new_hope result = get_frontiers(feild, n, m, p) return [len(ele) for ele in result] def test(): n, m, p = 1000, 1000, 9 speeds = [1000000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 1] feild = [[0, -1] * (m // 2) for i in range(n)] for i in range(m): if i % 4 != 1: feild[0][i] = 0 if i % 4 != 3: feild[n - 1][i] = 0 # feild[0][0] = 1 for i in range(9): feild[0][i * 8] = i + 1 # for ele in feild: # print(ele) tick = time.time() result = solve(speeds, feild, n, m, p) tock = time.time() print(' '.join(map(str, result))) print('T:', round(tock - tick, 5)) def main(): d = {str(i): i for i in range(1, 10)} d['.'] = 0 d['#'] = -1 n, m, p = map(int, input().split()) speeds = list(map(int, input().split())) feild = [] for i in range(n): feild.append(list(map(d.get, input()))) # for ele in feild: # print(ele) result = solve(speeds, feild, n, m, p) print(' '.join(map(str, result))) # for ele in feild: # print(ele) if __name__ == "__main__": main() ```
instruction
0
107,072
15
214,144
Yes
output
1
107,072
15
214,145
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Kilani is playing a game with his friends. This game can be represented as a grid of size n × m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell). The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that. The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends. Input The first line contains three integers n, m and p (1 ≤ n, m ≤ 1000, 1 ≤ p ≤ 9) — the size of the grid and the number of players. The second line contains p integers s_i (1 ≤ s ≤ 10^9) — the speed of the expansion for every player. The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 ≤ x ≤ p) denotes the castle owned by player x. It is guaranteed, that each player has at least one castle on the grid. Output Print p integers — the number of cells controlled by each player after the game ends. Examples Input 3 3 2 1 1 1.. ... ..2 Output 6 3 Input 3 4 4 1 1 1 1 .... #... 1234 Output 1 4 3 3 Note The picture below show the game before it started, the game after the first round and game after the second round in the first example: <image> In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left. Submitted Solution: ``` from collections import deque import sys DBG = False n,m,p = map(int, input().split()) spd = list(map(int, input().split())) spd.insert(0,-1) # p starts at 1 grid = [ [0] * m for i in range(n) ] c2d = { "#":-1, ".":0, "1":1, "2":2, "3":3, "4":4, "5":5, "6":6, "7":7, "8":8, "9":9 } castle = [ [] for i in range(p+1)] for i in range(n): s = input() for j in range(m): v = c2d[s[j]] grid[i][j] = v if v>0: castle[v].append([i,j]) if DBG: print(grid) print("\n") print(spd) print("\n") def mark(proc,t): global changed, grid, castle, newcastle dir = [ [1,0], [-1,0], [0,1], [0,-1] ] while len(proc) > 0: ent = proc.popleft() c = ent[0] s = ent[1] for d in dir: x = c[0]+d[0] y = c[1]+d[1] if x<0 or n<=x or y<0 or m<=y or grid[x][y]!=0: continue changed = True grid[x][y] = t if s>1: proc.append([ [x,y], s-1 ]) else: newcastle.append([x,y]) changed = True while changed: if DBG: print("---- new loop ----") changed = False for t in range(1,p+1): newcastle = [] proc = deque([]) for c in castle[t]: proc.append([c, spd[t]]) mark(proc, t) if False and DBG: print("turn for %d, (%d,%d) ended" % (t,c[0],c[1])) print(grid) #for x in $newcastle # $castle[t] << x #end castle[t] = newcastle a = [ 0 for i in range(p+1) ] for x in range(n): for y in range(m): if grid[x][y] != -1: a[grid[x][y]] += 1 for i in range(1,p+1): sys.stdout.write("%d " % a[i]) print("") ```
instruction
0
107,073
15
214,146
Yes
output
1
107,073
15
214,147
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Kilani is playing a game with his friends. This game can be represented as a grid of size n × m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell). The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that. The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends. Input The first line contains three integers n, m and p (1 ≤ n, m ≤ 1000, 1 ≤ p ≤ 9) — the size of the grid and the number of players. The second line contains p integers s_i (1 ≤ s ≤ 10^9) — the speed of the expansion for every player. The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 ≤ x ≤ p) denotes the castle owned by player x. It is guaranteed, that each player has at least one castle on the grid. Output Print p integers — the number of cells controlled by each player after the game ends. Examples Input 3 3 2 1 1 1.. ... ..2 Output 6 3 Input 3 4 4 1 1 1 1 .... #... 1234 Output 1 4 3 3 Note The picture below show the game before it started, the game after the first round and game after the second round in the first example: <image> In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left. Submitted Solution: ``` # -*- coding: utf-8 -*- # @Time : 2019/1/20 21:02 # @Author : LunaFire # @Email : gilgemesh2012@gmail.com # @File : D. Kilani and the Game.py # import atexit # import io # import sys # # _INPUT_LINES = sys.stdin.read().splitlines() # input = iter(_INPUT_LINES).__next__ # _OUTPUT_BUFFER = io.StringIO() # sys.stdout = _OUTPUT_BUFFER # # # @atexit.register # def write(): # sys.__stdout__.write(_OUTPUT_BUFFER.getvalue()) from collections import deque def check_empty(deque_array): count = 0 for q in deque_array: count += len(q) return count == 0 def print_grid(grid): for i in range(len(grid)): for j in range(len(grid[0])): print(grid[i][j], end='') print() print() def main(): n, m, p = map(int, input().split()) s = list(map(int, input().split())) grid = [list(input()) for _ in range(n)] deque_array = [deque() for _ in range(p)] for i in range(n): for j in range(m): if '1' <= grid[i][j] <= '9': x = int(grid[i][j]) deque_array[x - 1].append((i, j, 0)) # print(deque_array) curr_round = 1 while not check_empty(deque_array): for r in range(p): while deque_array[r]: x, y, step = deque_array[r].popleft() if step >= s[r] * curr_round: deque_array[r].appendleft((x, y, step)) break for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]: nx, ny = x + dx, y + dy # print(nx, ny) if nx < 0 or nx >= n or ny < 0 or ny >= m or grid[nx][ny] != '.': continue grid[nx][ny] = str(r + 1) deque_array[r].append((nx, ny, step + 1)) # print_grid(grid) curr_round += 1 cell_count = [0] * p for i in range(n): for j in range(m): if '1' <= grid[i][j] <= '9': x = int(grid[i][j]) cell_count[x - 1] += 1 for r in range(p): print(cell_count[r], end=' ') print() if __name__ == '__main__': main() ```
instruction
0
107,074
15
214,148
Yes
output
1
107,074
15
214,149
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Kilani is playing a game with his friends. This game can be represented as a grid of size n × m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell). The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that. The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends. Input The first line contains three integers n, m and p (1 ≤ n, m ≤ 1000, 1 ≤ p ≤ 9) — the size of the grid and the number of players. The second line contains p integers s_i (1 ≤ s ≤ 10^9) — the speed of the expansion for every player. The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 ≤ x ≤ p) denotes the castle owned by player x. It is guaranteed, that each player has at least one castle on the grid. Output Print p integers — the number of cells controlled by each player after the game ends. Examples Input 3 3 2 1 1 1.. ... ..2 Output 6 3 Input 3 4 4 1 1 1 1 .... #... 1234 Output 1 4 3 3 Note The picture below show the game before it started, the game after the first round and game after the second round in the first example: <image> In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left. Submitted Solution: ``` from collections import deque def bfs(queue,q,cell): while (queue): i,j=queue.popleft() num=int(cell[i][j]) for k in range(i+1,min(i+q[num],n-1)+1): if(cell[k][j]=="."): cell[k][j]=num queue.append([k,j]) else: break for k in range(i-1,max(i-q[num],0)-1,-1): if(cell[k][j]=="."): cell[k][j]=num queue.append([k,j]) else: break for k in range(j+1,min(j+q[num],m-1)+1): if(cell[i][k]=="."): cell[i][k]=num queue.append([i,k]) else: break for k in range(j-1,max(j-q[num],0)-1,-1): if(cell[i][k]=="."): cell[i][k]=num queue.append([i,k]) else: break n,m,p=map(int,input().split()) q=[0]+list(map(int,input().split())) cells=[] for i in range(n): cells.append(input()) cell=[[] for i in range(n)] for i in range(n): for j in range(m): cell[i].append(cells[i][j]) qqueue=[] for i in range(n): for j in range(m): if(cell[i][j]!="." and cell[i][j]!="#"): qqueue.append([int(cell[i][j]),i,j]) qqueue.sort() queue=deque() for i in qqueue: queue.append([i[1],i[2]]) bfs(queue,q,cell) counted=[0 for i in range(p+1)] for i in range(n): for j in range(m): if(cell[i][j]!="." and cell[i][j]!="#"): counted[int(cell[i][j])]+=1 print(*counted[1:]) ```
instruction
0
107,075
15
214,150
No
output
1
107,075
15
214,151
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Kilani is playing a game with his friends. This game can be represented as a grid of size n × m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell). The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that. The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends. Input The first line contains three integers n, m and p (1 ≤ n, m ≤ 1000, 1 ≤ p ≤ 9) — the size of the grid and the number of players. The second line contains p integers s_i (1 ≤ s ≤ 10^9) — the speed of the expansion for every player. The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 ≤ x ≤ p) denotes the castle owned by player x. It is guaranteed, that each player has at least one castle on the grid. Output Print p integers — the number of cells controlled by each player after the game ends. Examples Input 3 3 2 1 1 1.. ... ..2 Output 6 3 Input 3 4 4 1 1 1 1 .... #... 1234 Output 1 4 3 3 Note The picture below show the game before it started, the game after the first round and game after the second round in the first example: <image> In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left. Submitted Solution: ``` [n, m, p] = [int(i) for i in input().split(" ")] scores = [int(i) for i in input().split(" ")] grid = [] bitgrid = [([1] * m) for i in range(n)] for i in range(n): grid.append([str(i) for i in list(input())]) castles = [0] * p to_check = {} for i in range(n): for j in range(m): if grid[i][j] != '#' and grid[i][j] != '.': bitgrid[i][j] = 0 castles[int(grid[i][j]) - 1] += 1 if grid[i][j] in to_check: to_check[grid[i][j]].add((i,j)) else: to_check[grid[i][j]] = {(i,j)} while True: for player in to_check: for i in range(scores[int(player) - 1]): if len(to_check[player]) == 0: break temp = set() for cell in to_check[player]: if cell[0] > 0: if grid[cell[0] - 1][cell[1]] == '.': grid[cell[0] - 1][cell[1]] = player temp.add((cell[0] - 1, cell[1])) castles[int(player) - 1] += 1 if cell[0] < n - 1: if grid[cell[0] + 1][cell[1]] == '.': grid[cell[0] + 1][cell[1]] = player temp.add((cell[0] + 1, cell[1])) castles[int(player) - 1] += 1 if cell[1] > 0: if grid[cell[0]][cell[1] - 1] == '.': grid[cell[0]][cell[1] - 1] = player temp.add((cell[0], cell[1] - 1)) castles[int(player) - 1] += 1 if cell[1] < m - 1: if grid[cell[0]][cell[1] + 1] == '.': grid[cell[0]][cell[1] + 1] = player temp.add((cell[0], cell[1] + 1)) castles[int(player) - 1] += 1 to_check[player] = temp flag = False for i in to_check: if len(to_check[i]) > 0: flag = True break if not flag: break for i in castles: print(i) ```
instruction
0
107,076
15
214,152
No
output
1
107,076
15
214,153
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Kilani is playing a game with his friends. This game can be represented as a grid of size n × m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell). The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that. The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends. Input The first line contains three integers n, m and p (1 ≤ n, m ≤ 1000, 1 ≤ p ≤ 9) — the size of the grid and the number of players. The second line contains p integers s_i (1 ≤ s ≤ 10^9) — the speed of the expansion for every player. The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 ≤ x ≤ p) denotes the castle owned by player x. It is guaranteed, that each player has at least one castle on the grid. Output Print p integers — the number of cells controlled by each player after the game ends. Examples Input 3 3 2 1 1 1.. ... ..2 Output 6 3 Input 3 4 4 1 1 1 1 .... #... 1234 Output 1 4 3 3 Note The picture below show the game before it started, the game after the first round and game after the second round in the first example: <image> In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left. Submitted Solution: ``` [n, m, p] = [int(i) for i in input().split(" ")] scores = [int(i) for i in input().split(" ")] grid = [] bitgrid = [([1] * m) for i in range(n)] for i in range(n): grid.append([str(i) for i in list(input())]) castles = [0] * p to_check = {} for i in range(n): for j in range(m): if grid[i][j] != '#' and grid[i][j] != '.': bitgrid[i][j] = 0 castles[int(grid[i][j]) - 1] += 1 if grid[i][j] in to_check: to_check[int(grid[i][j])].add((i,j)) else: to_check[int(grid[i][j])] = {(i,j)} while True: for player in range(1, p + 1): for i in range(scores[player - 1]): if len(to_check[player]) == 0: break temp = set() for cell in to_check[player]: if cell[0] > 0: if grid[cell[0] - 1][cell[1]] == '.': grid[cell[0] - 1][cell[1]] = player temp.add((cell[0] - 1, cell[1])) castles[player - 1] += 1 if cell[0] < n - 1: if grid[cell[0] + 1][cell[1]] == '.': grid[cell[0] + 1][cell[1]] = player temp.add((cell[0] + 1, cell[1])) castles[player - 1] += 1 if cell[1] > 0: if grid[cell[0]][cell[1] - 1] == '.': grid[cell[0]][cell[1] - 1] = player temp.add((cell[0], cell[1] - 1)) castles[player - 1] += 1 if cell[1] < m - 1: if grid[cell[0]][cell[1] + 1] == '.': grid[cell[0]][cell[1] + 1] = player temp.add((cell[0], cell[1] + 1)) castles[player - 1] += 1 to_check[player] = temp flag = False for i in to_check: if len(to_check[i]) > 0: flag = True break if not flag: break for i in castles: print(i) ```
instruction
0
107,077
15
214,154
No
output
1
107,077
15
214,155
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Kilani is playing a game with his friends. This game can be represented as a grid of size n × m, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell). The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player i can expand from a cell with his castle to the empty cell if it's possible to reach it in at most s_i (where s_i is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that. The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends. Input The first line contains three integers n, m and p (1 ≤ n, m ≤ 1000, 1 ≤ p ≤ 9) — the size of the grid and the number of players. The second line contains p integers s_i (1 ≤ s ≤ 10^9) — the speed of the expansion for every player. The following n lines describe the game grid. Each of them consists of m symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit x (1 ≤ x ≤ p) denotes the castle owned by player x. It is guaranteed, that each player has at least one castle on the grid. Output Print p integers — the number of cells controlled by each player after the game ends. Examples Input 3 3 2 1 1 1.. ... ..2 Output 6 3 Input 3 4 4 1 1 1 1 .... #... 1234 Output 1 4 3 3 Note The picture below show the game before it started, the game after the first round and game after the second round in the first example: <image> In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left. Submitted Solution: ``` from collections import deque n, m, p = map(int, input().split()) speed = list(map(int, input().split())) grid = [input() for _ in range(n)] memo = [[0] * m for _ in range(n)] point = [0] * p start = [[] for _ in range(p)] for i in range(n): for j in range(m): if grid[i][j] != "#" and grid[i][j] != ".": start[int(grid[i][j]) - 1].append((i, j)) if grid[i][j] == "#": memo[i][j] = 1 # x = y, y = x q = deque([]) for i in range(p): x, y = start[i][0] q.append((i, x, y)) memo[x][y] = 1 point[i] += 1 dir = [[1, 0], [-1, 0], [0, 1], [0, -1]] while q: i, x, y = q.popleft() for dx, dy in dir: for _ in range(p): nx = x + dx ny = y + dy if 0 <= nx <= n - 1 and 0 <= ny <= m - 1: if memo[nx][ny] == 1: continue memo[nx][ny] = 1 point[i] += 1 q.append((i, nx, ny)) print(*point) ```
instruction
0
107,078
15
214,156
No
output
1
107,078
15
214,157