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 tags and a correct Python 3 solution for this coding contest problem.
The black king is standing on a chess field consisting of 109 rows and 109 columns. We will consider the rows of the field numbered with integers from 1 to 109 from top to bottom. The columns are similarly numbered with integers from 1 to 109 from left to right. We will denote a cell of the field that is located in the i-th row and j-th column as (i, j).
You know that some squares of the given chess field are allowed. All allowed cells of the chess field are given as n segments. Each segment is described by three integers ri, ai, bi (ai ≤ bi), denoting that cells in columns from number ai to number bi inclusive in the ri-th row are allowed.
Your task is to find the minimum number of moves the king needs to get from square (x0, y0) to square (x1, y1), provided that he only moves along the allowed cells. In other words, the king can be located only on allowed cells on his way.
Let us remind you that a chess king can move to any of the neighboring cells in one move. Two cells of a chess field are considered neighboring if they share at least one point.
Input
The first line contains four space-separated integers x0, y0, x1, y1 (1 ≤ x0, y0, x1, y1 ≤ 109), denoting the initial and the final positions of the king.
The second line contains a single integer n (1 ≤ n ≤ 105), denoting the number of segments of allowed cells. Next n lines contain the descriptions of these segments. The i-th line contains three space-separated integers ri, ai, bi (1 ≤ ri, ai, bi ≤ 109, ai ≤ bi), denoting that cells in columns from number ai to number bi inclusive in the ri-th row are allowed. Note that the segments of the allowed cells can intersect and embed arbitrarily.
It is guaranteed that the king's initial and final position are allowed cells. It is guaranteed that the king's initial and the final positions do not coincide. It is guaranteed that the total length of all given segments doesn't exceed 105.
Output
If there is no path between the initial and final position along allowed cells, print -1.
Otherwise print a single integer — the minimum number of moves the king needs to get from the initial position to the final one.
Examples
Input
5 7 6 11
3
5 3 8
6 7 11
5 2 5
Output
4
Input
3 4 3 10
3
3 1 4
4 5 9
3 10 10
Output
6
Input
1 1 2 10
2
1 1 3
2 6 10
Output
-1 | instruction | 0 | 57,384 | 15 | 114,768 |
Tags: dfs and similar, graphs, hashing, shortest paths
Correct Solution:
```
def main():
import sys
from collections import deque
x0,y0,x1,y1 = map(int,input().strip().split())
n = int(input())
S = list(map(int,sys.stdin.read().strip().split()))
h = set()
d = {}
def getchild(a):
c = []
xc = [1,-1,0,0,1,1,-1,-1]
yc = [0,0,1,-1,1,-1,1,-1]
for i in range(8):
if ((a[0]+xc[i],a[1]+yc[i]) in h):
c.append((a[0]+xc[i],a[1]+yc[i]))
return(c)
class g:
def __init__(self):
self.depth = -1
self.child = []
for i in range(0,3*n,3):
r,a,b = S[i],S[i+1],S[i+2]
for j in range(a,b+1):
h.add((r,j))
d[(r,j)] = g()
king = d[(x0,y0)]
destination = d[(x1,y1)]
for i in h:
d[i].child = getchild(i)
Q = deque()
Q.append(king)
king.depth = 0
while(len(Q)>0):
a = Q.popleft()
for i in a.child:
if d[i].depth==-1:
Q.append(d[i])
d[i].depth=a.depth+1
if a==destination:
break
print(destination.depth)
main()
``` | output | 1 | 57,384 | 15 | 114,769 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The black king is standing on a chess field consisting of 109 rows and 109 columns. We will consider the rows of the field numbered with integers from 1 to 109 from top to bottom. The columns are similarly numbered with integers from 1 to 109 from left to right. We will denote a cell of the field that is located in the i-th row and j-th column as (i, j).
You know that some squares of the given chess field are allowed. All allowed cells of the chess field are given as n segments. Each segment is described by three integers ri, ai, bi (ai ≤ bi), denoting that cells in columns from number ai to number bi inclusive in the ri-th row are allowed.
Your task is to find the minimum number of moves the king needs to get from square (x0, y0) to square (x1, y1), provided that he only moves along the allowed cells. In other words, the king can be located only on allowed cells on his way.
Let us remind you that a chess king can move to any of the neighboring cells in one move. Two cells of a chess field are considered neighboring if they share at least one point.
Input
The first line contains four space-separated integers x0, y0, x1, y1 (1 ≤ x0, y0, x1, y1 ≤ 109), denoting the initial and the final positions of the king.
The second line contains a single integer n (1 ≤ n ≤ 105), denoting the number of segments of allowed cells. Next n lines contain the descriptions of these segments. The i-th line contains three space-separated integers ri, ai, bi (1 ≤ ri, ai, bi ≤ 109, ai ≤ bi), denoting that cells in columns from number ai to number bi inclusive in the ri-th row are allowed. Note that the segments of the allowed cells can intersect and embed arbitrarily.
It is guaranteed that the king's initial and final position are allowed cells. It is guaranteed that the king's initial and the final positions do not coincide. It is guaranteed that the total length of all given segments doesn't exceed 105.
Output
If there is no path between the initial and final position along allowed cells, print -1.
Otherwise print a single integer — the minimum number of moves the king needs to get from the initial position to the final one.
Examples
Input
5 7 6 11
3
5 3 8
6 7 11
5 2 5
Output
4
Input
3 4 3 10
3
3 1 4
4 5 9
3 10 10
Output
6
Input
1 1 2 10
2
1 1 3
2 6 10
Output
-1 | instruction | 0 | 57,385 | 15 | 114,770 |
Tags: dfs and similar, graphs, hashing, shortest paths
Correct Solution:
```
from collections import deque, defaultdict
class Solution:
def solve(self):
kpos = input().split()
kpos = [int(x) for x in kpos]
startK = [kpos[0], kpos[1]]
endK = [kpos[2], kpos[3]]
N = int(input())
intervals_dict = defaultdict(list)
for _ in range(N):
allowed = input().split()
allowed = [int(x) for x in allowed]
row, start, end = allowed
intervals_dict[row].append([start, end])
# merge intervals
for row in intervals_dict:
intervals = intervals_dict[row]
intervals.sort()
final_merged = []
last_start, last_end = intervals[0]
for interval in intervals[1:]:
start, end = interval
if start <= last_end:
last_end = end
continue
final_merged.append([last_start, last_end])
last_start = start
last_end = end
final_merged.append([last_start, last_end])
last_start = start
intervals_dict[row] = final_merged
# BFS
queue = deque([startK])
moves = 0
seen = set((startK[0], startK[1]))
directions = [(i, j) for i in [1, 0, -1]
for j in [1, 0, -1] if not(i == j == 0)]
def get_intervals(intervals_dict):
for intervals in intervals_dict:
yield intervals
while queue:
moves += 1
for i in range(len(queue)):
x, y = queue.popleft()
for dx, dy in directions:
row, col = x + dx, y + dy
if row in intervals_dict and (row, col) not in seen:
valid = False
for interval in intervals_dict[row]:
start_int, end_int = interval
if start_int <= col <= end_int:
valid = True
break
if valid:
intervals_dict[row][0][0] <= col <= intervals_dict[row][0][1]
seen.add((row, col))
if (row, col) == (endK[0], endK[1]):
return moves
queue.append((row, col))
return -1
soln = Solution()
print(soln.solve())
``` | output | 1 | 57,385 | 15 | 114,771 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The black king is standing on a chess field consisting of 109 rows and 109 columns. We will consider the rows of the field numbered with integers from 1 to 109 from top to bottom. The columns are similarly numbered with integers from 1 to 109 from left to right. We will denote a cell of the field that is located in the i-th row and j-th column as (i, j).
You know that some squares of the given chess field are allowed. All allowed cells of the chess field are given as n segments. Each segment is described by three integers ri, ai, bi (ai ≤ bi), denoting that cells in columns from number ai to number bi inclusive in the ri-th row are allowed.
Your task is to find the minimum number of moves the king needs to get from square (x0, y0) to square (x1, y1), provided that he only moves along the allowed cells. In other words, the king can be located only on allowed cells on his way.
Let us remind you that a chess king can move to any of the neighboring cells in one move. Two cells of a chess field are considered neighboring if they share at least one point.
Input
The first line contains four space-separated integers x0, y0, x1, y1 (1 ≤ x0, y0, x1, y1 ≤ 109), denoting the initial and the final positions of the king.
The second line contains a single integer n (1 ≤ n ≤ 105), denoting the number of segments of allowed cells. Next n lines contain the descriptions of these segments. The i-th line contains three space-separated integers ri, ai, bi (1 ≤ ri, ai, bi ≤ 109, ai ≤ bi), denoting that cells in columns from number ai to number bi inclusive in the ri-th row are allowed. Note that the segments of the allowed cells can intersect and embed arbitrarily.
It is guaranteed that the king's initial and final position are allowed cells. It is guaranteed that the king's initial and the final positions do not coincide. It is guaranteed that the total length of all given segments doesn't exceed 105.
Output
If there is no path between the initial and final position along allowed cells, print -1.
Otherwise print a single integer — the minimum number of moves the king needs to get from the initial position to the final one.
Examples
Input
5 7 6 11
3
5 3 8
6 7 11
5 2 5
Output
4
Input
3 4 3 10
3
3 1 4
4 5 9
3 10 10
Output
6
Input
1 1 2 10
2
1 1 3
2 6 10
Output
-1 | instruction | 0 | 57,386 | 15 | 114,772 |
Tags: dfs and similar, graphs, hashing, shortest paths
Correct Solution:
```
from collections import *
import bisect
import heapq
import sys
from math import inf
def ri():
return int(input())
def rl():
return list(map(int, input().split()))
def isPossiblePlace(x, y):
for my_list in allowed[x]:
a, b = my_list
if y >= a and y <= b:
return True
return False
def bfs(x0, y0, x1, y1):
visited = defaultdict(list)
to_visit = deque()
to_visit.append((x0, y0, 0))
visited[x0].append(y0)
while to_visit:
x,y,d = to_visit.popleft()
for dx in [-1,0,1]:
for dy in [-1,0,1]:
if abs(dx) + abs(dy) != 0:
nx = x + dx
ny = y + dy
if nx >= 1 and nx <= 10**9 and ny >= 1 and ny <= 10**9:
if isPossiblePlace(nx, ny):
if (nx, ny) == (x1,y1):
return d + 1
if ny not in visited[nx]:
visited[nx].append(ny)
to_visit.append((nx, ny, d + 1))
return -1
x0, y0, x1, y1 = rl()
n = ri()
allowed = defaultdict(set)
for _ in range(n):
i, start, end = rl()
allowed[i].add((start, end))
print(bfs(x0, y0, x1, y1))
``` | output | 1 | 57,386 | 15 | 114,773 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The black king is standing on a chess field consisting of 109 rows and 109 columns. We will consider the rows of the field numbered with integers from 1 to 109 from top to bottom. The columns are similarly numbered with integers from 1 to 109 from left to right. We will denote a cell of the field that is located in the i-th row and j-th column as (i, j).
You know that some squares of the given chess field are allowed. All allowed cells of the chess field are given as n segments. Each segment is described by three integers ri, ai, bi (ai ≤ bi), denoting that cells in columns from number ai to number bi inclusive in the ri-th row are allowed.
Your task is to find the minimum number of moves the king needs to get from square (x0, y0) to square (x1, y1), provided that he only moves along the allowed cells. In other words, the king can be located only on allowed cells on his way.
Let us remind you that a chess king can move to any of the neighboring cells in one move. Two cells of a chess field are considered neighboring if they share at least one point.
Input
The first line contains four space-separated integers x0, y0, x1, y1 (1 ≤ x0, y0, x1, y1 ≤ 109), denoting the initial and the final positions of the king.
The second line contains a single integer n (1 ≤ n ≤ 105), denoting the number of segments of allowed cells. Next n lines contain the descriptions of these segments. The i-th line contains three space-separated integers ri, ai, bi (1 ≤ ri, ai, bi ≤ 109, ai ≤ bi), denoting that cells in columns from number ai to number bi inclusive in the ri-th row are allowed. Note that the segments of the allowed cells can intersect and embed arbitrarily.
It is guaranteed that the king's initial and final position are allowed cells. It is guaranteed that the king's initial and the final positions do not coincide. It is guaranteed that the total length of all given segments doesn't exceed 105.
Output
If there is no path between the initial and final position along allowed cells, print -1.
Otherwise print a single integer — the minimum number of moves the king needs to get from the initial position to the final one.
Examples
Input
5 7 6 11
3
5 3 8
6 7 11
5 2 5
Output
4
Input
3 4 3 10
3
3 1 4
4 5 9
3 10 10
Output
6
Input
1 1 2 10
2
1 1 3
2 6 10
Output
-1 | instruction | 0 | 57,387 | 15 | 114,774 |
Tags: dfs and similar, graphs, hashing, shortest paths
Correct Solution:
```
from collections import deque
x,y,x2,y2=map(int,input().split())
n=int(input())
kletki={}
for i in range(n):
r,a,b=map(int,input().split())
for j in range(a, b+1):
kletki[(r,j)]=-1
kletki[(x,y)]=0
deq = deque()
deq.append((x,y))
dx=[-1, -1, 0, 1, 1, 1, 0, -1]
dy=[0, -1, -1, -1, 0, 1, 1, 1]
while len(deq):
m = deq.popleft()
for k in range(8):
n = (m[0]+dx[k], m[1]+dy[k])
if n in kletki.keys():
if m in kletki and kletki[n] == -1:
kletki[n] = kletki[m]+1
deq.append(n)
print(kletki[(x2,y2)])
``` | output | 1 | 57,387 | 15 | 114,775 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The black king is standing on a chess field consisting of 109 rows and 109 columns. We will consider the rows of the field numbered with integers from 1 to 109 from top to bottom. The columns are similarly numbered with integers from 1 to 109 from left to right. We will denote a cell of the field that is located in the i-th row and j-th column as (i, j).
You know that some squares of the given chess field are allowed. All allowed cells of the chess field are given as n segments. Each segment is described by three integers ri, ai, bi (ai ≤ bi), denoting that cells in columns from number ai to number bi inclusive in the ri-th row are allowed.
Your task is to find the minimum number of moves the king needs to get from square (x0, y0) to square (x1, y1), provided that he only moves along the allowed cells. In other words, the king can be located only on allowed cells on his way.
Let us remind you that a chess king can move to any of the neighboring cells in one move. Two cells of a chess field are considered neighboring if they share at least one point.
Input
The first line contains four space-separated integers x0, y0, x1, y1 (1 ≤ x0, y0, x1, y1 ≤ 109), denoting the initial and the final positions of the king.
The second line contains a single integer n (1 ≤ n ≤ 105), denoting the number of segments of allowed cells. Next n lines contain the descriptions of these segments. The i-th line contains three space-separated integers ri, ai, bi (1 ≤ ri, ai, bi ≤ 109, ai ≤ bi), denoting that cells in columns from number ai to number bi inclusive in the ri-th row are allowed. Note that the segments of the allowed cells can intersect and embed arbitrarily.
It is guaranteed that the king's initial and final position are allowed cells. It is guaranteed that the king's initial and the final positions do not coincide. It is guaranteed that the total length of all given segments doesn't exceed 105.
Output
If there is no path between the initial and final position along allowed cells, print -1.
Otherwise print a single integer — the minimum number of moves the king needs to get from the initial position to the final one.
Examples
Input
5 7 6 11
3
5 3 8
6 7 11
5 2 5
Output
4
Input
3 4 3 10
3
3 1 4
4 5 9
3 10 10
Output
6
Input
1 1 2 10
2
1 1 3
2 6 10
Output
-1
Submitted Solution:
```
from collections import deque
px1, py1, px2, py2 = [int(x) for x in input().split()]
p_start, p_end = (px1, py1), (px2, py2)
n = int(input())
valid = set()
for _ in range(n):
r, a, b = [int(x) for x in input().split()]
valid.update(((r, j) for j in range(a, b + 1)))
d = deque()
d.append((p_start, 0))
visited = {p_start}
while d:
p1, m = d.popleft()
if p1 == p_end:
print(m)
quit()
for p2 in [(p1[0] + dx, p1[1] + dy) for dx in [-1, 0, 1] for dy in [-1, 0, 1]]:
if (p2 in valid) and (p2 not in visited):
visited.add(p2)
d.append((p2, m + 1))
print(-1)
``` | instruction | 0 | 57,388 | 15 | 114,776 |
Yes | output | 1 | 57,388 | 15 | 114,777 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The black king is standing on a chess field consisting of 109 rows and 109 columns. We will consider the rows of the field numbered with integers from 1 to 109 from top to bottom. The columns are similarly numbered with integers from 1 to 109 from left to right. We will denote a cell of the field that is located in the i-th row and j-th column as (i, j).
You know that some squares of the given chess field are allowed. All allowed cells of the chess field are given as n segments. Each segment is described by three integers ri, ai, bi (ai ≤ bi), denoting that cells in columns from number ai to number bi inclusive in the ri-th row are allowed.
Your task is to find the minimum number of moves the king needs to get from square (x0, y0) to square (x1, y1), provided that he only moves along the allowed cells. In other words, the king can be located only on allowed cells on his way.
Let us remind you that a chess king can move to any of the neighboring cells in one move. Two cells of a chess field are considered neighboring if they share at least one point.
Input
The first line contains four space-separated integers x0, y0, x1, y1 (1 ≤ x0, y0, x1, y1 ≤ 109), denoting the initial and the final positions of the king.
The second line contains a single integer n (1 ≤ n ≤ 105), denoting the number of segments of allowed cells. Next n lines contain the descriptions of these segments. The i-th line contains three space-separated integers ri, ai, bi (1 ≤ ri, ai, bi ≤ 109, ai ≤ bi), denoting that cells in columns from number ai to number bi inclusive in the ri-th row are allowed. Note that the segments of the allowed cells can intersect and embed arbitrarily.
It is guaranteed that the king's initial and final position are allowed cells. It is guaranteed that the king's initial and the final positions do not coincide. It is guaranteed that the total length of all given segments doesn't exceed 105.
Output
If there is no path between the initial and final position along allowed cells, print -1.
Otherwise print a single integer — the minimum number of moves the king needs to get from the initial position to the final one.
Examples
Input
5 7 6 11
3
5 3 8
6 7 11
5 2 5
Output
4
Input
3 4 3 10
3
3 1 4
4 5 9
3 10 10
Output
6
Input
1 1 2 10
2
1 1 3
2 6 10
Output
-1
Submitted Solution:
```
from collections import deque
x0, y0, x1, y1 = map(int, input().split())
n = int(input())
bars = {}
for i in range(n):
r, a, b = map(int, input().split())
if r not in bars:
bars[r] = {}
for j in range(a, b+1):
bars[r][j] = -1
bars[x0][y0] = 0
q = deque()
q.append(tuple([x0, y0]))
while q:
node = q.popleft()
for i in range(-1, 2):
row = node[0] + i
if row not in bars:
continue
for j in range(-1, 2):
col = node[1] + j
if col not in bars[row] or (i == 0 and j == 0):
continue
if bars[row][col] == -1:
bars[row][col] = bars[node[0]][node[1]] + 1
q.append(tuple([row, col]))
if bars[x1][y1] != -1:
break
print(bars[x1][y1])
``` | instruction | 0 | 57,389 | 15 | 114,778 |
Yes | output | 1 | 57,389 | 15 | 114,779 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The black king is standing on a chess field consisting of 109 rows and 109 columns. We will consider the rows of the field numbered with integers from 1 to 109 from top to bottom. The columns are similarly numbered with integers from 1 to 109 from left to right. We will denote a cell of the field that is located in the i-th row and j-th column as (i, j).
You know that some squares of the given chess field are allowed. All allowed cells of the chess field are given as n segments. Each segment is described by three integers ri, ai, bi (ai ≤ bi), denoting that cells in columns from number ai to number bi inclusive in the ri-th row are allowed.
Your task is to find the minimum number of moves the king needs to get from square (x0, y0) to square (x1, y1), provided that he only moves along the allowed cells. In other words, the king can be located only on allowed cells on his way.
Let us remind you that a chess king can move to any of the neighboring cells in one move. Two cells of a chess field are considered neighboring if they share at least one point.
Input
The first line contains four space-separated integers x0, y0, x1, y1 (1 ≤ x0, y0, x1, y1 ≤ 109), denoting the initial and the final positions of the king.
The second line contains a single integer n (1 ≤ n ≤ 105), denoting the number of segments of allowed cells. Next n lines contain the descriptions of these segments. The i-th line contains three space-separated integers ri, ai, bi (1 ≤ ri, ai, bi ≤ 109, ai ≤ bi), denoting that cells in columns from number ai to number bi inclusive in the ri-th row are allowed. Note that the segments of the allowed cells can intersect and embed arbitrarily.
It is guaranteed that the king's initial and final position are allowed cells. It is guaranteed that the king's initial and the final positions do not coincide. It is guaranteed that the total length of all given segments doesn't exceed 105.
Output
If there is no path between the initial and final position along allowed cells, print -1.
Otherwise print a single integer — the minimum number of moves the king needs to get from the initial position to the final one.
Examples
Input
5 7 6 11
3
5 3 8
6 7 11
5 2 5
Output
4
Input
3 4 3 10
3
3 1 4
4 5 9
3 10 10
Output
6
Input
1 1 2 10
2
1 1 3
2 6 10
Output
-1
Submitted Solution:
```
from sys import stdin
def main():
x, y, u, v = map(int, input().split())
input()
rows = set()
for st in stdin.read().splitlines():
r, a, b = map(int, st.split())
rows.update((r, x) for x in range(a, b + 1))
nxt, visited, cnt = [(x, y)], {(x, y)}, 0
while nxt:
cur, nxt = nxt, []
for x, y in cur:
if x == u and y == v:
print(cnt)
return
for xy in (x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1), (x - 1, y - 1), (x + 1, y - 1), (x - 1, y + 1), (
x + 1, y + 1):
if xy in rows and xy not in visited:
visited.add(xy)
nxt.append(xy)
cnt += 1
print(-1)
if __name__ == '__main__':
main()
# Made By Mostafa_Khaled
``` | instruction | 0 | 57,390 | 15 | 114,780 |
Yes | output | 1 | 57,390 | 15 | 114,781 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The black king is standing on a chess field consisting of 109 rows and 109 columns. We will consider the rows of the field numbered with integers from 1 to 109 from top to bottom. The columns are similarly numbered with integers from 1 to 109 from left to right. We will denote a cell of the field that is located in the i-th row and j-th column as (i, j).
You know that some squares of the given chess field are allowed. All allowed cells of the chess field are given as n segments. Each segment is described by three integers ri, ai, bi (ai ≤ bi), denoting that cells in columns from number ai to number bi inclusive in the ri-th row are allowed.
Your task is to find the minimum number of moves the king needs to get from square (x0, y0) to square (x1, y1), provided that he only moves along the allowed cells. In other words, the king can be located only on allowed cells on his way.
Let us remind you that a chess king can move to any of the neighboring cells in one move. Two cells of a chess field are considered neighboring if they share at least one point.
Input
The first line contains four space-separated integers x0, y0, x1, y1 (1 ≤ x0, y0, x1, y1 ≤ 109), denoting the initial and the final positions of the king.
The second line contains a single integer n (1 ≤ n ≤ 105), denoting the number of segments of allowed cells. Next n lines contain the descriptions of these segments. The i-th line contains three space-separated integers ri, ai, bi (1 ≤ ri, ai, bi ≤ 109, ai ≤ bi), denoting that cells in columns from number ai to number bi inclusive in the ri-th row are allowed. Note that the segments of the allowed cells can intersect and embed arbitrarily.
It is guaranteed that the king's initial and final position are allowed cells. It is guaranteed that the king's initial and the final positions do not coincide. It is guaranteed that the total length of all given segments doesn't exceed 105.
Output
If there is no path between the initial and final position along allowed cells, print -1.
Otherwise print a single integer — the minimum number of moves the king needs to get from the initial position to the final one.
Examples
Input
5 7 6 11
3
5 3 8
6 7 11
5 2 5
Output
4
Input
3 4 3 10
3
3 1 4
4 5 9
3 10 10
Output
6
Input
1 1 2 10
2
1 1 3
2 6 10
Output
-1
Submitted Solution:
```
from collections import deque
x_0, y_0, x_1, y_1 = list(map(int, input().split()))
n = int(input())
g = {}
for _ in range(n):
r, a, b = list(map(int, input().split()))
# all the valid cols have value -1
for i in range(a, b + 1):
g[(r, i)] = -1
# level zero
g[(x_0, y_0)] = 0
q = deque([(x_0, y_0)])
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]
while len(q) != 0:
current = q.popleft()
for i in range(8):
neigbor = (current[0] + dx[i], current[1] + dy[i])
if neigbor in g and g[neigbor] == -1:
q.append(neigbor)
g[neigbor] = g[current] + 1
if neigbor == (x_1, y_1):
break
print(g[(x_1, y_1)])
``` | instruction | 0 | 57,391 | 15 | 114,782 |
Yes | output | 1 | 57,391 | 15 | 114,783 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The black king is standing on a chess field consisting of 109 rows and 109 columns. We will consider the rows of the field numbered with integers from 1 to 109 from top to bottom. The columns are similarly numbered with integers from 1 to 109 from left to right. We will denote a cell of the field that is located in the i-th row and j-th column as (i, j).
You know that some squares of the given chess field are allowed. All allowed cells of the chess field are given as n segments. Each segment is described by three integers ri, ai, bi (ai ≤ bi), denoting that cells in columns from number ai to number bi inclusive in the ri-th row are allowed.
Your task is to find the minimum number of moves the king needs to get from square (x0, y0) to square (x1, y1), provided that he only moves along the allowed cells. In other words, the king can be located only on allowed cells on his way.
Let us remind you that a chess king can move to any of the neighboring cells in one move. Two cells of a chess field are considered neighboring if they share at least one point.
Input
The first line contains four space-separated integers x0, y0, x1, y1 (1 ≤ x0, y0, x1, y1 ≤ 109), denoting the initial and the final positions of the king.
The second line contains a single integer n (1 ≤ n ≤ 105), denoting the number of segments of allowed cells. Next n lines contain the descriptions of these segments. The i-th line contains three space-separated integers ri, ai, bi (1 ≤ ri, ai, bi ≤ 109, ai ≤ bi), denoting that cells in columns from number ai to number bi inclusive in the ri-th row are allowed. Note that the segments of the allowed cells can intersect and embed arbitrarily.
It is guaranteed that the king's initial and final position are allowed cells. It is guaranteed that the king's initial and the final positions do not coincide. It is guaranteed that the total length of all given segments doesn't exceed 105.
Output
If there is no path between the initial and final position along allowed cells, print -1.
Otherwise print a single integer — the minimum number of moves the king needs to get from the initial position to the final one.
Examples
Input
5 7 6 11
3
5 3 8
6 7 11
5 2 5
Output
4
Input
3 4 3 10
3
3 1 4
4 5 9
3 10 10
Output
6
Input
1 1 2 10
2
1 1 3
2 6 10
Output
-1
Submitted Solution:
```
import sys
input = sys.stdin.readline
read_tuple = lambda _type: map(_type, input().split(' '))
def neighbours(i, j):
return (
(i + 1, j), (i - 1, j),
(i, j + 1), (i, j - 1),
(i + 1, j + 1), (i - 1, j - 1),
(i + 1, j - 1), (i - 1, j + 1)
)
def solve():
x_0, y_0, x_1, y_1 = read_tuple(int)
n = int(input())
cells = set([(x_0, y_0), (x_1, y_1)])
for _ in range(n):
r, a, b = read_tuple(int)
for j in range(a, b + 1):
cells.add((r, j))
# bfs
steps = -1
queue = [(x_0, y_0)]
cells.discard((x_0, y_0))
while queue:
print(queue)
next_queue = []
steps += 1
for i, j in queue:
if i == x_1 and j == y_1:
break
for n_i, n_j in neighbours(i, j):
if (n_i, n_j) in cells:
next_queue.append((n_i, n_j))
cells.discard((n_i, n_j))
queue = next_queue
if (x_1, y_1) in cells:
print(-1)
else:
print(steps)
if __name__ == '__main__':
solve()
``` | instruction | 0 | 57,392 | 15 | 114,784 |
No | output | 1 | 57,392 | 15 | 114,785 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The black king is standing on a chess field consisting of 109 rows and 109 columns. We will consider the rows of the field numbered with integers from 1 to 109 from top to bottom. The columns are similarly numbered with integers from 1 to 109 from left to right. We will denote a cell of the field that is located in the i-th row and j-th column as (i, j).
You know that some squares of the given chess field are allowed. All allowed cells of the chess field are given as n segments. Each segment is described by three integers ri, ai, bi (ai ≤ bi), denoting that cells in columns from number ai to number bi inclusive in the ri-th row are allowed.
Your task is to find the minimum number of moves the king needs to get from square (x0, y0) to square (x1, y1), provided that he only moves along the allowed cells. In other words, the king can be located only on allowed cells on his way.
Let us remind you that a chess king can move to any of the neighboring cells in one move. Two cells of a chess field are considered neighboring if they share at least one point.
Input
The first line contains four space-separated integers x0, y0, x1, y1 (1 ≤ x0, y0, x1, y1 ≤ 109), denoting the initial and the final positions of the king.
The second line contains a single integer n (1 ≤ n ≤ 105), denoting the number of segments of allowed cells. Next n lines contain the descriptions of these segments. The i-th line contains three space-separated integers ri, ai, bi (1 ≤ ri, ai, bi ≤ 109, ai ≤ bi), denoting that cells in columns from number ai to number bi inclusive in the ri-th row are allowed. Note that the segments of the allowed cells can intersect and embed arbitrarily.
It is guaranteed that the king's initial and final position are allowed cells. It is guaranteed that the king's initial and the final positions do not coincide. It is guaranteed that the total length of all given segments doesn't exceed 105.
Output
If there is no path between the initial and final position along allowed cells, print -1.
Otherwise print a single integer — the minimum number of moves the king needs to get from the initial position to the final one.
Examples
Input
5 7 6 11
3
5 3 8
6 7 11
5 2 5
Output
4
Input
3 4 3 10
3
3 1 4
4 5 9
3 10 10
Output
6
Input
1 1 2 10
2
1 1 3
2 6 10
Output
-1
Submitted Solution:
```
import math
def clac_dest(xs, ys, xe, ye):
return math.sqrt(pow(xs - xe, 2) + pow(ys - ye, 2))
def get_next_move(xs, ys, xe, ye):
res = []
#x++, y++
res.append([xs + 1, ys + 1, clac_dest(xs + 1, ys + 1, xe, ye)])
#x--, y--
res.append([xs - 1, ys - 1, clac_dest(xs - 1, ys - 1, xe, ye)])
#x++, y--
res.append([xs + 1, ys - 1, clac_dest(xs + 1, ys - 1, xe, ye)])
#x--, y++
res.append([xs - 1, ys + 1, clac_dest(xs - 1, ys + 1, xe, ye)])
#x++, y
res.append([xs + 1, ys, clac_dest(xs + 1, ys, xe, ye)])
#x--, y
res.append([xs - 1, ys, clac_dest(xs - 1, ys, xe, ye)])
#x, y++
res.append([xs, ys + 1, clac_dest(xs, ys + 1, xe, ye)])
#x, y--
res.append([xs, ys - 1, clac_dest(xs, ys - 1, xe, ye)])
res = sorted(res, reverse= True)
return res
def check_path(xs, ys, xe, ye, steps, visited):
if xs == xe and ys == ye:
return steps
global dct
moves = get_next_move(xs, ys, xe, ye)
for m in moves:
my = m[1]
mx = m[0]
node = [mx, my]
if node not in visited and my in dct and dct[my][0] <= mx and dct[my][1] >= mx:
visited.append([xs, ys])
res = check_path(mx, my, xe, ye, steps + 1, visited)
if res :
return res
return -1
dct = {}
visited = []
y1, x1, y2, x2 =[int(i) for i in input().split()]
n = int(input())
for _ in range(n):
r, a, b =[int(i) for i in input().split()]
if r not in dct:
dct[r] = [a, b]
else:
v = dct[r]
if v[0] > a:
v[0] = a
if v[1] < b:
v[1] = b
dct[r] = v
print(check_path(x1, y1, x2, y2, 0, visited))
``` | instruction | 0 | 57,393 | 15 | 114,786 |
No | output | 1 | 57,393 | 15 | 114,787 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The black king is standing on a chess field consisting of 109 rows and 109 columns. We will consider the rows of the field numbered with integers from 1 to 109 from top to bottom. The columns are similarly numbered with integers from 1 to 109 from left to right. We will denote a cell of the field that is located in the i-th row and j-th column as (i, j).
You know that some squares of the given chess field are allowed. All allowed cells of the chess field are given as n segments. Each segment is described by three integers ri, ai, bi (ai ≤ bi), denoting that cells in columns from number ai to number bi inclusive in the ri-th row are allowed.
Your task is to find the minimum number of moves the king needs to get from square (x0, y0) to square (x1, y1), provided that he only moves along the allowed cells. In other words, the king can be located only on allowed cells on his way.
Let us remind you that a chess king can move to any of the neighboring cells in one move. Two cells of a chess field are considered neighboring if they share at least one point.
Input
The first line contains four space-separated integers x0, y0, x1, y1 (1 ≤ x0, y0, x1, y1 ≤ 109), denoting the initial and the final positions of the king.
The second line contains a single integer n (1 ≤ n ≤ 105), denoting the number of segments of allowed cells. Next n lines contain the descriptions of these segments. The i-th line contains three space-separated integers ri, ai, bi (1 ≤ ri, ai, bi ≤ 109, ai ≤ bi), denoting that cells in columns from number ai to number bi inclusive in the ri-th row are allowed. Note that the segments of the allowed cells can intersect and embed arbitrarily.
It is guaranteed that the king's initial and final position are allowed cells. It is guaranteed that the king's initial and the final positions do not coincide. It is guaranteed that the total length of all given segments doesn't exceed 105.
Output
If there is no path between the initial and final position along allowed cells, print -1.
Otherwise print a single integer — the minimum number of moves the king needs to get from the initial position to the final one.
Examples
Input
5 7 6 11
3
5 3 8
6 7 11
5 2 5
Output
4
Input
3 4 3 10
3
3 1 4
4 5 9
3 10 10
Output
6
Input
1 1 2 10
2
1 1 3
2 6 10
Output
-1
Submitted Solution:
```
import sys
from math import sqrt, gcd, ceil, log
# from bisect import bisect, bisect_left
from collections import defaultdict, Counter, deque
# from heapq import heapify, heappush, heappop
input = sys.stdin.readline
read = lambda: list(map(int, input().strip().split()))
def main():
# ans = []
a, b, c, d = read()
area = {}
direct = [[1, 1], [1, -1], [-1, 1], [-1, -1], [1, 0], [0, 1], [-1, 0], [0, -1]]
for _ in range(int(input())):
r, x, y = read()
if r in area:
area[r][0] = min(area[r][0], x)
area[r][1] = max(area[r][1], y)
else:
area[r] = [x, y]
# print(area)
quex, quey = deque([a]), deque([b])
dist = {(a, b): 0}
f = 0
while quex and quey:
currx, curry = quex.popleft(), quey.popleft()
for i in direct:
childx, childy = currx+i[0], curry+i[1]
if (childx, childy) == (c, d):
print(dist[(currx, curry)] + 1)
exit()
# f = 1
# break
if not (childx, childy) in dist and (childx in area) and (area[childx][0] <= childy <= area[childx][1]):
dist[(childx, childy)] = dist[(currx, curry)] + 1
quex.append(childx); quey.append(childy)
# if f:
# break
# if f:
# print(dist[(c, d)])
# else:
print(-1)
# print(dist)
# sep = " "
# print((sep).join(map(str, ans)))
if __name__ == "__main__":
main()
``` | instruction | 0 | 57,394 | 15 | 114,788 |
No | output | 1 | 57,394 | 15 | 114,789 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The black king is standing on a chess field consisting of 109 rows and 109 columns. We will consider the rows of the field numbered with integers from 1 to 109 from top to bottom. The columns are similarly numbered with integers from 1 to 109 from left to right. We will denote a cell of the field that is located in the i-th row and j-th column as (i, j).
You know that some squares of the given chess field are allowed. All allowed cells of the chess field are given as n segments. Each segment is described by three integers ri, ai, bi (ai ≤ bi), denoting that cells in columns from number ai to number bi inclusive in the ri-th row are allowed.
Your task is to find the minimum number of moves the king needs to get from square (x0, y0) to square (x1, y1), provided that he only moves along the allowed cells. In other words, the king can be located only on allowed cells on his way.
Let us remind you that a chess king can move to any of the neighboring cells in one move. Two cells of a chess field are considered neighboring if they share at least one point.
Input
The first line contains four space-separated integers x0, y0, x1, y1 (1 ≤ x0, y0, x1, y1 ≤ 109), denoting the initial and the final positions of the king.
The second line contains a single integer n (1 ≤ n ≤ 105), denoting the number of segments of allowed cells. Next n lines contain the descriptions of these segments. The i-th line contains three space-separated integers ri, ai, bi (1 ≤ ri, ai, bi ≤ 109, ai ≤ bi), denoting that cells in columns from number ai to number bi inclusive in the ri-th row are allowed. Note that the segments of the allowed cells can intersect and embed arbitrarily.
It is guaranteed that the king's initial and final position are allowed cells. It is guaranteed that the king's initial and the final positions do not coincide. It is guaranteed that the total length of all given segments doesn't exceed 105.
Output
If there is no path between the initial and final position along allowed cells, print -1.
Otherwise print a single integer — the minimum number of moves the king needs to get from the initial position to the final one.
Examples
Input
5 7 6 11
3
5 3 8
6 7 11
5 2 5
Output
4
Input
3 4 3 10
3
3 1 4
4 5 9
3 10 10
Output
6
Input
1 1 2 10
2
1 1 3
2 6 10
Output
-1
Submitted Solution:
```
def get_next_move(xs, ys, xe, ye):
res = []
#x++, y++
res.append([xs + 1, ys + 1])
#x--, y--
res.append([xs - 1, ys - 1])
#x++, y--
res.append([xs + 1, ys - 1])
#x--, y++
res.append([xs - 1, ys + 1])
#x++, y
res.append([xs + 1, ys])
#x--, y
res.append([xs - 1, ys])
#x, y++
res.append([xs, ys + 1])
#x, y--
res.append([xs, ys - 1])
res = sorted(res, reverse= True)
return res
def check_path(xs, ys, xe, ye, dct, visited):
i = 0
if ye == ys and xe == xs:
return 0
node = [ys, xs]
dct_steps = {}
visited.append(node)
dct_steps[str(node)] = 0
while i < len(visited):
xs = visited[i][1]
ys = visited[i][0]
steps = dct_steps[str(visited[i])] + 1
moves = get_next_move(xs, ys, xe, ye)
for m in moves:
my = m[1]
mx = m[0]
node = [my, mx]
if my == ye and mx == xe:
return steps
if node not in visited and my in dct and dct[my][0] <= mx and dct[my][1] >= mx:
visited.append(node)
dct_steps[str(node)] = steps
i += 1
return -1
dct = {}
answer = []
visited = []
y1, x1, y2, x2 =[int(i) for i in input().split()]
n = int(input())
for _ in range(n):
r, a, b =[int(i) for i in input().split()]
if r not in dct:
dct[r] = [a, b]
else:
v = dct[r]
if v[0] > a:
v[0] = a
if v[1] < b:
v[1] = b
dct[r] = v
print(check_path(x1, y1, x2, y2, dct, visited))
'''
10 10 13 12
5
9 11 13
10 13 13
11 13 13
12 12 12
13 12 12
'''
``` | instruction | 0 | 57,395 | 15 | 114,790 |
No | output | 1 | 57,395 | 15 | 114,791 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this problem you will meet the simplified model of game King of Thieves.
In a new ZeptoLab game called "King of Thieves" your aim is to reach a chest with gold by controlling your character, avoiding traps and obstacles on your way.
<image>
An interesting feature of the game is that you can design your own levels that will be available to other players. Let's consider the following simple design of a level.
A dungeon consists of n segments located at a same vertical level, each segment is either a platform that character can stand on, or a pit with a trap that makes player lose if he falls into it. All segments have the same length, platforms on the scheme of the level are represented as '*' and pits are represented as '.'.
One of things that affects speedrun characteristics of the level is a possibility to perform a series of consecutive jumps of the same length. More formally, when the character is on the platform number i1, he can make a sequence of jumps through the platforms i1 < i2 < ... < ik, if i2 - i1 = i3 - i2 = ... = ik - ik - 1. Of course, all segments i1, i2, ... ik should be exactly the platforms, not pits.
Let's call a level to be good if you can perform a sequence of four jumps of the same length or in the other words there must be a sequence i1, i2, ..., i5, consisting of five platforms so that the intervals between consecutive platforms are of the same length. Given the scheme of the level, check if it is good.
Input
The first line contains integer n (1 ≤ n ≤ 100) — the number of segments on the level.
Next line contains the scheme of the level represented as a string of n characters '*' and '.'.
Output
If the level is good, print the word "yes" (without the quotes), otherwise print the word "no" (without the quotes).
Examples
Input
16
.**.*..*.***.**.
Output
yes
Input
11
.*.*...*.*.
Output
no
Note
In the first sample test you may perform a sequence of jumps through platforms 2, 5, 8, 11, 14. | instruction | 0 | 57,524 | 15 | 115,048 |
Tags: brute force, implementation
Correct Solution:
```
import itertools
def king():
n= int(input())
s= input()
a= []
for i in range(len(s)):
if s[i]== "*":
a.append(i+1)
#print(a)
if len(a)< 5:
print("no")
return
for arr in itertools.combinations(a, 5):
arr= sorted(arr)
mine= []
for i in range(4):
mine.append(arr[i+1]- arr[i])
if len(set(mine))== 1:
print("yes")
return
print("no")
return
king()
``` | output | 1 | 57,524 | 15 | 115,049 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this problem you will meet the simplified model of game King of Thieves.
In a new ZeptoLab game called "King of Thieves" your aim is to reach a chest with gold by controlling your character, avoiding traps and obstacles on your way.
<image>
An interesting feature of the game is that you can design your own levels that will be available to other players. Let's consider the following simple design of a level.
A dungeon consists of n segments located at a same vertical level, each segment is either a platform that character can stand on, or a pit with a trap that makes player lose if he falls into it. All segments have the same length, platforms on the scheme of the level are represented as '*' and pits are represented as '.'.
One of things that affects speedrun characteristics of the level is a possibility to perform a series of consecutive jumps of the same length. More formally, when the character is on the platform number i1, he can make a sequence of jumps through the platforms i1 < i2 < ... < ik, if i2 - i1 = i3 - i2 = ... = ik - ik - 1. Of course, all segments i1, i2, ... ik should be exactly the platforms, not pits.
Let's call a level to be good if you can perform a sequence of four jumps of the same length or in the other words there must be a sequence i1, i2, ..., i5, consisting of five platforms so that the intervals between consecutive platforms are of the same length. Given the scheme of the level, check if it is good.
Input
The first line contains integer n (1 ≤ n ≤ 100) — the number of segments on the level.
Next line contains the scheme of the level represented as a string of n characters '*' and '.'.
Output
If the level is good, print the word "yes" (without the quotes), otherwise print the word "no" (without the quotes).
Examples
Input
16
.**.*..*.***.**.
Output
yes
Input
11
.*.*...*.*.
Output
no
Note
In the first sample test you may perform a sequence of jumps through platforms 2, 5, 8, 11, 14. | instruction | 0 | 57,525 | 15 | 115,050 |
Tags: brute force, implementation
Correct Solution:
```
n = int(input())
s = input().strip()
print('yes' if '*****' in ('.'.join(s[i::j] for i in range(n) for j in range(1, n))) else 'no')
# Made By Mostafa_Khaled
``` | output | 1 | 57,525 | 15 | 115,051 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this problem you will meet the simplified model of game King of Thieves.
In a new ZeptoLab game called "King of Thieves" your aim is to reach a chest with gold by controlling your character, avoiding traps and obstacles on your way.
<image>
An interesting feature of the game is that you can design your own levels that will be available to other players. Let's consider the following simple design of a level.
A dungeon consists of n segments located at a same vertical level, each segment is either a platform that character can stand on, or a pit with a trap that makes player lose if he falls into it. All segments have the same length, platforms on the scheme of the level are represented as '*' and pits are represented as '.'.
One of things that affects speedrun characteristics of the level is a possibility to perform a series of consecutive jumps of the same length. More formally, when the character is on the platform number i1, he can make a sequence of jumps through the platforms i1 < i2 < ... < ik, if i2 - i1 = i3 - i2 = ... = ik - ik - 1. Of course, all segments i1, i2, ... ik should be exactly the platforms, not pits.
Let's call a level to be good if you can perform a sequence of four jumps of the same length or in the other words there must be a sequence i1, i2, ..., i5, consisting of five platforms so that the intervals between consecutive platforms are of the same length. Given the scheme of the level, check if it is good.
Input
The first line contains integer n (1 ≤ n ≤ 100) — the number of segments on the level.
Next line contains the scheme of the level represented as a string of n characters '*' and '.'.
Output
If the level is good, print the word "yes" (without the quotes), otherwise print the word "no" (without the quotes).
Examples
Input
16
.**.*..*.***.**.
Output
yes
Input
11
.*.*...*.*.
Output
no
Note
In the first sample test you may perform a sequence of jumps through platforms 2, 5, 8, 11, 14. | instruction | 0 | 57,526 | 15 | 115,052 |
Tags: brute force, implementation
Correct Solution:
```
n,s = int(input()),input()
for i in range(1,len(s)):
for j in range(len(s)):
if s[j] == '.':
continue
L = s[j::i]
mx = 0; c = 1
for k in range(1,len(L)):
if L[k] == L[k - 1] and L[k] == '*':
c += 1
else:
c = 1
mx = max (mx,c)
if mx == 5:
print("yes")
exit(0)
print ("no")
``` | output | 1 | 57,526 | 15 | 115,053 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this problem you will meet the simplified model of game King of Thieves.
In a new ZeptoLab game called "King of Thieves" your aim is to reach a chest with gold by controlling your character, avoiding traps and obstacles on your way.
<image>
An interesting feature of the game is that you can design your own levels that will be available to other players. Let's consider the following simple design of a level.
A dungeon consists of n segments located at a same vertical level, each segment is either a platform that character can stand on, or a pit with a trap that makes player lose if he falls into it. All segments have the same length, platforms on the scheme of the level are represented as '*' and pits are represented as '.'.
One of things that affects speedrun characteristics of the level is a possibility to perform a series of consecutive jumps of the same length. More formally, when the character is on the platform number i1, he can make a sequence of jumps through the platforms i1 < i2 < ... < ik, if i2 - i1 = i3 - i2 = ... = ik - ik - 1. Of course, all segments i1, i2, ... ik should be exactly the platforms, not pits.
Let's call a level to be good if you can perform a sequence of four jumps of the same length or in the other words there must be a sequence i1, i2, ..., i5, consisting of five platforms so that the intervals between consecutive platforms are of the same length. Given the scheme of the level, check if it is good.
Input
The first line contains integer n (1 ≤ n ≤ 100) — the number of segments on the level.
Next line contains the scheme of the level represented as a string of n characters '*' and '.'.
Output
If the level is good, print the word "yes" (without the quotes), otherwise print the word "no" (without the quotes).
Examples
Input
16
.**.*..*.***.**.
Output
yes
Input
11
.*.*...*.*.
Output
no
Note
In the first sample test you may perform a sequence of jumps through platforms 2, 5, 8, 11, 14. | instruction | 0 | 57,527 | 15 | 115,054 |
Tags: brute force, implementation
Correct Solution:
```
n,s = int(input()),input()
for i in range(1,len(s)):
for j in range(len(s)):
if s[j] == '.':
continue
L = s[j::i]
mx = 0; c = 1
for k in range(1,len(L)):
if L[k] == L[k - 1] and L[k] == '*':
c += 1
else:
c = 1
mx = max (mx,c)
if mx >= 5:
print("yes")
exit(0)
print ("no")
``` | output | 1 | 57,527 | 15 | 115,055 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this problem you will meet the simplified model of game King of Thieves.
In a new ZeptoLab game called "King of Thieves" your aim is to reach a chest with gold by controlling your character, avoiding traps and obstacles on your way.
<image>
An interesting feature of the game is that you can design your own levels that will be available to other players. Let's consider the following simple design of a level.
A dungeon consists of n segments located at a same vertical level, each segment is either a platform that character can stand on, or a pit with a trap that makes player lose if he falls into it. All segments have the same length, platforms on the scheme of the level are represented as '*' and pits are represented as '.'.
One of things that affects speedrun characteristics of the level is a possibility to perform a series of consecutive jumps of the same length. More formally, when the character is on the platform number i1, he can make a sequence of jumps through the platforms i1 < i2 < ... < ik, if i2 - i1 = i3 - i2 = ... = ik - ik - 1. Of course, all segments i1, i2, ... ik should be exactly the platforms, not pits.
Let's call a level to be good if you can perform a sequence of four jumps of the same length or in the other words there must be a sequence i1, i2, ..., i5, consisting of five platforms so that the intervals between consecutive platforms are of the same length. Given the scheme of the level, check if it is good.
Input
The first line contains integer n (1 ≤ n ≤ 100) — the number of segments on the level.
Next line contains the scheme of the level represented as a string of n characters '*' and '.'.
Output
If the level is good, print the word "yes" (without the quotes), otherwise print the word "no" (without the quotes).
Examples
Input
16
.**.*..*.***.**.
Output
yes
Input
11
.*.*...*.*.
Output
no
Note
In the first sample test you may perform a sequence of jumps through platforms 2, 5, 8, 11, 14. | instruction | 0 | 57,528 | 15 | 115,056 |
Tags: brute force, implementation
Correct Solution:
```
import sys
n = int(input())
S = input()
for l in range(n):
if S[l] == "*":
d = 1
while l + 4 * d < n:
if S[l + d] == S[l + 2 * d] == S[l + 3 * d] == S[l + 4 * d] == "*":
print("yes")
sys.exit(0)
d += 1
print("no")
``` | output | 1 | 57,528 | 15 | 115,057 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this problem you will meet the simplified model of game King of Thieves.
In a new ZeptoLab game called "King of Thieves" your aim is to reach a chest with gold by controlling your character, avoiding traps and obstacles on your way.
<image>
An interesting feature of the game is that you can design your own levels that will be available to other players. Let's consider the following simple design of a level.
A dungeon consists of n segments located at a same vertical level, each segment is either a platform that character can stand on, or a pit with a trap that makes player lose if he falls into it. All segments have the same length, platforms on the scheme of the level are represented as '*' and pits are represented as '.'.
One of things that affects speedrun characteristics of the level is a possibility to perform a series of consecutive jumps of the same length. More formally, when the character is on the platform number i1, he can make a sequence of jumps through the platforms i1 < i2 < ... < ik, if i2 - i1 = i3 - i2 = ... = ik - ik - 1. Of course, all segments i1, i2, ... ik should be exactly the platforms, not pits.
Let's call a level to be good if you can perform a sequence of four jumps of the same length or in the other words there must be a sequence i1, i2, ..., i5, consisting of five platforms so that the intervals between consecutive platforms are of the same length. Given the scheme of the level, check if it is good.
Input
The first line contains integer n (1 ≤ n ≤ 100) — the number of segments on the level.
Next line contains the scheme of the level represented as a string of n characters '*' and '.'.
Output
If the level is good, print the word "yes" (without the quotes), otherwise print the word "no" (without the quotes).
Examples
Input
16
.**.*..*.***.**.
Output
yes
Input
11
.*.*...*.*.
Output
no
Note
In the first sample test you may perform a sequence of jumps through platforms 2, 5, 8, 11, 14. | instruction | 0 | 57,529 | 15 | 115,058 |
Tags: brute force, implementation
Correct Solution:
```
n = int(input())
level = input()
yes = False
for i in range(n):
for j in range(n):
count = 0
f = 5
a = []
for k in range(i, n, j + 1):
if f > 0:
if level[k] == '*':
count += 1
f -= 1
if count >= 5:
yes = True
if yes:
print('yes')
else:
print('no')
``` | output | 1 | 57,529 | 15 | 115,059 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this problem you will meet the simplified model of game King of Thieves.
In a new ZeptoLab game called "King of Thieves" your aim is to reach a chest with gold by controlling your character, avoiding traps and obstacles on your way.
<image>
An interesting feature of the game is that you can design your own levels that will be available to other players. Let's consider the following simple design of a level.
A dungeon consists of n segments located at a same vertical level, each segment is either a platform that character can stand on, or a pit with a trap that makes player lose if he falls into it. All segments have the same length, platforms on the scheme of the level are represented as '*' and pits are represented as '.'.
One of things that affects speedrun characteristics of the level is a possibility to perform a series of consecutive jumps of the same length. More formally, when the character is on the platform number i1, he can make a sequence of jumps through the platforms i1 < i2 < ... < ik, if i2 - i1 = i3 - i2 = ... = ik - ik - 1. Of course, all segments i1, i2, ... ik should be exactly the platforms, not pits.
Let's call a level to be good if you can perform a sequence of four jumps of the same length or in the other words there must be a sequence i1, i2, ..., i5, consisting of five platforms so that the intervals between consecutive platforms are of the same length. Given the scheme of the level, check if it is good.
Input
The first line contains integer n (1 ≤ n ≤ 100) — the number of segments on the level.
Next line contains the scheme of the level represented as a string of n characters '*' and '.'.
Output
If the level is good, print the word "yes" (without the quotes), otherwise print the word "no" (without the quotes).
Examples
Input
16
.**.*..*.***.**.
Output
yes
Input
11
.*.*...*.*.
Output
no
Note
In the first sample test you may perform a sequence of jumps through platforms 2, 5, 8, 11, 14. | instruction | 0 | 57,530 | 15 | 115,060 |
Tags: brute force, implementation
Correct Solution:
```
import sys, os
import fileinput
n = int(input())
s = input().strip()
for i in range(1, n + 1):
for j in range(1, n + 1):
if j - 1 >= n or j - 1 + i >= n or j - 1 + 2*i >= n or j - 1 + 3*i >= n or j - 1 + 4*i >= n: break
t = "%s%s%s%s%s" % (s[j - 1], s[j - 1 + i], s[j - 1 + 2*i], s[j - 1 + 3*i], s[j - 1 + 4*i])
if t == "*****":
print("yes")
sys.exit(0)
print("no")
``` | output | 1 | 57,530 | 15 | 115,061 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this problem you will meet the simplified model of game King of Thieves.
In a new ZeptoLab game called "King of Thieves" your aim is to reach a chest with gold by controlling your character, avoiding traps and obstacles on your way.
<image>
An interesting feature of the game is that you can design your own levels that will be available to other players. Let's consider the following simple design of a level.
A dungeon consists of n segments located at a same vertical level, each segment is either a platform that character can stand on, or a pit with a trap that makes player lose if he falls into it. All segments have the same length, platforms on the scheme of the level are represented as '*' and pits are represented as '.'.
One of things that affects speedrun characteristics of the level is a possibility to perform a series of consecutive jumps of the same length. More formally, when the character is on the platform number i1, he can make a sequence of jumps through the platforms i1 < i2 < ... < ik, if i2 - i1 = i3 - i2 = ... = ik - ik - 1. Of course, all segments i1, i2, ... ik should be exactly the platforms, not pits.
Let's call a level to be good if you can perform a sequence of four jumps of the same length or in the other words there must be a sequence i1, i2, ..., i5, consisting of five platforms so that the intervals between consecutive platforms are of the same length. Given the scheme of the level, check if it is good.
Input
The first line contains integer n (1 ≤ n ≤ 100) — the number of segments on the level.
Next line contains the scheme of the level represented as a string of n characters '*' and '.'.
Output
If the level is good, print the word "yes" (without the quotes), otherwise print the word "no" (without the quotes).
Examples
Input
16
.**.*..*.***.**.
Output
yes
Input
11
.*.*...*.*.
Output
no
Note
In the first sample test you may perform a sequence of jumps through platforms 2, 5, 8, 11, 14. | instruction | 0 | 57,531 | 15 | 115,062 |
Tags: brute force, implementation
Correct Solution:
```
n=int(input())
s=input()
print('yes' if any(any(all(s[i+k*j]=='*' for k in range(5)) for j in range(1, (n-i+3)//4)) for i in range(n-4)) else 'no')
``` | output | 1 | 57,531 | 15 | 115,063 |
Provide a correct Python 3 solution for this coding contest problem.
Two coordinates (a1, a2) and (b1, b2) on a two-dimensional grid of r × c are given. The cost of moving from a cell (e, f) to one of the cells (e + 1, f), (e-1, f), (e, f + 1), (e, f-1) is 1. And. You can also move between (e, c-1) and (e, 0), and between (r-1, f) and (0, f) at a cost of 1. At this time, find the number of routes that can be moved from the first coordinate to the second coordinate at the shortest cost.
Input
The input is given in the following format.
r c a1 a2 b1 b2
Input meets the following constraints
1 ≤ r, c ≤ 1,000
0 ≤ a1, b1 <r
0 ≤ a2, b2 <c
Output
Output the remainder of the answer value divided by 100,000,007.
Examples
Input
4 4 0 0 3 3
Output
2
Input
4 4 0 0 1 1
Output
2
Input
2 3 0 0 1 2
Output
4
Input
500 500 0 0 200 200
Output
34807775 | instruction | 0 | 57,935 | 15 | 115,870 |
"Correct Solution:
```
from math import factorial
def comb (x,y):
return factorial(x)//factorial(x-y)//factorial(y)
w,h,ax,ay,bx,by=map(int,input().split())
dx=abs(ax-bx)
dx=min(dx,w-dx)
dy=abs(ay-by)
dy=min(dy,h-dy)
an=1
if dx*2==w:an*=2
if dy*2==h:an*=2
an*=comb(dx+dy,dx)
print(an%int(1E8+7))
``` | output | 1 | 57,935 | 15 | 115,871 |
Provide a correct Python 3 solution for this coding contest problem.
Two coordinates (a1, a2) and (b1, b2) on a two-dimensional grid of r × c are given. The cost of moving from a cell (e, f) to one of the cells (e + 1, f), (e-1, f), (e, f + 1), (e, f-1) is 1. And. You can also move between (e, c-1) and (e, 0), and between (r-1, f) and (0, f) at a cost of 1. At this time, find the number of routes that can be moved from the first coordinate to the second coordinate at the shortest cost.
Input
The input is given in the following format.
r c a1 a2 b1 b2
Input meets the following constraints
1 ≤ r, c ≤ 1,000
0 ≤ a1, b1 <r
0 ≤ a2, b2 <c
Output
Output the remainder of the answer value divided by 100,000,007.
Examples
Input
4 4 0 0 3 3
Output
2
Input
4 4 0 0 1 1
Output
2
Input
2 3 0 0 1 2
Output
4
Input
500 500 0 0 200 200
Output
34807775 | instruction | 0 | 57,936 | 15 | 115,872 |
"Correct Solution:
```
# Edit: 2014/09/09
# Lang: Python3
# Time: .00s
from math import factorial
if __name__ == "__main__":
# a, b, c, d, e, f = map(int, input().strip("\n").split(" "))
r, c, ar, ac, br, bc = map(int, input().strip("\n").split(" "))
maxans = 100000007 # 100,000,007
# maxn = 100000007
# tate Row
dr = min(abs(br - ar), r - abs(br - ar))
if 2 * dr == r:
gainr = 2
else:
gainr = 1
# yoko Column
dc = min(abs(bc - ac), c - abs(bc - ac))
if 2 * dc == c:
gainc = 2
else:
gainc = 1
#dr = int(dr)
#dc = int(dc)
#gainr = int(gainr)
#gainc = int(gainc)
# print(dr, dc)
# print(gainr, gainc)
ans1 = factorial(dr+dc)
#print("ans1",ans1)
#print("type",type(ans1))
ans2 = factorial(dr)
#print("ans2",ans2)
#print("type",type(ans2))
ans3 = factorial(dc)
#print("ans3",ans3)
#print("type",type(ans3))
ans4 = ans1//ans2//ans3*gainr*gainc
#print("ans4",ans4)
#print("type",type(ans4))
ans = ans4
## ans = factorial(dr + dc) / factorial(dr) / factorial(dc) # * gainr * gainc
# python overflow error integer division result too large for a float
#ans = factorial(dr + dc) // factorial(dr) // factorial(dc) * gainr * gainc
# t = 1
#for i in range(dr, dr + dc):
#print(t)
# t *= i
#ans = t / factorial(dc) * gainr * gainc
#print(ans)
#print(ans, type(ans))
print(ans % maxans)
# print("34807775")
#67,352,549
``` | output | 1 | 57,936 | 15 | 115,873 |
Provide a correct Python 3 solution for this coding contest problem.
Two coordinates (a1, a2) and (b1, b2) on a two-dimensional grid of r × c are given. The cost of moving from a cell (e, f) to one of the cells (e + 1, f), (e-1, f), (e, f + 1), (e, f-1) is 1. And. You can also move between (e, c-1) and (e, 0), and between (r-1, f) and (0, f) at a cost of 1. At this time, find the number of routes that can be moved from the first coordinate to the second coordinate at the shortest cost.
Input
The input is given in the following format.
r c a1 a2 b1 b2
Input meets the following constraints
1 ≤ r, c ≤ 1,000
0 ≤ a1, b1 <r
0 ≤ a2, b2 <c
Output
Output the remainder of the answer value divided by 100,000,007.
Examples
Input
4 4 0 0 3 3
Output
2
Input
4 4 0 0 1 1
Output
2
Input
2 3 0 0 1 2
Output
4
Input
500 500 0 0 200 200
Output
34807775 | instruction | 0 | 57,937 | 15 | 115,874 |
"Correct Solution:
```
mod = 10**8+7
n = 1001
def comb(x, y):
if 2*y > x: return comb(x, x-y)
return fac[x] * inv[y]* inv[x-y] % mod
fac = [1]*(n+1)
inv = [1]*(n+1)
for i in range(2, n+1):
fac[i] = fac[i-1] * i % mod
inv[n] = pow(fac[n], mod-2, mod)
for i in range(n-1, 1, -1):
inv[i] = inv[i+1] * (i+1) % mod
r, c, a1, a2, b1, b2 = map(int, input().split())
row = min(abs(b1-a1), r-abs(b1-a1))
col = min(abs(b2-a2), c-abs(b2-a2))
ans = 1
if row*2 == r: ans *= 2
if col*2 == c: ans *= 2
ans *= comb(row+col, row)
ans %= mod
print(ans)
``` | output | 1 | 57,937 | 15 | 115,875 |
Provide a correct Python 3 solution for this coding contest problem.
Two coordinates (a1, a2) and (b1, b2) on a two-dimensional grid of r × c are given. The cost of moving from a cell (e, f) to one of the cells (e + 1, f), (e-1, f), (e, f + 1), (e, f-1) is 1. And. You can also move between (e, c-1) and (e, 0), and between (r-1, f) and (0, f) at a cost of 1. At this time, find the number of routes that can be moved from the first coordinate to the second coordinate at the shortest cost.
Input
The input is given in the following format.
r c a1 a2 b1 b2
Input meets the following constraints
1 ≤ r, c ≤ 1,000
0 ≤ a1, b1 <r
0 ≤ a2, b2 <c
Output
Output the remainder of the answer value divided by 100,000,007.
Examples
Input
4 4 0 0 3 3
Output
2
Input
4 4 0 0 1 1
Output
2
Input
2 3 0 0 1 2
Output
4
Input
500 500 0 0 200 200
Output
34807775 | instruction | 0 | 57,938 | 15 | 115,876 |
"Correct Solution:
```
# AOJ 1501: Grid
# Python3 2018.7.13 bal4u
MOD = 100000007
def dp(n, k):
if tbl[n][k]: return tbl[n][k]
if (k << 1) > n: k = n-k
if k == 0: ans = 1
elif k == 1: ans = n
else: ans = dp(n-1, k) + dp(n-1, k-1)
tbl[n][k] = ans % MOD
return tbl[n][k]
tbl = [[0 for j in range(1001)] for i in range(1001)]
k = 0
r, c, a1, a2, b1, b2 = map(int, input().split())
dr = abs(a1-b1)
if dr > r-dr: dr = r-dr
if (dr << 1) == r: k += 1
dc = abs(a2-b2)
if dc > c-dc: dc = c-dc
if (dc << 1) == c: k += 1
print((dp(dr+dc, min(dr, dc)) << k) % MOD)
``` | output | 1 | 57,938 | 15 | 115,877 |
Provide a correct Python 3 solution for this coding contest problem.
Two coordinates (a1, a2) and (b1, b2) on a two-dimensional grid of r × c are given. The cost of moving from a cell (e, f) to one of the cells (e + 1, f), (e-1, f), (e, f + 1), (e, f-1) is 1. And. You can also move between (e, c-1) and (e, 0), and between (r-1, f) and (0, f) at a cost of 1. At this time, find the number of routes that can be moved from the first coordinate to the second coordinate at the shortest cost.
Input
The input is given in the following format.
r c a1 a2 b1 b2
Input meets the following constraints
1 ≤ r, c ≤ 1,000
0 ≤ a1, b1 <r
0 ≤ a2, b2 <c
Output
Output the remainder of the answer value divided by 100,000,007.
Examples
Input
4 4 0 0 3 3
Output
2
Input
4 4 0 0 1 1
Output
2
Input
2 3 0 0 1 2
Output
4
Input
500 500 0 0 200 200
Output
34807775 | instruction | 0 | 57,939 | 15 | 115,878 |
"Correct Solution:
```
# Edit: 2014/09/09
# Lang: Python3
# Time: .00s
from math import factorial
if __name__ == "__main__":
r, c, ar, ac, br, bc = map(int, input().strip("\n").split(" "))
maxans = 100000007 # 100,000,007
# tate Row
dr = min(abs(br - ar), r - abs(br - ar))
if 2 * dr == r:
gainr = 2
else:
gainr = 1
# yoko Column
dc = min(abs(bc - ac), c - abs(bc - ac))
if 2 * dc == c:
gainc = 2
else:
gainc = 1
#dr = int(dr)
#dc = int(dc)
#gainr = int(gainr)
#gainc = int(gainc)
# print(dr, dc)
# print(gainr, gainc)
#ans1 = factorial(dr+dc)
#print("ans1",ans1)
#print("type",type(ans1))
#ans2 = factorial(dr)
#print("ans2",ans2)
#print("type",type(ans2))
#ans3 = factorial(dc)
#print("ans3",ans3)
#print("type",type(ans3))
#ans4 = ans1//ans2//ans3*gainr*gainc
#print("ans4",ans4)
#print("type",type(ans4))
#ans = ans4
## ans = factorial(dr + dc) / factorial(dr) / factorial(dc) # * gainr * gainc
# python overflow error integer division result too large for a float
ans = factorial(dr + dc) // factorial(dr) // factorial(dc) * gainr * gainc
# t = 1
#for i in range(dr, dr + dc):
#print(t)
# t *= i
#ans = t / factorial(dc) * gainr * gainc
#print(ans)
#print(ans, type(ans))
print(ans % maxans)
# print("34807775")
#67,352,549
``` | output | 1 | 57,939 | 15 | 115,879 |
Provide a correct Python 3 solution for this coding contest problem.
Two coordinates (a1, a2) and (b1, b2) on a two-dimensional grid of r × c are given. The cost of moving from a cell (e, f) to one of the cells (e + 1, f), (e-1, f), (e, f + 1), (e, f-1) is 1. And. You can also move between (e, c-1) and (e, 0), and between (r-1, f) and (0, f) at a cost of 1. At this time, find the number of routes that can be moved from the first coordinate to the second coordinate at the shortest cost.
Input
The input is given in the following format.
r c a1 a2 b1 b2
Input meets the following constraints
1 ≤ r, c ≤ 1,000
0 ≤ a1, b1 <r
0 ≤ a2, b2 <c
Output
Output the remainder of the answer value divided by 100,000,007.
Examples
Input
4 4 0 0 3 3
Output
2
Input
4 4 0 0 1 1
Output
2
Input
2 3 0 0 1 2
Output
4
Input
500 500 0 0 200 200
Output
34807775 | instruction | 0 | 57,940 | 15 | 115,880 |
"Correct Solution:
```
MOD = 100000007
r, c, a1, a2, b1, b2 = map(int, input().split())
dx = abs(a1 - b1)
dy = abs(a2 - b2)
cnt = 1
if dx * 2 == r:cnt *= 2
if dy * 2 == c:cnt *= 2
dx = min(dx, r - dx)
dy = min(dy, c - dy)
dp = [[0] * (dx + 2) for _ in range(dy + 2)]
dp[0][1] = 1
for y in range(1, dy + 2):
for x in range(1, dx + 2):
dp[y][x] = (dp[y - 1][x] + dp[y][x - 1]) % MOD
print(dp[dy + 1][dx + 1] * cnt % MOD)
``` | output | 1 | 57,940 | 15 | 115,881 |
Provide a correct Python 3 solution for this coding contest problem.
Two coordinates (a1, a2) and (b1, b2) on a two-dimensional grid of r × c are given. The cost of moving from a cell (e, f) to one of the cells (e + 1, f), (e-1, f), (e, f + 1), (e, f-1) is 1. And. You can also move between (e, c-1) and (e, 0), and between (r-1, f) and (0, f) at a cost of 1. At this time, find the number of routes that can be moved from the first coordinate to the second coordinate at the shortest cost.
Input
The input is given in the following format.
r c a1 a2 b1 b2
Input meets the following constraints
1 ≤ r, c ≤ 1,000
0 ≤ a1, b1 <r
0 ≤ a2, b2 <c
Output
Output the remainder of the answer value divided by 100,000,007.
Examples
Input
4 4 0 0 3 3
Output
2
Input
4 4 0 0 1 1
Output
2
Input
2 3 0 0 1 2
Output
4
Input
500 500 0 0 200 200
Output
34807775 | instruction | 0 | 57,941 | 15 | 115,882 |
"Correct Solution:
```
# AOJ 1501: Grid
# Python3 2018.7.13 bal4u
from math import factorial
def comb (n, k):
return factorial(n)//factorial(n-k)//factorial(k)
k = 0
r, c, a1, a2, b1, b2 = map(int, input().split())
dr = abs(a1-b1)
if dr > r-dr: dr = r-dr
if (dr << 1) == r: k += 1
dc = abs(a2-b2)
if dc > c-dc: dc = c-dc
if (dc << 1) == c: k += 1
print((comb(dr+dc, min(dr, dc)) << k) % 100000007)
``` | output | 1 | 57,941 | 15 | 115,883 |
Provide a correct Python 3 solution for this coding contest problem.
Two coordinates (a1, a2) and (b1, b2) on a two-dimensional grid of r × c are given. The cost of moving from a cell (e, f) to one of the cells (e + 1, f), (e-1, f), (e, f + 1), (e, f-1) is 1. And. You can also move between (e, c-1) and (e, 0), and between (r-1, f) and (0, f) at a cost of 1. At this time, find the number of routes that can be moved from the first coordinate to the second coordinate at the shortest cost.
Input
The input is given in the following format.
r c a1 a2 b1 b2
Input meets the following constraints
1 ≤ r, c ≤ 1,000
0 ≤ a1, b1 <r
0 ≤ a2, b2 <c
Output
Output the remainder of the answer value divided by 100,000,007.
Examples
Input
4 4 0 0 3 3
Output
2
Input
4 4 0 0 1 1
Output
2
Input
2 3 0 0 1 2
Output
4
Input
500 500 0 0 200 200
Output
34807775 | instruction | 0 | 57,942 | 15 | 115,884 |
"Correct Solution:
```
def func (x):
if x:
return int(x)*func(x-1)
else:
return (1)
def comb (x,y):
return func(x)//func(x-y)//func(y)
import sys
sys.setrecursionlimit(10000)
w,h,ax,ay,bx,by=map(int,input().split())
dx=min(w-abs(ax-bx),abs(ax-bx))
dy=min(h-abs(ay-by),abs(ay-by))
ans=1
if dx*2==w:ans*=2
if dy*2==h:ans*=2
ans*=comb(dx+dy,dx)
print(ans%100000007)
``` | output | 1 | 57,942 | 15 | 115,885 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Two coordinates (a1, a2) and (b1, b2) on a two-dimensional grid of r × c are given. The cost of moving from a cell (e, f) to one of the cells (e + 1, f), (e-1, f), (e, f + 1), (e, f-1) is 1. And. You can also move between (e, c-1) and (e, 0), and between (r-1, f) and (0, f) at a cost of 1. At this time, find the number of routes that can be moved from the first coordinate to the second coordinate at the shortest cost.
Input
The input is given in the following format.
r c a1 a2 b1 b2
Input meets the following constraints
1 ≤ r, c ≤ 1,000
0 ≤ a1, b1 <r
0 ≤ a2, b2 <c
Output
Output the remainder of the answer value divided by 100,000,007.
Examples
Input
4 4 0 0 3 3
Output
2
Input
4 4 0 0 1 1
Output
2
Input
2 3 0 0 1 2
Output
4
Input
500 500 0 0 200 200
Output
34807775
Submitted Solution:
```
from math import factorial
def comb (x,y):
return factorial(x)//factorial(x-y)//factorial(y)
w,h,ax,ay,bx,by=map(int,input().split())
dx=abs(ax-bx)
dx=min(dx,w-dx)
dy=abs(ay-by)
dy=min(dy,h-dy)
ans=1
if dx*2==w:ans*=2
if dy*2==h:ans*=2
ans*=comb(dx+dy,dx)
print(ans%100000007)
``` | instruction | 0 | 57,943 | 15 | 115,886 |
Yes | output | 1 | 57,943 | 15 | 115,887 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Two coordinates (a1, a2) and (b1, b2) on a two-dimensional grid of r × c are given. The cost of moving from a cell (e, f) to one of the cells (e + 1, f), (e-1, f), (e, f + 1), (e, f-1) is 1. And. You can also move between (e, c-1) and (e, 0), and between (r-1, f) and (0, f) at a cost of 1. At this time, find the number of routes that can be moved from the first coordinate to the second coordinate at the shortest cost.
Input
The input is given in the following format.
r c a1 a2 b1 b2
Input meets the following constraints
1 ≤ r, c ≤ 1,000
0 ≤ a1, b1 <r
0 ≤ a2, b2 <c
Output
Output the remainder of the answer value divided by 100,000,007.
Examples
Input
4 4 0 0 3 3
Output
2
Input
4 4 0 0 1 1
Output
2
Input
2 3 0 0 1 2
Output
4
Input
500 500 0 0 200 200
Output
34807775
Submitted Solution:
```
from math import factorial
def comb (x,y):
return factorial(x)//factorial(x-y)//factorial(y)
w,h,ax,ay,bx,by=map(int,input().split())
dx=min(w-abs(ax-bx),abs(ax-bx))
dy=min(h-abs(ay-by),abs(ay-by))
ans=1
if dx*2==w:ans*=2
if dy*2==h:ans*=2
ans*=comb(dx+dy,dx)
print(ans%100000007)
``` | instruction | 0 | 57,944 | 15 | 115,888 |
Yes | output | 1 | 57,944 | 15 | 115,889 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Two coordinates (a1, a2) and (b1, b2) on a two-dimensional grid of r × c are given. The cost of moving from a cell (e, f) to one of the cells (e + 1, f), (e-1, f), (e, f + 1), (e, f-1) is 1. And. You can also move between (e, c-1) and (e, 0), and between (r-1, f) and (0, f) at a cost of 1. At this time, find the number of routes that can be moved from the first coordinate to the second coordinate at the shortest cost.
Input
The input is given in the following format.
r c a1 a2 b1 b2
Input meets the following constraints
1 ≤ r, c ≤ 1,000
0 ≤ a1, b1 <r
0 ≤ a2, b2 <c
Output
Output the remainder of the answer value divided by 100,000,007.
Examples
Input
4 4 0 0 3 3
Output
2
Input
4 4 0 0 1 1
Output
2
Input
2 3 0 0 1 2
Output
4
Input
500 500 0 0 200 200
Output
34807775
Submitted Solution:
```
# Edit: 2014/09/17
# Lang: Python3
# Time: 00.04s
from math import factorial
if __name__ == "__main__":
r, c, ar, ac, br, bc = map(int, input().strip("\n").split(" "))
maxans = 100000007 # 100,000,007
# tate Row
dr = min(abs(br - ar), r - abs(br - ar))
if 2 * dr == r:
gainr = 2
else:
gainr = 1
# yoko Column
dc = min(abs(bc - ac), c - abs(bc - ac))
if 2 * dc == c:
gainc = 2
else:
gainc = 1
ans = factorial(dr + dc) // factorial(dr) // factorial(dc) * gainr * gainc
print(ans % maxans)
``` | instruction | 0 | 57,945 | 15 | 115,890 |
Yes | output | 1 | 57,945 | 15 | 115,891 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Two coordinates (a1, a2) and (b1, b2) on a two-dimensional grid of r × c are given. The cost of moving from a cell (e, f) to one of the cells (e + 1, f), (e-1, f), (e, f + 1), (e, f-1) is 1. And. You can also move between (e, c-1) and (e, 0), and between (r-1, f) and (0, f) at a cost of 1. At this time, find the number of routes that can be moved from the first coordinate to the second coordinate at the shortest cost.
Input
The input is given in the following format.
r c a1 a2 b1 b2
Input meets the following constraints
1 ≤ r, c ≤ 1,000
0 ≤ a1, b1 <r
0 ≤ a2, b2 <c
Output
Output the remainder of the answer value divided by 100,000,007.
Examples
Input
4 4 0 0 3 3
Output
2
Input
4 4 0 0 1 1
Output
2
Input
2 3 0 0 1 2
Output
4
Input
500 500 0 0 200 200
Output
34807775
Submitted Solution:
```
import sys
from math import factorial
sys.setrecursionlimit(10000)
def comb (x,y):
return factorial(x)//factorial(x-y)//factorial(y)
w,h,ax,ay,bx,by=map(int,input().split())
dx=min(w-abs(ax-bx),abs(ax-bx))
dy=min(h-abs(ay-by),abs(ay-by))
ans=1
if dx*2==w:ans*=2
if dy*2==h:ans*=2
ans*=comb(dx+dy,dx)
print(ans%100000007)
``` | instruction | 0 | 57,946 | 15 | 115,892 |
Yes | output | 1 | 57,946 | 15 | 115,893 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Two coordinates (a1, a2) and (b1, b2) on a two-dimensional grid of r × c are given. The cost of moving from a cell (e, f) to one of the cells (e + 1, f), (e-1, f), (e, f + 1), (e, f-1) is 1. And. You can also move between (e, c-1) and (e, 0), and between (r-1, f) and (0, f) at a cost of 1. At this time, find the number of routes that can be moved from the first coordinate to the second coordinate at the shortest cost.
Input
The input is given in the following format.
r c a1 a2 b1 b2
Input meets the following constraints
1 ≤ r, c ≤ 1,000
0 ≤ a1, b1 <r
0 ≤ a2, b2 <c
Output
Output the remainder of the answer value divided by 100,000,007.
Examples
Input
4 4 0 0 3 3
Output
2
Input
4 4 0 0 1 1
Output
2
Input
2 3 0 0 1 2
Output
4
Input
500 500 0 0 200 200
Output
34807775
Submitted Solution:
```
# Edit: 2014/09/09
# Lang: Python3
# Time: .00s
from itertools import product
from math import factorial
if __name__ == "__main__":
# a, b, c, d, e, f = map(int, input().strip("\n").split(" "))
r, c, ar, ac, br, bc = map(int, input().strip("\n").split(" "))
maxans = 100000007 # 100,000,007
# maxn = 100000007
# tate Row
dr = abs(br - ar)
if dr == r / 2:
dr = dr
gainr = 2
elif dr > r / 2:
dr = r - dr
gainr = 1
else:
dr = dr
gainr = 1
# yoko Column
dc = abs(bc - ac)
if dc == r / 2:
dc = dc
gainc = 2
elif dc > r / 2:
dc = c - dc
gainc = 1
else:
dc = dc
gainc = 1
dr = int(dr)
dc = int(dc)
gainr = int(gainr)
gainc = int(gainc)
#print(dr, dc)
#print(gainr, gainc)
"""
ans = 0
a = factorial(400) % maxans
b = factorial(200) % maxans
c = factorial(200) % maxans
ans = int(a / b / c)
print(a)
print(b)
"""
#ans = factorial(dr + dc) / factorial(dr) / factorial(dc) # * gainr * gainc
# python overflow error integer division result too large for a float
ans = factorial(dr + dc) // factorial(dr) // factorial(dc) * gainr * gainc
# t = 1
#for i in range(dr, dr + dc):
#print(t)
# t *= i
#ans = t / factorial(dc) * gainr * gainc
#print(ans)
print(ans % maxans)
# print("34807775")
#67,352,549
``` | instruction | 0 | 57,947 | 15 | 115,894 |
No | output | 1 | 57,947 | 15 | 115,895 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Two coordinates (a1, a2) and (b1, b2) on a two-dimensional grid of r × c are given. The cost of moving from a cell (e, f) to one of the cells (e + 1, f), (e-1, f), (e, f + 1), (e, f-1) is 1. And. You can also move between (e, c-1) and (e, 0), and between (r-1, f) and (0, f) at a cost of 1. At this time, find the number of routes that can be moved from the first coordinate to the second coordinate at the shortest cost.
Input
The input is given in the following format.
r c a1 a2 b1 b2
Input meets the following constraints
1 ≤ r, c ≤ 1,000
0 ≤ a1, b1 <r
0 ≤ a2, b2 <c
Output
Output the remainder of the answer value divided by 100,000,007.
Examples
Input
4 4 0 0 3 3
Output
2
Input
4 4 0 0 1 1
Output
2
Input
2 3 0 0 1 2
Output
4
Input
500 500 0 0 200 200
Output
34807775
Submitted Solution:
```
def func (x):
if x:
return int(x)*func(x-1)
else:
return (1)
def comb (x,y):
return func(x)//func(x-y)//func(y)
w,h,ax,ay,bx,by=map(int,input().split())
dx=min(w-abs(ax-bx),abs(ax-bx))
dy=min(h-abs(ay-by),abs(ay-by))
ans=1
if dx*2==w:ans*=2
if dy*2==h:ans*=2
ans*=comb(dx+dy,dx)
print(ans%100000007)
``` | instruction | 0 | 57,948 | 15 | 115,896 |
No | output | 1 | 57,948 | 15 | 115,897 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Two coordinates (a1, a2) and (b1, b2) on a two-dimensional grid of r × c are given. The cost of moving from a cell (e, f) to one of the cells (e + 1, f), (e-1, f), (e, f + 1), (e, f-1) is 1. And. You can also move between (e, c-1) and (e, 0), and between (r-1, f) and (0, f) at a cost of 1. At this time, find the number of routes that can be moved from the first coordinate to the second coordinate at the shortest cost.
Input
The input is given in the following format.
r c a1 a2 b1 b2
Input meets the following constraints
1 ≤ r, c ≤ 1,000
0 ≤ a1, b1 <r
0 ≤ a2, b2 <c
Output
Output the remainder of the answer value divided by 100,000,007.
Examples
Input
4 4 0 0 3 3
Output
2
Input
4 4 0 0 1 1
Output
2
Input
2 3 0 0 1 2
Output
4
Input
500 500 0 0 200 200
Output
34807775
Submitted Solution:
```
from math import factorial
def comb (x,y):
return factorial(x)//factorial(x-y)//factorial(y)
w,h,ax,ay,bx,by=map(int,input().split())
dx=abs(ax-bx)
dx=min(dx,w-dx)
dy=abs(ay-by)
dy=min(dy,h-dy)
an=1
if dx*2==w:an*=2
if dy*2==h:an*=2
an*=comb(dx+dy,dx)
print(an%int(1E9+7))
``` | instruction | 0 | 57,949 | 15 | 115,898 |
No | output | 1 | 57,949 | 15 | 115,899 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Two coordinates (a1, a2) and (b1, b2) on a two-dimensional grid of r × c are given. The cost of moving from a cell (e, f) to one of the cells (e + 1, f), (e-1, f), (e, f + 1), (e, f-1) is 1. And. You can also move between (e, c-1) and (e, 0), and between (r-1, f) and (0, f) at a cost of 1. At this time, find the number of routes that can be moved from the first coordinate to the second coordinate at the shortest cost.
Input
The input is given in the following format.
r c a1 a2 b1 b2
Input meets the following constraints
1 ≤ r, c ≤ 1,000
0 ≤ a1, b1 <r
0 ≤ a2, b2 <c
Output
Output the remainder of the answer value divided by 100,000,007.
Examples
Input
4 4 0 0 3 3
Output
2
Input
4 4 0 0 1 1
Output
2
Input
2 3 0 0 1 2
Output
4
Input
500 500 0 0 200 200
Output
34807775
Submitted Solution:
```
import math
def comb (x,y):
return factorial(x)//factorial(x-y)//factorial(y)
w,h,ax,ay,bx,by=map(int,input().split())
dx=min(w-abs(ax-bx),abs(ax-bx))
dy=min(h-abs(ay-by),abs(ay-by))
ans=1
if dx*2==w:ans*=2
if dy*2==h:ans*=2
ans*=comb(dx+dy,dx)
print(ans%100000007)
``` | instruction | 0 | 57,950 | 15 | 115,900 |
No | output | 1 | 57,950 | 15 | 115,901 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let’s define a grid to be a set of tiles with 2 rows and 13 columns. Each tile has an English letter written in it. The letters don't have to be unique: there might be two or more tiles with the same letter written on them. Here is an example of a grid:
ABCDEFGHIJKLM
NOPQRSTUVWXYZ
We say that two tiles are adjacent if they share a side or a corner. In the example grid above, the tile with the letter 'A' is adjacent only to the tiles with letters 'B', 'N', and 'O'. A tile is not adjacent to itself.
A sequence of tiles is called a path if each tile in the sequence is adjacent to the tile which follows it (except for the last tile in the sequence, which of course has no successor). In this example, "ABC" is a path, and so is "KXWIHIJK". "MAB" is not a path because 'M' is not adjacent to 'A'. A single tile can be used more than once by a path (though the tile cannot occupy two consecutive places in the path because no tile is adjacent to itself).
You’re given a string s which consists of 27 upper-case English letters. Each English letter occurs at least once in s. Find a grid that contains a path whose tiles, viewed in the order that the path visits them, form the string s. If there’s no solution, print "Impossible" (without the quotes).
Input
The only line of the input contains the string s, consisting of 27 upper-case English letters. Each English letter occurs at least once in s.
Output
Output two lines, each consisting of 13 upper-case English characters, representing the rows of the grid. If there are multiple solutions, print any of them. If there is no solution print "Impossible".
Examples
Input
ABCDEFGHIJKLMNOPQRSGTUVWXYZ
Output
YXWVUTGHIJKLM
ZABCDEFSRQPON
Input
BUVTYZFQSNRIWOXXGJLKACPEMDH
Output
Impossible
Submitted Solution:
```
# fin = open("input.txt", "r");
# s = fin.readline().strip();
# fin.close();
s = input().strip();
length = 27;
d = {};
firsti, secondi = -1, -1;
for i in range(length):
if s[i] in d:
firsti = d[s[i]];
secondi = i;
break;
d[s[i]] = i;
if secondi - firsti > 1:
table = [[0]*13, [0]*13];
blen = secondi - firsti;
midindx = 13 - (blen//2 + blen%2);
for i in range(blen//2 + blen%2):
table[0][midindx + i] = s[firsti + i];
tmpindx = firsti + (blen//2 + blen%2);
for i in range(blen//2):
table[1][12 - i] = s[tmpindx + i];
clen = 26 - secondi;
alen = firsti;
curindx = -1;
curlen = 0;
step = -1;
if clen > alen:
curindx = secondi + 1;
curlen = clen;
step = 1;
else:
curindx = firsti - 1;
curlen = alen;
step = -1;
direction = -1;
indx1 = 1;
indx2 = midindx - 1 + (blen % 2);
while curindx >= 0 and curindx < 27 and indx2 >= 0 and indx2 < 13:
table[indx1][indx2] = s[curindx];
curindx += step;
if indx2 == 0 and direction == -1:
direction = 1;
else:
indx2 += direction;
indx1 = (indx1 + 1) % 2;
if clen > alen:
curindx = firsti - 1;
curlen = alen;
step = -1;
else:
curindx = secondi + 1;
curlen = clen;
step = 1;
direction = -1;
indx1 = (blen % 2);
indx2 = midindx - 1;
while curindx >= 0 and curindx < 27 and indx2 >= 0 and indx2 < 13:
table[indx1][indx2] = s[curindx];
curindx += step;
if indx2 == 0 and direction == -1:
direction = 1;
else:
indx2 += direction;
indx1 = (indx1 + 1) % 2;
print("".join(table[0]));
print("".join(table[1]));
else:
print("Impossible");
``` | instruction | 0 | 58,525 | 15 | 117,050 |
Yes | output | 1 | 58,525 | 15 | 117,051 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let’s define a grid to be a set of tiles with 2 rows and 13 columns. Each tile has an English letter written in it. The letters don't have to be unique: there might be two or more tiles with the same letter written on them. Here is an example of a grid:
ABCDEFGHIJKLM
NOPQRSTUVWXYZ
We say that two tiles are adjacent if they share a side or a corner. In the example grid above, the tile with the letter 'A' is adjacent only to the tiles with letters 'B', 'N', and 'O'. A tile is not adjacent to itself.
A sequence of tiles is called a path if each tile in the sequence is adjacent to the tile which follows it (except for the last tile in the sequence, which of course has no successor). In this example, "ABC" is a path, and so is "KXWIHIJK". "MAB" is not a path because 'M' is not adjacent to 'A'. A single tile can be used more than once by a path (though the tile cannot occupy two consecutive places in the path because no tile is adjacent to itself).
You’re given a string s which consists of 27 upper-case English letters. Each English letter occurs at least once in s. Find a grid that contains a path whose tiles, viewed in the order that the path visits them, form the string s. If there’s no solution, print "Impossible" (without the quotes).
Input
The only line of the input contains the string s, consisting of 27 upper-case English letters. Each English letter occurs at least once in s.
Output
Output two lines, each consisting of 13 upper-case English characters, representing the rows of the grid. If there are multiple solutions, print any of them. If there is no solution print "Impossible".
Examples
Input
ABCDEFGHIJKLMNOPQRSGTUVWXYZ
Output
YXWVUTGHIJKLM
ZABCDEFSRQPON
Input
BUVTYZFQSNRIWOXXGJLKACPEMDH
Output
Impossible
Submitted Solution:
```
from collections import Counter
def display(r1, r2):
if len(r1) > 13:
r1, r2 = r1[:13], r2 + (r1[13:])[::-1]
elif len(r2) > 13:
r1, r2 = r1 + (r2[13:])[::-1], r2[:13]
print(r1)
print(r2)
def main():
s = input()
counter = Counter(s)
double = None
for c in s:
if counter[c] == 2:
double = c
a = s.index(double)
b = s.index(double, a + 1)
if a + 1 == b:
print("Impossible")
return
grid = [['0' for _ in range(13)] for _ in range(2)]
first = s[:a]
second = s[b + 1:]
middle_chunk = s[a + 1:b]
mid_len = len(middle_chunk)
if mid_len == 1:
display(double + first[::-1], middle_chunk + second)
return
else:
# otherwise we have at least 4 edges to consider
cols = mid_len // 2
r1 = (middle_chunk[:cols])[::-1] + double + first[::-1]
r2 = middle_chunk[cols:] + second
display(r1, r2)
main()
``` | instruction | 0 | 58,526 | 15 | 117,052 |
Yes | output | 1 | 58,526 | 15 | 117,053 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let’s define a grid to be a set of tiles with 2 rows and 13 columns. Each tile has an English letter written in it. The letters don't have to be unique: there might be two or more tiles with the same letter written on them. Here is an example of a grid:
ABCDEFGHIJKLM
NOPQRSTUVWXYZ
We say that two tiles are adjacent if they share a side or a corner. In the example grid above, the tile with the letter 'A' is adjacent only to the tiles with letters 'B', 'N', and 'O'. A tile is not adjacent to itself.
A sequence of tiles is called a path if each tile in the sequence is adjacent to the tile which follows it (except for the last tile in the sequence, which of course has no successor). In this example, "ABC" is a path, and so is "KXWIHIJK". "MAB" is not a path because 'M' is not adjacent to 'A'. A single tile can be used more than once by a path (though the tile cannot occupy two consecutive places in the path because no tile is adjacent to itself).
You’re given a string s which consists of 27 upper-case English letters. Each English letter occurs at least once in s. Find a grid that contains a path whose tiles, viewed in the order that the path visits them, form the string s. If there’s no solution, print "Impossible" (without the quotes).
Input
The only line of the input contains the string s, consisting of 27 upper-case English letters. Each English letter occurs at least once in s.
Output
Output two lines, each consisting of 13 upper-case English characters, representing the rows of the grid. If there are multiple solutions, print any of them. If there is no solution print "Impossible".
Examples
Input
ABCDEFGHIJKLMNOPQRSGTUVWXYZ
Output
YXWVUTGHIJKLM
ZABCDEFSRQPON
Input
BUVTYZFQSNRIWOXXGJLKACPEMDH
Output
Impossible
Submitted Solution:
```
import sys
s = input()
possible = False
d = {}
l, r = -1, -1
for i in range(len(s)):
if not s[i] in d:
d[s[i]] = []
d[s[i]].append(i)
for i, v in d.items():
if max(v) - min(v) > 1:
l, r = min(v), max(v)
possible = True
if not possible:
print('Impossible')
sys.exit(0)
g = [['' for _ in range(13)] for _ in range(2)]
k = (r - l - 1) // 2
g[0][k] = s[l]
for i in range(1, k + 1):
g[0][k - i] = s[l + i]
for i in range(r - l - k - 1):
g[1][i] = s[l + k + i + 1]
h = s[:l][::-1] + s[r + 1:][::-1]
i = 0
while k + i + 1 < 13:
g[0][k + i + 1] = h[i]
i += 1
j = 12
while j >= 0 and g[1][j] == '':
g[1][j] = h[i]
i += 1
j -= 1
print(''.join(g[0]))
print(''.join(g[1]))
``` | instruction | 0 | 58,527 | 15 | 117,054 |
Yes | output | 1 | 58,527 | 15 | 117,055 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let’s define a grid to be a set of tiles with 2 rows and 13 columns. Each tile has an English letter written in it. The letters don't have to be unique: there might be two or more tiles with the same letter written on them. Here is an example of a grid:
ABCDEFGHIJKLM
NOPQRSTUVWXYZ
We say that two tiles are adjacent if they share a side or a corner. In the example grid above, the tile with the letter 'A' is adjacent only to the tiles with letters 'B', 'N', and 'O'. A tile is not adjacent to itself.
A sequence of tiles is called a path if each tile in the sequence is adjacent to the tile which follows it (except for the last tile in the sequence, which of course has no successor). In this example, "ABC" is a path, and so is "KXWIHIJK". "MAB" is not a path because 'M' is not adjacent to 'A'. A single tile can be used more than once by a path (though the tile cannot occupy two consecutive places in the path because no tile is adjacent to itself).
You’re given a string s which consists of 27 upper-case English letters. Each English letter occurs at least once in s. Find a grid that contains a path whose tiles, viewed in the order that the path visits them, form the string s. If there’s no solution, print "Impossible" (without the quotes).
Input
The only line of the input contains the string s, consisting of 27 upper-case English letters. Each English letter occurs at least once in s.
Output
Output two lines, each consisting of 13 upper-case English characters, representing the rows of the grid. If there are multiple solutions, print any of them. If there is no solution print "Impossible".
Examples
Input
ABCDEFGHIJKLMNOPQRSGTUVWXYZ
Output
YXWVUTGHIJKLM
ZABCDEFSRQPON
Input
BUVTYZFQSNRIWOXXGJLKACPEMDH
Output
Impossible
Submitted Solution:
```
s = input()
gx = None
for i in range(26):
x = chr(ord('A') + i)
if s.count(x) == 2:
gx = x
x = gx
sz = s.rindex(x) - s.index(x) - 1
ss = s[s.index(x) + 1:s.rindex(x)]
if sz == 0:
print('Impossible')
else:
cols = sz // 2
ans = [[None for _ in range(13)], [None for _ in range(13)]]
st = 0
for i in range(cols - 1, -1, -1):
ans[0][st] = ss[i]
ans[1][st] = ss[sz - i - 1]
st += 1
ans[0][cols] = gx
if sz & 1:
ans[1] = [ss[cols]] + ans[1][:-1]
w = 0
idx = cols + 1
for c in (s[:s.index(x)])[::-1]:
if idx == 13:
idx = 12
w = 1
if w == 0:
ans[0][idx] = c
idx += 1
else:
ans[1][idx] = c
idx -= 1
w = 0
idx = cols if sz % 2 == 0 else cols + 1
for c in s[s.rindex(x) + 1:]:
if idx == 13:
idx = 12
w = 1
if w == 0:
ans[1][idx] = c
idx += 1
else:
ans[0][idx] = c
idx -= 1
print(''.join(ans[0]))
print(''.join(ans[1]))
``` | instruction | 0 | 58,528 | 15 | 117,056 |
Yes | output | 1 | 58,528 | 15 | 117,057 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let’s define a grid to be a set of tiles with 2 rows and 13 columns. Each tile has an English letter written in it. The letters don't have to be unique: there might be two or more tiles with the same letter written on them. Here is an example of a grid:
ABCDEFGHIJKLM
NOPQRSTUVWXYZ
We say that two tiles are adjacent if they share a side or a corner. In the example grid above, the tile with the letter 'A' is adjacent only to the tiles with letters 'B', 'N', and 'O'. A tile is not adjacent to itself.
A sequence of tiles is called a path if each tile in the sequence is adjacent to the tile which follows it (except for the last tile in the sequence, which of course has no successor). In this example, "ABC" is a path, and so is "KXWIHIJK". "MAB" is not a path because 'M' is not adjacent to 'A'. A single tile can be used more than once by a path (though the tile cannot occupy two consecutive places in the path because no tile is adjacent to itself).
You’re given a string s which consists of 27 upper-case English letters. Each English letter occurs at least once in s. Find a grid that contains a path whose tiles, viewed in the order that the path visits them, form the string s. If there’s no solution, print "Impossible" (without the quotes).
Input
The only line of the input contains the string s, consisting of 27 upper-case English letters. Each English letter occurs at least once in s.
Output
Output two lines, each consisting of 13 upper-case English characters, representing the rows of the grid. If there are multiple solutions, print any of them. If there is no solution print "Impossible".
Examples
Input
ABCDEFGHIJKLMNOPQRSGTUVWXYZ
Output
YXWVUTGHIJKLM
ZABCDEFSRQPON
Input
BUVTYZFQSNRIWOXXGJLKACPEMDH
Output
Impossible
Submitted Solution:
```
from collections import Counter
from sys import *
s = input()
for i in range(1, len(s)):
if s[i] == s[i - 1]:
print("Impossible")
exit()
a = Counter()
for i in range(len(s)):
a[s[i]] += 1
elem = ''
for elem1 in a:
if a[elem1] == 2:
elem = elem1
index = s.find(elem)
print(s[:13])
ans = ""
for i in range(13, 27):
if s[i] != elem:
ans += s[i]
print(ans[::-1])
``` | instruction | 0 | 58,529 | 15 | 117,058 |
No | output | 1 | 58,529 | 15 | 117,059 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let’s define a grid to be a set of tiles with 2 rows and 13 columns. Each tile has an English letter written in it. The letters don't have to be unique: there might be two or more tiles with the same letter written on them. Here is an example of a grid:
ABCDEFGHIJKLM
NOPQRSTUVWXYZ
We say that two tiles are adjacent if they share a side or a corner. In the example grid above, the tile with the letter 'A' is adjacent only to the tiles with letters 'B', 'N', and 'O'. A tile is not adjacent to itself.
A sequence of tiles is called a path if each tile in the sequence is adjacent to the tile which follows it (except for the last tile in the sequence, which of course has no successor). In this example, "ABC" is a path, and so is "KXWIHIJK". "MAB" is not a path because 'M' is not adjacent to 'A'. A single tile can be used more than once by a path (though the tile cannot occupy two consecutive places in the path because no tile is adjacent to itself).
You’re given a string s which consists of 27 upper-case English letters. Each English letter occurs at least once in s. Find a grid that contains a path whose tiles, viewed in the order that the path visits them, form the string s. If there’s no solution, print "Impossible" (without the quotes).
Input
The only line of the input contains the string s, consisting of 27 upper-case English letters. Each English letter occurs at least once in s.
Output
Output two lines, each consisting of 13 upper-case English characters, representing the rows of the grid. If there are multiple solutions, print any of them. If there is no solution print "Impossible".
Examples
Input
ABCDEFGHIJKLMNOPQRSGTUVWXYZ
Output
YXWVUTGHIJKLM
ZABCDEFSRQPON
Input
BUVTYZFQSNRIWOXXGJLKACPEMDH
Output
Impossible
Submitted Solution:
```
from collections import Counter
from sys import *
s = input()
for i in range(1, len(s)):
if s[i] == s[i - 1]:
print("Impossible")
exit()
a = Counter()
for i in range(len(s)):
a[s[i]] += 1
elem = ''
for elem1 in a:
if a[elem1] == 2:
elem = elem1
index = s.find(elem)
ans = ""
c = 0
for i in range(13):
if s[i] != elem:
ans += s[i]
elif s[i] == elem and c == 0:
ans += s[i]
c += 1
else:
c += 1
st = 13
if c == 2:
st += 1
ans += s[13]
print(ans)
ans = ""
for i in range(st, 27):
if s[i] != elem:
ans += s[i]
print(ans[::-1])
``` | instruction | 0 | 58,530 | 15 | 117,060 |
No | output | 1 | 58,530 | 15 | 117,061 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let’s define a grid to be a set of tiles with 2 rows and 13 columns. Each tile has an English letter written in it. The letters don't have to be unique: there might be two or more tiles with the same letter written on them. Here is an example of a grid:
ABCDEFGHIJKLM
NOPQRSTUVWXYZ
We say that two tiles are adjacent if they share a side or a corner. In the example grid above, the tile with the letter 'A' is adjacent only to the tiles with letters 'B', 'N', and 'O'. A tile is not adjacent to itself.
A sequence of tiles is called a path if each tile in the sequence is adjacent to the tile which follows it (except for the last tile in the sequence, which of course has no successor). In this example, "ABC" is a path, and so is "KXWIHIJK". "MAB" is not a path because 'M' is not adjacent to 'A'. A single tile can be used more than once by a path (though the tile cannot occupy two consecutive places in the path because no tile is adjacent to itself).
You’re given a string s which consists of 27 upper-case English letters. Each English letter occurs at least once in s. Find a grid that contains a path whose tiles, viewed in the order that the path visits them, form the string s. If there’s no solution, print "Impossible" (without the quotes).
Input
The only line of the input contains the string s, consisting of 27 upper-case English letters. Each English letter occurs at least once in s.
Output
Output two lines, each consisting of 13 upper-case English characters, representing the rows of the grid. If there are multiple solutions, print any of them. If there is no solution print "Impossible".
Examples
Input
ABCDEFGHIJKLMNOPQRSGTUVWXYZ
Output
YXWVUTGHIJKLM
ZABCDEFSRQPON
Input
BUVTYZFQSNRIWOXXGJLKACPEMDH
Output
Impossible
Submitted Solution:
```
from collections import Counter
a = Counter()
s = input()
for i in range(len(s)):
a[s[i]] += 1
elem = ''
for elem1 in a:
if a[elem1] == 2:
elem = elem1
if abs(s.find(elem) + 1 - 27 + s.rfind(elem)) > 1:
print("Impossible")
else:
print(s[:13])
ans = ""
for i in range(13, 27):
if s[i] != elem:
ans += s[i]
print(ans[::-1])
``` | instruction | 0 | 58,531 | 15 | 117,062 |
No | output | 1 | 58,531 | 15 | 117,063 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let’s define a grid to be a set of tiles with 2 rows and 13 columns. Each tile has an English letter written in it. The letters don't have to be unique: there might be two or more tiles with the same letter written on them. Here is an example of a grid:
ABCDEFGHIJKLM
NOPQRSTUVWXYZ
We say that two tiles are adjacent if they share a side or a corner. In the example grid above, the tile with the letter 'A' is adjacent only to the tiles with letters 'B', 'N', and 'O'. A tile is not adjacent to itself.
A sequence of tiles is called a path if each tile in the sequence is adjacent to the tile which follows it (except for the last tile in the sequence, which of course has no successor). In this example, "ABC" is a path, and so is "KXWIHIJK". "MAB" is not a path because 'M' is not adjacent to 'A'. A single tile can be used more than once by a path (though the tile cannot occupy two consecutive places in the path because no tile is adjacent to itself).
You’re given a string s which consists of 27 upper-case English letters. Each English letter occurs at least once in s. Find a grid that contains a path whose tiles, viewed in the order that the path visits them, form the string s. If there’s no solution, print "Impossible" (without the quotes).
Input
The only line of the input contains the string s, consisting of 27 upper-case English letters. Each English letter occurs at least once in s.
Output
Output two lines, each consisting of 13 upper-case English characters, representing the rows of the grid. If there are multiple solutions, print any of them. If there is no solution print "Impossible".
Examples
Input
ABCDEFGHIJKLMNOPQRSGTUVWXYZ
Output
YXWVUTGHIJKLM
ZABCDEFSRQPON
Input
BUVTYZFQSNRIWOXXGJLKACPEMDH
Output
Impossible
Submitted Solution:
```
s = input()
for c in s:
if s.count(c) == 2:
break
i = s.index(c)
j = i + 1 + s[i+1:].index(c)
if i + 1 == j:
print("Impossible")
s = s.replace(c, "", 1)*3
mid = 13+(i+j)//2
print(s[mid-13:mid])
print(s[mid:mid+13][::-1])
``` | instruction | 0 | 58,532 | 15 | 117,064 |
No | output | 1 | 58,532 | 15 | 117,065 |
Provide a correct Python 3 solution for this coding contest problem.
Problem Statement
There is a maze which can be described as a W \times H grid. The upper-left cell is denoted as (1, 1), and the lower-right cell is (W, H). You are now at the cell (1, 1) and have to go to the cell (W, H). However, you can only move to the right adjacent cell or to the lower adjacent cell. The following figure is an example of a maze.
...#......
a###.#####
.bc...A...
.#C#d#.#
.#B#.#.###
.#...#e.D.
.#A..###.#
..e.c#..E.
d###.#
....#.#.#
E...d.C.
In the maze, some cells are free (denoted by `.`) and some cells are occupied by rocks (denoted by `#`), where you cannot enter. Also there are jewels (denoted by lowercase alphabets) in some of the free cells and holes to place jewels (denoted by uppercase alphabets). Different alphabets correspond to different types of jewels, i.e. a cell denoted by `a` contains a jewel of type A, and a cell denoted by `A` contains a hole to place a jewel of type A. It is said that, when we place a jewel to a corresponding hole, something happy will happen.
At the cells with jewels, you can choose whether you pick a jewel or not. Similarly, at the cells with holes, you can choose whether you place a jewel you have or not. Initially you do not have any jewels. You have a very big bag, so you can bring arbitrarily many jewels. However, your bag is a stack, that is, you can only place the jewel that you picked up last.
On the way from cell (1, 1) to cell (W, H), how many jewels can you place to correct holes?
Input
The input contains a sequence of datasets. The end of the input is indicated by a line containing two zeroes. Each dataset is formatted as follows.
H W
C_{11} C_{12} ... C_{1W}
C_{21} C_{22} ... C_{2W}
...
C_{H1} C_{H2} ... C_{HW}
Here, H and W are the height and width of the grid. You may assume 1 \leq W, H \leq 50. The rest of the datasets consists of H lines, each of which is composed of W letters. Each letter C_{ij} specifies the type of the cell (i, j) as described before. It is guaranteed that C_{11} and C_{WH} are never `#`.
You may also assume that each lowercase or uppercase alphabet appear at most 10 times in each dataset.
Output
For each dataset, output the maximum number of jewels that you can place to corresponding holes. If you cannot reach the cell (W, H), output -1.
Examples
Input
3 3
ac#
b#C
.BA
3 3
aaZ
a#Z
aZZ
3 3
..#
.#.
#..
1 50
abcdefghijklmnopqrstuvwxyYXWVUTSRQPONMLKJIHGFEDCBA
1 50
aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyY
1 50
abcdefghijklmnopqrstuvwxyABCDEFGHIJKLMNOPQRSTUVWXY
1 50
aaaaaaaaaabbbbbbbbbbcccccCCCCCBBBBBBBBBBAAAAAAAAAA
10 10
...#......
a###.#####
.bc...A...
##.#C#d#.#
.#B#.#.###
.#...#e.D.
.#A..###.#
..e.c#..E.
####d###.#
##E...D.C.
0 0
Output
2
0
-1
25
25
1
25
4
Input
3 3
ac#
b#C
.BA
3 3
aaZ
a#Z
aZZ
3 3
..#
.#.
..
1 50
abcdefghijklmnopqrstuvwxyYXWVUTSRQPONMLKJIHGFEDCBA
1 50
aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyY
1 50
abcdefghijklmnopqrstuvwxyABCDEFGHIJKLMNOPQRSTUVWXY
1 50
aaaaaaaaaabbbbbbbbbbcccccCCCCCBBBBBBBBBBAAAAAAAAAA
10 10
...#......
a###.#####
.bc...A...
.#C#d#.#
.#B#.#.###
.#...#e.D.
.#A..###.#
..e.c#..E.
d###.#
E...D.C.
0 0
Output
2
0
-1
25
25
1
25
4 | instruction | 0 | 58,802 | 15 | 117,604 |
"Correct Solution:
```
import sys
readline = sys.stdin.readline
write = sys.stdout.write
from string import ascii_lowercase, ascii_uppercase
from collections import deque
dd = ((1, 0), (0, 1))
def solve():
H, W = map(int, readline().split())
if H == W == 0:
return False
C = [readline().strip() for i in range(H)]
INF = 10**9
def search(x0, y0, c):
que = deque([(x0, y0)])
used = [[0]*W for i in range(H)]
used[y0][x0] = 1
r = []
while que:
x, y = que.popleft()
if C[y][x] == c:
r.append((x, y))
if x+1 < W and C[y][x+1] != '#' and not used[y][x+1]:
que.append((x+1, y))
used[y][x+1] = 1
if y+1 < H and C[y+1][x] != '#' and not used[y+1][x]:
que.append((x, y+1))
used[y+1][x] = 1
return r, used
D0, U0 = search(0, 0, None)
if not U0[H-1][W-1]:
write("-1\n")
return True
D = [[None]*W for i in range(H)]
U = [[None]*W for i in range(H)]
K = [[-1]*W for i in range(H)]
for i in range(H):
for j in range(W):
d = C[i][j]
if d == '#':
continue
k = ascii_lowercase.find(C[i][j])
c = ascii_uppercase[k] if k != -1 else None
D[i][j], U[i][j] = search(j, i, c)
K[i][j] = k
dp = [[[[0]*W for i in range(H)] for j in range(W)] for k in range(H)]
for i0 in range(H-1, -1, -1):
for j0 in range(W-1, -1, -1):
if C[i0][j0] == '#':
continue
k = K[i0][j0]
for i1 in range(H-1, i0-1, -1):
for j1 in range(W-1, j0-1, -1):
if not U[i0][j0][i1][j1]:
continue
r = max(
(dp[i0+1][j0][i1][j1] if i0+1 <= i1 and C[i0+1][j0] != '#' else 0),
(dp[i0][j0+1][i1][j1] if j0+1 <= j1 and C[i0][j0+1] != '#' else 0),
)
if k != -1:
for x, y in D[i0][j0]:
if not i0 <= y <= i1 or not j0 <= x <= j1 or not U[i0][j0][y][x] or not U[y][x][i1][j1]:
continue
if (x-j0)+(y-i0) == 1:
A = 1
else:
if i0 == y:
A = dp[i0][j0+1][y][x-1] + 1
elif j0 == x:
A = dp[i0+1][j0][y-1][x] + 1
else:
A = max(
dp[i0+1][j0][y][x-1],
dp[i0][j0+1][y-1][x],
dp[i0+1][j0][y-1][x] if i0+1 <= y-1 else 0,
dp[i0][j0+1][y][x-1] if j0+1 <= x-1 else 0,
) + 1
r = max(r, A + dp[y][x][i1][j1])
dp[i0][j0][i1][j1] = r
write("%d\n" % dp[0][0][H-1][W-1])
return True
while solve():
...
``` | output | 1 | 58,802 | 15 | 117,605 |
Provide a correct Python 3 solution for this coding contest problem.
We have a rectangular grid of squares 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. On this grid, there is a piece, which is initially placed at square (s_r,s_c).
Takahashi and Aoki will play a game, where each player has a string of length N. Takahashi's string is S, and Aoki's string is T. S and T both consist of four kinds of letters: `L`, `R`, `U` and `D`.
The game consists of N steps. The i-th step proceeds as follows:
* First, Takahashi performs a move. He either moves the piece in the direction of S_i, or does not move the piece.
* Second, Aoki performs a move. He either moves the piece in the direction of T_i, or does not move the piece.
Here, to move the piece in the direction of `L`, `R`, `U` and `D`, is to move the piece from square (r,c) to square (r,c-1), (r,c+1), (r-1,c) and (r+1,c), respectively. If the destination square does not exist, the piece is removed from the grid, and the game ends, even if less than N steps are done.
Takahashi wants to remove the piece from the grid in one of the N steps. Aoki, on the other hand, wants to finish the N steps with the piece remaining on the grid. Determine if the piece will remain on the grid at the end of the game when both players play optimally.
Constraints
* 2 \leq H,W \leq 2 \times 10^5
* 2 \leq N \leq 2 \times 10^5
* 1 \leq s_r \leq H
* 1 \leq s_c \leq W
* |S|=|T|=N
* S and T consists of the four kinds of letters `L`, `R`, `U` and `D`.
Input
Input is given from Standard Input in the following format:
H W N
s_r s_c
S
T
Output
If the piece will remain on the grid at the end of the game, print `YES`; otherwise, print `NO`.
Examples
Input
2 3 3
2 2
RRL
LUD
Output
YES
Input
4 3 5
2 2
UDRRR
LLDUD
Output
NO
Input
5 6 11
2 1
RLDRRUDDLRL
URRDRLLDLRD
Output
NO | instruction | 0 | 59,465 | 15 | 118,930 |
"Correct Solution:
```
h, w, n = map(int, input().split())
sr, sc = map(int, input().split())
S = input()
T = input()
win = 0
SR = sr
SC = sc
sr = SR
sc = SC
for i in range(n):
if S[i] == "L":
sc -= 1
if sc <= 0:
break
if T[i] == "R" and sc < w:
sc += 1
if sc <= 0:
win = 1
sr = SR
sc = SC
for i in range(n):
if S[i] == "R":
sc += 1
if sc > w:
break
if T[i] == "L" and sc > 1:
sc -= 1
if sc > w:
win = 1
sr = SR
sc = SC
for i in range(n):
if S[i] == "D":
sr += 1
if sr > h:
break
if T[i] == "U" and sr > 1:
sr -= 1
if sr > h:
win = 1
sr = SR
sc = SC
for i in range(n):
if S[i] == "U":
sr -= 1
if sr <= 0:
break
if T[i] == "D" and sr < h:
sr += 1
if sr <= 0:
win = 1
if win:
print("NO")
else:
print("YES")
``` | output | 1 | 59,465 | 15 | 118,931 |
Provide a correct Python 3 solution for this coding contest problem.
We have a rectangular grid of squares 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. On this grid, there is a piece, which is initially placed at square (s_r,s_c).
Takahashi and Aoki will play a game, where each player has a string of length N. Takahashi's string is S, and Aoki's string is T. S and T both consist of four kinds of letters: `L`, `R`, `U` and `D`.
The game consists of N steps. The i-th step proceeds as follows:
* First, Takahashi performs a move. He either moves the piece in the direction of S_i, or does not move the piece.
* Second, Aoki performs a move. He either moves the piece in the direction of T_i, or does not move the piece.
Here, to move the piece in the direction of `L`, `R`, `U` and `D`, is to move the piece from square (r,c) to square (r,c-1), (r,c+1), (r-1,c) and (r+1,c), respectively. If the destination square does not exist, the piece is removed from the grid, and the game ends, even if less than N steps are done.
Takahashi wants to remove the piece from the grid in one of the N steps. Aoki, on the other hand, wants to finish the N steps with the piece remaining on the grid. Determine if the piece will remain on the grid at the end of the game when both players play optimally.
Constraints
* 2 \leq H,W \leq 2 \times 10^5
* 2 \leq N \leq 2 \times 10^5
* 1 \leq s_r \leq H
* 1 \leq s_c \leq W
* |S|=|T|=N
* S and T consists of the four kinds of letters `L`, `R`, `U` and `D`.
Input
Input is given from Standard Input in the following format:
H W N
s_r s_c
S
T
Output
If the piece will remain on the grid at the end of the game, print `YES`; otherwise, print `NO`.
Examples
Input
2 3 3
2 2
RRL
LUD
Output
YES
Input
4 3 5
2 2
UDRRR
LLDUD
Output
NO
Input
5 6 11
2 1
RLDRRUDDLRL
URRDRLLDLRD
Output
NO | instruction | 0 | 59,466 | 15 | 118,932 |
"Correct Solution:
```
h,w,n = map(int,input().split())
y,x = map(int,input().split())
s = input()[::-1]
t = input()[::-1]
r = w
l = 1
d = h
u = 1
for i in range(n):
if t[i] == "R":
l = max(1,l-1)
elif t[i] == "L":
r = min(w,r+1)
elif t[i] == "D":
u = max(1,u-1)
else:
d = min(h,d+1)
if s[i] == "R":
r -= 1
elif s[i] == "L":
l += 1
elif s[i] == "D":
d -= 1
else:
u += 1
if r < l or d < u:
print("NO")
exit()
if l <= x <= r and u <= y <= d:
print("YES")
else:
print("NO")
``` | output | 1 | 59,466 | 15 | 118,933 |
Provide a correct Python 3 solution for this coding contest problem.
We have a rectangular grid of squares 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. On this grid, there is a piece, which is initially placed at square (s_r,s_c).
Takahashi and Aoki will play a game, where each player has a string of length N. Takahashi's string is S, and Aoki's string is T. S and T both consist of four kinds of letters: `L`, `R`, `U` and `D`.
The game consists of N steps. The i-th step proceeds as follows:
* First, Takahashi performs a move. He either moves the piece in the direction of S_i, or does not move the piece.
* Second, Aoki performs a move. He either moves the piece in the direction of T_i, or does not move the piece.
Here, to move the piece in the direction of `L`, `R`, `U` and `D`, is to move the piece from square (r,c) to square (r,c-1), (r,c+1), (r-1,c) and (r+1,c), respectively. If the destination square does not exist, the piece is removed from the grid, and the game ends, even if less than N steps are done.
Takahashi wants to remove the piece from the grid in one of the N steps. Aoki, on the other hand, wants to finish the N steps with the piece remaining on the grid. Determine if the piece will remain on the grid at the end of the game when both players play optimally.
Constraints
* 2 \leq H,W \leq 2 \times 10^5
* 2 \leq N \leq 2 \times 10^5
* 1 \leq s_r \leq H
* 1 \leq s_c \leq W
* |S|=|T|=N
* S and T consists of the four kinds of letters `L`, `R`, `U` and `D`.
Input
Input is given from Standard Input in the following format:
H W N
s_r s_c
S
T
Output
If the piece will remain on the grid at the end of the game, print `YES`; otherwise, print `NO`.
Examples
Input
2 3 3
2 2
RRL
LUD
Output
YES
Input
4 3 5
2 2
UDRRR
LLDUD
Output
NO
Input
5 6 11
2 1
RLDRRUDDLRL
URRDRLLDLRD
Output
NO | instruction | 0 | 59,467 | 15 | 118,934 |
"Correct Solution:
```
h,w,n=[int(j) for j in input().split()]
R,C=[int(j) for j in input().split()]
s=input()
t=input()
#LR
x=C
l,r=1,w
for i in range(n)[::-1]:
if t[i]=="R":
l-=1
elif t[i]=="L":
r+=1
if l==0:
l=1
if r>w:
r=w
if s[i]=="R":
r-=1
elif s[i]=="L":
l+=1
if l>r:
print("NO")
exit()
if not l<=x<=r:
print("NO")
exit()
#UD
x=h+1-R
l,r=1,h
for i in range(n)[::-1]:
if t[i]=="U":
l-=1
elif t[i]=="D":
r+=1
if l==0:
l=1
if r>h:
r=h
if s[i]=="U":
r-=1
elif s[i]=="D":
l+=1
if l>r:
print("NO")
exit()
if not l<=x<=r:
print("NO")
exit()
print("YES")
``` | output | 1 | 59,467 | 15 | 118,935 |
Provide a correct Python 3 solution for this coding contest problem.
We have a rectangular grid of squares 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. On this grid, there is a piece, which is initially placed at square (s_r,s_c).
Takahashi and Aoki will play a game, where each player has a string of length N. Takahashi's string is S, and Aoki's string is T. S and T both consist of four kinds of letters: `L`, `R`, `U` and `D`.
The game consists of N steps. The i-th step proceeds as follows:
* First, Takahashi performs a move. He either moves the piece in the direction of S_i, or does not move the piece.
* Second, Aoki performs a move. He either moves the piece in the direction of T_i, or does not move the piece.
Here, to move the piece in the direction of `L`, `R`, `U` and `D`, is to move the piece from square (r,c) to square (r,c-1), (r,c+1), (r-1,c) and (r+1,c), respectively. If the destination square does not exist, the piece is removed from the grid, and the game ends, even if less than N steps are done.
Takahashi wants to remove the piece from the grid in one of the N steps. Aoki, on the other hand, wants to finish the N steps with the piece remaining on the grid. Determine if the piece will remain on the grid at the end of the game when both players play optimally.
Constraints
* 2 \leq H,W \leq 2 \times 10^5
* 2 \leq N \leq 2 \times 10^5
* 1 \leq s_r \leq H
* 1 \leq s_c \leq W
* |S|=|T|=N
* S and T consists of the four kinds of letters `L`, `R`, `U` and `D`.
Input
Input is given from Standard Input in the following format:
H W N
s_r s_c
S
T
Output
If the piece will remain on the grid at the end of the game, print `YES`; otherwise, print `NO`.
Examples
Input
2 3 3
2 2
RRL
LUD
Output
YES
Input
4 3 5
2 2
UDRRR
LLDUD
Output
NO
Input
5 6 11
2 1
RLDRRUDDLRL
URRDRLLDLRD
Output
NO | instruction | 0 | 59,468 | 15 | 118,936 |
"Correct Solution:
```
H, W , N = (int(i) for i in input().split())
sr, sc = (int(i) for i in input().split())
S = input()
T = input()
# 高橋はUR、青木はDL重視
su = sr
sd = sr
sle = sc
sri = sc
flag = False
for ss, tt in zip(S, T):
if ss == "U":
su -= 1
if ss == "D":
sd += 1
if ss == "R":
sri += 1
if ss == "L":
sle -= 1
if su == 0 or sd > H or sri > W or sle == 0:
flag = True
if tt == "U" and sd > 1:
sd -= 1
if tt == "D" and su < H:
su += 1
if tt == "R" and sle < W:
sle += 1
if tt == "L" and sri > 1:
sri -= 1
if flag==True:
print("NO")
else:
print("YES")
``` | output | 1 | 59,468 | 15 | 118,937 |
Provide a correct Python 3 solution for this coding contest problem.
We have a rectangular grid of squares 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. On this grid, there is a piece, which is initially placed at square (s_r,s_c).
Takahashi and Aoki will play a game, where each player has a string of length N. Takahashi's string is S, and Aoki's string is T. S and T both consist of four kinds of letters: `L`, `R`, `U` and `D`.
The game consists of N steps. The i-th step proceeds as follows:
* First, Takahashi performs a move. He either moves the piece in the direction of S_i, or does not move the piece.
* Second, Aoki performs a move. He either moves the piece in the direction of T_i, or does not move the piece.
Here, to move the piece in the direction of `L`, `R`, `U` and `D`, is to move the piece from square (r,c) to square (r,c-1), (r,c+1), (r-1,c) and (r+1,c), respectively. If the destination square does not exist, the piece is removed from the grid, and the game ends, even if less than N steps are done.
Takahashi wants to remove the piece from the grid in one of the N steps. Aoki, on the other hand, wants to finish the N steps with the piece remaining on the grid. Determine if the piece will remain on the grid at the end of the game when both players play optimally.
Constraints
* 2 \leq H,W \leq 2 \times 10^5
* 2 \leq N \leq 2 \times 10^5
* 1 \leq s_r \leq H
* 1 \leq s_c \leq W
* |S|=|T|=N
* S and T consists of the four kinds of letters `L`, `R`, `U` and `D`.
Input
Input is given from Standard Input in the following format:
H W N
s_r s_c
S
T
Output
If the piece will remain on the grid at the end of the game, print `YES`; otherwise, print `NO`.
Examples
Input
2 3 3
2 2
RRL
LUD
Output
YES
Input
4 3 5
2 2
UDRRR
LLDUD
Output
NO
Input
5 6 11
2 1
RLDRRUDDLRL
URRDRLLDLRD
Output
NO | instruction | 0 | 59,469 | 15 | 118,938 |
"Correct Solution:
```
H, W, N = map(int, input().split())
h, w = map(int, input().split())
S = input()
T = input()
h -= 1
w -= 1
for i in range(N):
if S[i] == "L":
if w-1 < W-w-1:
w -= 1
elif S[i] == "R":
if w > W-w-2:
w += 1
elif S[i] == "U":
if h-1 < H-h-1:
h -= 1
elif S[i] == "D":
if h > H-h-2:
h += 1
if w == 0 or h == 0 or h == H or w == W:
print("NO")
break
if T[i] == "L":
if w-1 > W-w-1:
w -= 1
elif T[i] == "R":
if w < W-w-2:
w += 1
elif T[i] == "U":
if h-1 > H-h-1:
h -= 1
elif T[i] == "D":
if h < H-h-2:
h += 1
else:
print("YES")
``` | output | 1 | 59,469 | 15 | 118,939 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.