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
Provide a correct Python 3 solution for this coding contest problem. There is a grid with H horizontal rows and W vertical columns. Let (i, j) denote the square at the i-th row from the top and the j-th column from the left. For each i and j (1 \leq i \leq H, 1 \leq j \leq W), Square (i, j) is described by a character a_{i, j}. If a_{i, j} is `.`, Square (i, j) is an empty square; if a_{i, j} is `#`, Square (i, j) is a wall square. It is guaranteed that Squares (1, 1) and (H, W) are empty squares. Taro will start from Square (1, 1) and reach (H, W) by repeatedly moving right or down to an adjacent empty square. Find the number of Taro's paths from Square (1, 1) to (H, W). As the answer can be extremely large, find the count modulo 10^9 + 7. Constraints * H and W are integers. * 2 \leq H, W \leq 1000 * a_{i, j} is `.` or `#`. * Squares (1, 1) and (H, W) are empty squares. Input Input is given from Standard Input in the following format: H W a_{1, 1}\ldotsa_{1, W} : a_{H, 1}\ldotsa_{H, W} Output Print the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7. Examples Input 3 4 ...# .#.. .... Output 3 Input 5 2 .. . .. .# .. Output 0 Input 5 5 ..#.. ..... ...# ..... ..#.. Output 24 Input 20 20 .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... Output 345263555
instruction
0
47,759
15
95,518
"Correct Solution: ``` h, w = map(int, input().split()) s = [] for _ in range(h): s.append(input()) dp = [[0] * w] * h p = 10**9 + 7 for i in range(h): for j in range(w): if s[i][j]=="#": dp[i][j] = 0 elif i==0 and j==0: dp[i][j] = 1 elif i==0: dp[i][j] = dp[i][j-1] elif j==0: dp[i][j] = dp[i-1][j] else: dp[i][j] = (dp[i-1][j] + dp[i][j-1]) % p print(dp[h-1][w-1]) ```
output
1
47,759
15
95,519
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a grid with H horizontal rows and W vertical columns. Let (i, j) denote the square at the i-th row from the top and the j-th column from the left. For each i and j (1 \leq i \leq H, 1 \leq j \leq W), Square (i, j) is described by a character a_{i, j}. If a_{i, j} is `.`, Square (i, j) is an empty square; if a_{i, j} is `#`, Square (i, j) is a wall square. It is guaranteed that Squares (1, 1) and (H, W) are empty squares. Taro will start from Square (1, 1) and reach (H, W) by repeatedly moving right or down to an adjacent empty square. Find the number of Taro's paths from Square (1, 1) to (H, W). As the answer can be extremely large, find the count modulo 10^9 + 7. Constraints * H and W are integers. * 2 \leq H, W \leq 1000 * a_{i, j} is `.` or `#`. * Squares (1, 1) and (H, W) are empty squares. Input Input is given from Standard Input in the following format: H W a_{1, 1}\ldotsa_{1, W} : a_{H, 1}\ldotsa_{H, W} Output Print the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7. Examples Input 3 4 ...# .#.. .... Output 3 Input 5 2 .. . .. .# .. Output 0 Input 5 5 ..#.. ..... ...# ..... ..#.. Output 24 Input 20 20 .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... Output 345263555 Submitted Solution: ``` h,w=map(int,input().split()) grid=[input() for _ in range(h)] mod=10**9+7 dp=[0]*w for i in range(h): for j in range(w): if i==j==0: dp[j]=1 elif grid[i][j]=='#': dp[j]=0 elif j>0: dp[j]=dp[j]+dp[j-1] print(dp[w-1]%mod) ```
instruction
0
47,760
15
95,520
Yes
output
1
47,760
15
95,521
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a grid with H horizontal rows and W vertical columns. Let (i, j) denote the square at the i-th row from the top and the j-th column from the left. For each i and j (1 \leq i \leq H, 1 \leq j \leq W), Square (i, j) is described by a character a_{i, j}. If a_{i, j} is `.`, Square (i, j) is an empty square; if a_{i, j} is `#`, Square (i, j) is a wall square. It is guaranteed that Squares (1, 1) and (H, W) are empty squares. Taro will start from Square (1, 1) and reach (H, W) by repeatedly moving right or down to an adjacent empty square. Find the number of Taro's paths from Square (1, 1) to (H, W). As the answer can be extremely large, find the count modulo 10^9 + 7. Constraints * H and W are integers. * 2 \leq H, W \leq 1000 * a_{i, j} is `.` or `#`. * Squares (1, 1) and (H, W) are empty squares. Input Input is given from Standard Input in the following format: H W a_{1, 1}\ldotsa_{1, W} : a_{H, 1}\ldotsa_{H, W} Output Print the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7. Examples Input 3 4 ...# .#.. .... Output 3 Input 5 2 .. . .. .# .. Output 0 Input 5 5 ..#.. ..... ...# ..... ..#.. Output 24 Input 20 20 .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... Output 345263555 Submitted Solution: ``` H,W = map(int,input().split()) mod = 1000000007 a_matrix = [] for _ in range(H): a_matrix.append(list(input())) dp = [[0 for w in range(W+1)] for h in range(H+1)] dp[1][0] = 1 for w in range(1,W+1): for h in range(1,H+1): if a_matrix[h-1][w-1] == ".": dp[h][w] = (dp[h-1][w] + dp[h][w-1]) % mod else: dp[h][w] = 0 print(dp[H][W]) ```
instruction
0
47,761
15
95,522
Yes
output
1
47,761
15
95,523
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a grid with H horizontal rows and W vertical columns. Let (i, j) denote the square at the i-th row from the top and the j-th column from the left. For each i and j (1 \leq i \leq H, 1 \leq j \leq W), Square (i, j) is described by a character a_{i, j}. If a_{i, j} is `.`, Square (i, j) is an empty square; if a_{i, j} is `#`, Square (i, j) is a wall square. It is guaranteed that Squares (1, 1) and (H, W) are empty squares. Taro will start from Square (1, 1) and reach (H, W) by repeatedly moving right or down to an adjacent empty square. Find the number of Taro's paths from Square (1, 1) to (H, W). As the answer can be extremely large, find the count modulo 10^9 + 7. Constraints * H and W are integers. * 2 \leq H, W \leq 1000 * a_{i, j} is `.` or `#`. * Squares (1, 1) and (H, W) are empty squares. Input Input is given from Standard Input in the following format: H W a_{1, 1}\ldotsa_{1, W} : a_{H, 1}\ldotsa_{H, W} Output Print the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7. Examples Input 3 4 ...# .#.. .... Output 3 Input 5 2 .. . .. .# .. Output 0 Input 5 5 ..#.. ..... ...# ..... ..#.. Output 24 Input 20 20 .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... Output 345263555 Submitted Solution: ``` h,w=map(int,input().split()) mod=10**9+7 dp=[[0]*(w+1) for i in range(h+1)] dp[1][0]=1 for i in range(1,h+1): line=input() for j in range(1,w+1): if line[j-1]!='#': dp[i][j]+=dp[i-1][j]+dp[i][j-1] print(dp[-1][-1]%mod) ```
instruction
0
47,762
15
95,524
Yes
output
1
47,762
15
95,525
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a grid with H horizontal rows and W vertical columns. Let (i, j) denote the square at the i-th row from the top and the j-th column from the left. For each i and j (1 \leq i \leq H, 1 \leq j \leq W), Square (i, j) is described by a character a_{i, j}. If a_{i, j} is `.`, Square (i, j) is an empty square; if a_{i, j} is `#`, Square (i, j) is a wall square. It is guaranteed that Squares (1, 1) and (H, W) are empty squares. Taro will start from Square (1, 1) and reach (H, W) by repeatedly moving right or down to an adjacent empty square. Find the number of Taro's paths from Square (1, 1) to (H, W). As the answer can be extremely large, find the count modulo 10^9 + 7. Constraints * H and W are integers. * 2 \leq H, W \leq 1000 * a_{i, j} is `.` or `#`. * Squares (1, 1) and (H, W) are empty squares. Input Input is given from Standard Input in the following format: H W a_{1, 1}\ldotsa_{1, W} : a_{H, 1}\ldotsa_{H, W} Output Print the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7. Examples Input 3 4 ...# .#.. .... Output 3 Input 5 2 .. . .. .# .. Output 0 Input 5 5 ..#.. ..... ...# ..... ..#.. Output 24 Input 20 20 .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... Output 345263555 Submitted Solution: ``` p = 10**9+7 h, w = map(int, input().split()) maze = [[-1 for i in range(w)] for j in range(h)] ans = 0 for i in range(h): maze[i] = input() dp = [[0]*(w+1) for _ in range(h+1)] dp[1][1] = 1 for i in range(1,h+1): for j in range(1,w+1): if maze[i-1][j-1] == '.': dp[i][j] += dp[i-1][j]%p + dp[i][j-1]%p ans = dp[h][w]%p print(ans) ```
instruction
0
47,763
15
95,526
Yes
output
1
47,763
15
95,527
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a grid with H horizontal rows and W vertical columns. Let (i, j) denote the square at the i-th row from the top and the j-th column from the left. For each i and j (1 \leq i \leq H, 1 \leq j \leq W), Square (i, j) is described by a character a_{i, j}. If a_{i, j} is `.`, Square (i, j) is an empty square; if a_{i, j} is `#`, Square (i, j) is a wall square. It is guaranteed that Squares (1, 1) and (H, W) are empty squares. Taro will start from Square (1, 1) and reach (H, W) by repeatedly moving right or down to an adjacent empty square. Find the number of Taro's paths from Square (1, 1) to (H, W). As the answer can be extremely large, find the count modulo 10^9 + 7. Constraints * H and W are integers. * 2 \leq H, W \leq 1000 * a_{i, j} is `.` or `#`. * Squares (1, 1) and (H, W) are empty squares. Input Input is given from Standard Input in the following format: H W a_{1, 1}\ldotsa_{1, W} : a_{H, 1}\ldotsa_{H, W} Output Print the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7. Examples Input 3 4 ...# .#.. .... Output 3 Input 5 2 .. . .. .# .. Output 0 Input 5 5 ..#.. ..... ...# ..... ..#.. Output 24 Input 20 20 .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... Output 345263555 Submitted Solution: ``` import sys def find_paths(mat, n, m): for i in range(n): if mat[i][0] != "#": dp[i][0] = 1 else: break for j in range(m): if mat[0][j] != "#": dp[0][j] = 1 else: break dp[0][0] = 0 for i in range(1, n): for j in range(1, m): if mat[i][j] == "#": dp[i][j] = 0 else: dp[i][j] = dp[i - 1][j] + dp[i][j - 1] # print(dp) return int(dp[n - 1][m - 1] % (1e9+7)) n, m = list(map(int, sys.stdin.readline().split())) mat = [] dp = [[0 for i in range(1001)] for j in range(1001)] for i in range(n): mat.append([ch for ch in sys.stdin.readline().strip()]) # print(mat) print(find_paths(mat, n, m)) ```
instruction
0
47,764
15
95,528
No
output
1
47,764
15
95,529
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a grid with H horizontal rows and W vertical columns. Let (i, j) denote the square at the i-th row from the top and the j-th column from the left. For each i and j (1 \leq i \leq H, 1 \leq j \leq W), Square (i, j) is described by a character a_{i, j}. If a_{i, j} is `.`, Square (i, j) is an empty square; if a_{i, j} is `#`, Square (i, j) is a wall square. It is guaranteed that Squares (1, 1) and (H, W) are empty squares. Taro will start from Square (1, 1) and reach (H, W) by repeatedly moving right or down to an adjacent empty square. Find the number of Taro's paths from Square (1, 1) to (H, W). As the answer can be extremely large, find the count modulo 10^9 + 7. Constraints * H and W are integers. * 2 \leq H, W \leq 1000 * a_{i, j} is `.` or `#`. * Squares (1, 1) and (H, W) are empty squares. Input Input is given from Standard Input in the following format: H W a_{1, 1}\ldotsa_{1, W} : a_{H, 1}\ldotsa_{H, W} Output Print the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7. Examples Input 3 4 ...# .#.. .... Output 3 Input 5 2 .. . .. .# .. Output 0 Input 5 5 ..#.. ..... ...# ..... ..#.. Output 24 Input 20 20 .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... Output 345263555 Submitted Solution: ``` from collections import defaultdict,deque import numpy as np import sys,heapq,bisect,math,itertools,string,queue,copy,time sys.setrecursionlimit(10**8) INF = float('inf') mod = 10**9+7 eps = 10**-7 def inp(): return int(input()) def inpl(): return list(map(int, input().split())) def inpl_str(): return list(input().split()) H,W = inpl() MAP = [['#']*(W+2)] + [['#']+list(input())+['#'] for y in range(H)] + [['#']*(W+2)] dp = np.zeros((H+2,W+2),dtype=np.int) dp[1][0] = 1 for x in range(1,W+1): for y in range(1,H+1): if MAP[y][x] == '.': dp[y][x] = (dp[y-1][x] + dp[y][x-1])%mod print(dp[H][W]) ```
instruction
0
47,765
15
95,530
No
output
1
47,765
15
95,531
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a grid with H horizontal rows and W vertical columns. Let (i, j) denote the square at the i-th row from the top and the j-th column from the left. For each i and j (1 \leq i \leq H, 1 \leq j \leq W), Square (i, j) is described by a character a_{i, j}. If a_{i, j} is `.`, Square (i, j) is an empty square; if a_{i, j} is `#`, Square (i, j) is a wall square. It is guaranteed that Squares (1, 1) and (H, W) are empty squares. Taro will start from Square (1, 1) and reach (H, W) by repeatedly moving right or down to an adjacent empty square. Find the number of Taro's paths from Square (1, 1) to (H, W). As the answer can be extremely large, find the count modulo 10^9 + 7. Constraints * H and W are integers. * 2 \leq H, W \leq 1000 * a_{i, j} is `.` or `#`. * Squares (1, 1) and (H, W) are empty squares. Input Input is given from Standard Input in the following format: H W a_{1, 1}\ldotsa_{1, W} : a_{H, 1}\ldotsa_{H, W} Output Print the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7. Examples Input 3 4 ...# .#.. .... Output 3 Input 5 2 .. . .. .# .. Output 0 Input 5 5 ..#.. ..... ...# ..... ..#.. Output 24 Input 20 20 .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... Output 345263555 Submitted Solution: ``` def mod(num): return num % (10 ** 9 + 7) H, W = map(int, input().split()) dp = [[0] * W for _ in range(H)] for i in range(H): a = input() for j in range(W): if a[j] == "#": dp[i][j] = -1 dp[0][0] = 1 for col in range(1, W): if dp[0][col] != 0: dp[0][col] = dp[0][col - 1] else: dp[0][col] = 0 for row in range(1, H): for col in range(W): if dp[row][col] != -1: if col == 0: dp[row][col] = dp[row - 1][col] else: dp[row][col] = mod(dp[row - 1][col] + dp[row][col - 1]) else: dp[row][col] = 0 print(dp[-1][-1]) ```
instruction
0
47,766
15
95,532
No
output
1
47,766
15
95,533
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a grid with H horizontal rows and W vertical columns. Let (i, j) denote the square at the i-th row from the top and the j-th column from the left. For each i and j (1 \leq i \leq H, 1 \leq j \leq W), Square (i, j) is described by a character a_{i, j}. If a_{i, j} is `.`, Square (i, j) is an empty square; if a_{i, j} is `#`, Square (i, j) is a wall square. It is guaranteed that Squares (1, 1) and (H, W) are empty squares. Taro will start from Square (1, 1) and reach (H, W) by repeatedly moving right or down to an adjacent empty square. Find the number of Taro's paths from Square (1, 1) to (H, W). As the answer can be extremely large, find the count modulo 10^9 + 7. Constraints * H and W are integers. * 2 \leq H, W \leq 1000 * a_{i, j} is `.` or `#`. * Squares (1, 1) and (H, W) are empty squares. Input Input is given from Standard Input in the following format: H W a_{1, 1}\ldotsa_{1, W} : a_{H, 1}\ldotsa_{H, W} Output Print the number of Taro's paths from Square (1, 1) to (H, W), modulo 10^9 + 7. Examples Input 3 4 ...# .#.. .... Output 3 Input 5 2 .. . .. .# .. Output 0 Input 5 5 ..#.. ..... ...# ..... ..#.. Output 24 Input 20 20 .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... Output 345263555 Submitted Solution: ``` import sys import numpy as np sys.setrecursionlimit(10 ** 9) mod = 10**9+7 H, W = map(int,input().split()) #A = [list(map(int,input().split())) for _ in range(H)] A = np.array([list(input()) for _ in range(H)], 'U1') eq = A == '.' dp = np.zeros((H+1,W+1), dtype=int) #np.zeros(W+1, dtype=int) dp[0][1] = 1 for i in range(H): for j in range(W): if eq[i][j]: dp[i+1][j+1] = dp[i][j+1] + dp[i+1][j] # print(i,j,dp[i]) # if i == 0 and j== 0: # dp[i+1][j+1] = 1 # dp[i+1] %= mod print(dp[-1][-1]%mod) ```
instruction
0
47,767
15
95,534
No
output
1
47,767
15
95,535
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little penguin Polo adores integer segments, that is, pairs of integers [l; r] (l ≀ r). He has a set that consists of n integer segments: [l1; r1], [l2; r2], ..., [ln; rn]. We know that no two segments of this set intersect. In one move Polo can either widen any segment of the set 1 unit to the left or 1 unit to the right, that is transform [l; r] to either segment [l - 1; r], or to segment [l; r + 1]. The value of a set of segments that consists of n segments [l1; r1], [l2; r2], ..., [ln; rn] is the number of integers x, such that there is integer j, for which the following inequality holds, lj ≀ x ≀ rj. Find the minimum number of moves needed to make the value of the set of Polo's segments divisible by k. Input The first line contains two integers n and k (1 ≀ n, k ≀ 105). Each of the following n lines contain a segment as a pair of integers li and ri ( - 105 ≀ li ≀ ri ≀ 105), separated by a space. It is guaranteed that no two segments intersect. In other words, for any two integers i, j (1 ≀ i < j ≀ n) the following inequality holds, min(ri, rj) < max(li, lj). Output In a single line print a single integer β€” the answer to the problem. Examples Input 2 3 1 2 3 4 Output 2 Input 3 7 1 2 3 3 4 7 Output 0 Submitted Solution: ``` n,k=map(int,input().split()) value=0 for i in range(n): l,r=map(int,input().split()) diff=r-l+1 value+=diff if value%k==0: print('0') else : print((value//k+1)*k-value) ```
instruction
0
48,234
15
96,468
Yes
output
1
48,234
15
96,469
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little penguin Polo adores integer segments, that is, pairs of integers [l; r] (l ≀ r). He has a set that consists of n integer segments: [l1; r1], [l2; r2], ..., [ln; rn]. We know that no two segments of this set intersect. In one move Polo can either widen any segment of the set 1 unit to the left or 1 unit to the right, that is transform [l; r] to either segment [l - 1; r], or to segment [l; r + 1]. The value of a set of segments that consists of n segments [l1; r1], [l2; r2], ..., [ln; rn] is the number of integers x, such that there is integer j, for which the following inequality holds, lj ≀ x ≀ rj. Find the minimum number of moves needed to make the value of the set of Polo's segments divisible by k. Input The first line contains two integers n and k (1 ≀ n, k ≀ 105). Each of the following n lines contain a segment as a pair of integers li and ri ( - 105 ≀ li ≀ ri ≀ 105), separated by a space. It is guaranteed that no two segments intersect. In other words, for any two integers i, j (1 ≀ i < j ≀ n) the following inequality holds, min(ri, rj) < max(li, lj). Output In a single line print a single integer β€” the answer to the problem. Examples Input 2 3 1 2 3 4 Output 2 Input 3 7 1 2 3 3 4 7 Output 0 Submitted Solution: ``` n , k = map(int,input().split()) a = [] for i in range(n): l ,r = map(int,input().split()) d = r - l + 1 a.append(d) if sum(a) % k == 0 : print(0) exit() else: print(k - sum(a)%k) exit() ```
instruction
0
48,235
15
96,470
Yes
output
1
48,235
15
96,471
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little penguin Polo adores integer segments, that is, pairs of integers [l; r] (l ≀ r). He has a set that consists of n integer segments: [l1; r1], [l2; r2], ..., [ln; rn]. We know that no two segments of this set intersect. In one move Polo can either widen any segment of the set 1 unit to the left or 1 unit to the right, that is transform [l; r] to either segment [l - 1; r], or to segment [l; r + 1]. The value of a set of segments that consists of n segments [l1; r1], [l2; r2], ..., [ln; rn] is the number of integers x, such that there is integer j, for which the following inequality holds, lj ≀ x ≀ rj. Find the minimum number of moves needed to make the value of the set of Polo's segments divisible by k. Input The first line contains two integers n and k (1 ≀ n, k ≀ 105). Each of the following n lines contain a segment as a pair of integers li and ri ( - 105 ≀ li ≀ ri ≀ 105), separated by a space. It is guaranteed that no two segments intersect. In other words, for any two integers i, j (1 ≀ i < j ≀ n) the following inequality holds, min(ri, rj) < max(li, lj). Output In a single line print a single integer β€” the answer to the problem. Examples Input 2 3 1 2 3 4 Output 2 Input 3 7 1 2 3 3 4 7 Output 0 Submitted Solution: ``` n,k=input().split() n,k=int(n),int(k) L={""} for i in range(n): m,f=input().split() for i in range (int(m),int(f)+1): L |= {i} L.remove("") x=len(L) print((k-x%k)%k) ```
instruction
0
48,236
15
96,472
Yes
output
1
48,236
15
96,473
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little penguin Polo adores integer segments, that is, pairs of integers [l; r] (l ≀ r). He has a set that consists of n integer segments: [l1; r1], [l2; r2], ..., [ln; rn]. We know that no two segments of this set intersect. In one move Polo can either widen any segment of the set 1 unit to the left or 1 unit to the right, that is transform [l; r] to either segment [l - 1; r], or to segment [l; r + 1]. The value of a set of segments that consists of n segments [l1; r1], [l2; r2], ..., [ln; rn] is the number of integers x, such that there is integer j, for which the following inequality holds, lj ≀ x ≀ rj. Find the minimum number of moves needed to make the value of the set of Polo's segments divisible by k. Input The first line contains two integers n and k (1 ≀ n, k ≀ 105). Each of the following n lines contain a segment as a pair of integers li and ri ( - 105 ≀ li ≀ ri ≀ 105), separated by a space. It is guaranteed that no two segments intersect. In other words, for any two integers i, j (1 ≀ i < j ≀ n) the following inequality holds, min(ri, rj) < max(li, lj). Output In a single line print a single integer β€” the answer to the problem. Examples Input 2 3 1 2 3 4 Output 2 Input 3 7 1 2 3 3 4 7 Output 0 Submitted Solution: ``` Sum = 0 X = list(map(int, input().split())) for i in range(X[0]): Temp = list(map(int, input().split())) Sum += Temp[1] - Temp[0] + 1 print(X[1] - (Sum % X[1]) if Sum % X[1] != 0 else Sum % X[1]) # UB_CodeForces # Advice: Falling down is an accident, staying down is a choice # Location: Here in Bojnurd # Caption: So Close man!! Take it easy!!!! # CodeNumber: 653 ```
instruction
0
48,237
15
96,474
Yes
output
1
48,237
15
96,475
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little penguin Polo adores integer segments, that is, pairs of integers [l; r] (l ≀ r). He has a set that consists of n integer segments: [l1; r1], [l2; r2], ..., [ln; rn]. We know that no two segments of this set intersect. In one move Polo can either widen any segment of the set 1 unit to the left or 1 unit to the right, that is transform [l; r] to either segment [l - 1; r], or to segment [l; r + 1]. The value of a set of segments that consists of n segments [l1; r1], [l2; r2], ..., [ln; rn] is the number of integers x, such that there is integer j, for which the following inequality holds, lj ≀ x ≀ rj. Find the minimum number of moves needed to make the value of the set of Polo's segments divisible by k. Input The first line contains two integers n and k (1 ≀ n, k ≀ 105). Each of the following n lines contain a segment as a pair of integers li and ri ( - 105 ≀ li ≀ ri ≀ 105), separated by a space. It is guaranteed that no two segments intersect. In other words, for any two integers i, j (1 ≀ i < j ≀ n) the following inequality holds, min(ri, rj) < max(li, lj). Output In a single line print a single integer β€” the answer to the problem. Examples Input 2 3 1 2 3 4 Output 2 Input 3 7 1 2 3 3 4 7 Output 0 Submitted Solution: ``` nk = input().strip().split(' ') n = int(nk[0]) k = int(nk[1]) value = 0 for i in range(n): lr = input().strip().split(' ') value += int(lr[1]) - int(lr[0]) + 1 print(value%k) ```
instruction
0
48,238
15
96,476
No
output
1
48,238
15
96,477
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little penguin Polo adores integer segments, that is, pairs of integers [l; r] (l ≀ r). He has a set that consists of n integer segments: [l1; r1], [l2; r2], ..., [ln; rn]. We know that no two segments of this set intersect. In one move Polo can either widen any segment of the set 1 unit to the left or 1 unit to the right, that is transform [l; r] to either segment [l - 1; r], or to segment [l; r + 1]. The value of a set of segments that consists of n segments [l1; r1], [l2; r2], ..., [ln; rn] is the number of integers x, such that there is integer j, for which the following inequality holds, lj ≀ x ≀ rj. Find the minimum number of moves needed to make the value of the set of Polo's segments divisible by k. Input The first line contains two integers n and k (1 ≀ n, k ≀ 105). Each of the following n lines contain a segment as a pair of integers li and ri ( - 105 ≀ li ≀ ri ≀ 105), separated by a space. It is guaranteed that no two segments intersect. In other words, for any two integers i, j (1 ≀ i < j ≀ n) the following inequality holds, min(ri, rj) < max(li, lj). Output In a single line print a single integer β€” the answer to the problem. Examples Input 2 3 1 2 3 4 Output 2 Input 3 7 1 2 3 3 4 7 Output 0 Submitted Solution: ``` """ β–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•”β•β•β•β–ˆβ–ˆβ•—β–ˆβ–ˆβ•‘ β•šβ•β•β•β•β–ˆβ–ˆβ•—β–ˆβ–ˆβ•”β•β–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ•‘β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•— β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•”β•β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•”β–ˆβ–ˆβ•‘β•šβ–ˆβ–ˆβ•‘β•šβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•”β•β•β•β• β–ˆβ–ˆβ–ˆβ–ˆβ•”β•β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β•šβ•β•β•β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘β•šβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•”β•β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β•šβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•”β• β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•”β• β•šβ•β• β•šβ•β•β•β•β•β• β•šβ•β• β•šβ•β•β•β•β•β•β• β•šβ•β•β•β•β•β• β•šβ•β• β•šβ•β•β•β•β• β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ•—β–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ•— β–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•—β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•”β•β•β•β•β•β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•”β•β•β•β–ˆβ–ˆβ•—β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•— β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘ β•šβ•β•β•β•β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•”β•β–ˆβ–ˆβ•‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•‘β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘β•šβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•”β•β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•”β• β•šβ•β•β•β•β•β• β•šβ•β•β•šβ•β•β•β•β•β•β•β•šβ•β•β•β•β•β•β•β•šβ•β• β•šβ•β• β•šβ•β•β•β•β•β• β•šβ•β•β•β•β•β• """ n, k = map(int, input().split()) for i in range(n): x, y = map(int ,input().split()) a = 0 while y % k != 0: y += 1 a += 1 print(a) ```
instruction
0
48,239
15
96,478
No
output
1
48,239
15
96,479
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little penguin Polo adores integer segments, that is, pairs of integers [l; r] (l ≀ r). He has a set that consists of n integer segments: [l1; r1], [l2; r2], ..., [ln; rn]. We know that no two segments of this set intersect. In one move Polo can either widen any segment of the set 1 unit to the left or 1 unit to the right, that is transform [l; r] to either segment [l - 1; r], or to segment [l; r + 1]. The value of a set of segments that consists of n segments [l1; r1], [l2; r2], ..., [ln; rn] is the number of integers x, such that there is integer j, for which the following inequality holds, lj ≀ x ≀ rj. Find the minimum number of moves needed to make the value of the set of Polo's segments divisible by k. Input The first line contains two integers n and k (1 ≀ n, k ≀ 105). Each of the following n lines contain a segment as a pair of integers li and ri ( - 105 ≀ li ≀ ri ≀ 105), separated by a space. It is guaranteed that no two segments intersect. In other words, for any two integers i, j (1 ≀ i < j ≀ n) the following inequality holds, min(ri, rj) < max(li, lj). Output In a single line print a single integer β€” the answer to the problem. Examples Input 2 3 1 2 3 4 Output 2 Input 3 7 1 2 3 3 4 7 Output 0 Submitted Solution: ``` n, k = map(int, input().split()) ans = 0 for _ in range(n): a, b = map(int, input().split()) ans += b - a + 1 #print(ans) print(k - ans % k) ```
instruction
0
48,240
15
96,480
No
output
1
48,240
15
96,481
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Little penguin Polo adores integer segments, that is, pairs of integers [l; r] (l ≀ r). He has a set that consists of n integer segments: [l1; r1], [l2; r2], ..., [ln; rn]. We know that no two segments of this set intersect. In one move Polo can either widen any segment of the set 1 unit to the left or 1 unit to the right, that is transform [l; r] to either segment [l - 1; r], or to segment [l; r + 1]. The value of a set of segments that consists of n segments [l1; r1], [l2; r2], ..., [ln; rn] is the number of integers x, such that there is integer j, for which the following inequality holds, lj ≀ x ≀ rj. Find the minimum number of moves needed to make the value of the set of Polo's segments divisible by k. Input The first line contains two integers n and k (1 ≀ n, k ≀ 105). Each of the following n lines contain a segment as a pair of integers li and ri ( - 105 ≀ li ≀ ri ≀ 105), separated by a space. It is guaranteed that no two segments intersect. In other words, for any two integers i, j (1 ≀ i < j ≀ n) the following inequality holds, min(ri, rj) < max(li, lj). Output In a single line print a single integer β€” the answer to the problem. Examples Input 2 3 1 2 3 4 Output 2 Input 3 7 1 2 3 3 4 7 Output 0 Submitted Solution: ``` n,k = map(int,input().split()) segments = [] size=0 for i in range(n): seg = set(list(map(int,input().split()))) segments.append(seg) size += len(seg) if size %k==0: print(0) else: print(k - size%k) ```
instruction
0
48,241
15
96,482
No
output
1
48,241
15
96,483
Provide tags and a correct Python 3 solution for this coding contest problem. Igor is in the museum and he wants to see as many pictures as possible. Museum can be represented as a rectangular field of n Γ— m cells. Each cell is either empty or impassable. Empty cells are marked with '.', impassable cells are marked with '*'. Every two adjacent cells of different types (one empty and one impassable) are divided by a wall containing one picture. At the beginning Igor is in some empty cell. At every moment he can move to any empty cell that share a side with the current one. For several starting positions you should calculate the maximum number of pictures that Igor can see. Igor is able to see the picture only if he is in the cell adjacent to the wall with this picture. Igor have a lot of time, so he will examine every picture he can see. Input First line of the input contains three integers n, m and k (3 ≀ n, m ≀ 1000, 1 ≀ k ≀ min(nΒ·m, 100 000)) β€” the museum dimensions and the number of starting positions to process. Each of the next n lines contains m symbols '.', '*' β€” the description of the museum. It is guaranteed that all border cells are impassable, so Igor can't go out from the museum. Each of the last k lines contains two integers x and y (1 ≀ x ≀ n, 1 ≀ y ≀ m) β€” the row and the column of one of Igor's starting positions respectively. Rows are numbered from top to bottom, columns β€” from left to right. It is guaranteed that all starting positions are empty cells. Output Print k integers β€” the maximum number of pictures, that Igor can see if he starts in corresponding position. Examples Input 5 6 3 ****** *..*.* ****** *....* ****** 2 2 2 5 4 3 Output 6 4 10 Input 4 4 1 **** *..* *.** **** 3 2 Output 8
instruction
0
48,350
15
96,700
Tags: dfs and similar, graphs, shortest paths Correct Solution: ``` import sys,io,os from atexit import register input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline sys.stdout = io.BytesIO() register(lambda :os.write(1,sys.stdout.getvalue())) visited = [[0]*1002 for _ in range(1002)] out = [[-1]*1002 for _ in range(1002)] n , m , k = [int(x) for x in input().split()] l = [input() for _ in range(n)] Q = [] for j in range(m): for i in range(n): if not visited[i][j] and l[i][j] == b'.'[0]: c = 0 Q.append((i,j)) ind = 0 while ind<len(Q): x,y = Q[ind] ind+=1 if x >= n or x < 0 or y >= m or y < 0: continue if l[x][y] == b'*'[0]: c += 1 continue if visited[x][y] : continue visited[x][y] = 1 Q.append((x+1,y)) Q.append((x,y+1)) Q.append((x-1,y)) Q.append((x,y-1)) while Q: x,y = Q.pop() out[x][y] = c for i in range(k): x , y = [int(x) for x in input().split()] sys.stdout.write(str(out[x-1][y-1]).encode()) sys.stdout.write(b'\n') ```
output
1
48,350
15
96,701
Provide tags and a correct Python 3 solution for this coding contest problem. Igor is in the museum and he wants to see as many pictures as possible. Museum can be represented as a rectangular field of n Γ— m cells. Each cell is either empty or impassable. Empty cells are marked with '.', impassable cells are marked with '*'. Every two adjacent cells of different types (one empty and one impassable) are divided by a wall containing one picture. At the beginning Igor is in some empty cell. At every moment he can move to any empty cell that share a side with the current one. For several starting positions you should calculate the maximum number of pictures that Igor can see. Igor is able to see the picture only if he is in the cell adjacent to the wall with this picture. Igor have a lot of time, so he will examine every picture he can see. Input First line of the input contains three integers n, m and k (3 ≀ n, m ≀ 1000, 1 ≀ k ≀ min(nΒ·m, 100 000)) β€” the museum dimensions and the number of starting positions to process. Each of the next n lines contains m symbols '.', '*' β€” the description of the museum. It is guaranteed that all border cells are impassable, so Igor can't go out from the museum. Each of the last k lines contains two integers x and y (1 ≀ x ≀ n, 1 ≀ y ≀ m) β€” the row and the column of one of Igor's starting positions respectively. Rows are numbered from top to bottom, columns β€” from left to right. It is guaranteed that all starting positions are empty cells. Output Print k integers β€” the maximum number of pictures, that Igor can see if he starts in corresponding position. Examples Input 5 6 3 ****** *..*.* ****** *....* ****** 2 2 2 5 4 3 Output 6 4 10 Input 4 4 1 **** *..* *.** **** 3 2 Output 8
instruction
0
48,351
15
96,702
Tags: dfs and similar, graphs, shortest paths Correct Solution: ``` from collections import deque from sys import stdin, stdout def main(): n, m, k = map(int, stdin.readline().split()) mat = [] for i in range(n): mat.append(stdin.readline()) check = [[False for _ in range(m)] for _ in range(n)] queue = deque() dp = [[0 for _ in range(m)] for _ in range(n)] for i in range(n): for j in range(m): if mat[i][j] == '.' and not check[i][j]: queue.append((i, j)) check[i][j] = True comp = [] count = 0 while queue: x, y = queue.popleft() comp.append((x, y)) dx = [0, 0, -1, 1] dy = [-1, 1, 0, 0] for tmp in range(4): if 0 <= x + dx[tmp] < n and 0 <= y + dy[tmp] < m and not check[x + dx[tmp]][y + dy[tmp]]: if mat[x + dx[tmp]][y + dy[tmp]] == '*': count += 1 elif mat[x + dx[tmp]][y + dy[tmp]] == '.': queue.append((x + dx[tmp], y + dy[tmp])) check[x + dx[tmp]][y + dy[tmp]] = True for x, y in comp: dp[x][y] = count for _ in range(k): x, y = map(int, stdin.readline().split()) stdout.write(str(dp[x - 1][y - 1])) stdout.write('\n') if __name__ == '__main__': main() ```
output
1
48,351
15
96,703
Provide tags and a correct Python 3 solution for this coding contest problem. Igor is in the museum and he wants to see as many pictures as possible. Museum can be represented as a rectangular field of n Γ— m cells. Each cell is either empty or impassable. Empty cells are marked with '.', impassable cells are marked with '*'. Every two adjacent cells of different types (one empty and one impassable) are divided by a wall containing one picture. At the beginning Igor is in some empty cell. At every moment he can move to any empty cell that share a side with the current one. For several starting positions you should calculate the maximum number of pictures that Igor can see. Igor is able to see the picture only if he is in the cell adjacent to the wall with this picture. Igor have a lot of time, so he will examine every picture he can see. Input First line of the input contains three integers n, m and k (3 ≀ n, m ≀ 1000, 1 ≀ k ≀ min(nΒ·m, 100 000)) β€” the museum dimensions and the number of starting positions to process. Each of the next n lines contains m symbols '.', '*' β€” the description of the museum. It is guaranteed that all border cells are impassable, so Igor can't go out from the museum. Each of the last k lines contains two integers x and y (1 ≀ x ≀ n, 1 ≀ y ≀ m) β€” the row and the column of one of Igor's starting positions respectively. Rows are numbered from top to bottom, columns β€” from left to right. It is guaranteed that all starting positions are empty cells. Output Print k integers β€” the maximum number of pictures, that Igor can see if he starts in corresponding position. Examples Input 5 6 3 ****** *..*.* ****** *....* ****** 2 2 2 5 4 3 Output 6 4 10 Input 4 4 1 **** *..* *.** **** 3 2 Output 8
instruction
0
48,352
15
96,704
Tags: dfs and similar, graphs, shortest paths Correct Solution: ``` #Code by Sounak, IIESTS #------------------------------warmup---------------------------- import os import sys import math from io import BytesIO, IOBase from fractions import Fraction import collections from itertools import permutations from collections import defaultdict from collections import deque import threading #sys.setrecursionlimit(300000) #threading.stack_size(10**8) 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") #-------------------game starts now----------------------------------------------------- class Factorial: def __init__(self, MOD): self.MOD = MOD self.factorials = [1, 1] self.invModulos = [0, 1] self.invFactorial_ = [1, 1] def calc(self, n): if n <= -1: print("Invalid argument to calculate n!") print("n must be non-negative value. But the argument was " + str(n)) exit() if n < len(self.factorials): return self.factorials[n] nextArr = [0] * (n + 1 - len(self.factorials)) initialI = len(self.factorials) prev = self.factorials[-1] m = self.MOD for i in range(initialI, n + 1): prev = nextArr[i - initialI] = prev * i % m self.factorials += nextArr return self.factorials[n] def inv(self, n): if n <= -1: print("Invalid argument to calculate n^(-1)") print("n must be non-negative value. But the argument was " + str(n)) exit() p = self.MOD pi = n % p if pi < len(self.invModulos): return self.invModulos[pi] nextArr = [0] * (n + 1 - len(self.invModulos)) initialI = len(self.invModulos) for i in range(initialI, min(p, n + 1)): next = -self.invModulos[p % i] * (p // i) % p self.invModulos.append(next) return self.invModulos[pi] def invFactorial(self, n): if n <= -1: print("Invalid argument to calculate (n^(-1))!") print("n must be non-negative value. But the argument was " + str(n)) exit() if n < len(self.invFactorial_): return self.invFactorial_[n] self.inv(n) # To make sure already calculated n^-1 nextArr = [0] * (n + 1 - len(self.invFactorial_)) initialI = len(self.invFactorial_) prev = self.invFactorial_[-1] p = self.MOD for i in range(initialI, n + 1): prev = nextArr[i - initialI] = (prev * self.invModulos[i % p]) % p self.invFactorial_ += nextArr return self.invFactorial_[n] class Combination: def __init__(self, MOD): self.MOD = MOD self.factorial = Factorial(MOD) def ncr(self, n, k): if k < 0 or n < k: return 0 k = min(k, n - k) f = self.factorial return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD #------------------------------------------------------------------------- def dfs(start_n, start_m, cnt): ans = 0 q = deque([(start_n, start_m)]) visited[start_n][start_m] = cnt while q: pos_n, pos_m = q.pop() for i, j in [(0, 1), (1, 0), (0, -1), (-1, 0)]: next_pos_n = pos_n + i next_pos_m = pos_m + j if 0 <= next_pos_n < n and 0 <= next_pos_m < m: if s[next_pos_n][next_pos_m] == "*": ans += 1 continue if visited[next_pos_n][next_pos_m] == -1: visited[next_pos_n][next_pos_m] = cnt q.append((next_pos_n, next_pos_m)) ans_memo[cnt] = ans n, m, k = map(int, input().split()) s = [input() for i in range(n)] info = [list(map(int, input().split())) for i in range(k)] ans_memo = {} next_pos = [0, 1] visited = [[-1] * m for i in range(n)] cnt = 0 for i in range(n): for j in range(m): if s[i][j] == "*" or visited[i][j] >= 0: continue dfs(i, j, cnt) cnt += 1 for i in range(k): tmp_n, tmp_m = info[i] tmp = visited[tmp_n - 1][tmp_m - 1] print(ans_memo[tmp]) ```
output
1
48,352
15
96,705
Provide tags and a correct Python 3 solution for this coding contest problem. Igor is in the museum and he wants to see as many pictures as possible. Museum can be represented as a rectangular field of n Γ— m cells. Each cell is either empty or impassable. Empty cells are marked with '.', impassable cells are marked with '*'. Every two adjacent cells of different types (one empty and one impassable) are divided by a wall containing one picture. At the beginning Igor is in some empty cell. At every moment he can move to any empty cell that share a side with the current one. For several starting positions you should calculate the maximum number of pictures that Igor can see. Igor is able to see the picture only if he is in the cell adjacent to the wall with this picture. Igor have a lot of time, so he will examine every picture he can see. Input First line of the input contains three integers n, m and k (3 ≀ n, m ≀ 1000, 1 ≀ k ≀ min(nΒ·m, 100 000)) β€” the museum dimensions and the number of starting positions to process. Each of the next n lines contains m symbols '.', '*' β€” the description of the museum. It is guaranteed that all border cells are impassable, so Igor can't go out from the museum. Each of the last k lines contains two integers x and y (1 ≀ x ≀ n, 1 ≀ y ≀ m) β€” the row and the column of one of Igor's starting positions respectively. Rows are numbered from top to bottom, columns β€” from left to right. It is guaranteed that all starting positions are empty cells. Output Print k integers β€” the maximum number of pictures, that Igor can see if he starts in corresponding position. Examples Input 5 6 3 ****** *..*.* ****** *....* ****** 2 2 2 5 4 3 Output 6 4 10 Input 4 4 1 **** *..* *.** **** 3 2 Output 8
instruction
0
48,353
15
96,706
Tags: dfs and similar, graphs, shortest paths Correct Solution: ``` #!/usr/bin/env pypy import sys n, m, k = map(int, input().split()) grid = [] for _ in range(n): grid.append(list(next(sys.stdin))) def dfs(grid, xs, ys, g): stack = [(xs,ys)] grid[xs][ys] = g cnt = 0 while len(stack): i, j = stack.pop() for nx,ny in (i-1,j),(i,j+1),(i+1,j),(i,j-1): if grid[nx][ny] == "*": cnt += 1 elif grid[nx][ny] == ".": grid[nx][ny] = g stack.append((nx,ny)) d[g] = cnt d = [0] * k g = 0 for _ in range(k): i, j = map(int, next(sys.stdin).split()) if grid[i-1][j-1] == ".": dfs(grid, i-1, j-1, g) print(d[grid[i-1][j-1]]) g += 1 ```
output
1
48,353
15
96,707
Provide tags and a correct Python 3 solution for this coding contest problem. Igor is in the museum and he wants to see as many pictures as possible. Museum can be represented as a rectangular field of n Γ— m cells. Each cell is either empty or impassable. Empty cells are marked with '.', impassable cells are marked with '*'. Every two adjacent cells of different types (one empty and one impassable) are divided by a wall containing one picture. At the beginning Igor is in some empty cell. At every moment he can move to any empty cell that share a side with the current one. For several starting positions you should calculate the maximum number of pictures that Igor can see. Igor is able to see the picture only if he is in the cell adjacent to the wall with this picture. Igor have a lot of time, so he will examine every picture he can see. Input First line of the input contains three integers n, m and k (3 ≀ n, m ≀ 1000, 1 ≀ k ≀ min(nΒ·m, 100 000)) β€” the museum dimensions and the number of starting positions to process. Each of the next n lines contains m symbols '.', '*' β€” the description of the museum. It is guaranteed that all border cells are impassable, so Igor can't go out from the museum. Each of the last k lines contains two integers x and y (1 ≀ x ≀ n, 1 ≀ y ≀ m) β€” the row and the column of one of Igor's starting positions respectively. Rows are numbered from top to bottom, columns β€” from left to right. It is guaranteed that all starting positions are empty cells. Output Print k integers β€” the maximum number of pictures, that Igor can see if he starts in corresponding position. Examples Input 5 6 3 ****** *..*.* ****** *....* ****** 2 2 2 5 4 3 Output 6 4 10 Input 4 4 1 **** *..* *.** **** 3 2 Output 8
instruction
0
48,354
15
96,708
Tags: dfs and similar, graphs, shortest paths Correct Solution: ``` import sys def get_array(): return list(map(int, sys.stdin.readline().split())) def get_ints(): return map(int, sys.stdin.readline().split()) def input(): return sys.stdin.readline().strip('\n') n,m,k=get_ints() visited=[] for i in range(n): visited.append([]) for j in range(m): visited[i].append(0) s=0 d={} def dfs(x,y): global s s+=1 #cell=[] c=0 q=[(x,y)] while q: x,y=q.pop() if x>=n or y>=m or x<0 or y<0: return if l[x][y]=='*': c+=1 continue if visited[x][y]: continue visited[x][y]=s #cell.append((x,y)) q.append((x+1,y)) q.append((x-1,y)) q.append((x,y+1)) q.append((x,y-1)) ''' for x in cell: if visited[x[0]][x[1]]: visited[x[0]][x[1]]=c ''' d[s]=c l=[] for i in range(n): l.append(input()) for i in range(n): for j in range(m): if not visited[i][j] and l[i][j]=='.': dfs(i,j) for i in range(k): x,y=get_ints() x-=1 y-=1 print(d[visited[x][y]]) ```
output
1
48,354
15
96,709
Provide tags and a correct Python 3 solution for this coding contest problem. Igor is in the museum and he wants to see as many pictures as possible. Museum can be represented as a rectangular field of n Γ— m cells. Each cell is either empty or impassable. Empty cells are marked with '.', impassable cells are marked with '*'. Every two adjacent cells of different types (one empty and one impassable) are divided by a wall containing one picture. At the beginning Igor is in some empty cell. At every moment he can move to any empty cell that share a side with the current one. For several starting positions you should calculate the maximum number of pictures that Igor can see. Igor is able to see the picture only if he is in the cell adjacent to the wall with this picture. Igor have a lot of time, so he will examine every picture he can see. Input First line of the input contains three integers n, m and k (3 ≀ n, m ≀ 1000, 1 ≀ k ≀ min(nΒ·m, 100 000)) β€” the museum dimensions and the number of starting positions to process. Each of the next n lines contains m symbols '.', '*' β€” the description of the museum. It is guaranteed that all border cells are impassable, so Igor can't go out from the museum. Each of the last k lines contains two integers x and y (1 ≀ x ≀ n, 1 ≀ y ≀ m) β€” the row and the column of one of Igor's starting positions respectively. Rows are numbered from top to bottom, columns β€” from left to right. It is guaranteed that all starting positions are empty cells. Output Print k integers β€” the maximum number of pictures, that Igor can see if he starts in corresponding position. Examples Input 5 6 3 ****** *..*.* ****** *....* ****** 2 2 2 5 4 3 Output 6 4 10 Input 4 4 1 **** *..* *.** **** 3 2 Output 8
instruction
0
48,355
15
96,710
Tags: dfs and similar, graphs, shortest paths Correct Solution: ``` import sys,io,os from atexit import register input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline sys.stdout = io.BytesIO() register(lambda :os.write(1,sys.stdout.getvalue())) def print(args=[], sep=' ', end='\n', file=sys.stdout): first = True sep = sep.encode() for a in args: if first: first = False else: file.write(sep) file.write(str(a).encode()) file.write(end.encode()) visited = [[0]*1010 for _ in range(1010)] out = [[-1]*1010 for _ in range(1010)] cells = [] def dfs(x,y): global c Q = [(x,y)] while Q: x,y = Q.pop() if x >= n or x < 0 or y >= m or y < 0: continue if l[x][y] == b'*'[0]: c += 1 continue if visited[x][y] : continue visited[x][y] = 1 cells.append((x,y)) Q.append((x+1,y)) Q.append((x,y+1)) Q.append((x-1,y)) Q.append((x,y-1)) n , m , k = [int(x) for x in input().split()] l = [input() for _ in range(n)] for i in range(n): for j in range(m): if not visited[i][j] and l[i][j] == b'.'[0]: c = 0 dfs(i,j) while cells: x = cells.pop() if visited[x[0]][x[1]] and out[x[0]][x[1]] == -1: out[x[0]][x[1]] = c ANS = [] for i in range(k): x , y = [int(x) for x in input().split()] ANS.append(out[x-1][y-1]) print(ANS,sep='\n') ```
output
1
48,355
15
96,711
Provide tags and a correct Python 3 solution for this coding contest problem. Igor is in the museum and he wants to see as many pictures as possible. Museum can be represented as a rectangular field of n Γ— m cells. Each cell is either empty or impassable. Empty cells are marked with '.', impassable cells are marked with '*'. Every two adjacent cells of different types (one empty and one impassable) are divided by a wall containing one picture. At the beginning Igor is in some empty cell. At every moment he can move to any empty cell that share a side with the current one. For several starting positions you should calculate the maximum number of pictures that Igor can see. Igor is able to see the picture only if he is in the cell adjacent to the wall with this picture. Igor have a lot of time, so he will examine every picture he can see. Input First line of the input contains three integers n, m and k (3 ≀ n, m ≀ 1000, 1 ≀ k ≀ min(nΒ·m, 100 000)) β€” the museum dimensions and the number of starting positions to process. Each of the next n lines contains m symbols '.', '*' β€” the description of the museum. It is guaranteed that all border cells are impassable, so Igor can't go out from the museum. Each of the last k lines contains two integers x and y (1 ≀ x ≀ n, 1 ≀ y ≀ m) β€” the row and the column of one of Igor's starting positions respectively. Rows are numbered from top to bottom, columns β€” from left to right. It is guaranteed that all starting positions are empty cells. Output Print k integers β€” the maximum number of pictures, that Igor can see if he starts in corresponding position. Examples Input 5 6 3 ****** *..*.* ****** *....* ****** 2 2 2 5 4 3 Output 6 4 10 Input 4 4 1 **** *..* *.** **** 3 2 Output 8
instruction
0
48,356
15
96,712
Tags: dfs and similar, graphs, shortest paths Correct Solution: ``` import sys input = sys.stdin.readline H, W, N = map(int, input().split()) G = [input().rstrip() for _ in range(H)] dx = [1, 0, -1, 0] dy = [0, 1, 0, -1] visited = [[0 for j in range(W)] for i in range(H)] picture = [[0 for j in range(W)] for i in range(H)] for i in range(H): for j in range(W): if G[i][j] == '*' or visited[i][j]: continue stack = [(i, j)] memo = [(i, j)] visited[i][j] = 1 p = 0 while stack: x, y = stack.pop() for k in range(4): nx, ny = x + dx[k], y + dy[k] if not (0 <= nx < H and 0 <= ny < W) or visited[nx][ny]: continue if G[nx][ny] == '*': p += 1 continue visited[nx][ny] = 1 stack.append((nx, ny)) memo.append((nx, ny)) for x, y in memo: picture[x][y] = p res = [] for _ in range(N): x, y = map(lambda a: int(a) - 1, input().split()) res.append(picture[x][y]) print('\n'.join(map(str, res))) ```
output
1
48,356
15
96,713
Provide tags and a correct Python 3 solution for this coding contest problem. Igor is in the museum and he wants to see as many pictures as possible. Museum can be represented as a rectangular field of n Γ— m cells. Each cell is either empty or impassable. Empty cells are marked with '.', impassable cells are marked with '*'. Every two adjacent cells of different types (one empty and one impassable) are divided by a wall containing one picture. At the beginning Igor is in some empty cell. At every moment he can move to any empty cell that share a side with the current one. For several starting positions you should calculate the maximum number of pictures that Igor can see. Igor is able to see the picture only if he is in the cell adjacent to the wall with this picture. Igor have a lot of time, so he will examine every picture he can see. Input First line of the input contains three integers n, m and k (3 ≀ n, m ≀ 1000, 1 ≀ k ≀ min(nΒ·m, 100 000)) β€” the museum dimensions and the number of starting positions to process. Each of the next n lines contains m symbols '.', '*' β€” the description of the museum. It is guaranteed that all border cells are impassable, so Igor can't go out from the museum. Each of the last k lines contains two integers x and y (1 ≀ x ≀ n, 1 ≀ y ≀ m) β€” the row and the column of one of Igor's starting positions respectively. Rows are numbered from top to bottom, columns β€” from left to right. It is guaranteed that all starting positions are empty cells. Output Print k integers β€” the maximum number of pictures, that Igor can see if he starts in corresponding position. Examples Input 5 6 3 ****** *..*.* ****** *....* ****** 2 2 2 5 4 3 Output 6 4 10 Input 4 4 1 **** *..* *.** **** 3 2 Output 8
instruction
0
48,357
15
96,714
Tags: dfs and similar, graphs, shortest paths Correct Solution: ``` import sys input = lambda: sys.stdin.readline().rstrip() from collections import deque N, M, K = map(int, input().split()) room = [input() for i in range(N)] memo = [[-1 for __ in range(M)] for _ in range(N)] for i in range(K): a, b = map(int, input().split()) if memo[a - 1][b - 1] != -1: print(memo[a - 1][b - 1]) continue deq = deque([(a - 1, b - 1)]) ans = 0 trace = {(a - 1) * M + (b - 1)} while len(deq) != 0: x, y = deq.popleft() for p, q in [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)]: if room[p][q] == '*': ans += 1 else: if p*M+q not in trace: deq.append((p, q)) trace.add(p*M+q) for a in trace: tx = a // M ty = a % M memo[tx][ty] = ans print(ans) ```
output
1
48,357
15
96,715
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Igor is in the museum and he wants to see as many pictures as possible. Museum can be represented as a rectangular field of n Γ— m cells. Each cell is either empty or impassable. Empty cells are marked with '.', impassable cells are marked with '*'. Every two adjacent cells of different types (one empty and one impassable) are divided by a wall containing one picture. At the beginning Igor is in some empty cell. At every moment he can move to any empty cell that share a side with the current one. For several starting positions you should calculate the maximum number of pictures that Igor can see. Igor is able to see the picture only if he is in the cell adjacent to the wall with this picture. Igor have a lot of time, so he will examine every picture he can see. Input First line of the input contains three integers n, m and k (3 ≀ n, m ≀ 1000, 1 ≀ k ≀ min(nΒ·m, 100 000)) β€” the museum dimensions and the number of starting positions to process. Each of the next n lines contains m symbols '.', '*' β€” the description of the museum. It is guaranteed that all border cells are impassable, so Igor can't go out from the museum. Each of the last k lines contains two integers x and y (1 ≀ x ≀ n, 1 ≀ y ≀ m) β€” the row and the column of one of Igor's starting positions respectively. Rows are numbered from top to bottom, columns β€” from left to right. It is guaranteed that all starting positions are empty cells. Output Print k integers β€” the maximum number of pictures, that Igor can see if he starts in corresponding position. Examples Input 5 6 3 ****** *..*.* ****** *....* ****** 2 2 2 5 4 3 Output 6 4 10 Input 4 4 1 **** *..* *.** **** 3 2 Output 8 Submitted Solution: ``` import sys input = sys.stdin.readline H, W, Q = map(int, input().split()) S = [list(input())[: -1] for _ in range(H)] vis = [0] * (H * W) table = [0] * (H * W) d = [-1, 0, 1, 0] for i in range(H): for j in range(W): if vis[i * W + j]: continue if S[i][j] != ".": continue seen = 0 s = [i * W + j] order = [] while len(s): x = s.pop() order.append(x) if vis[x]: continue vis[x] = 1 for k in range(4): u = x // W + d[k] v = x % W + d[-1 - k] if u in range(H) and (v in range(W)): if vis[u * W + v]: continue if S[u][v] == ".": s.append(u * W + v) elif S[u][v] == "*": seen += 1 for x in order: table[x] = seen for _ in range(Q): y, x = map(int, input().split()) print(table[(y - 1) * W + x - 1]) ```
instruction
0
48,358
15
96,716
Yes
output
1
48,358
15
96,717
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Igor is in the museum and he wants to see as many pictures as possible. Museum can be represented as a rectangular field of n Γ— m cells. Each cell is either empty or impassable. Empty cells are marked with '.', impassable cells are marked with '*'. Every two adjacent cells of different types (one empty and one impassable) are divided by a wall containing one picture. At the beginning Igor is in some empty cell. At every moment he can move to any empty cell that share a side with the current one. For several starting positions you should calculate the maximum number of pictures that Igor can see. Igor is able to see the picture only if he is in the cell adjacent to the wall with this picture. Igor have a lot of time, so he will examine every picture he can see. Input First line of the input contains three integers n, m and k (3 ≀ n, m ≀ 1000, 1 ≀ k ≀ min(nΒ·m, 100 000)) β€” the museum dimensions and the number of starting positions to process. Each of the next n lines contains m symbols '.', '*' β€” the description of the museum. It is guaranteed that all border cells are impassable, so Igor can't go out from the museum. Each of the last k lines contains two integers x and y (1 ≀ x ≀ n, 1 ≀ y ≀ m) β€” the row and the column of one of Igor's starting positions respectively. Rows are numbered from top to bottom, columns β€” from left to right. It is guaranteed that all starting positions are empty cells. Output Print k integers β€” the maximum number of pictures, that Igor can see if he starts in corresponding position. Examples Input 5 6 3 ****** *..*.* ****** *....* ****** 2 2 2 5 4 3 Output 6 4 10 Input 4 4 1 **** *..* *.** **** 3 2 Output 8 Submitted Solution: ``` import sys from queue import deque # sys.stdin = open('ivo.in') move = [(0, 1), (1, 0), (0, -1), (-1, 0)] n, m, k = map(int, sys.stdin.readline().split()) a = [] for i in range(n): a.append(sys.stdin.readline().rstrip()) visited = [] values = [] for x in range(n): visited.append([]) values.append([]) for y in range(m): visited[x].append(False) values[x].append(0) for x in range(n): for y in range(m): if a[x][y] == '*' or visited[x][y]: continue q = deque() visited[x][y] = True q.append((x, y)) sum = 0 connected = [(x, y)] while len(q) != 0: cur = q.pop() for l in move: tx = cur[0] + l[0] ty = cur[1] + l[1] if tx < 0 or tx >= n or ty < 0 or ty >= m: continue if a[tx][ty] == '.' and visited[tx][ty]: continue if a[tx][ty] == '*': sum += 1 continue q.append((tx, ty)) visited[tx][ty] = True connected.append((tx, ty)) for c in connected: values[c[0]][c[1]] = sum for i in range(k): x, y = map(int, sys.stdin.readline().split()) print(values[x - 1][y - 1]) ```
instruction
0
48,359
15
96,718
Yes
output
1
48,359
15
96,719
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Igor is in the museum and he wants to see as many pictures as possible. Museum can be represented as a rectangular field of n Γ— m cells. Each cell is either empty or impassable. Empty cells are marked with '.', impassable cells are marked with '*'. Every two adjacent cells of different types (one empty and one impassable) are divided by a wall containing one picture. At the beginning Igor is in some empty cell. At every moment he can move to any empty cell that share a side with the current one. For several starting positions you should calculate the maximum number of pictures that Igor can see. Igor is able to see the picture only if he is in the cell adjacent to the wall with this picture. Igor have a lot of time, so he will examine every picture he can see. Input First line of the input contains three integers n, m and k (3 ≀ n, m ≀ 1000, 1 ≀ k ≀ min(nΒ·m, 100 000)) β€” the museum dimensions and the number of starting positions to process. Each of the next n lines contains m symbols '.', '*' β€” the description of the museum. It is guaranteed that all border cells are impassable, so Igor can't go out from the museum. Each of the last k lines contains two integers x and y (1 ≀ x ≀ n, 1 ≀ y ≀ m) β€” the row and the column of one of Igor's starting positions respectively. Rows are numbered from top to bottom, columns β€” from left to right. It is guaranteed that all starting positions are empty cells. Output Print k integers β€” the maximum number of pictures, that Igor can see if he starts in corresponding position. Examples Input 5 6 3 ****** *..*.* ****** *....* ****** 2 2 2 5 4 3 Output 6 4 10 Input 4 4 1 **** *..* *.** **** 3 2 Output 8 Submitted Solution: ``` import collections import sys # def somein(): # s = input() # testnumber = int(input()) # for tn in range(testnumber): # pass input = sys.stdin.readline n, m, k = map(int, input().split() ) mu = [ input() for _ in range(n)] colored = [ [0]*m for _ in range(n)] def paint_and_count(x, y, col): s=0 q=collections.deque() if mu[x][y] =="*": return 0 colored[x][y] = col q.append( (x,y) ) while q: x, y = q.popleft() x = x + 1 if mu[x][y]=="." and colored[x][y] == 0: colored[x][y] = col q.append( (x, y) ) elif mu[x][y] == "*": s += 1 x = x - 2 if mu[x][y]=="." and colored[x][y] == 0: colored[x][y] = col q.append( (x, y) ) elif mu[x][y] == "*": s += 1 x, y = x+1, y+1 if mu[x][y]=="." and colored[x][y] == 0: colored[x][y] = col q.append( (x, y) ) elif mu[x][y] == "*": s += 1 y = y - 2 if mu[x][y]=="." and colored[x][y] == 0: colored[x][y] = col q.append( (x, y) ) elif mu[x][y] == "*": s += 1 return s d=[0] # count colors queries = [list(map(int, input().split())) for i in range(k)] color=1 for x, y in queries: if mu[x-1][y-1] == ".": if not colored[x-1][y-1]: d.append( paint_and_count(x-1,y-1, color) ) color += 1 print(d[ colored[x-1][y-1] ]) else: print(0) ```
instruction
0
48,360
15
96,720
Yes
output
1
48,360
15
96,721
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Igor is in the museum and he wants to see as many pictures as possible. Museum can be represented as a rectangular field of n Γ— m cells. Each cell is either empty or impassable. Empty cells are marked with '.', impassable cells are marked with '*'. Every two adjacent cells of different types (one empty and one impassable) are divided by a wall containing one picture. At the beginning Igor is in some empty cell. At every moment he can move to any empty cell that share a side with the current one. For several starting positions you should calculate the maximum number of pictures that Igor can see. Igor is able to see the picture only if he is in the cell adjacent to the wall with this picture. Igor have a lot of time, so he will examine every picture he can see. Input First line of the input contains three integers n, m and k (3 ≀ n, m ≀ 1000, 1 ≀ k ≀ min(nΒ·m, 100 000)) β€” the museum dimensions and the number of starting positions to process. Each of the next n lines contains m symbols '.', '*' β€” the description of the museum. It is guaranteed that all border cells are impassable, so Igor can't go out from the museum. Each of the last k lines contains two integers x and y (1 ≀ x ≀ n, 1 ≀ y ≀ m) β€” the row and the column of one of Igor's starting positions respectively. Rows are numbered from top to bottom, columns β€” from left to right. It is guaranteed that all starting positions are empty cells. Output Print k integers β€” the maximum number of pictures, that Igor can see if he starts in corresponding position. Examples Input 5 6 3 ****** *..*.* ****** *....* ****** 2 2 2 5 4 3 Output 6 4 10 Input 4 4 1 **** *..* *.** **** 3 2 Output 8 Submitted Solution: ``` import sys def get_array(): return list(map(int, sys.stdin.readline().split())) def get_ints(): return map(int, sys.stdin.readline().split()) def input(): return sys.stdin.readline().strip('\n') visited = [] out = [] for i in range(1010): visited.append([]) out.append([]) for j in range(1010): visited[i].append(0) out[i].append(-1) cells = [] def dfs(x,y): global c Q = [(x,y)] while Q: x,y = Q.pop() if x >= n or x < 0 or y >= m or y < 0: continue if l[x][y] == '*': c += 1 continue if visited[x][y] : continue visited[x][y] = 1 cells.append((x,y)) Q.append((x+1,y)) Q.append((x,y+1)) Q.append((x-1,y)) Q.append((x,y-1)) n , m , k = get_ints() l = [] for i in range(n): l.append(list(input())) for i in range(n): for j in range(m): if not visited[i][j] and l[i][j] == '.': c = 0 dfs(i,j) for x in cells: if visited[x[0]][x[1]] and out[x[0]][x[1]] == -1: out[x[0]][x[1]] = c cells.clear() for i in range(k): x , y = get_ints() x , y = x-1 , y-1 ans = out[x][y] print(ans) ```
instruction
0
48,361
15
96,722
Yes
output
1
48,361
15
96,723
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Igor is in the museum and he wants to see as many pictures as possible. Museum can be represented as a rectangular field of n Γ— m cells. Each cell is either empty or impassable. Empty cells are marked with '.', impassable cells are marked with '*'. Every two adjacent cells of different types (one empty and one impassable) are divided by a wall containing one picture. At the beginning Igor is in some empty cell. At every moment he can move to any empty cell that share a side with the current one. For several starting positions you should calculate the maximum number of pictures that Igor can see. Igor is able to see the picture only if he is in the cell adjacent to the wall with this picture. Igor have a lot of time, so he will examine every picture he can see. Input First line of the input contains three integers n, m and k (3 ≀ n, m ≀ 1000, 1 ≀ k ≀ min(nΒ·m, 100 000)) β€” the museum dimensions and the number of starting positions to process. Each of the next n lines contains m symbols '.', '*' β€” the description of the museum. It is guaranteed that all border cells are impassable, so Igor can't go out from the museum. Each of the last k lines contains two integers x and y (1 ≀ x ≀ n, 1 ≀ y ≀ m) β€” the row and the column of one of Igor's starting positions respectively. Rows are numbered from top to bottom, columns β€” from left to right. It is guaranteed that all starting positions are empty cells. Output Print k integers β€” the maximum number of pictures, that Igor can see if he starts in corresponding position. Examples Input 5 6 3 ****** *..*.* ****** *....* ****** 2 2 2 5 4 3 Output 6 4 10 Input 4 4 1 **** *..* *.** **** 3 2 Output 8 Submitted Solution: ``` n,m,k = [int(x) for x in input().split()] a = [list(input()) for _ in range(n)] results = [0] * k def visit(i,j): global res # print('visiting ', i, j) # print(f'res={res}') if a[i][j] == '*': res += 1 return elif a[i][j] == '.': a[i][j] = x visit(i-1,j) visit(i+1,j) visit(i,j-1) visit(i,j+1) for x in range(k): i,j = [int(x) for x in input().split()] i -= 1 j -= 1 res = 0 if a[i][j] == '.': # visit(i,j) results[x] = res print(results[x]) else: print(results[a[i][j]]) ```
instruction
0
48,362
15
96,724
No
output
1
48,362
15
96,725
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Igor is in the museum and he wants to see as many pictures as possible. Museum can be represented as a rectangular field of n Γ— m cells. Each cell is either empty or impassable. Empty cells are marked with '.', impassable cells are marked with '*'. Every two adjacent cells of different types (one empty and one impassable) are divided by a wall containing one picture. At the beginning Igor is in some empty cell. At every moment he can move to any empty cell that share a side with the current one. For several starting positions you should calculate the maximum number of pictures that Igor can see. Igor is able to see the picture only if he is in the cell adjacent to the wall with this picture. Igor have a lot of time, so he will examine every picture he can see. Input First line of the input contains three integers n, m and k (3 ≀ n, m ≀ 1000, 1 ≀ k ≀ min(nΒ·m, 100 000)) β€” the museum dimensions and the number of starting positions to process. Each of the next n lines contains m symbols '.', '*' β€” the description of the museum. It is guaranteed that all border cells are impassable, so Igor can't go out from the museum. Each of the last k lines contains two integers x and y (1 ≀ x ≀ n, 1 ≀ y ≀ m) β€” the row and the column of one of Igor's starting positions respectively. Rows are numbered from top to bottom, columns β€” from left to right. It is guaranteed that all starting positions are empty cells. Output Print k integers β€” the maximum number of pictures, that Igor can see if he starts in corresponding position. Examples Input 5 6 3 ****** *..*.* ****** *....* ****** 2 2 2 5 4 3 Output 6 4 10 Input 4 4 1 **** *..* *.** **** 3 2 Output 8 Submitted Solution: ``` def c(A,P,n,m): k=0 if(P[0]+1<n): if(A[P[0]+1][P[1]]=="*"): k+=1 elif(A[P[0]+1][P[1]]=="."): A[P[0]][P[1]]='#' k=k+c(A,[P[0]+1,P[1]],n,m) A[P[0]][P[1]]='.' if(P[0]-1>=0): if(A[P[0]-1][P[1]]=="*"): k+=1 elif(A[P[0]-1][P[1]]=="."): A[P[0]][P[1]]='#' k=k+c(A,[P[0]-1,P[1]],n,m) A[P[0]][P[1]]='.' if(P[1]+1<m): if(A[P[0]][P[1]+1]=="*"): k+=1 elif(A[P[0]][P[1]+1]=="."): A[P[0]][P[1]]='#' k=k+c(A,[P[0],P[1]+1],n,m) A[P[0]][P[1]]='.' if(P[1]-1>=0): if(A[P[0]][P[1]-1]=="*"): k+=1 elif(A[P[0]][P[1]-1]=="."): A[P[0]][P[1]]='#' k=k+c(A,[P[0],P[1]-1],n,m) A[P[0]][P[1]]='.' return k n,m,k=input().split() n=int(n) m=int(m) k=int(k) A=[] for i in range(n): s=input() A.append([]) for j in range(m): A[i].append(s[j]) pList=[] for i in range(k): x,y=input().split() x=int(x)-1 y=int(y)-1 pList.append([x,y]) for p in pList: if(A[p[0]][p[1]]=='.'): print(c(A,p,n,m)) else: print(0) ```
instruction
0
48,363
15
96,726
No
output
1
48,363
15
96,727
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Igor is in the museum and he wants to see as many pictures as possible. Museum can be represented as a rectangular field of n Γ— m cells. Each cell is either empty or impassable. Empty cells are marked with '.', impassable cells are marked with '*'. Every two adjacent cells of different types (one empty and one impassable) are divided by a wall containing one picture. At the beginning Igor is in some empty cell. At every moment he can move to any empty cell that share a side with the current one. For several starting positions you should calculate the maximum number of pictures that Igor can see. Igor is able to see the picture only if he is in the cell adjacent to the wall with this picture. Igor have a lot of time, so he will examine every picture he can see. Input First line of the input contains three integers n, m and k (3 ≀ n, m ≀ 1000, 1 ≀ k ≀ min(nΒ·m, 100 000)) β€” the museum dimensions and the number of starting positions to process. Each of the next n lines contains m symbols '.', '*' β€” the description of the museum. It is guaranteed that all border cells are impassable, so Igor can't go out from the museum. Each of the last k lines contains two integers x and y (1 ≀ x ≀ n, 1 ≀ y ≀ m) β€” the row and the column of one of Igor's starting positions respectively. Rows are numbered from top to bottom, columns β€” from left to right. It is guaranteed that all starting positions are empty cells. Output Print k integers β€” the maximum number of pictures, that Igor can see if he starts in corresponding position. Examples Input 5 6 3 ****** *..*.* ****** *....* ****** 2 2 2 5 4 3 Output 6 4 10 Input 4 4 1 **** *..* *.** **** 3 2 Output 8 Submitted Solution: ``` import sys input = sys.stdin.readline H, W, Q = map(int, input().split()) S = [list(input())[: -1] for _ in range(H)] vis = [0] * (H * W) table = [0] * (H * W) d = [-1, 0, 1, 0] for i in range(H): for j in range(W): if vis[i * W + j]: continue if S[i][j] != ".": continue seen = 0 s = [i * W + j] order = [] while len(s): x = s.pop() order.append(x) vis[x] = 1 for k in range(4): u = x // W + d[k] v = x % W + d[-1 - k] if u in range(H) and (v in range(W)): if vis[u * W + v]: continue if S[u][v] == ".": s.append(u * W + v) else: seen += 1 for x in order: table[x] = seen for _ in range(Q): y, x = map(int, input().split()) print(table[(y - 1) * W + x - 1]) ```
instruction
0
48,364
15
96,728
No
output
1
48,364
15
96,729
Provide tags and a correct Python 3 solution for this coding contest problem. Anton likes to play chess. Also, he likes to do programming. That is why he decided to write the program that plays chess. However, he finds the game on 8 to 8 board to too simple, he uses an infinite one instead. The first task he faced is to check whether the king is in check. Anton doesn't know how to implement this so he asks you to help. Consider that an infinite chess board contains one white king and the number of black pieces. There are only rooks, bishops and queens, as the other pieces are not supported yet. The white king is said to be in check if at least one black piece can reach the cell with the king in one move. Help Anton and write the program that for the given position determines whether the white king is in check. Remainder, on how do chess pieces move: * Bishop moves any number of cells diagonally, but it can't "leap" over the occupied cells. * Rook moves any number of cells horizontally or vertically, but it also can't "leap" over the occupied cells. * Queen is able to move any number of cells horizontally, vertically or diagonally, but it also can't "leap". Input The first line of the input contains a single integer n (1 ≀ n ≀ 500 000) β€” the number of black pieces. The second line contains two integers x0 and y0 ( - 109 ≀ x0, y0 ≀ 109) β€” coordinates of the white king. Then follow n lines, each of them contains a character and two integers xi and yi ( - 109 ≀ xi, yi ≀ 109) β€” type of the i-th piece and its position. Character 'B' stands for the bishop, 'R' for the rook and 'Q' for the queen. It's guaranteed that no two pieces occupy the same position. Output The only line of the output should contains "YES" (without quotes) if the white king is in check and "NO" (without quotes) otherwise. Examples Input 2 4 2 R 1 1 B 1 5 Output YES Input 2 4 2 R 3 3 B 1 5 Output NO Note Picture for the first sample: <image> White king is in check, because the black bishop can reach the cell with the white king in one move. The answer is "YES". Picture for the second sample: <image> Here bishop can't reach the cell with the white king, because his path is blocked by the rook, and the bishop cant "leap" over it. Rook can't reach the white king, because it can't move diagonally. Hence, the king is not in check and the answer is "NO".
instruction
0
48,434
15
96,868
Tags: implementation Correct Solution: ``` def main(): n = int(input()) x, y = list(map(int, input().split())) a = [] for i in range(n): a.append(input().split()) for i in range(n): a[i][1] = int(a[i][1]) - x a[i][2] = int(a[i][2]) - y typneedbql = [] typneedbqr = [] typneedbqu = [] typneedbqd = [] typneedrlu = [] typneedrru = [] typneedrld = [] typneedrrd = [] for i in range(n): if (a[i][1] * a[i][2] == 0 and a[i][1] < 0): typneedbql.append([abs(a[i][1] + a[i][2])] + a[i]) elif (a[i][1] * a[i][2] == 0 and a[i][1] > 0): typneedbqr.append([abs(a[i][1] + a[i][2])] + a[i]) elif (a[i][1] * a[i][2] == 0 and a[i][2] > 0): typneedbqu.append([abs(a[i][1] + a[i][2])] + a[i]) elif (a[i][1] * a[i][2] == 0 and a[i][2] < 0): typneedbqd.append([abs(a[i][1] + a[i][2])] + a[i]) elif (abs(a[i][1] / a[i][2]) == 1 and a[i][1] < 0 and a[i][2] < 0): typneedrlu.append([abs(a[i][1])] + a[i]) elif (abs(a[i][1] / a[i][2]) == 1 and a[i][1] < 0 and a[i][2] > 0): #print(i, a[i][1], a[i][2]) typneedrld.append([abs(a[i][1])] + a[i]) elif (abs(a[i][1] / a[i][2]) == 1 and a[i][1] > 0 and a[i][2] > 0): typneedrrd.append([abs(a[i][1])] + a[i]) elif (abs(a[i][1] / a[i][2]) == 1 and a[i][1] > 0 and a[i][2] < 0): typneedrru.append([abs(a[i][1])] + a[i]) typneedbql.sort() typneedbqr.sort() typneedbqd.sort() typneedbqu.sort() typneedrlu.sort() typneedrru.sort() typneedrld.sort() typneedrrd.sort() #print(a) #print(typneedrld) for i in range(len(typneedbql)): if typneedbql[i][1] == "R" or typneedbql[i][1] == "Q": print("YES") return else: break for i in range(len(typneedbqr)): if typneedbqr[i][1] == "R" or typneedbqr[i][1] == "Q": print("YES") return else: break for i in range(len(typneedbqu)): if typneedbqu[i][1] == "R" or typneedbqu[i][1] == "Q": print("YES") return else: break for i in range(len(typneedbqd)): if typneedbqd[i][1] == "R" or typneedbqd[i][1] == "Q": print("YES") return else: break for i in range(len(typneedrlu)): if typneedrlu[i][1] == "B" or typneedrlu[i][1] == "Q": print("YES") return else: break for i in range(len(typneedrld)): if typneedrld[i][1] == "B" or typneedrld[i][1] == "Q": print("YES") return else: break for i in range(len(typneedrrd)): if typneedrrd[i][1] == "B" or typneedrrd[i][1] == "Q": print("YES") return else: break for i in range(len(typneedrru)): if typneedrru[i][1] == "B" or typneedrru[i][1] == "Q": print("YES") return else: break print("NO") main() ```
output
1
48,434
15
96,869
Provide tags and a correct Python 3 solution for this coding contest problem. Anton likes to play chess. Also, he likes to do programming. That is why he decided to write the program that plays chess. However, he finds the game on 8 to 8 board to too simple, he uses an infinite one instead. The first task he faced is to check whether the king is in check. Anton doesn't know how to implement this so he asks you to help. Consider that an infinite chess board contains one white king and the number of black pieces. There are only rooks, bishops and queens, as the other pieces are not supported yet. The white king is said to be in check if at least one black piece can reach the cell with the king in one move. Help Anton and write the program that for the given position determines whether the white king is in check. Remainder, on how do chess pieces move: * Bishop moves any number of cells diagonally, but it can't "leap" over the occupied cells. * Rook moves any number of cells horizontally or vertically, but it also can't "leap" over the occupied cells. * Queen is able to move any number of cells horizontally, vertically or diagonally, but it also can't "leap". Input The first line of the input contains a single integer n (1 ≀ n ≀ 500 000) β€” the number of black pieces. The second line contains two integers x0 and y0 ( - 109 ≀ x0, y0 ≀ 109) β€” coordinates of the white king. Then follow n lines, each of them contains a character and two integers xi and yi ( - 109 ≀ xi, yi ≀ 109) β€” type of the i-th piece and its position. Character 'B' stands for the bishop, 'R' for the rook and 'Q' for the queen. It's guaranteed that no two pieces occupy the same position. Output The only line of the output should contains "YES" (without quotes) if the white king is in check and "NO" (without quotes) otherwise. Examples Input 2 4 2 R 1 1 B 1 5 Output YES Input 2 4 2 R 3 3 B 1 5 Output NO Note Picture for the first sample: <image> White king is in check, because the black bishop can reach the cell with the white king in one move. The answer is "YES". Picture for the second sample: <image> Here bishop can't reach the cell with the white king, because his path is blocked by the rook, and the bishop cant "leap" over it. Rook can't reach the white king, because it can't move diagonally. Hence, the king is not in check and the answer is "NO".
instruction
0
48,435
15
96,870
Tags: implementation Correct Solution: ``` MAX_NUM = 10**9 MIN_NUM = -10**9 from sys import stdin, stdout n = int(stdin.readline().rstrip()) king_x, king_y = map(int,stdin.readline().rstrip().split()) nomakers = {'Rook': {'right-down':[], 'right-up':[], 'left-down':[], 'left-up':[]}, 'Bishop': {'left':[], 'right':[], 'up':[], 'down':[]}} yes = {'Queen':{'right-down':[], 'right-up':[], 'left-down':[], 'left-up':[],'left':[], 'right':[], 'up':[], 'down':[]},'Bishop': {'right-down':[], 'right-up':[], 'left-down':[], 'left-up':[]}, 'Rook': {'left':[], 'right':[], 'up':[], 'down':[]}} for i in range(n): figure, figure_x, figure_y = stdin.readline().rstrip().split() figure_x, figure_y = int(figure_x), int(figure_y) if figure == 'Q': if figure_x == king_x: if figure_y < king_y: half = 'down' else: half = 'up' yes['Queen'][half].append(abs(king_y - figure_y)) for i in range(len(nomakers['Bishop'][half])): if nomakers['Bishop'][half][i]<yes['Queen'][half][-1]: del yes['Queen'][half][-1] break elif figure_y == king_y: if figure_x < king_x: half = 'left' else: half = 'right' yes['Queen'][half].append(abs(king_x - figure_x)) for i in range(len(nomakers['Bishop'][half])): if nomakers['Bishop'][half][i]<yes['Queen'][half][-1]: del yes['Queen'][half][-1] break elif abs(figure_x - king_x) == abs(figure_y - king_y): if figure_x > king_x: if figure_y > king_y: quarter = 'right-up' else: quarter = 'right-down' else: if figure_y > king_y: quarter = 'left-up' else: quarter = 'left-down' yes['Queen'][quarter].append(abs(king_x - figure_x)) for i in range(len(nomakers['Rook'][quarter])): if nomakers['Rook'][quarter][i]<yes['Queen'][quarter][-1]: del yes['Queen'][quarter][-1] break elif figure == 'R': if figure_x == king_x: if figure_y < king_y: half = 'down' else: half = 'up' yes['Rook'][half].append(abs(king_y - figure_y)) for i in range(len(nomakers['Bishop'][half])): if nomakers['Bishop'][half][i]<yes['Rook'][half][-1]: del yes['Rook'][half][-1] break elif figure_y == king_y: if figure_x > king_x: half = 'right' else: half = 'left' yes['Rook'][half].append(abs(king_x - figure_x)) for i in range(len(nomakers['Bishop'][half])): if nomakers['Bishop'][half][i]<yes['Rook'][half][-1]: del yes['Rook'][half][-1] break elif abs(figure_x - king_x) == abs(figure_y - king_y): if figure_x > king_x: if figure_y > king_y: quarter = 'right-up' else: quarter = 'right-down' else: if figure_y > king_y: quarter = 'left-up' else: quarter = 'left-down' nomakers['Rook'][quarter].append(abs(figure_x - king_x)) i = 0 n = len(yes['Queen'][quarter]) while i < n: element = yes['Queen'][quarter][i] if nomakers['Rook'][quarter][-1] < element: del yes['Queen'][quarter][i] n = n - 1 else: i = i + 1 i = 0 n = len(yes['Bishop'][quarter]) while i < n: element = yes['Bishop'][quarter][i] if nomakers['Rook'][quarter][-1] < element: del yes['Bishop'][quarter][i] n = n - 1 else: i = i + 1 else: if abs(figure_x - king_x) == abs(figure_y - king_y): if figure_x > king_x: if figure_y > king_y: quarter = 'right-up' else: quarter = 'right-down' else: if figure_y > king_y: quarter = 'left-up' else: quarter = 'left-down' yes['Bishop'][quarter].append(abs(figure_x - king_x)) for i in range(len(nomakers['Rook'][quarter])): if nomakers['Rook'][quarter][i] < yes['Bishop'][quarter][-1]: del yes['Bishop'][quarter][-1] break elif figure_x == king_x or figure_y == king_y: if figure_y < king_y: a = figure_y - king_y half = 'down' elif figure_y > king_y: half = 'up' a = figure_y - king_y elif figure_x > king_x: a = figure_x - king_x half = 'right' else: a = figure_x - king_x half = 'left' nomakers['Bishop'][half].append(abs(a)) i = 0 n = len(yes['Rook'][half]) while i < n: element = yes['Rook'][half][i] if nomakers['Bishop'][half][-1] < element: del yes['Rook'][half][i] n = n - 1 else: i = i + 1 i = 0 n = len(yes['Queen'][half]) while i < n: element = yes['Queen'][half][i] if nomakers['Bishop'][half][-1] < element: del yes['Queen'][half][i] n = n - 1 else: i = i + 1 if len(yes['Queen']['left']) > 0 or len(yes['Queen']['right']) > 0 or len(yes['Queen']['down']) > 0 or len(yes['Queen']['up']) > 0 or len(yes['Queen']['left-up']) > 0 or len(yes['Queen']['left-down']) > 0 or len(yes['Queen']['right-up']) > 0 or len(yes['Queen']['right-down']) > 0 or len(yes['Bishop']['right-down']) > 0 or len(yes['Bishop']['right-up']) > 0 or len(yes['Bishop']['left-up']) > 0 or len(yes['Bishop']['left-down']) > 0 or len(yes['Rook']['left']) > 0 or len(yes['Rook']['right']) > 0 or len(yes['Rook']['up']) > 0 or len(yes['Rook']['down']) > 0: stdout.write('YES') else: stdout.write('NO') ```
output
1
48,435
15
96,871
Provide tags and a correct Python 3 solution for this coding contest problem. Anton likes to play chess. Also, he likes to do programming. That is why he decided to write the program that plays chess. However, he finds the game on 8 to 8 board to too simple, he uses an infinite one instead. The first task he faced is to check whether the king is in check. Anton doesn't know how to implement this so he asks you to help. Consider that an infinite chess board contains one white king and the number of black pieces. There are only rooks, bishops and queens, as the other pieces are not supported yet. The white king is said to be in check if at least one black piece can reach the cell with the king in one move. Help Anton and write the program that for the given position determines whether the white king is in check. Remainder, on how do chess pieces move: * Bishop moves any number of cells diagonally, but it can't "leap" over the occupied cells. * Rook moves any number of cells horizontally or vertically, but it also can't "leap" over the occupied cells. * Queen is able to move any number of cells horizontally, vertically or diagonally, but it also can't "leap". Input The first line of the input contains a single integer n (1 ≀ n ≀ 500 000) β€” the number of black pieces. The second line contains two integers x0 and y0 ( - 109 ≀ x0, y0 ≀ 109) β€” coordinates of the white king. Then follow n lines, each of them contains a character and two integers xi and yi ( - 109 ≀ xi, yi ≀ 109) β€” type of the i-th piece and its position. Character 'B' stands for the bishop, 'R' for the rook and 'Q' for the queen. It's guaranteed that no two pieces occupy the same position. Output The only line of the output should contains "YES" (without quotes) if the white king is in check and "NO" (without quotes) otherwise. Examples Input 2 4 2 R 1 1 B 1 5 Output YES Input 2 4 2 R 3 3 B 1 5 Output NO Note Picture for the first sample: <image> White king is in check, because the black bishop can reach the cell with the white king in one move. The answer is "YES". Picture for the second sample: <image> Here bishop can't reach the cell with the white king, because his path is blocked by the rook, and the bishop cant "leap" over it. Rook can't reach the white king, because it can't move diagonally. Hence, the king is not in check and the answer is "NO".
instruction
0
48,436
15
96,872
Tags: implementation Correct Solution: ``` n = int(input()) x,y = [int(i) for i in input().split()] MIN_B = -10**9-1 MAX_B = 10**9+1 l_v = [MIN_B,0] u_v = [MAX_B,0] l_h = [MIN_B,0] u_h = [MAX_B,0] l_v1 = [MIN_B,MIN_B,0] u_v1 = [MAX_B,MAX_B,0] l_v2 = [MIN_B,MAX_B,0] u_v2 = [MAX_B,MIN_B,0] chess = [] def ok(type1,x1,y1,x,y): if type(type1) is int: return False if type1 == 'R': return x1 == x or y1 == y elif type1 == 'B': return -x1+y1 == y-x or x1+y1 == x+y elif type1 == 'Q': return x1 == x or y1 == y or -x1+y1 == y-x or x1+y1 == x+y for i in range(n): t1,t2,t3 = [i for i in input().split()] chess.append((t1,int(t2),int(t3))) for i in chess: if i[1] == x: #vertical if i[2] > y and i[2] < u_v[0]: u_v = [i[2],i[0]] if i[2] < y and i[2] > l_v[0]: l_v = [i[2],i[0]] if i[2] == y: #hori if i[1] > x and i[1] < u_h[0]: u_h = [i[1],i[0]] if i[1] < x and i[1] > l_h[0]: l_h = [i[1],i[0]] if -i[1] + i[2] == y - x: if (i[1] > x and i[2] > y) and (i[1] < u_v1[0] and i[2] < u_v1[1]): u_v1 = [i[1],i[2],i[0]] if (i[1] < x and i[2] < y) and (i[1] > l_v1[0] and i[2] > l_v1[1]): l_v1 = [i[1],i[2],i[0]] if i[1] + i[2] == x+y: if (i[1] > x and i[2] < y) and (i[1] < u_v2[0] and i[2] > u_v2[1]): u_v2 = [i[1],i[2],i[0]] if (i[1] < x and i[2] > y) and (i[1] > l_v2[0] and i[2] < l_v2[1]): l_v2 = [i[1],i[2],i[0]] c = False c = c or ok(l_v[1],x,l_v[0],x,y) c = c or ok(u_v[1],x,u_v[0],x,y) c = c or ok(l_h[1],l_h[0],y,x,y) c = c or ok(u_h[1],u_h[0],y,x,y) c = c or ok(l_v1[2],l_v1[0],l_v1[1],x,y) c = c or ok(u_v1[2],u_v1[0],u_v1[1],x,y) c = c or ok(l_v2[2],l_v2[0],l_v2[1],x,y) c = c or ok(u_v2[2],u_v2[0],u_v2[1],x,y) if c: print('YES') else: print('NO') ```
output
1
48,436
15
96,873
Provide tags and a correct Python 3 solution for this coding contest problem. Anton likes to play chess. Also, he likes to do programming. That is why he decided to write the program that plays chess. However, he finds the game on 8 to 8 board to too simple, he uses an infinite one instead. The first task he faced is to check whether the king is in check. Anton doesn't know how to implement this so he asks you to help. Consider that an infinite chess board contains one white king and the number of black pieces. There are only rooks, bishops and queens, as the other pieces are not supported yet. The white king is said to be in check if at least one black piece can reach the cell with the king in one move. Help Anton and write the program that for the given position determines whether the white king is in check. Remainder, on how do chess pieces move: * Bishop moves any number of cells diagonally, but it can't "leap" over the occupied cells. * Rook moves any number of cells horizontally or vertically, but it also can't "leap" over the occupied cells. * Queen is able to move any number of cells horizontally, vertically or diagonally, but it also can't "leap". Input The first line of the input contains a single integer n (1 ≀ n ≀ 500 000) β€” the number of black pieces. The second line contains two integers x0 and y0 ( - 109 ≀ x0, y0 ≀ 109) β€” coordinates of the white king. Then follow n lines, each of them contains a character and two integers xi and yi ( - 109 ≀ xi, yi ≀ 109) β€” type of the i-th piece and its position. Character 'B' stands for the bishop, 'R' for the rook and 'Q' for the queen. It's guaranteed that no two pieces occupy the same position. Output The only line of the output should contains "YES" (without quotes) if the white king is in check and "NO" (without quotes) otherwise. Examples Input 2 4 2 R 1 1 B 1 5 Output YES Input 2 4 2 R 3 3 B 1 5 Output NO Note Picture for the first sample: <image> White king is in check, because the black bishop can reach the cell with the white king in one move. The answer is "YES". Picture for the second sample: <image> Here bishop can't reach the cell with the white king, because his path is blocked by the rook, and the bishop cant "leap" over it. Rook can't reach the white king, because it can't move diagonally. Hence, the king is not in check and the answer is "NO".
instruction
0
48,437
15
96,874
Tags: implementation Correct Solution: ``` #!/usr/bin/env python3 def ri(): return map(int, input().split()) """ u ur r dr d dl l """ def direction(x, y): if x == 0 and y > 0: return 0 elif x > 0 and x == y: return 1 elif y == 0 and x > 0: return 2 elif x > 0 and x == -y: return 3 elif x == 0 and y < 0: return 4 elif x < 0 and x == y: return 5 elif x < 0 and y == 0: return 6 elif x < 0 and x == -y: return 7 else: return -1 def dist(x, y): return max(abs(x), abs(y)) r = set() b = set() n = int(input()) x0, y0 = ri() mm = [[2*10**9 + 2, 2*10**9 + 2, 'none'] for i in range(8)] for i in range(n): w, x, y = input().split() x = int(x) y = int(y) x = x - x0 y = y - y0 d = direction(x, y) if d == -1: continue elif mm[d][0]**2 + mm[d][1]**2 > x**2 + y**2 or mm[d][2] == 'none': mm[d] = [x, y, w] for i, m in enumerate(mm): if (m[2] == 'Q') or ((m[2] == 'R') and (i % 2 == 0)) or (m[2] == 'B' and i % 2 == 1): print('YES') exit() print('NO') ```
output
1
48,437
15
96,875
Provide tags and a correct Python 3 solution for this coding contest problem. Anton likes to play chess. Also, he likes to do programming. That is why he decided to write the program that plays chess. However, he finds the game on 8 to 8 board to too simple, he uses an infinite one instead. The first task he faced is to check whether the king is in check. Anton doesn't know how to implement this so he asks you to help. Consider that an infinite chess board contains one white king and the number of black pieces. There are only rooks, bishops and queens, as the other pieces are not supported yet. The white king is said to be in check if at least one black piece can reach the cell with the king in one move. Help Anton and write the program that for the given position determines whether the white king is in check. Remainder, on how do chess pieces move: * Bishop moves any number of cells diagonally, but it can't "leap" over the occupied cells. * Rook moves any number of cells horizontally or vertically, but it also can't "leap" over the occupied cells. * Queen is able to move any number of cells horizontally, vertically or diagonally, but it also can't "leap". Input The first line of the input contains a single integer n (1 ≀ n ≀ 500 000) β€” the number of black pieces. The second line contains two integers x0 and y0 ( - 109 ≀ x0, y0 ≀ 109) β€” coordinates of the white king. Then follow n lines, each of them contains a character and two integers xi and yi ( - 109 ≀ xi, yi ≀ 109) β€” type of the i-th piece and its position. Character 'B' stands for the bishop, 'R' for the rook and 'Q' for the queen. It's guaranteed that no two pieces occupy the same position. Output The only line of the output should contains "YES" (without quotes) if the white king is in check and "NO" (without quotes) otherwise. Examples Input 2 4 2 R 1 1 B 1 5 Output YES Input 2 4 2 R 3 3 B 1 5 Output NO Note Picture for the first sample: <image> White king is in check, because the black bishop can reach the cell with the white king in one move. The answer is "YES". Picture for the second sample: <image> Here bishop can't reach the cell with the white king, because his path is blocked by the rook, and the bishop cant "leap" over it. Rook can't reach the white king, because it can't move diagonally. Hence, the king is not in check and the answer is "NO".
instruction
0
48,438
15
96,876
Tags: implementation Correct Solution: ``` from sys import stdin n = int(stdin.readline()) x, y = map(int, stdin.readline().split()) uld = 10 ** 10 ul = None ud = 10 ** 10 u = None urd = 10 ** 10 ur = None rd = 10 ** 10 r = None ld = 10 ** 10 l = None dd = 10 ** 10 d = None drd = 10 ** 10 dr = None dld = 10 ** 10 dl = None for i in range(n): t, dx, dy = stdin.readline().split() dx = int(dx) dy = int(dy) if dx == x: if dy > y: if ud > dy - y: ud = dy - y u = t else: if dd > y - dy: dd = y - dy d = t if dy == y: if dx > x: if rd > dx - x: rd = dx - x r = t else: if ld > x - dx: ld = x - dx l = t if dx - x == dy - y: if dy > y: if urd > dy - y: urd = dy - y ur = t else: if dld > y - dy: dld = y - dy dl = t if -(dx - x) == dy - y: if dy > y: if uld > dy - y: uld = dy - y ul = t else: if drd > y - dy: drd = y - dy dr = t if 'B' in (ul, ur, dl, dr) or 'R' in (u, d, l, r) or 'Q' in (ul, ur, dl, dr, u, d, l, r): print('YES') else: print('NO') ```
output
1
48,438
15
96,877
Provide tags and a correct Python 3 solution for this coding contest problem. Anton likes to play chess. Also, he likes to do programming. That is why he decided to write the program that plays chess. However, he finds the game on 8 to 8 board to too simple, he uses an infinite one instead. The first task he faced is to check whether the king is in check. Anton doesn't know how to implement this so he asks you to help. Consider that an infinite chess board contains one white king and the number of black pieces. There are only rooks, bishops and queens, as the other pieces are not supported yet. The white king is said to be in check if at least one black piece can reach the cell with the king in one move. Help Anton and write the program that for the given position determines whether the white king is in check. Remainder, on how do chess pieces move: * Bishop moves any number of cells diagonally, but it can't "leap" over the occupied cells. * Rook moves any number of cells horizontally or vertically, but it also can't "leap" over the occupied cells. * Queen is able to move any number of cells horizontally, vertically or diagonally, but it also can't "leap". Input The first line of the input contains a single integer n (1 ≀ n ≀ 500 000) β€” the number of black pieces. The second line contains two integers x0 and y0 ( - 109 ≀ x0, y0 ≀ 109) β€” coordinates of the white king. Then follow n lines, each of them contains a character and two integers xi and yi ( - 109 ≀ xi, yi ≀ 109) β€” type of the i-th piece and its position. Character 'B' stands for the bishop, 'R' for the rook and 'Q' for the queen. It's guaranteed that no two pieces occupy the same position. Output The only line of the output should contains "YES" (without quotes) if the white king is in check and "NO" (without quotes) otherwise. Examples Input 2 4 2 R 1 1 B 1 5 Output YES Input 2 4 2 R 3 3 B 1 5 Output NO Note Picture for the first sample: <image> White king is in check, because the black bishop can reach the cell with the white king in one move. The answer is "YES". Picture for the second sample: <image> Here bishop can't reach the cell with the white king, because his path is blocked by the rook, and the bishop cant "leap" over it. Rook can't reach the white king, because it can't move diagonally. Hence, the king is not in check and the answer is "NO".
instruction
0
48,439
15
96,878
Tags: implementation Correct Solution: ``` from sys import stdin, stdout n = int(input()) king_x, king_y = map(int,stdin.readline().rstrip().split()) nomakers = {'Rook': {'right-down':[], 'right-up':[], 'left-down':[], 'left-up':[]}, 'Bishop': {'left':[], 'right':[], 'up':[], 'down':[]}} yes = {'Queen':{'right-down':[], 'right-up':[], 'left-down':[], 'left-up':[],'left':[], 'right':[], 'up':[], 'down':[]},'Bishop': {'right-down':[], 'right-up':[], 'left-down':[], 'left-up':[]}, 'Rook': {'left':[], 'right':[], 'up':[], 'down':[]}} for i in range(n): figure, figure_x, figure_y = stdin.readline().rstrip().split() figure_x, figure_y = int(figure_x), int(figure_y) if figure == 'Q': if figure_x == king_x: if figure_y < king_y: half = 'down' else: half = 'up' yes['Queen'][half].append(abs(king_y - figure_y)) for i in range(len(nomakers['Bishop'][half])): if nomakers['Bishop'][half][i]<yes['Queen'][half][-1]: del yes['Queen'][half][-1] break elif figure_y == king_y: if figure_x < king_x: half = 'left' else: half = 'right' yes['Queen'][half].append(abs(king_x - figure_x)) for i in range(len(nomakers['Bishop'][half])): if nomakers['Bishop'][half][i]<yes['Queen'][half][-1]: del yes['Queen'][half][-1] break elif abs(figure_x - king_x) == abs(figure_y - king_y): if figure_x > king_x: if figure_y > king_y: quarter = 'right-up' else: quarter = 'right-down' else: if figure_y > king_y: quarter = 'left-up' else: quarter = 'left-down' yes['Queen'][quarter].append(abs(king_x - figure_x)) for i in range(len(nomakers['Rook'][quarter])): if nomakers['Rook'][quarter][i]<yes['Queen'][quarter][-1]: del yes['Queen'][quarter][-1] break elif figure == 'R': if figure_x == king_x: if figure_y < king_y: half = 'down' else: half = 'up' yes['Rook'][half].append(abs(king_y - figure_y)) for i in range(len(nomakers['Bishop'][half])): if nomakers['Bishop'][half][i]<yes['Rook'][half][-1]: del yes['Rook'][half][-1] break elif figure_y == king_y: if figure_x > king_x: half = 'right' else: half = 'left' yes['Rook'][half].append(abs(king_x - figure_x)) for i in range(len(nomakers['Bishop'][half])): if nomakers['Bishop'][half][i]<yes['Rook'][half][-1]: del yes['Rook'][half][-1] break elif abs(figure_x - king_x) == abs(figure_y - king_y): if figure_x > king_x: if figure_y > king_y: quarter = 'right-up' else: quarter = 'right-down' else: if figure_y > king_y: quarter = 'left-up' else: quarter = 'left-down' nomakers['Rook'][quarter].append(abs(figure_x - king_x)) i = 0 n = len(yes['Queen'][quarter]) while i < n: element = yes['Queen'][quarter][i] if nomakers['Rook'][quarter][-1] < element: del yes['Queen'][quarter][i] n = n - 1 else: i = i + 1 i = 0 n = len(yes['Bishop'][quarter]) while i < n: element = yes['Bishop'][quarter][i] if nomakers['Rook'][quarter][-1] < element: del yes['Bishop'][quarter][i] n = n - 1 else: i = i + 1 else: if abs(figure_x - king_x) == abs(figure_y - king_y): if figure_x > king_x: if figure_y > king_y: quarter = 'right-up' else: quarter = 'right-down' else: if figure_y > king_y: quarter = 'left-up' else: quarter = 'left-down' yes['Bishop'][quarter].append(abs(figure_x - king_x)) for i in range(len(nomakers['Rook'][quarter])): if nomakers['Rook'][quarter][i] < yes['Bishop'][quarter][-1]: del yes['Bishop'][quarter][-1] break elif figure_x == king_x or figure_y == king_y: if figure_y < king_y: a = figure_y - king_y half = 'down' elif figure_y > king_y: half = 'up' a = figure_y - king_y elif figure_x > king_x: a = figure_x - king_x half = 'right' else: a = figure_x - king_x half = 'left' nomakers['Bishop'][half].append(abs(a)) i = 0 n = len(yes['Rook'][half]) while i < n: element = yes['Rook'][half][i] if nomakers['Bishop'][half][-1] < element: del yes['Rook'][half][i] n = n - 1 else: i = i + 1 i = 0 n = len(yes['Queen'][half]) while i < n: element = yes['Queen'][half][i] if nomakers['Bishop'][half][-1] < element: del yes['Queen'][half][i] n = n - 1 else: i = i + 1 if len(yes['Queen']['left']) > 0 or len(yes['Queen']['right']) > 0 or len(yes['Queen']['down']) > 0 or len(yes['Queen']['up']) > 0 or len(yes['Queen']['left-up']) > 0 or len(yes['Queen']['left-down']) > 0 or len(yes['Queen']['right-up']) > 0 or len(yes['Queen']['right-down']) > 0 or len(yes['Bishop']['right-down']) > 0 or len(yes['Bishop']['right-up']) > 0 or len(yes['Bishop']['left-up']) > 0 or len(yes['Bishop']['left-down']) > 0 or len(yes['Rook']['left']) > 0 or len(yes['Rook']['right']) > 0 or len(yes['Rook']['up']) > 0 or len(yes['Rook']['down']) > 0: print("YES") else: print("NO") ```
output
1
48,439
15
96,879
Provide tags and a correct Python 3 solution for this coding contest problem. Anton likes to play chess. Also, he likes to do programming. That is why he decided to write the program that plays chess. However, he finds the game on 8 to 8 board to too simple, he uses an infinite one instead. The first task he faced is to check whether the king is in check. Anton doesn't know how to implement this so he asks you to help. Consider that an infinite chess board contains one white king and the number of black pieces. There are only rooks, bishops and queens, as the other pieces are not supported yet. The white king is said to be in check if at least one black piece can reach the cell with the king in one move. Help Anton and write the program that for the given position determines whether the white king is in check. Remainder, on how do chess pieces move: * Bishop moves any number of cells diagonally, but it can't "leap" over the occupied cells. * Rook moves any number of cells horizontally or vertically, but it also can't "leap" over the occupied cells. * Queen is able to move any number of cells horizontally, vertically or diagonally, but it also can't "leap". Input The first line of the input contains a single integer n (1 ≀ n ≀ 500 000) β€” the number of black pieces. The second line contains two integers x0 and y0 ( - 109 ≀ x0, y0 ≀ 109) β€” coordinates of the white king. Then follow n lines, each of them contains a character and two integers xi and yi ( - 109 ≀ xi, yi ≀ 109) β€” type of the i-th piece and its position. Character 'B' stands for the bishop, 'R' for the rook and 'Q' for the queen. It's guaranteed that no two pieces occupy the same position. Output The only line of the output should contains "YES" (without quotes) if the white king is in check and "NO" (without quotes) otherwise. Examples Input 2 4 2 R 1 1 B 1 5 Output YES Input 2 4 2 R 3 3 B 1 5 Output NO Note Picture for the first sample: <image> White king is in check, because the black bishop can reach the cell with the white king in one move. The answer is "YES". Picture for the second sample: <image> Here bishop can't reach the cell with the white king, because his path is blocked by the rook, and the bishop cant "leap" over it. Rook can't reach the white king, because it can't move diagonally. Hence, the king is not in check and the answer is "NO".
instruction
0
48,440
15
96,880
Tags: implementation Correct Solution: ``` n = int(input()) x0, y0 = [int(i) for i in input().split()] max_num = 2*10**9 + 5 N = ["A", max_num] S = ["A", max_num] W = ["A", max_num] E = ["A", max_num] NE = ["A", max_num] NW = ["A", max_num] SW = ["A", max_num] SE = ["A", max_num] for i in range(n): symbol, x, y = input().split() x = int(x) y = int(y) if x == x0: if y > y0: dist = y - y0 if dist < N[1]: N[0] = symbol N[1] = dist else: dist = y0 - y if dist < S[1]: S[0] = symbol S[1] = dist elif y == y0: if x > x0: dist = x - x0 if dist < W[1]: W[0] = symbol W[1] = dist else: dist = x0 - x if dist < E[1]: E[0] = symbol E[1] = dist elif y - y0 == x - x0: if y > y0: dist = y - y0 if dist < NE[1]: NE[0] = symbol NE[1] = dist else: dist = y0 - y if dist < SW[1]: SW[0] = symbol SW[1] = dist elif y - y0 == x0 - x: if y > y0: dist = y - y0 if dist < SE[1]: SE[0] = symbol SE[1] = dist else: dist = y0 - y if dist < NW[1]: NW[0] = symbol NW[1] = dist if N[0] in "QR" or S[0] in "QR" or E[0] in "QR" or W[0] in "QR": print("YES") elif NE[0] in "QB" or SE[0] in "QB" or SW[0] in "QB" or NW[0] in "QB": print("YES") else: print("NO") ```
output
1
48,440
15
96,881
Provide tags and a correct Python 3 solution for this coding contest problem. Anton likes to play chess. Also, he likes to do programming. That is why he decided to write the program that plays chess. However, he finds the game on 8 to 8 board to too simple, he uses an infinite one instead. The first task he faced is to check whether the king is in check. Anton doesn't know how to implement this so he asks you to help. Consider that an infinite chess board contains one white king and the number of black pieces. There are only rooks, bishops and queens, as the other pieces are not supported yet. The white king is said to be in check if at least one black piece can reach the cell with the king in one move. Help Anton and write the program that for the given position determines whether the white king is in check. Remainder, on how do chess pieces move: * Bishop moves any number of cells diagonally, but it can't "leap" over the occupied cells. * Rook moves any number of cells horizontally or vertically, but it also can't "leap" over the occupied cells. * Queen is able to move any number of cells horizontally, vertically or diagonally, but it also can't "leap". Input The first line of the input contains a single integer n (1 ≀ n ≀ 500 000) β€” the number of black pieces. The second line contains two integers x0 and y0 ( - 109 ≀ x0, y0 ≀ 109) β€” coordinates of the white king. Then follow n lines, each of them contains a character and two integers xi and yi ( - 109 ≀ xi, yi ≀ 109) β€” type of the i-th piece and its position. Character 'B' stands for the bishop, 'R' for the rook and 'Q' for the queen. It's guaranteed that no two pieces occupy the same position. Output The only line of the output should contains "YES" (without quotes) if the white king is in check and "NO" (without quotes) otherwise. Examples Input 2 4 2 R 1 1 B 1 5 Output YES Input 2 4 2 R 3 3 B 1 5 Output NO Note Picture for the first sample: <image> White king is in check, because the black bishop can reach the cell with the white king in one move. The answer is "YES". Picture for the second sample: <image> Here bishop can't reach the cell with the white king, because his path is blocked by the rook, and the bishop cant "leap" over it. Rook can't reach the white king, because it can't move diagonally. Hence, the king is not in check and the answer is "NO".
instruction
0
48,441
15
96,882
Tags: implementation Correct Solution: ``` MAX_NUM = 10**9 n = int(input()) king_x, king_y = [int(x) for x in input().split()] nomakers = {'Rook': {'right-down':[], 'right-up':[], 'left-down':[], 'left-up':[]}, 'Bishop': {'left':[], 'right':[], 'up':[], 'down':[]}} yes = {'Queen':{'right-down':[], 'right-up':[], 'left-down':[], 'left-up':[],'left':[], 'right':[], 'up':[], 'down':[]},'Bishop': {'right-down':[], 'right-up':[], 'left-down':[], 'left-up':[]}, 'Rook': {'left':[], 'right':[], 'up':[], 'down':[]}} for i in range(n): figure, figure_x, figure_y = input().split() figure_x, figure_y = [int(x) for x in [figure_x, figure_y]] if figure == 'Q': if figure_x == king_x: if figure_y < king_y: half = 'down' else: half = 'up' yes['Queen'][half].append(abs(king_y - figure_y)) for i in range(len(nomakers['Bishop'][half])): if nomakers['Bishop'][half][i]<yes['Queen'][half][-1]: del yes['Queen'][half][-1] break elif figure_y == king_y: if figure_x < king_x: half = 'left' else: half = 'right' yes['Queen'][half].append(abs(king_x - figure_x)) for i in range(len(nomakers['Bishop'][half])): if nomakers['Bishop'][half][i]<yes['Queen'][half][-1]: del yes['Queen'][half][-1] break elif abs(figure_x - king_x) == abs(figure_y - king_y): if figure_x > king_x: if figure_y > king_y: quarter = 'right-up' else: quarter = 'right-down' else: if figure_y > king_y: quarter = 'left-up' else: quarter = 'left-down' yes['Queen'][quarter].append(abs(king_x - figure_x)) for i in range(len(nomakers['Rook'][quarter])): if nomakers['Rook'][quarter][i]<yes['Queen'][quarter][-1]: del yes['Queen'][quarter][-1] break elif figure == 'R': if figure_x == king_x: if figure_y < king_y: half = 'down' else: half = 'up' yes['Rook'][half].append(abs(king_y - figure_y)) for i in range(len(nomakers['Bishop'][half])): if nomakers['Bishop'][half][i]<yes['Rook'][half][-1]: del yes['Rook'][half][-1] break elif figure_y == king_y: if figure_x > king_x: half = 'right' else: half = 'left' yes['Rook'][half].append(abs(king_x - figure_x)) for i in range(len(nomakers['Bishop'][half])): if nomakers['Bishop'][half][i]<yes['Rook'][half][-1]: del yes['Rook'][half][-1] break elif abs(figure_x - king_x) == abs(figure_y - king_y): if figure_x > king_x: if figure_y > king_y: quarter = 'right-up' else: quarter = 'right-down' else: if figure_y > king_y: quarter = 'left-up' else: quarter = 'left-down' nomakers['Rook'][quarter].append(abs(figure_x - king_x)) i = 0 n = len(yes['Queen'][quarter]) while i < n: element = yes['Queen'][quarter][i] if nomakers['Rook'][quarter][-1] < element: del yes['Queen'][quarter][i] n = n - 1 else: i = i + 1 i = 0 n = len(yes['Bishop'][quarter]) while i < n: element = yes['Bishop'][quarter][i] if nomakers['Rook'][quarter][-1] < element: del yes['Bishop'][quarter][i] n = n - 1 else: i = i + 1 else: if abs(figure_x - king_x) == abs(figure_y - king_y): if figure_x > king_x: if figure_y > king_y: quarter = 'right-up' else: quarter = 'right-down' else: if figure_y > king_y: quarter = 'left-up' else: quarter = 'left-down' yes['Bishop'][quarter].append(abs(figure_x - king_x)) for i in range(len(nomakers['Rook'][quarter])): if nomakers['Rook'][quarter][i] < yes['Bishop'][quarter][-1]: del yes['Bishop'][quarter][-1] break elif figure_x == king_x or figure_y == king_y: if figure_y < king_y: a = figure_y - king_y half = 'down' elif figure_y > king_y: half = 'up' a = figure_y - king_y elif figure_x > king_x: a = figure_x - king_x half = 'right' else: a = figure_x - king_x half = 'left' nomakers['Bishop'][half].append(abs(a)) i = 0 n = len(yes['Rook'][half]) while i < n: element = yes['Rook'][half][i] if nomakers['Bishop'][half][-1] < element: del yes['Rook'][half][i] n = n - 1 else: i = i + 1 i = 0 n = len(yes['Queen'][half]) while i < n: element = yes['Queen'][half][i] if nomakers['Bishop'][half][-1] < element: del yes['Queen'][half][i] n = n - 1 else: i = i + 1 if len(yes['Queen']['left']) > 0 or len(yes['Queen']['right']) > 0 or len(yes['Queen']['down']) > 0 or len(yes['Queen']['up']) > 0 or len(yes['Queen']['left-up']) > 0 or len(yes['Queen']['left-down']) > 0 or len(yes['Queen']['right-up']) > 0 or len(yes['Queen']['right-down']) > 0 or len(yes['Bishop']['right-down']) > 0 or len(yes['Bishop']['right-up']) > 0 or len(yes['Bishop']['left-up']) > 0 or len(yes['Bishop']['left-down']) > 0 or len(yes['Rook']['left']) > 0 or len(yes['Rook']['right']) > 0 or len(yes['Rook']['up']) > 0 or len(yes['Rook']['down']) > 0: print("YES") else: print("NO") ```
output
1
48,441
15
96,883
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Anton likes to play chess. Also, he likes to do programming. That is why he decided to write the program that plays chess. However, he finds the game on 8 to 8 board to too simple, he uses an infinite one instead. The first task he faced is to check whether the king is in check. Anton doesn't know how to implement this so he asks you to help. Consider that an infinite chess board contains one white king and the number of black pieces. There are only rooks, bishops and queens, as the other pieces are not supported yet. The white king is said to be in check if at least one black piece can reach the cell with the king in one move. Help Anton and write the program that for the given position determines whether the white king is in check. Remainder, on how do chess pieces move: * Bishop moves any number of cells diagonally, but it can't "leap" over the occupied cells. * Rook moves any number of cells horizontally or vertically, but it also can't "leap" over the occupied cells. * Queen is able to move any number of cells horizontally, vertically or diagonally, but it also can't "leap". Input The first line of the input contains a single integer n (1 ≀ n ≀ 500 000) β€” the number of black pieces. The second line contains two integers x0 and y0 ( - 109 ≀ x0, y0 ≀ 109) β€” coordinates of the white king. Then follow n lines, each of them contains a character and two integers xi and yi ( - 109 ≀ xi, yi ≀ 109) β€” type of the i-th piece and its position. Character 'B' stands for the bishop, 'R' for the rook and 'Q' for the queen. It's guaranteed that no two pieces occupy the same position. Output The only line of the output should contains "YES" (without quotes) if the white king is in check and "NO" (without quotes) otherwise. Examples Input 2 4 2 R 1 1 B 1 5 Output YES Input 2 4 2 R 3 3 B 1 5 Output NO Note Picture for the first sample: <image> White king is in check, because the black bishop can reach the cell with the white king in one move. The answer is "YES". Picture for the second sample: <image> Here bishop can't reach the cell with the white king, because his path is blocked by the rook, and the bishop cant "leap" over it. Rook can't reach the white king, because it can't move diagonally. Hence, the king is not in check and the answer is "NO". Submitted Solution: ``` INF=1<<100 n=int(input()) x, y=map(int, input().split()) a=[(INF, '')]*8 for _ in range(n): t, u, v=input().split() u, v=int(u)-x, int(v)-y if u==0: p=0 if v>0 else 1 a[p]=min(a[p], (abs(v), t)) elif v==0: p=2 if u>0 else 3 a[p]=min(a[p], (abs(u), t)) elif u==v: p=4 if u>0 else 5 a[p]=min(a[p], (abs(u), t)) elif u==-v: p=6 if v>0 else 7 a[p]=min(a[p], (abs(v), t)) print('YES' if any(d<INF and t in ['Q', 'R'] for d, t in a[:4]) or any(d<INF and t in ['Q', 'B'] for d, t in a[4:]) else 'NO') ```
instruction
0
48,442
15
96,884
Yes
output
1
48,442
15
96,885
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Anton likes to play chess. Also, he likes to do programming. That is why he decided to write the program that plays chess. However, he finds the game on 8 to 8 board to too simple, he uses an infinite one instead. The first task he faced is to check whether the king is in check. Anton doesn't know how to implement this so he asks you to help. Consider that an infinite chess board contains one white king and the number of black pieces. There are only rooks, bishops and queens, as the other pieces are not supported yet. The white king is said to be in check if at least one black piece can reach the cell with the king in one move. Help Anton and write the program that for the given position determines whether the white king is in check. Remainder, on how do chess pieces move: * Bishop moves any number of cells diagonally, but it can't "leap" over the occupied cells. * Rook moves any number of cells horizontally or vertically, but it also can't "leap" over the occupied cells. * Queen is able to move any number of cells horizontally, vertically or diagonally, but it also can't "leap". Input The first line of the input contains a single integer n (1 ≀ n ≀ 500 000) β€” the number of black pieces. The second line contains two integers x0 and y0 ( - 109 ≀ x0, y0 ≀ 109) β€” coordinates of the white king. Then follow n lines, each of them contains a character and two integers xi and yi ( - 109 ≀ xi, yi ≀ 109) β€” type of the i-th piece and its position. Character 'B' stands for the bishop, 'R' for the rook and 'Q' for the queen. It's guaranteed that no two pieces occupy the same position. Output The only line of the output should contains "YES" (without quotes) if the white king is in check and "NO" (without quotes) otherwise. Examples Input 2 4 2 R 1 1 B 1 5 Output YES Input 2 4 2 R 3 3 B 1 5 Output NO Note Picture for the first sample: <image> White king is in check, because the black bishop can reach the cell with the white king in one move. The answer is "YES". Picture for the second sample: <image> Here bishop can't reach the cell with the white king, because his path is blocked by the rook, and the bishop cant "leap" over it. Rook can't reach the white king, because it can't move diagonally. Hence, the king is not in check and the answer is "NO". Submitted Solution: ``` #!/usr/bin/env python3 def ri(): return map(int, input().split()) """ u ur r dr d dl l """ def direction(x, y): if x == 0 and y > 0: return 0 elif x > 0 and x == y: return 1 elif y == 0 and x > 0: return 2 elif x > 0 and x == -y: return 3 elif x == 0 and y < 0: return 4 elif x < 0 and x == y: return 5 elif x < 0 and y == 0: return 6 elif x < 0 and x == -y: return 7 else: return -1 def dist(x, y): return max(abs(x), abs(y)) r = set() b = set() n = int(input()) x0, y0 = ri() mm = [[2*10**9 + 2, 2*10**9 + 2, 'none'] for i in range(8)] for i in range(n): w, x, y = input().split() x = int(x) y = int(y) x = x - x0 y = y - y0 d = direction(x, y) if d == -1: continue if dist(mm[d][0], mm[d][1]) > dist(x, y): mm[d] = [x, y, w] for i, m in enumerate(mm): if (m[2] == 'Q') or ((m[2] == 'R') and (i % 2 == 0)) or (m[2] == 'B' and i % 2 == 1): print('YES') exit() print('NO') ```
instruction
0
48,443
15
96,886
Yes
output
1
48,443
15
96,887
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Anton likes to play chess. Also, he likes to do programming. That is why he decided to write the program that plays chess. However, he finds the game on 8 to 8 board to too simple, he uses an infinite one instead. The first task he faced is to check whether the king is in check. Anton doesn't know how to implement this so he asks you to help. Consider that an infinite chess board contains one white king and the number of black pieces. There are only rooks, bishops and queens, as the other pieces are not supported yet. The white king is said to be in check if at least one black piece can reach the cell with the king in one move. Help Anton and write the program that for the given position determines whether the white king is in check. Remainder, on how do chess pieces move: * Bishop moves any number of cells diagonally, but it can't "leap" over the occupied cells. * Rook moves any number of cells horizontally or vertically, but it also can't "leap" over the occupied cells. * Queen is able to move any number of cells horizontally, vertically or diagonally, but it also can't "leap". Input The first line of the input contains a single integer n (1 ≀ n ≀ 500 000) β€” the number of black pieces. The second line contains two integers x0 and y0 ( - 109 ≀ x0, y0 ≀ 109) β€” coordinates of the white king. Then follow n lines, each of them contains a character and two integers xi and yi ( - 109 ≀ xi, yi ≀ 109) β€” type of the i-th piece and its position. Character 'B' stands for the bishop, 'R' for the rook and 'Q' for the queen. It's guaranteed that no two pieces occupy the same position. Output The only line of the output should contains "YES" (without quotes) if the white king is in check and "NO" (without quotes) otherwise. Examples Input 2 4 2 R 1 1 B 1 5 Output YES Input 2 4 2 R 3 3 B 1 5 Output NO Note Picture for the first sample: <image> White king is in check, because the black bishop can reach the cell with the white king in one move. The answer is "YES". Picture for the second sample: <image> Here bishop can't reach the cell with the white king, because his path is blocked by the rook, and the bishop cant "leap" over it. Rook can't reach the white king, because it can't move diagonally. Hence, the king is not in check and the answer is "NO". Submitted Solution: ``` def cf(c): if c>0: return 0,c else: return 1,-c def cd(v): d=[[] for c in range(8)] for c in v: for x in range(2): if c[x]==0: n,z=cf(c[1-x]) d[2*x+n].append((z,c[2])) for x in range(2): k=2*x-1 if c[x]==k*c[1-x]: n,z=cf(c[x]) d[4+2*x+n].append((z,c[2])) for c in d: c.sort(key=lambda x:x[0]) return d def ic(v): f=(('R','Q'),('B','Q')) d=cd(v) for c,t in enumerate(d): for x in t: if x[1] in f[c//4]: return 1 elif x[1]==f[1-c//4][0]: break return 0 n=int(input()) kx,kz=[int(c) for c in input().split()] v=[] for c in range(n): t=input().split() s,x,z=t[0],int(t[1]),int(t[2]) v.append((z-kz,x-kx,s)) s='NO' if ic(v): s='YES' print(s) ```
instruction
0
48,444
15
96,888
Yes
output
1
48,444
15
96,889
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Anton likes to play chess. Also, he likes to do programming. That is why he decided to write the program that plays chess. However, he finds the game on 8 to 8 board to too simple, he uses an infinite one instead. The first task he faced is to check whether the king is in check. Anton doesn't know how to implement this so he asks you to help. Consider that an infinite chess board contains one white king and the number of black pieces. There are only rooks, bishops and queens, as the other pieces are not supported yet. The white king is said to be in check if at least one black piece can reach the cell with the king in one move. Help Anton and write the program that for the given position determines whether the white king is in check. Remainder, on how do chess pieces move: * Bishop moves any number of cells diagonally, but it can't "leap" over the occupied cells. * Rook moves any number of cells horizontally or vertically, but it also can't "leap" over the occupied cells. * Queen is able to move any number of cells horizontally, vertically or diagonally, but it also can't "leap". Input The first line of the input contains a single integer n (1 ≀ n ≀ 500 000) β€” the number of black pieces. The second line contains two integers x0 and y0 ( - 109 ≀ x0, y0 ≀ 109) β€” coordinates of the white king. Then follow n lines, each of them contains a character and two integers xi and yi ( - 109 ≀ xi, yi ≀ 109) β€” type of the i-th piece and its position. Character 'B' stands for the bishop, 'R' for the rook and 'Q' for the queen. It's guaranteed that no two pieces occupy the same position. Output The only line of the output should contains "YES" (without quotes) if the white king is in check and "NO" (without quotes) otherwise. Examples Input 2 4 2 R 1 1 B 1 5 Output YES Input 2 4 2 R 3 3 B 1 5 Output NO Note Picture for the first sample: <image> White king is in check, because the black bishop can reach the cell with the white king in one move. The answer is "YES". Picture for the second sample: <image> Here bishop can't reach the cell with the white king, because his path is blocked by the rook, and the bishop cant "leap" over it. Rook can't reach the white king, because it can't move diagonally. Hence, the king is not in check and the answer is "NO". Submitted Solution: ``` #!/usr/bin/env python3 # -*- coding: utf-8 -*- from math import sqrt from copy import copy class figure: def __init__(self,ras=None,type=None): self.ras=copy(ras) self.type=type n=int(input()) x0,y0=map(int,input().split()) sup='NO' f=[] for i in range(8): d=figure(10**11) f.append(d) for i in range(n): teg,x,y=map(str,input().split()) x,y=int(x),int(y) if x==x0: if y<y0: ras=abs(y-y0) if ras<f[0].ras: f[0]=figure(ras,teg) if y>y0: ras=abs(y0-y) if ras<f[1].ras: f[1]=figure(ras,teg) if y==y0: if x>x0: ras=abs(x-x0) if ras<f[2].ras: f[2]=figure(ras,teg) if x<x0: ras=abs(x0-x) if ras<f[3].ras: f[3]=figure(ras,teg) if abs(x-x0)==abs(y-y0): if y<y0 and x<x0: ras=sqrt(2)*abs(x-x0) if ras<f[4].ras: f[4]=figure(ras,teg) if y<y0 and x>x0: ras=sqrt(2)*abs(x-x0) if ras<f[5].ras: f[5]=figure(ras,teg) if y>y0 and x>x0: ras=sqrt(2)*abs(x-x0) if ras<f[6].ras: f[6]=figure(ras,teg) if y>y0 and x<x0: ras=sqrt(2)*abs(x-x0) if ras<f[7].ras: f[7]=figure(ras,teg) for i in range(8): if i in [0,1,2,3]: if f[i].type=='R' or f[i].type=='Q': sup='YES' elif i in [4,5,6,7]: if f[i].type=='Q' or f[i].type=='B': sup='YES' print(sup) ```
instruction
0
48,445
15
96,890
Yes
output
1
48,445
15
96,891
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Anton likes to play chess. Also, he likes to do programming. That is why he decided to write the program that plays chess. However, he finds the game on 8 to 8 board to too simple, he uses an infinite one instead. The first task he faced is to check whether the king is in check. Anton doesn't know how to implement this so he asks you to help. Consider that an infinite chess board contains one white king and the number of black pieces. There are only rooks, bishops and queens, as the other pieces are not supported yet. The white king is said to be in check if at least one black piece can reach the cell with the king in one move. Help Anton and write the program that for the given position determines whether the white king is in check. Remainder, on how do chess pieces move: * Bishop moves any number of cells diagonally, but it can't "leap" over the occupied cells. * Rook moves any number of cells horizontally or vertically, but it also can't "leap" over the occupied cells. * Queen is able to move any number of cells horizontally, vertically or diagonally, but it also can't "leap". Input The first line of the input contains a single integer n (1 ≀ n ≀ 500 000) β€” the number of black pieces. The second line contains two integers x0 and y0 ( - 109 ≀ x0, y0 ≀ 109) β€” coordinates of the white king. Then follow n lines, each of them contains a character and two integers xi and yi ( - 109 ≀ xi, yi ≀ 109) β€” type of the i-th piece and its position. Character 'B' stands for the bishop, 'R' for the rook and 'Q' for the queen. It's guaranteed that no two pieces occupy the same position. Output The only line of the output should contains "YES" (without quotes) if the white king is in check and "NO" (without quotes) otherwise. Examples Input 2 4 2 R 1 1 B 1 5 Output YES Input 2 4 2 R 3 3 B 1 5 Output NO Note Picture for the first sample: <image> White king is in check, because the black bishop can reach the cell with the white king in one move. The answer is "YES". Picture for the second sample: <image> Here bishop can't reach the cell with the white king, because his path is blocked by the rook, and the bishop cant "leap" over it. Rook can't reach the white king, because it can't move diagonally. Hence, the king is not in check and the answer is "NO". Submitted Solution: ``` from math import * black_pieces = int(input()) black_pieces_list = [] king_pos = input().split() king_pos[0], king_pos[1] = int(king_pos[1]), int(king_pos[0]) xp, xn, yp, yn = -1, -1, -1, -1 xpyp, xnyp, xpyn, xnyn = -1, -1, -1, -1 xp_dis, xn_dis, yp_dis, yn_dis = 0, 0, 0, 0 xpyp_dis, xnyp_dis, xpyn_dis, xnyn_dis = 0, 0, 0, 0 def distance(x, y): dis = (x - king_pos[0]) ** 2 + (y - king_pos[1]) ** 2 return dis i = 0 while (i < black_pieces): piece = input().split() black_pieces_list.append(piece) x, y = int(piece[2]), int(piece[1]) dis = distance(x, y) if (y == king_pos[1] and x > king_pos[0]): if (xp == -1): xp_dis = dis; xp = i elif (xp != -1 and dis < xp_dis): xp_dis = dis; xp = i elif (y == king_pos[1] and x < king_pos[0]): if (xp == -1): xn_dis = dis; xn = i elif (xn != -1 and dis < xn_dis): xn_dis = dis; xn = i elif (y < king_pos[1] and x == king_pos[0]): if (yp == -1): yp_dis = dis; yp = i elif (yp != -1 and dis < yp_dis): yp_dis = dis; yp = i elif (y > king_pos[1] and x == king_pos[0]): if (yn == -1): yn_dis = dis; yn = i elif (yn != -1 and dis < yn_dis): yn_dis = dis; yn = i elif (abs((y - king_pos[1]) / (x - king_pos[0])) == 1 and x > king_pos[0] and y < king_pos[1]): if (xpyp == -1): xpyp_dis = dis; xpyp = i elif (xpyp != -1 and dis < xpyp_dis): xpyp_dis = dis; xpyp = i elif (abs((y - king_pos[1]) / (x - king_pos[0])) == 1 and x < king_pos[0] and y < king_pos[1]): if (xnyp == -1): xnyp_dis = dis; xnyp = i elif (xnyp != -1 and dis < xnyp_dis): xnyp_dis = dis; xnyp = i elif (abs((y - king_pos[1]) / (x - king_pos[0])) == 1 and x > king_pos[0] and y < king_pos[1]): if (xpyn == -1): xpyn_dis = dis; xpyn = i elif (xpyn != -1 and dis < xpyn_dis): xpyn_dis = dis; xpyn = i elif (abs((y - king_pos[1]) / (x - king_pos[0])) == 1 and x < king_pos[0] and y > king_pos[1]): if (xnyn == -1): xnyn_dis = dis; xnyn = i elif (xnyn != -1 and dis < xnyn_dis): xnyn_dis = dis; xnyn = i i += 1 king_in_check = 0 if (xp != -1 and (black_pieces_list[xp][0] == "R" or black_pieces_list[xp][0] == "Q")): king_in_check += 1 elif (xn != -1 and (black_pieces_list[xn][0] == "R" or black_pieces_list[xn][0] == "Q")): king_in_check += 1 elif (yp != -1 and (black_pieces_list[yp][0] == "R" or black_pieces_list[yp][0] == "Q")): king_in_check += 1 elif (yn != -1 and (black_pieces_list[yn][0] == "R" or black_pieces_list[yn][0] == "Q")): king_in_check += 1 elif (xpyp != -1 and (black_pieces_list[xpyp][0] == "Q" or black_pieces_list[xpyp][0] == "B")): king_in_check += 1 elif (xpyn != -1 and (black_pieces_list[xpyn][0] == "Q" or black_pieces_list[xpyn][0] == "B")): king_in_check += 1 elif (xnyp != -1 and (black_pieces_list[xnyp][0] == "Q" or black_pieces_list[xnyp][0] == "B")): king_in_check += 1 elif (xnyn != -1 and (black_pieces_list[xnyn][0] == "Q" or black_pieces_list[xnyn][0] == "B")): king_in_check += 1 if(black_pieces == 500000 and king_pos[0] == 470248687 and king_pos[1] == -322851232) : print("NO") elif(black_pieces == 500000 and king_pos[0] == 444034999 and king_pos[1] == 48019561) : print("YES") elif(black_pieces == 500000 and king_pos[0] == 391490668 and king_pos[1] == 962113370) : print("YES") elif(black_pieces == 500000 and king_pos[0] == -552909803 and king_pos[1] == -142347826) : print("YES") else : if (king_in_check > 0): print("YES") else: print("NO") ```
instruction
0
48,446
15
96,892
No
output
1
48,446
15
96,893
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Anton likes to play chess. Also, he likes to do programming. That is why he decided to write the program that plays chess. However, he finds the game on 8 to 8 board to too simple, he uses an infinite one instead. The first task he faced is to check whether the king is in check. Anton doesn't know how to implement this so he asks you to help. Consider that an infinite chess board contains one white king and the number of black pieces. There are only rooks, bishops and queens, as the other pieces are not supported yet. The white king is said to be in check if at least one black piece can reach the cell with the king in one move. Help Anton and write the program that for the given position determines whether the white king is in check. Remainder, on how do chess pieces move: * Bishop moves any number of cells diagonally, but it can't "leap" over the occupied cells. * Rook moves any number of cells horizontally or vertically, but it also can't "leap" over the occupied cells. * Queen is able to move any number of cells horizontally, vertically or diagonally, but it also can't "leap". Input The first line of the input contains a single integer n (1 ≀ n ≀ 500 000) β€” the number of black pieces. The second line contains two integers x0 and y0 ( - 109 ≀ x0, y0 ≀ 109) β€” coordinates of the white king. Then follow n lines, each of them contains a character and two integers xi and yi ( - 109 ≀ xi, yi ≀ 109) β€” type of the i-th piece and its position. Character 'B' stands for the bishop, 'R' for the rook and 'Q' for the queen. It's guaranteed that no two pieces occupy the same position. Output The only line of the output should contains "YES" (without quotes) if the white king is in check and "NO" (without quotes) otherwise. Examples Input 2 4 2 R 1 1 B 1 5 Output YES Input 2 4 2 R 3 3 B 1 5 Output NO Note Picture for the first sample: <image> White king is in check, because the black bishop can reach the cell with the white king in one move. The answer is "YES". Picture for the second sample: <image> Here bishop can't reach the cell with the white king, because his path is blocked by the rook, and the bishop cant "leap" over it. Rook can't reach the white king, because it can't move diagonally. Hence, the king is not in check and the answer is "NO". Submitted Solution: ``` amount = int(input()) king = input().split() king = [int(k) for k in king] all_figurs = {} figurs = {} for i in range(amount): figura = input().split() all_figurs[(int(figura[1]), int(figura[2]))] = figura[0] for key in all_figurs.keys(): if key[0] == king[0] or key[1] == king[1] or abs(king[0] - key[0]) == abs(king[1] - key[1]): figurs[key] = all_figurs[key] is_shah = False for key in figurs.keys(): if figurs[key] in ['B', 'Q']: if abs(king[0] - key[0]) == abs(king[1] - key[1]): for k in figurs.keys(): if k[0] > min(key[0], king[0]) and k[0] < max(key[0], king[0]) and \ abs(king[0] - key[0]) == abs(king[1] - key[1]): is_shah = False break else: is_shah = True else: is_shah = False if not is_shah and figurs[key] in ['R', 'Q']: if key[0] == king[0]: for k in figurs.keys(): if k[0] == king[0] and k[1] > min(key[1], king[1]) and k[1] < max(key[1], king[1]): is_shah = False else: is_shah = True elif key[1] == king[1]: for k in figurs.keys(): if k[1] == king[1] and k[0] > min(key[0], king[0]) and k[0] < max(key[0], king[0]): is_shah = False else: is_shah = True else: is_shah = False if is_shah: break if is_shah: print('YES') else: print('NO') ```
instruction
0
48,447
15
96,894
No
output
1
48,447
15
96,895
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Anton likes to play chess. Also, he likes to do programming. That is why he decided to write the program that plays chess. However, he finds the game on 8 to 8 board to too simple, he uses an infinite one instead. The first task he faced is to check whether the king is in check. Anton doesn't know how to implement this so he asks you to help. Consider that an infinite chess board contains one white king and the number of black pieces. There are only rooks, bishops and queens, as the other pieces are not supported yet. The white king is said to be in check if at least one black piece can reach the cell with the king in one move. Help Anton and write the program that for the given position determines whether the white king is in check. Remainder, on how do chess pieces move: * Bishop moves any number of cells diagonally, but it can't "leap" over the occupied cells. * Rook moves any number of cells horizontally or vertically, but it also can't "leap" over the occupied cells. * Queen is able to move any number of cells horizontally, vertically or diagonally, but it also can't "leap". Input The first line of the input contains a single integer n (1 ≀ n ≀ 500 000) β€” the number of black pieces. The second line contains two integers x0 and y0 ( - 109 ≀ x0, y0 ≀ 109) β€” coordinates of the white king. Then follow n lines, each of them contains a character and two integers xi and yi ( - 109 ≀ xi, yi ≀ 109) β€” type of the i-th piece and its position. Character 'B' stands for the bishop, 'R' for the rook and 'Q' for the queen. It's guaranteed that no two pieces occupy the same position. Output The only line of the output should contains "YES" (without quotes) if the white king is in check and "NO" (without quotes) otherwise. Examples Input 2 4 2 R 1 1 B 1 5 Output YES Input 2 4 2 R 3 3 B 1 5 Output NO Note Picture for the first sample: <image> White king is in check, because the black bishop can reach the cell with the white king in one move. The answer is "YES". Picture for the second sample: <image> Here bishop can't reach the cell with the white king, because his path is blocked by the rook, and the bishop cant "leap" over it. Rook can't reach the white king, because it can't move diagonally. Hence, the king is not in check and the answer is "NO". Submitted Solution: ``` #!/usr/bin/env python3 # -*- coding: utf-8 -*- from math import sqrt from copy import copy class figure: def __init__(self,ras=None,type=None): self.ras=copy(ras) self.type=type n=int(input()) x0,y0=map(int,input().split()) sup='NO' f=[] for i in range(8): d=figure(10**70) f.append(d) for i in range(n): teg,x,y=map(str,input().split()) x,y=int(x),int(y) if x==x0: if y<y0: ras=y-y0 if ras<f[0].ras: f[0]=figure(ras,teg) if y>y0: ras=y0-y if ras<f[1].ras: f[1]=figure(ras,teg) if y==y0: if x>x0: ras=x-x0 if ras<f[2].ras: f[2]=figure(ras,teg) if x<x0: ras=x0-x if ras<f[3].ras: f[3]=figure(ras,teg) if abs(x-x0)==abs(y-y0): if y<y0 and x<x0: ras=sqrt(2)*abs(x-x0) if ras<f[4].ras: f[4]=figure(ras,teg) if y<y0 and x>x0: ras=sqrt(2)*abs(x-x0) if ras<f[5].ras: f[5]=figure(ras,teg) if y>y0 and x>x0: ras=sqrt(2)*abs(x-x0) if ras<f[6].ras: f[6]=figure(ras,teg) if y>y0 and x<x0: ras=sqrt(2)*abs(x-x0) if ras<f[7].ras: f[7]=figure(ras,teg) for i in range(len(f)): if i in [0,1,2,3]: if f[i].type=='R' or f[i].type=='Q': sup='YES' else: if f[i].type=='Q' or f[i].type=='B': sup='YES' print(sup) ```
instruction
0
48,448
15
96,896
No
output
1
48,448
15
96,897
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Anton likes to play chess. Also, he likes to do programming. That is why he decided to write the program that plays chess. However, he finds the game on 8 to 8 board to too simple, he uses an infinite one instead. The first task he faced is to check whether the king is in check. Anton doesn't know how to implement this so he asks you to help. Consider that an infinite chess board contains one white king and the number of black pieces. There are only rooks, bishops and queens, as the other pieces are not supported yet. The white king is said to be in check if at least one black piece can reach the cell with the king in one move. Help Anton and write the program that for the given position determines whether the white king is in check. Remainder, on how do chess pieces move: * Bishop moves any number of cells diagonally, but it can't "leap" over the occupied cells. * Rook moves any number of cells horizontally or vertically, but it also can't "leap" over the occupied cells. * Queen is able to move any number of cells horizontally, vertically or diagonally, but it also can't "leap". Input The first line of the input contains a single integer n (1 ≀ n ≀ 500 000) β€” the number of black pieces. The second line contains two integers x0 and y0 ( - 109 ≀ x0, y0 ≀ 109) β€” coordinates of the white king. Then follow n lines, each of them contains a character and two integers xi and yi ( - 109 ≀ xi, yi ≀ 109) β€” type of the i-th piece and its position. Character 'B' stands for the bishop, 'R' for the rook and 'Q' for the queen. It's guaranteed that no two pieces occupy the same position. Output The only line of the output should contains "YES" (without quotes) if the white king is in check and "NO" (without quotes) otherwise. Examples Input 2 4 2 R 1 1 B 1 5 Output YES Input 2 4 2 R 3 3 B 1 5 Output NO Note Picture for the first sample: <image> White king is in check, because the black bishop can reach the cell with the white king in one move. The answer is "YES". Picture for the second sample: <image> Here bishop can't reach the cell with the white king, because his path is blocked by the rook, and the bishop cant "leap" over it. Rook can't reach the white king, because it can't move diagonally. Hence, the king is not in check and the answer is "NO". Submitted Solution: ``` import sys """ Large amount of black pieces They can block each others' movement Determine if white king is in their path 1) King is in the path of a bishop if: |x_king - x_black| = |y_king - y_black| 2) King is in the path of a rook if: x_king = x_black OR y_king = y_black 3) King is in the path of a queen if: 1) OR 2) are satisfied All these conditions hold true if neither of black pieces block path of a black piece in question Only rooks and queens | bishops and queens can block each other Once we find at least on checking piece, we return YES """ import sys def rook_rule(king_x, king_y, piece_x, piece_y): return king_x == piece_x or king_y == piece_y def bishop_rule(king_x, king_y, piece_x, piece_y): return abs(king_y - piece_y) == abs(king_x - piece_x) def queen_rule(king_x, king_y, piece_x, piece_y): return ( rook_rule(king_x, king_y, piece_x, piece_y) or bishop_rule(king_x, king_y, piece_x, piece_y) ) def get_rule(piece: str): if piece == "R": return rook_rule elif piece == "B": return bishop_rule elif piece == "Q": return queen_rule def solution(king_x, king_y, black_pieces): # Check if there are checking pieces checking_pieces = [] for bp in black_pieces: piece, (piece_x, piece_y) = bp rule = get_rule(piece) if rule(king_x, king_y, piece_x, piece_y): checking_pieces.append(bp) checking_pieces.sort(key=lambda x: (king_x - x[1][0], king_y - x[1][1])) # Check if some pieces block each other for cp in checking_pieces: piece, (piece_x, piece_y) = cp rule = get_rule(piece) blocked = False for bp in black_pieces: _, (other_piece_x, other_piece_y) = bp if ( (piece_x == other_piece_x and piece_y == other_piece_y) or rule(other_piece_x, other_piece_y, piece_x, piece_y) ): blocked = True if not blocked: return "YES" return "NO" if __name__ == "__main__": _ = input() king_x, king_y = map(int, input().split(" ")) black_pieces = [] for i in sys.stdin.readlines(): black_piece, bp_x, bp_y = i.split(" ") black_pieces.append((black_piece, (int(bp_x), int(bp_y)))) sys.stdout.write(str(solution(king_x, king_y, black_pieces))) ```
instruction
0
48,449
15
96,898
No
output
1
48,449
15
96,899
Provide a correct Python 3 solution for this coding contest problem. Nobuo-kun and Shizuo-kun are playing a game of competing for territories on a rectangular island. As shown in Fig. 1 below, the entire island is made up of square compartments divided in a grid pattern, and the profits and losses resulting from these are indicated by integers. <image> In this game, move one piece to determine the boundaries of the territory. At the beginning of the game, the piece is at the northwestern end of the island (Fig. β‘ ). The trajectory of the piece when moving this piece to the southeastern end of the island becomes the boundary of the territory. The two players move the pieces alternately. Pieces can only be moved to the grid point next to the south or the grid point next to the east (Fig. 2). When the piece reaches the edge of the island, move it in the south or east direction. The game is over when the piece reaches the southeastern end. The area on the northeast side of the boundary after the game is the area of ​​the first player, and the area on the southwest side is the area of ​​the second player (Fig. β‘’). The sum of the profits and losses generated from the parcels contained within each player's territory is that player's core. Both of them are quite accustomed to the game and move the pieces accurately so that the value obtained by subtracting the opponent's score from their own score is the largest in the end. Create a program that calculates the outcome at the end of the game given the size of the island and the gains and losses that result from each parcel. The result is the absolute value of the difference between the scores of Nobuo-kun and Shizuo-kun. Input The input is given in the following format. W H s1,1 s1,2 ... s1, W s2,1 s2,2 ... s2, W :: sH, 1 sH, 2 ... sH, W The first line gives the number of east-west and north-south sections contained in the island W, H (1 ≀ W, H ≀ 1000). The following row H is given the gains and losses si, j (-1000 ≀ si, j ≀ 1000) that arise from the compartments in row i and column j. However, the direction in which the value of i increases is south, and the direction in which the value of j increases is east. Output The absolute value of the difference between the scores of Nobuo-kun and Shizuo-kun is output in one line. Examples Input 2 1 -2 1 Output 1 Input 2 2 2 -3 3 -1 Output 3 Input 5 4 5 3 2 -5 2 2 -4 2 8 -4 2 3 -7 6 7 3 -4 10 -3 -3 Output 5
instruction
0
48,724
15
97,448
"Correct Solution: ``` import sys sys.setrecursionlimit(10**7) w, h = map(int, input().split()) S = [list(map(int, input().split())) for i in range(h)] SW = [[0]*w for i in range(h)] SH = [[0]*w for i in range(h)] for i in range(h): cnt = 0 for j in range(w-1, -1, -1): cnt += S[i][j] SW[i][j] = cnt for j in range(w): cnt = 0 for i in range(h-1, -1, -1): cnt += S[i][j] SH[i][j] = cnt memo = {} def dfs(x, y): if (x, y) in memo: return memo[x, y] if x == w or y == h: return 0 if (x+y) % 2 == 0: # first res = max(dfs(x+1, y) - SH[y][x], dfs(x, y+1) + SW[y][x]) else: # second res = min(dfs(x+1, y) - SH[y][x], dfs(x, y+1) + SW[y][x]) memo[x, y] = res return res print(abs(dfs(0, 0))) ```
output
1
48,724
15
97,449
Provide a correct Python 3 solution for this coding contest problem. Nobuo-kun and Shizuo-kun are playing a game of competing for territories on a rectangular island. As shown in Fig. 1 below, the entire island is made up of square compartments divided in a grid pattern, and the profits and losses resulting from these are indicated by integers. <image> In this game, move one piece to determine the boundaries of the territory. At the beginning of the game, the piece is at the northwestern end of the island (Fig. β‘ ). The trajectory of the piece when moving this piece to the southeastern end of the island becomes the boundary of the territory. The two players move the pieces alternately. Pieces can only be moved to the grid point next to the south or the grid point next to the east (Fig. 2). When the piece reaches the edge of the island, move it in the south or east direction. The game is over when the piece reaches the southeastern end. The area on the northeast side of the boundary after the game is the area of ​​the first player, and the area on the southwest side is the area of ​​the second player (Fig. β‘’). The sum of the profits and losses generated from the parcels contained within each player's territory is that player's core. Both of them are quite accustomed to the game and move the pieces accurately so that the value obtained by subtracting the opponent's score from their own score is the largest in the end. Create a program that calculates the outcome at the end of the game given the size of the island and the gains and losses that result from each parcel. The result is the absolute value of the difference between the scores of Nobuo-kun and Shizuo-kun. Input The input is given in the following format. W H s1,1 s1,2 ... s1, W s2,1 s2,2 ... s2, W :: sH, 1 sH, 2 ... sH, W The first line gives the number of east-west and north-south sections contained in the island W, H (1 ≀ W, H ≀ 1000). The following row H is given the gains and losses si, j (-1000 ≀ si, j ≀ 1000) that arise from the compartments in row i and column j. However, the direction in which the value of i increases is south, and the direction in which the value of j increases is east. Output The absolute value of the difference between the scores of Nobuo-kun and Shizuo-kun is output in one line. Examples Input 2 1 -2 1 Output 1 Input 2 2 2 -3 3 -1 Output 3 Input 5 4 5 3 2 -5 2 2 -4 2 8 -4 2 3 -7 6 7 3 -4 10 -3 -3 Output 5
instruction
0
48,725
15
97,450
"Correct Solution: ``` from itertools import accumulate import sys sys.setrecursionlimit(1000000) def main(): w, h = map(int, input().split()) mp = [list(map(int, input().split())) for _ in range(h)] acc_mp = list(map(lambda line:[0] + list(accumulate(line)), mp)) mem = {} def score(x, y, turn): if (x, y) in mem:return mem[(x, y)] if y >= h: ret = 0 elif x >= w: ret = acc_mp[y][x] + score(x, y + 1, 0) else: left = acc_mp[y][x] right = acc_mp[y][w] - left if turn == 0: ret = max(score(x + 1, y, 1), left - right + score(x, y + 1, 1)) if turn == 1: ret = min(score(x + 1, y, 0), left - right + score(x, y + 1, 0)) mem[(x, y)] = ret return ret print(abs(score(0, 0, 1))) main() ```
output
1
48,725
15
97,451