message stringlengths 2 19.9k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 322 108k | cluster float64 15 15 | __index_level_0__ int64 644 217k |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recently you have bought a snow walking robot and brought it home. Suppose your home is a cell (0, 0) on an infinite grid.
You also have the sequence of instructions of this robot. It is written as the string s consisting of characters 'L', 'R', 'U' and 'D'. If the robot is in the cell (x, y) right now, he can move to one of the adjacent cells (depending on the current instruction).
* If the current instruction is 'L', then the robot can move to the left to (x - 1, y);
* if the current instruction is 'R', then the robot can move to the right to (x + 1, y);
* if the current instruction is 'U', then the robot can move to the top to (x, y + 1);
* if the current instruction is 'D', then the robot can move to the bottom to (x, y - 1).
You've noticed the warning on the last page of the manual: if the robot visits some cell (except (0, 0)) twice then it breaks.
So the sequence of instructions is valid if the robot starts in the cell (0, 0), performs the given instructions, visits no cell other than (0, 0) two or more times and ends the path in the cell (0, 0). Also cell (0, 0) should be visited at most two times: at the beginning and at the end (if the path is empty then it is visited only once). For example, the following sequences of instructions are considered valid: "UD", "RL", "UUURULLDDDDLDDRRUU", and the following are considered invalid: "U" (the endpoint is not (0, 0)) and "UUDD" (the cell (0, 1) is visited twice).
The initial sequence of instructions, however, might be not valid. You don't want your robot to break so you decided to reprogram it in the following way: you will remove some (possibly, all or none) instructions from the initial sequence of instructions, then rearrange the remaining instructions as you wish and turn on your robot to move.
Your task is to remove as few instructions from the initial sequence as possible and rearrange the remaining ones so that the sequence is valid. Report the valid sequence of the maximum length you can obtain.
Note that you can choose any order of remaining instructions (you don't need to minimize the number of swaps or any other similar metric).
You have to answer q independent test cases.
Input
The first line of the input contains one integer q (1 β€ q β€ 2 β
10^4) β the number of test cases.
The next q lines contain test cases. The i-th test case is given as the string s consisting of at least 1 and no more than 10^5 characters 'L', 'R', 'U' and 'D' β the initial sequence of instructions.
It is guaranteed that the sum of |s| (where |s| is the length of s) does not exceed 10^5 over all test cases (β |s| β€ 10^5).
Output
For each test case print the answer on it. In the first line print the maximum number of remaining instructions. In the second line print the valid sequence of remaining instructions t the robot has to perform. The moves are performed from left to right in the order of the printed sequence. If there are several answers, you can print any. If the answer is 0, you are allowed to print an empty line (but you can don't print it).
Example
Input
6
LRU
DURLDRUDRULRDURDDL
LRUDDLRUDRUL
LLLLRRRR
URDUR
LLL
Output
2
LR
14
RUURDDDDLLLUUR
12
ULDDDRRRUULL
2
LR
2
UD
0
Note
There are only two possible answers in the first test case: "LR" and "RL".
The picture corresponding to the second test case:
<image> Note that the direction of traverse does not matter
Another correct answer to the third test case: "URDDLLLUURDR".
Submitted Solution:
```
for _ in " "*int(input()):
z=input()
k=min(z.count("L"),z.count("R"));k1=min(z.count("U"),z.count("D"))
if k==0:
if k1:print(2);print("UD")
else:print(0)
elif k1==0:
if k:print(2);print("LR")
else:print(0)
else:
print(2*k+2*k1)
print("L"*k+"U"*k1+"D"*k1+"R"*k)
``` | instruction | 0 | 55,712 | 15 | 111,424 |
No | output | 1 | 55,712 | 15 | 111,425 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recently you have bought a snow walking robot and brought it home. Suppose your home is a cell (0, 0) on an infinite grid.
You also have the sequence of instructions of this robot. It is written as the string s consisting of characters 'L', 'R', 'U' and 'D'. If the robot is in the cell (x, y) right now, he can move to one of the adjacent cells (depending on the current instruction).
* If the current instruction is 'L', then the robot can move to the left to (x - 1, y);
* if the current instruction is 'R', then the robot can move to the right to (x + 1, y);
* if the current instruction is 'U', then the robot can move to the top to (x, y + 1);
* if the current instruction is 'D', then the robot can move to the bottom to (x, y - 1).
You've noticed the warning on the last page of the manual: if the robot visits some cell (except (0, 0)) twice then it breaks.
So the sequence of instructions is valid if the robot starts in the cell (0, 0), performs the given instructions, visits no cell other than (0, 0) two or more times and ends the path in the cell (0, 0). Also cell (0, 0) should be visited at most two times: at the beginning and at the end (if the path is empty then it is visited only once). For example, the following sequences of instructions are considered valid: "UD", "RL", "UUURULLDDDDLDDRRUU", and the following are considered invalid: "U" (the endpoint is not (0, 0)) and "UUDD" (the cell (0, 1) is visited twice).
The initial sequence of instructions, however, might be not valid. You don't want your robot to break so you decided to reprogram it in the following way: you will remove some (possibly, all or none) instructions from the initial sequence of instructions, then rearrange the remaining instructions as you wish and turn on your robot to move.
Your task is to remove as few instructions from the initial sequence as possible and rearrange the remaining ones so that the sequence is valid. Report the valid sequence of the maximum length you can obtain.
Note that you can choose any order of remaining instructions (you don't need to minimize the number of swaps or any other similar metric).
You have to answer q independent test cases.
Input
The first line of the input contains one integer q (1 β€ q β€ 2 β
10^4) β the number of test cases.
The next q lines contain test cases. The i-th test case is given as the string s consisting of at least 1 and no more than 10^5 characters 'L', 'R', 'U' and 'D' β the initial sequence of instructions.
It is guaranteed that the sum of |s| (where |s| is the length of s) does not exceed 10^5 over all test cases (β |s| β€ 10^5).
Output
For each test case print the answer on it. In the first line print the maximum number of remaining instructions. In the second line print the valid sequence of remaining instructions t the robot has to perform. The moves are performed from left to right in the order of the printed sequence. If there are several answers, you can print any. If the answer is 0, you are allowed to print an empty line (but you can don't print it).
Example
Input
6
LRU
DURLDRUDRULRDURDDL
LRUDDLRUDRUL
LLLLRRRR
URDUR
LLL
Output
2
LR
14
RUURDDDDLLLUUR
12
ULDDDRRRUULL
2
LR
2
UD
0
Note
There are only two possible answers in the first test case: "LR" and "RL".
The picture corresponding to the second test case:
<image> Note that the direction of traverse does not matter
Another correct answer to the third test case: "URDDLLLUURDR".
Submitted Solution:
```
n = int(input())
for i in range(n):
s = input()
L = list(s)
nU = nD = nL = nR = 0
for c in L:
if c == "U": nU += 1
elif c == "D": nD += 1
elif c == "R": nR += 1
else:
nL += 1
if nD != nU: nD = nU = min(nD,nU)
if nL != nR: nL = nR = min(nL,nR)
if nU == 0 and nD==0 and nL==0 and nR==0: print("0")
elif nU != 0 and nD !=0 and nL ==0 and nR ==0:
if nU == 1 and nD == 1:
print("2")
print("UD")
else:
print(0)
#print(nR*"R" + nU*"U" + nL*"L" + nD*"D")
elif nL != 0 and nR != 0 and nU == 0 and nD ==0:
if nL == 1 and nR == 1:
print("2")
print("LR")
else:
print(0)
else:
print(nU + nD + nL + nR)
print(nR*"R" + nU*"U" + nL*"L" + nD*"D")
``` | instruction | 0 | 55,713 | 15 | 111,426 |
No | output | 1 | 55,713 | 15 | 111,427 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recently you have bought a snow walking robot and brought it home. Suppose your home is a cell (0, 0) on an infinite grid.
You also have the sequence of instructions of this robot. It is written as the string s consisting of characters 'L', 'R', 'U' and 'D'. If the robot is in the cell (x, y) right now, he can move to one of the adjacent cells (depending on the current instruction).
* If the current instruction is 'L', then the robot can move to the left to (x - 1, y);
* if the current instruction is 'R', then the robot can move to the right to (x + 1, y);
* if the current instruction is 'U', then the robot can move to the top to (x, y + 1);
* if the current instruction is 'D', then the robot can move to the bottom to (x, y - 1).
You've noticed the warning on the last page of the manual: if the robot visits some cell (except (0, 0)) twice then it breaks.
So the sequence of instructions is valid if the robot starts in the cell (0, 0), performs the given instructions, visits no cell other than (0, 0) two or more times and ends the path in the cell (0, 0). Also cell (0, 0) should be visited at most two times: at the beginning and at the end (if the path is empty then it is visited only once). For example, the following sequences of instructions are considered valid: "UD", "RL", "UUURULLDDDDLDDRRUU", and the following are considered invalid: "U" (the endpoint is not (0, 0)) and "UUDD" (the cell (0, 1) is visited twice).
The initial sequence of instructions, however, might be not valid. You don't want your robot to break so you decided to reprogram it in the following way: you will remove some (possibly, all or none) instructions from the initial sequence of instructions, then rearrange the remaining instructions as you wish and turn on your robot to move.
Your task is to remove as few instructions from the initial sequence as possible and rearrange the remaining ones so that the sequence is valid. Report the valid sequence of the maximum length you can obtain.
Note that you can choose any order of remaining instructions (you don't need to minimize the number of swaps or any other similar metric).
You have to answer q independent test cases.
Input
The first line of the input contains one integer q (1 β€ q β€ 2 β
10^4) β the number of test cases.
The next q lines contain test cases. The i-th test case is given as the string s consisting of at least 1 and no more than 10^5 characters 'L', 'R', 'U' and 'D' β the initial sequence of instructions.
It is guaranteed that the sum of |s| (where |s| is the length of s) does not exceed 10^5 over all test cases (β |s| β€ 10^5).
Output
For each test case print the answer on it. In the first line print the maximum number of remaining instructions. In the second line print the valid sequence of remaining instructions t the robot has to perform. The moves are performed from left to right in the order of the printed sequence. If there are several answers, you can print any. If the answer is 0, you are allowed to print an empty line (but you can don't print it).
Example
Input
6
LRU
DURLDRUDRULRDURDDL
LRUDDLRUDRUL
LLLLRRRR
URDUR
LLL
Output
2
LR
14
RUURDDDDLLLUUR
12
ULDDDRRRUULL
2
LR
2
UD
0
Note
There are only two possible answers in the first test case: "LR" and "RL".
The picture corresponding to the second test case:
<image> Note that the direction of traverse does not matter
Another correct answer to the third test case: "URDDLLLUURDR".
Submitted Solution:
```
n = int(input())
result =[]
for i in range(n):
s = input()
nou,nod,nor,nol = 0,0,0,0
for i in s :
if i == 'L':
nol += 1
if i == 'R':
nor += 1
if i == 'U':
nou += 1
if i == 'D':
nod += 1
a = max(nou,nod)- min(nou,nod)
b = max(nol,nor)- min(nol,nor)
result.append(len(s)-a-b)
ans = min(nou,nod)*'U' +min(nol,nor)*'L'+min(nou,nod)*'D'+min(nol,nor)*'R'
result.append(ans)
for i in result :
print(i)
``` | instruction | 0 | 55,714 | 15 | 111,428 |
No | output | 1 | 55,714 | 15 | 111,429 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recently you have bought a snow walking robot and brought it home. Suppose your home is a cell (0, 0) on an infinite grid.
You also have the sequence of instructions of this robot. It is written as the string s consisting of characters 'L', 'R', 'U' and 'D'. If the robot is in the cell (x, y) right now, he can move to one of the adjacent cells (depending on the current instruction).
* If the current instruction is 'L', then the robot can move to the left to (x - 1, y);
* if the current instruction is 'R', then the robot can move to the right to (x + 1, y);
* if the current instruction is 'U', then the robot can move to the top to (x, y + 1);
* if the current instruction is 'D', then the robot can move to the bottom to (x, y - 1).
You've noticed the warning on the last page of the manual: if the robot visits some cell (except (0, 0)) twice then it breaks.
So the sequence of instructions is valid if the robot starts in the cell (0, 0), performs the given instructions, visits no cell other than (0, 0) two or more times and ends the path in the cell (0, 0). Also cell (0, 0) should be visited at most two times: at the beginning and at the end (if the path is empty then it is visited only once). For example, the following sequences of instructions are considered valid: "UD", "RL", "UUURULLDDDDLDDRRUU", and the following are considered invalid: "U" (the endpoint is not (0, 0)) and "UUDD" (the cell (0, 1) is visited twice).
The initial sequence of instructions, however, might be not valid. You don't want your robot to break so you decided to reprogram it in the following way: you will remove some (possibly, all or none) instructions from the initial sequence of instructions, then rearrange the remaining instructions as you wish and turn on your robot to move.
Your task is to remove as few instructions from the initial sequence as possible and rearrange the remaining ones so that the sequence is valid. Report the valid sequence of the maximum length you can obtain.
Note that you can choose any order of remaining instructions (you don't need to minimize the number of swaps or any other similar metric).
You have to answer q independent test cases.
Input
The first line of the input contains one integer q (1 β€ q β€ 2 β
10^4) β the number of test cases.
The next q lines contain test cases. The i-th test case is given as the string s consisting of at least 1 and no more than 10^5 characters 'L', 'R', 'U' and 'D' β the initial sequence of instructions.
It is guaranteed that the sum of |s| (where |s| is the length of s) does not exceed 10^5 over all test cases (β |s| β€ 10^5).
Output
For each test case print the answer on it. In the first line print the maximum number of remaining instructions. In the second line print the valid sequence of remaining instructions t the robot has to perform. The moves are performed from left to right in the order of the printed sequence. If there are several answers, you can print any. If the answer is 0, you are allowed to print an empty line (but you can don't print it).
Example
Input
6
LRU
DURLDRUDRULRDURDDL
LRUDDLRUDRUL
LLLLRRRR
URDUR
LLL
Output
2
LR
14
RUURDDDDLLLUUR
12
ULDDDRRRUULL
2
LR
2
UD
0
Note
There are only two possible answers in the first test case: "LR" and "RL".
The picture corresponding to the second test case:
<image> Note that the direction of traverse does not matter
Another correct answer to the third test case: "URDDLLLUURDR".
Submitted Solution:
```
for i in range(int(input())):
g = list(input())
r = g.count('R')
l = g.count('L')
u = g.count('U')
d = g.count('D')
if r > l:
for i in range(r - l):
g.remove('R')
if r < l:
for i in range(l - r):
g.remove('L')
if u > d:
for i in range(u - d):
g.remove('U')
if u < d:
for i in range(d - u):
g.remove('D')
print(len(g))
print("".join(sorted(g)))
``` | instruction | 0 | 55,715 | 15 | 111,430 |
No | output | 1 | 55,715 | 15 | 111,431 |
Provide a correct Python 3 solution for this coding contest problem.
It was the last day of the summer camp you strayed into the labyrinth on the way to Komaba Campus, the University of Tokyo. The contest has just begun. Your teammates must impatiently wait for you. So you have to escape from this labyrinth as soon as possible.
The labyrinth is represented by a grid map. Initially, each grid except for walls and stairs is either on the first floor or on the second floor. Some grids have a switch which can move up or down some of the grids (the grids on the first floor move to the second floor, and the grids on the second floor to the first floor).
In each step, you can take one of the following actions:
* Move to an adjacent grid (includes stairs) on the same floor you are now in.
* Move to another floor (if you are in the stairs grid).
* Operate the switch (if you are in a grid with a switch).
Luckily, you have just found a map of the labyrinth for some unknown reason. Let's calculate the minimum step to escape from the labyrinth, and go to the place your teammates are waiting!
Input
The format of the input is as follows.
> W H
> M11M12M13...M1W
> M21M22M23...M2W
> ........
> MH1MH2MH3...MHW
> S
> MS111MS112MS113...MS11W
> MS121MS122MS123...MS12W
> ........
> MS1H1MS1H2MS1H3...MS1HW
> MS211MS212MS213...MS21W
> MS221MS222MS223...MS22W
> ........
> MS2H1MS2H2MS2H3...MS2HW
> MSS11MSS12MSS13...MSS1W
> MSS21MSS22MSS23...MSS2W
> ........
> MSSH1MSSH2MSSH3...MSSHW
>
The first line contains two integers W (3 β€ W β€ 50) and H (3 β€ H β€ 50). They represent the width and height of the labyrinth, respectively.
The following H lines represent the initial state of the labyrinth. Each of Mij is one of the following symbols:
* '#' representing a wall,
* '|' representing stairs,
* '_' representing a grid which is initially on the first floor,
* '^' representing a grid which is initially on the second floor,
* a lowercase letter from 'a' to 'j' representing a switch the grid has, and the grid is initially on the first floor,
* an uppercase letter from 'A' to 'J' representing a switch the grid has, and the grid is initially on the second floor,
* '%' representing the grid you are initially in (which is initially on the first floor) or
* '&' representing the exit of the labyrinth (which is initially on the first floor).
The next line contains one integer S (0 β€ S β€ 10), and then the following SH lines represent the information of the switches. Each of MSkij is one of:
* '#' if Mij is a '#',
* '|' if Mij is a '|',
* '*' if the grid is moved by the switch represented by the k-th alphabet letter, or
* '.' otherwise.
Note that the grid which contains a switch may be moved by operating the switch. In this case, you will move together with the grid.
You may assume each of the '%' (start) and '&' (goal) appears exacyly once, that the map is surround by walls, and that each alphabet in the map is any of the letters from 'A' (or 'a') to S-th alphabet letter.
Output
Print the minimum step to reach the goal in one line. If there is no solution, print "-1".
Examples
Input
6 6
######
#_|A%#
#B#_|#
#^BBa#
#B&A##
######
2
######
#*|*.#
#.#.|#
#*.**#
#...##
######
######
#*|*.#
#*#.|#
#..**#
#..*##
######
Output
21
Input
6 6
_|A%#
B#_|#
^BBa#
B&A##
2
*|*.#
.#.|#
*.**#
...##
*|*.#
*#.|#
..**#
..*##
Output
21
Input
8 3
%||Aa&#
2
*||*..#
.||*.*#
Output
7
Input
3 4
%#
&#
0
Output
1
Input
3 5
%#
^#
&#
0
Output
-1 | instruction | 0 | 56,370 | 15 | 112,740 |
"Correct Solution:
```
from collections import deque
import sys
def main():
readline = sys.stdin.readline
write = sys.stdout.write
W, H = map(int, readline().split())
MP = [readline() for i in range(H)]
A = [[-1]*W for i in range(H)]
B = [[-1]*W for i in range(H)]
C = [[0]*W for i in range(H)]
SW0 = 'ABCDEFGHIJ'
SW1 = 'abcdefghij'
for i, mp in enumerate(MP):
Ai = A[i]; Bi = B[i]
for j, c in enumerate(mp):
if c == '#':
continue
if c in '^' or c in SW0:
Ai[j] = 1
if c != '^':
Bi[j] = SW0.index(c)+1
elif c in '_%&' or c in SW1:
Ai[j] = 0
if c == '%':
sx = j; sy = i
elif c == '&':
gx = j; gy = i
elif c != '_':
Bi[j] = SW1.index(c)+1
elif c == '|':
Bi[j] = 0
Ai[j] = 2
S = int(readline())
for k in range(S):
MP = [readline() for i in range(H)]
for i, mp in enumerate(MP):
Ci = C[i]
for j, c in enumerate(mp):
if c == '*':
Ci[j] |= (2 << k)
dist = [[{} for i in range(W)] for j in range(H)]
dist[sy][sx][0] = 0
dd = ((-1, 0), (0, -1), (1, 0), (0, 1))
bc = [0]*(2 << S)
for i in range(1, 2 << S):
bc[i] = bc[i ^ (i & -i)] ^ 1
que = deque([(0, sx, sy, 0)])
while que:
state, x, y, d = que.popleft()
if B[y][x] == 0:
if state^1 not in dist[y][x]:
dist[y][x][state^1] = d+1
que.append((state^1, x, y, d+1))
elif B[y][x] != -1:
n_state = state ^ (1 << B[y][x]) ^ (state & 1)
n_state ^= bc[n_state & C[y][x]] ^ A[y][x]
if n_state not in dist[y][x]:
dist[y][x][n_state] = d+1
que.append((n_state, x, y, d+1))
for dx, dy in dd:
nx = x + dx; ny = y + dy
if not 0 <= nx < W or not 0 <= ny < H or A[ny][nx] == -1:
continue
if A[ny][nx] == 2:
if state not in dist[ny][nx]:
dist[ny][nx][state] = d+1
que.append((state, nx, ny, d+1))
else:
np = bc[state & C[ny][nx]] ^ A[ny][nx]
if state&1 == np and state not in dist[ny][nx]:
dist[ny][nx][state] = d+1
que.append((state, nx, ny, d+1))
if dist[gy][gx]:
write("%d\n" % min(dist[gy][gx].values()))
else:
write("-1\n")
main()
``` | output | 1 | 56,370 | 15 | 112,741 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bob are playing chess on a huge chessboard with dimensions n Γ n. Alice has a single piece left β a queen, located at (a_x, a_y), while Bob has only the king standing at (b_x, b_y). Alice thinks that as her queen is dominating the chessboard, victory is hers.
But Bob has made a devious plan to seize the victory for himself β he needs to march his king to (c_x, c_y) in order to claim the victory for himself. As Alice is distracted by her sense of superiority, she no longer moves any pieces around, and it is only Bob who makes any turns.
Bob will win if he can move his king from (b_x, b_y) to (c_x, c_y) without ever getting in check. Remember that a king can move to any of the 8 adjacent squares. A king is in check if it is on the same rank (i.e. row), file (i.e. column), or diagonal as the enemy queen.
Find whether Bob can win or not.
Input
The first line contains a single integer n (3 β€ n β€ 1000) β the dimensions of the chessboard.
The second line contains two integers a_x and a_y (1 β€ a_x, a_y β€ n) β the coordinates of Alice's queen.
The third line contains two integers b_x and b_y (1 β€ b_x, b_y β€ n) β the coordinates of Bob's king.
The fourth line contains two integers c_x and c_y (1 β€ c_x, c_y β€ n) β the coordinates of the location that Bob wants to get to.
It is guaranteed that Bob's king is currently not in check and the target location is not in check either.
Furthermore, the king is not located on the same square as the queen (i.e. a_x β b_x or a_y β b_y), and the target does coincide neither with the queen's position (i.e. c_x β a_x or c_y β a_y) nor with the king's position (i.e. c_x β b_x or c_y β b_y).
Output
Print "YES" (without quotes) if Bob can get from (b_x, b_y) to (c_x, c_y) without ever getting in check, otherwise print "NO".
You can print each letter in any case (upper or lower).
Examples
Input
8
4 4
1 3
3 1
Output
YES
Input
8
4 4
2 3
1 6
Output
NO
Input
8
3 5
1 2
6 1
Output
NO
Note
In the diagrams below, the squares controlled by the black queen are marked red, and the target square is marked blue.
In the first case, the king can move, for instance, via the squares (2, 3) and (3, 2). Note that the direct route through (2, 2) goes through check.
<image>
In the second case, the queen watches the fourth rank, and the king has no means of crossing it.
<image>
In the third case, the queen watches the third file.
<image> | instruction | 0 | 56,407 | 15 | 112,814 |
Tags: dfs and similar, graphs, implementation
Correct Solution:
```
n=int(input())
ax, ay = map(int,input().split())
bx, by = map(int,input().split())
cx, cy = map(int,input().split())
if ((bx<ax and cx<ax) or (bx>ax and cx>ax)) and ((by<ay and cy<ay) or (by>ay and cy>ay)):
print('YES')
else:
print('NO')
``` | output | 1 | 56,407 | 15 | 112,815 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bob are playing chess on a huge chessboard with dimensions n Γ n. Alice has a single piece left β a queen, located at (a_x, a_y), while Bob has only the king standing at (b_x, b_y). Alice thinks that as her queen is dominating the chessboard, victory is hers.
But Bob has made a devious plan to seize the victory for himself β he needs to march his king to (c_x, c_y) in order to claim the victory for himself. As Alice is distracted by her sense of superiority, she no longer moves any pieces around, and it is only Bob who makes any turns.
Bob will win if he can move his king from (b_x, b_y) to (c_x, c_y) without ever getting in check. Remember that a king can move to any of the 8 adjacent squares. A king is in check if it is on the same rank (i.e. row), file (i.e. column), or diagonal as the enemy queen.
Find whether Bob can win or not.
Input
The first line contains a single integer n (3 β€ n β€ 1000) β the dimensions of the chessboard.
The second line contains two integers a_x and a_y (1 β€ a_x, a_y β€ n) β the coordinates of Alice's queen.
The third line contains two integers b_x and b_y (1 β€ b_x, b_y β€ n) β the coordinates of Bob's king.
The fourth line contains two integers c_x and c_y (1 β€ c_x, c_y β€ n) β the coordinates of the location that Bob wants to get to.
It is guaranteed that Bob's king is currently not in check and the target location is not in check either.
Furthermore, the king is not located on the same square as the queen (i.e. a_x β b_x or a_y β b_y), and the target does coincide neither with the queen's position (i.e. c_x β a_x or c_y β a_y) nor with the king's position (i.e. c_x β b_x or c_y β b_y).
Output
Print "YES" (without quotes) if Bob can get from (b_x, b_y) to (c_x, c_y) without ever getting in check, otherwise print "NO".
You can print each letter in any case (upper or lower).
Examples
Input
8
4 4
1 3
3 1
Output
YES
Input
8
4 4
2 3
1 6
Output
NO
Input
8
3 5
1 2
6 1
Output
NO
Note
In the diagrams below, the squares controlled by the black queen are marked red, and the target square is marked blue.
In the first case, the king can move, for instance, via the squares (2, 3) and (3, 2). Note that the direct route through (2, 2) goes through check.
<image>
In the second case, the queen watches the fourth rank, and the king has no means of crossing it.
<image>
In the third case, the queen watches the third file.
<image> | instruction | 0 | 56,408 | 15 | 112,816 |
Tags: dfs and similar, graphs, implementation
Correct Solution:
```
import sys
from collections import defaultdict
mod = 1000000007
def get_array(): return list(map(int, sys.stdin.readline().split()))
def get_ints(): return map(int, sys.stdin.readline().split())
def input(): return sys.stdin.readline()
def print_array(a): print(" ".join(map(str, a)))
def main():
n = int(input())
qx, qy = get_ints()
kx, ky = get_ints()
tx, ty = get_ints()
if ((kx < qx and tx < qx) or (kx > qx and tx > qx)) and ((ky < qy and ty < qy) or (ky > qy and ty > qy)): print("YES")
else: print("NO")
if __name__ == "__main__":
main()
``` | output | 1 | 56,408 | 15 | 112,817 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bob are playing chess on a huge chessboard with dimensions n Γ n. Alice has a single piece left β a queen, located at (a_x, a_y), while Bob has only the king standing at (b_x, b_y). Alice thinks that as her queen is dominating the chessboard, victory is hers.
But Bob has made a devious plan to seize the victory for himself β he needs to march his king to (c_x, c_y) in order to claim the victory for himself. As Alice is distracted by her sense of superiority, she no longer moves any pieces around, and it is only Bob who makes any turns.
Bob will win if he can move his king from (b_x, b_y) to (c_x, c_y) without ever getting in check. Remember that a king can move to any of the 8 adjacent squares. A king is in check if it is on the same rank (i.e. row), file (i.e. column), or diagonal as the enemy queen.
Find whether Bob can win or not.
Input
The first line contains a single integer n (3 β€ n β€ 1000) β the dimensions of the chessboard.
The second line contains two integers a_x and a_y (1 β€ a_x, a_y β€ n) β the coordinates of Alice's queen.
The third line contains two integers b_x and b_y (1 β€ b_x, b_y β€ n) β the coordinates of Bob's king.
The fourth line contains two integers c_x and c_y (1 β€ c_x, c_y β€ n) β the coordinates of the location that Bob wants to get to.
It is guaranteed that Bob's king is currently not in check and the target location is not in check either.
Furthermore, the king is not located on the same square as the queen (i.e. a_x β b_x or a_y β b_y), and the target does coincide neither with the queen's position (i.e. c_x β a_x or c_y β a_y) nor with the king's position (i.e. c_x β b_x or c_y β b_y).
Output
Print "YES" (without quotes) if Bob can get from (b_x, b_y) to (c_x, c_y) without ever getting in check, otherwise print "NO".
You can print each letter in any case (upper or lower).
Examples
Input
8
4 4
1 3
3 1
Output
YES
Input
8
4 4
2 3
1 6
Output
NO
Input
8
3 5
1 2
6 1
Output
NO
Note
In the diagrams below, the squares controlled by the black queen are marked red, and the target square is marked blue.
In the first case, the king can move, for instance, via the squares (2, 3) and (3, 2). Note that the direct route through (2, 2) goes through check.
<image>
In the second case, the queen watches the fourth rank, and the king has no means of crossing it.
<image>
In the third case, the queen watches the third file.
<image> | instruction | 0 | 56,409 | 15 | 112,818 |
Tags: dfs and similar, graphs, implementation
Correct Solution:
```
n = int(input())
ax,ay = map(int,input().split())
bx,by = map(int,input().split())
cx,cy = map(int,input().split())
if (bx>=ax and cx<=ax) or (bx<=ax and cx>=ax) or (by>=ay and cy<=ay) or (by<=ay and cy>=ay):print('NO')
else:print('YES')
``` | output | 1 | 56,409 | 15 | 112,819 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bob are playing chess on a huge chessboard with dimensions n Γ n. Alice has a single piece left β a queen, located at (a_x, a_y), while Bob has only the king standing at (b_x, b_y). Alice thinks that as her queen is dominating the chessboard, victory is hers.
But Bob has made a devious plan to seize the victory for himself β he needs to march his king to (c_x, c_y) in order to claim the victory for himself. As Alice is distracted by her sense of superiority, she no longer moves any pieces around, and it is only Bob who makes any turns.
Bob will win if he can move his king from (b_x, b_y) to (c_x, c_y) without ever getting in check. Remember that a king can move to any of the 8 adjacent squares. A king is in check if it is on the same rank (i.e. row), file (i.e. column), or diagonal as the enemy queen.
Find whether Bob can win or not.
Input
The first line contains a single integer n (3 β€ n β€ 1000) β the dimensions of the chessboard.
The second line contains two integers a_x and a_y (1 β€ a_x, a_y β€ n) β the coordinates of Alice's queen.
The third line contains two integers b_x and b_y (1 β€ b_x, b_y β€ n) β the coordinates of Bob's king.
The fourth line contains two integers c_x and c_y (1 β€ c_x, c_y β€ n) β the coordinates of the location that Bob wants to get to.
It is guaranteed that Bob's king is currently not in check and the target location is not in check either.
Furthermore, the king is not located on the same square as the queen (i.e. a_x β b_x or a_y β b_y), and the target does coincide neither with the queen's position (i.e. c_x β a_x or c_y β a_y) nor with the king's position (i.e. c_x β b_x or c_y β b_y).
Output
Print "YES" (without quotes) if Bob can get from (b_x, b_y) to (c_x, c_y) without ever getting in check, otherwise print "NO".
You can print each letter in any case (upper or lower).
Examples
Input
8
4 4
1 3
3 1
Output
YES
Input
8
4 4
2 3
1 6
Output
NO
Input
8
3 5
1 2
6 1
Output
NO
Note
In the diagrams below, the squares controlled by the black queen are marked red, and the target square is marked blue.
In the first case, the king can move, for instance, via the squares (2, 3) and (3, 2). Note that the direct route through (2, 2) goes through check.
<image>
In the second case, the queen watches the fourth rank, and the king has no means of crossing it.
<image>
In the third case, the queen watches the third file.
<image> | instruction | 0 | 56,410 | 15 | 112,820 |
Tags: dfs and similar, graphs, implementation
Correct Solution:
```
n = int(input())
ax, ay = map(int, input().split())
bx, by = map(int, input().split())
cx, cy = map(int, input().split())
if ((bx < ax)==(cx < ax)) and ((by < ay)==(cy < ay)):
print("YES")
else:
print('NO')
``` | output | 1 | 56,410 | 15 | 112,821 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bob are playing chess on a huge chessboard with dimensions n Γ n. Alice has a single piece left β a queen, located at (a_x, a_y), while Bob has only the king standing at (b_x, b_y). Alice thinks that as her queen is dominating the chessboard, victory is hers.
But Bob has made a devious plan to seize the victory for himself β he needs to march his king to (c_x, c_y) in order to claim the victory for himself. As Alice is distracted by her sense of superiority, she no longer moves any pieces around, and it is only Bob who makes any turns.
Bob will win if he can move his king from (b_x, b_y) to (c_x, c_y) without ever getting in check. Remember that a king can move to any of the 8 adjacent squares. A king is in check if it is on the same rank (i.e. row), file (i.e. column), or diagonal as the enemy queen.
Find whether Bob can win or not.
Input
The first line contains a single integer n (3 β€ n β€ 1000) β the dimensions of the chessboard.
The second line contains two integers a_x and a_y (1 β€ a_x, a_y β€ n) β the coordinates of Alice's queen.
The third line contains two integers b_x and b_y (1 β€ b_x, b_y β€ n) β the coordinates of Bob's king.
The fourth line contains two integers c_x and c_y (1 β€ c_x, c_y β€ n) β the coordinates of the location that Bob wants to get to.
It is guaranteed that Bob's king is currently not in check and the target location is not in check either.
Furthermore, the king is not located on the same square as the queen (i.e. a_x β b_x or a_y β b_y), and the target does coincide neither with the queen's position (i.e. c_x β a_x or c_y β a_y) nor with the king's position (i.e. c_x β b_x or c_y β b_y).
Output
Print "YES" (without quotes) if Bob can get from (b_x, b_y) to (c_x, c_y) without ever getting in check, otherwise print "NO".
You can print each letter in any case (upper or lower).
Examples
Input
8
4 4
1 3
3 1
Output
YES
Input
8
4 4
2 3
1 6
Output
NO
Input
8
3 5
1 2
6 1
Output
NO
Note
In the diagrams below, the squares controlled by the black queen are marked red, and the target square is marked blue.
In the first case, the king can move, for instance, via the squares (2, 3) and (3, 2). Note that the direct route through (2, 2) goes through check.
<image>
In the second case, the queen watches the fourth rank, and the king has no means of crossing it.
<image>
In the third case, the queen watches the third file.
<image> | instruction | 0 | 56,411 | 15 | 112,822 |
Tags: dfs and similar, graphs, implementation
Correct Solution:
```
sign = lambda x: (1, -1)[x<0]
def solve(n, ax, ay, bx, by, cx, cy):
axdif = sign(bx-ax)
aydif = sign(by-ay)
cxdif = sign(cx-ax)
cydif = sign(cy-ay)
return 'YES' if axdif == cxdif and aydif == cydif else 'NO'
n = int(input())
ax, ay = map(lambda x: int(x)-1, input().split())
bx, by = map(lambda x: int(x)-1, input().split())
cx, cy = map(lambda x: int(x)-1, input().split())
print(solve(n, ax, ay, bx, by, cx, cy))
``` | output | 1 | 56,411 | 15 | 112,823 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bob are playing chess on a huge chessboard with dimensions n Γ n. Alice has a single piece left β a queen, located at (a_x, a_y), while Bob has only the king standing at (b_x, b_y). Alice thinks that as her queen is dominating the chessboard, victory is hers.
But Bob has made a devious plan to seize the victory for himself β he needs to march his king to (c_x, c_y) in order to claim the victory for himself. As Alice is distracted by her sense of superiority, she no longer moves any pieces around, and it is only Bob who makes any turns.
Bob will win if he can move his king from (b_x, b_y) to (c_x, c_y) without ever getting in check. Remember that a king can move to any of the 8 adjacent squares. A king is in check if it is on the same rank (i.e. row), file (i.e. column), or diagonal as the enemy queen.
Find whether Bob can win or not.
Input
The first line contains a single integer n (3 β€ n β€ 1000) β the dimensions of the chessboard.
The second line contains two integers a_x and a_y (1 β€ a_x, a_y β€ n) β the coordinates of Alice's queen.
The third line contains two integers b_x and b_y (1 β€ b_x, b_y β€ n) β the coordinates of Bob's king.
The fourth line contains two integers c_x and c_y (1 β€ c_x, c_y β€ n) β the coordinates of the location that Bob wants to get to.
It is guaranteed that Bob's king is currently not in check and the target location is not in check either.
Furthermore, the king is not located on the same square as the queen (i.e. a_x β b_x or a_y β b_y), and the target does coincide neither with the queen's position (i.e. c_x β a_x or c_y β a_y) nor with the king's position (i.e. c_x β b_x or c_y β b_y).
Output
Print "YES" (without quotes) if Bob can get from (b_x, b_y) to (c_x, c_y) without ever getting in check, otherwise print "NO".
You can print each letter in any case (upper or lower).
Examples
Input
8
4 4
1 3
3 1
Output
YES
Input
8
4 4
2 3
1 6
Output
NO
Input
8
3 5
1 2
6 1
Output
NO
Note
In the diagrams below, the squares controlled by the black queen are marked red, and the target square is marked blue.
In the first case, the king can move, for instance, via the squares (2, 3) and (3, 2). Note that the direct route through (2, 2) goes through check.
<image>
In the second case, the queen watches the fourth rank, and the king has no means of crossing it.
<image>
In the third case, the queen watches the third file.
<image> | instruction | 0 | 56,412 | 15 | 112,824 |
Tags: dfs and similar, graphs, implementation
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
def four(x, y):
xf = a[0]
yf = a[1]
if x > xf and y > yf:
return 1
if x < xf and y > yf:
return 2
if x < xf and y < yf:
return 3
if x > xf and y < yf:
return 4
print("YES" if four(b[0], b[1]) == four(c[0], c[1]) else "NO")
``` | output | 1 | 56,412 | 15 | 112,825 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bob are playing chess on a huge chessboard with dimensions n Γ n. Alice has a single piece left β a queen, located at (a_x, a_y), while Bob has only the king standing at (b_x, b_y). Alice thinks that as her queen is dominating the chessboard, victory is hers.
But Bob has made a devious plan to seize the victory for himself β he needs to march his king to (c_x, c_y) in order to claim the victory for himself. As Alice is distracted by her sense of superiority, she no longer moves any pieces around, and it is only Bob who makes any turns.
Bob will win if he can move his king from (b_x, b_y) to (c_x, c_y) without ever getting in check. Remember that a king can move to any of the 8 adjacent squares. A king is in check if it is on the same rank (i.e. row), file (i.e. column), or diagonal as the enemy queen.
Find whether Bob can win or not.
Input
The first line contains a single integer n (3 β€ n β€ 1000) β the dimensions of the chessboard.
The second line contains two integers a_x and a_y (1 β€ a_x, a_y β€ n) β the coordinates of Alice's queen.
The third line contains two integers b_x and b_y (1 β€ b_x, b_y β€ n) β the coordinates of Bob's king.
The fourth line contains two integers c_x and c_y (1 β€ c_x, c_y β€ n) β the coordinates of the location that Bob wants to get to.
It is guaranteed that Bob's king is currently not in check and the target location is not in check either.
Furthermore, the king is not located on the same square as the queen (i.e. a_x β b_x or a_y β b_y), and the target does coincide neither with the queen's position (i.e. c_x β a_x or c_y β a_y) nor with the king's position (i.e. c_x β b_x or c_y β b_y).
Output
Print "YES" (without quotes) if Bob can get from (b_x, b_y) to (c_x, c_y) without ever getting in check, otherwise print "NO".
You can print each letter in any case (upper or lower).
Examples
Input
8
4 4
1 3
3 1
Output
YES
Input
8
4 4
2 3
1 6
Output
NO
Input
8
3 5
1 2
6 1
Output
NO
Note
In the diagrams below, the squares controlled by the black queen are marked red, and the target square is marked blue.
In the first case, the king can move, for instance, via the squares (2, 3) and (3, 2). Note that the direct route through (2, 2) goes through check.
<image>
In the second case, the queen watches the fourth rank, and the king has no means of crossing it.
<image>
In the third case, the queen watches the third file.
<image> | instruction | 0 | 56,413 | 15 | 112,826 |
Tags: dfs and similar, graphs, implementation
Correct Solution:
```
import queue
n = int(input())
[qx,qy] = map(int, input().split())
[kx,ky] = map(int, input().split())
[tx,ty] = map(int, input().split())
kdx = kx - qx
kdy = ky - qy
tdx = tx - qx
tdy = ty - qy
if tdx * kdx > 0 and tdy * kdy > 0:
print("YES")
else:
print("NO")
``` | output | 1 | 56,413 | 15 | 112,827 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Alice and Bob are playing chess on a huge chessboard with dimensions n Γ n. Alice has a single piece left β a queen, located at (a_x, a_y), while Bob has only the king standing at (b_x, b_y). Alice thinks that as her queen is dominating the chessboard, victory is hers.
But Bob has made a devious plan to seize the victory for himself β he needs to march his king to (c_x, c_y) in order to claim the victory for himself. As Alice is distracted by her sense of superiority, she no longer moves any pieces around, and it is only Bob who makes any turns.
Bob will win if he can move his king from (b_x, b_y) to (c_x, c_y) without ever getting in check. Remember that a king can move to any of the 8 adjacent squares. A king is in check if it is on the same rank (i.e. row), file (i.e. column), or diagonal as the enemy queen.
Find whether Bob can win or not.
Input
The first line contains a single integer n (3 β€ n β€ 1000) β the dimensions of the chessboard.
The second line contains two integers a_x and a_y (1 β€ a_x, a_y β€ n) β the coordinates of Alice's queen.
The third line contains two integers b_x and b_y (1 β€ b_x, b_y β€ n) β the coordinates of Bob's king.
The fourth line contains two integers c_x and c_y (1 β€ c_x, c_y β€ n) β the coordinates of the location that Bob wants to get to.
It is guaranteed that Bob's king is currently not in check and the target location is not in check either.
Furthermore, the king is not located on the same square as the queen (i.e. a_x β b_x or a_y β b_y), and the target does coincide neither with the queen's position (i.e. c_x β a_x or c_y β a_y) nor with the king's position (i.e. c_x β b_x or c_y β b_y).
Output
Print "YES" (without quotes) if Bob can get from (b_x, b_y) to (c_x, c_y) without ever getting in check, otherwise print "NO".
You can print each letter in any case (upper or lower).
Examples
Input
8
4 4
1 3
3 1
Output
YES
Input
8
4 4
2 3
1 6
Output
NO
Input
8
3 5
1 2
6 1
Output
NO
Note
In the diagrams below, the squares controlled by the black queen are marked red, and the target square is marked blue.
In the first case, the king can move, for instance, via the squares (2, 3) and (3, 2). Note that the direct route through (2, 2) goes through check.
<image>
In the second case, the queen watches the fourth rank, and the king has no means of crossing it.
<image>
In the third case, the queen watches the third file.
<image> | instruction | 0 | 56,414 | 15 | 112,828 |
Tags: dfs and similar, graphs, implementation
Correct Solution:
```
n = int(input())
ax, ay = [int(x) for x in input().split()]
bx, by = [int(x) for x in input().split()]
cx, cy = [int(x) for x in input().split()]
dxb = ax - bx
dxc = ax - cx
dyb = ay - by
dyc = ay - cy
dxba = int(dxb / abs(dxb))
dxca = int(dxc / abs(dxc))
dyba = int(dyb / abs(dyb))
dyca = int(dyc / abs(dyc))
if dxba == dxca and dyba == dyca:
print('YES')
else:
print('NO')
``` | output | 1 | 56,414 | 15 | 112,829 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice and Bob are playing chess on a huge chessboard with dimensions n Γ n. Alice has a single piece left β a queen, located at (a_x, a_y), while Bob has only the king standing at (b_x, b_y). Alice thinks that as her queen is dominating the chessboard, victory is hers.
But Bob has made a devious plan to seize the victory for himself β he needs to march his king to (c_x, c_y) in order to claim the victory for himself. As Alice is distracted by her sense of superiority, she no longer moves any pieces around, and it is only Bob who makes any turns.
Bob will win if he can move his king from (b_x, b_y) to (c_x, c_y) without ever getting in check. Remember that a king can move to any of the 8 adjacent squares. A king is in check if it is on the same rank (i.e. row), file (i.e. column), or diagonal as the enemy queen.
Find whether Bob can win or not.
Input
The first line contains a single integer n (3 β€ n β€ 1000) β the dimensions of the chessboard.
The second line contains two integers a_x and a_y (1 β€ a_x, a_y β€ n) β the coordinates of Alice's queen.
The third line contains two integers b_x and b_y (1 β€ b_x, b_y β€ n) β the coordinates of Bob's king.
The fourth line contains two integers c_x and c_y (1 β€ c_x, c_y β€ n) β the coordinates of the location that Bob wants to get to.
It is guaranteed that Bob's king is currently not in check and the target location is not in check either.
Furthermore, the king is not located on the same square as the queen (i.e. a_x β b_x or a_y β b_y), and the target does coincide neither with the queen's position (i.e. c_x β a_x or c_y β a_y) nor with the king's position (i.e. c_x β b_x or c_y β b_y).
Output
Print "YES" (without quotes) if Bob can get from (b_x, b_y) to (c_x, c_y) without ever getting in check, otherwise print "NO".
You can print each letter in any case (upper or lower).
Examples
Input
8
4 4
1 3
3 1
Output
YES
Input
8
4 4
2 3
1 6
Output
NO
Input
8
3 5
1 2
6 1
Output
NO
Note
In the diagrams below, the squares controlled by the black queen are marked red, and the target square is marked blue.
In the first case, the king can move, for instance, via the squares (2, 3) and (3, 2). Note that the direct route through (2, 2) goes through check.
<image>
In the second case, the queen watches the fourth rank, and the king has no means of crossing it.
<image>
In the third case, the queen watches the third file.
<image>
Submitted Solution:
```
n = int(input())
bx, by = map(int, input().split())
ax, ay = map(int, input().split())
cx, cy = map(int, input().split())
if (bx > min(ax, cx) and bx < max(ax, cx)) or (by > min(ay, cy) and by < max(ay, cy)):
print('NO')
else:
print('YES')
``` | instruction | 0 | 56,415 | 15 | 112,830 |
Yes | output | 1 | 56,415 | 15 | 112,831 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice and Bob are playing chess on a huge chessboard with dimensions n Γ n. Alice has a single piece left β a queen, located at (a_x, a_y), while Bob has only the king standing at (b_x, b_y). Alice thinks that as her queen is dominating the chessboard, victory is hers.
But Bob has made a devious plan to seize the victory for himself β he needs to march his king to (c_x, c_y) in order to claim the victory for himself. As Alice is distracted by her sense of superiority, she no longer moves any pieces around, and it is only Bob who makes any turns.
Bob will win if he can move his king from (b_x, b_y) to (c_x, c_y) without ever getting in check. Remember that a king can move to any of the 8 adjacent squares. A king is in check if it is on the same rank (i.e. row), file (i.e. column), or diagonal as the enemy queen.
Find whether Bob can win or not.
Input
The first line contains a single integer n (3 β€ n β€ 1000) β the dimensions of the chessboard.
The second line contains two integers a_x and a_y (1 β€ a_x, a_y β€ n) β the coordinates of Alice's queen.
The third line contains two integers b_x and b_y (1 β€ b_x, b_y β€ n) β the coordinates of Bob's king.
The fourth line contains two integers c_x and c_y (1 β€ c_x, c_y β€ n) β the coordinates of the location that Bob wants to get to.
It is guaranteed that Bob's king is currently not in check and the target location is not in check either.
Furthermore, the king is not located on the same square as the queen (i.e. a_x β b_x or a_y β b_y), and the target does coincide neither with the queen's position (i.e. c_x β a_x or c_y β a_y) nor with the king's position (i.e. c_x β b_x or c_y β b_y).
Output
Print "YES" (without quotes) if Bob can get from (b_x, b_y) to (c_x, c_y) without ever getting in check, otherwise print "NO".
You can print each letter in any case (upper or lower).
Examples
Input
8
4 4
1 3
3 1
Output
YES
Input
8
4 4
2 3
1 6
Output
NO
Input
8
3 5
1 2
6 1
Output
NO
Note
In the diagrams below, the squares controlled by the black queen are marked red, and the target square is marked blue.
In the first case, the king can move, for instance, via the squares (2, 3) and (3, 2). Note that the direct route through (2, 2) goes through check.
<image>
In the second case, the queen watches the fourth rank, and the king has no means of crossing it.
<image>
In the third case, the queen watches the third file.
<image>
Submitted Solution:
```
import sys
next(sys.stdin)
q_x, q_y = map(int, next(sys.stdin).rstrip().split())
k_x, k_y = map(int, next(sys.stdin).rstrip().split())
dest_x, dest_y = map(int, next(sys.stdin).rstrip().split())
def sign(x):
return 1 if x >= 0 else -1
def which_square(x, y):
return sign(x - q_x), sign(y - q_y)
if which_square(k_x, k_y) == which_square(dest_x, dest_y):
print("YES")
else:
print("NO")
``` | instruction | 0 | 56,416 | 15 | 112,832 |
Yes | output | 1 | 56,416 | 15 | 112,833 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice and Bob are playing chess on a huge chessboard with dimensions n Γ n. Alice has a single piece left β a queen, located at (a_x, a_y), while Bob has only the king standing at (b_x, b_y). Alice thinks that as her queen is dominating the chessboard, victory is hers.
But Bob has made a devious plan to seize the victory for himself β he needs to march his king to (c_x, c_y) in order to claim the victory for himself. As Alice is distracted by her sense of superiority, she no longer moves any pieces around, and it is only Bob who makes any turns.
Bob will win if he can move his king from (b_x, b_y) to (c_x, c_y) without ever getting in check. Remember that a king can move to any of the 8 adjacent squares. A king is in check if it is on the same rank (i.e. row), file (i.e. column), or diagonal as the enemy queen.
Find whether Bob can win or not.
Input
The first line contains a single integer n (3 β€ n β€ 1000) β the dimensions of the chessboard.
The second line contains two integers a_x and a_y (1 β€ a_x, a_y β€ n) β the coordinates of Alice's queen.
The third line contains two integers b_x and b_y (1 β€ b_x, b_y β€ n) β the coordinates of Bob's king.
The fourth line contains two integers c_x and c_y (1 β€ c_x, c_y β€ n) β the coordinates of the location that Bob wants to get to.
It is guaranteed that Bob's king is currently not in check and the target location is not in check either.
Furthermore, the king is not located on the same square as the queen (i.e. a_x β b_x or a_y β b_y), and the target does coincide neither with the queen's position (i.e. c_x β a_x or c_y β a_y) nor with the king's position (i.e. c_x β b_x or c_y β b_y).
Output
Print "YES" (without quotes) if Bob can get from (b_x, b_y) to (c_x, c_y) without ever getting in check, otherwise print "NO".
You can print each letter in any case (upper or lower).
Examples
Input
8
4 4
1 3
3 1
Output
YES
Input
8
4 4
2 3
1 6
Output
NO
Input
8
3 5
1 2
6 1
Output
NO
Note
In the diagrams below, the squares controlled by the black queen are marked red, and the target square is marked blue.
In the first case, the king can move, for instance, via the squares (2, 3) and (3, 2). Note that the direct route through (2, 2) goes through check.
<image>
In the second case, the queen watches the fourth rank, and the king has no means of crossing it.
<image>
In the third case, the queen watches the third file.
<image>
Submitted Solution:
```
n=int(input())
a1,a2=list(map(int,input().split()))
b1,b2=list(map(int,input().split()))
c1,c2=list(map(int,input().split()))
if c1<a1 and c2<a2 and b1<a1 and b2<a2:
print("YES")
elif c1>a1 and c2<a2 and b1>a1 and b2<a2:
print("YES")
elif c1<a1 and c2>a2 and b1<a1 and b2>a2:
print("YES")
elif c1>a1 and c2>a2 and b1>a1 and b2>a2:
print("YES")
else:
print("NO")
``` | instruction | 0 | 56,417 | 15 | 112,834 |
Yes | output | 1 | 56,417 | 15 | 112,835 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice and Bob are playing chess on a huge chessboard with dimensions n Γ n. Alice has a single piece left β a queen, located at (a_x, a_y), while Bob has only the king standing at (b_x, b_y). Alice thinks that as her queen is dominating the chessboard, victory is hers.
But Bob has made a devious plan to seize the victory for himself β he needs to march his king to (c_x, c_y) in order to claim the victory for himself. As Alice is distracted by her sense of superiority, she no longer moves any pieces around, and it is only Bob who makes any turns.
Bob will win if he can move his king from (b_x, b_y) to (c_x, c_y) without ever getting in check. Remember that a king can move to any of the 8 adjacent squares. A king is in check if it is on the same rank (i.e. row), file (i.e. column), or diagonal as the enemy queen.
Find whether Bob can win or not.
Input
The first line contains a single integer n (3 β€ n β€ 1000) β the dimensions of the chessboard.
The second line contains two integers a_x and a_y (1 β€ a_x, a_y β€ n) β the coordinates of Alice's queen.
The third line contains two integers b_x and b_y (1 β€ b_x, b_y β€ n) β the coordinates of Bob's king.
The fourth line contains two integers c_x and c_y (1 β€ c_x, c_y β€ n) β the coordinates of the location that Bob wants to get to.
It is guaranteed that Bob's king is currently not in check and the target location is not in check either.
Furthermore, the king is not located on the same square as the queen (i.e. a_x β b_x or a_y β b_y), and the target does coincide neither with the queen's position (i.e. c_x β a_x or c_y β a_y) nor with the king's position (i.e. c_x β b_x or c_y β b_y).
Output
Print "YES" (without quotes) if Bob can get from (b_x, b_y) to (c_x, c_y) without ever getting in check, otherwise print "NO".
You can print each letter in any case (upper or lower).
Examples
Input
8
4 4
1 3
3 1
Output
YES
Input
8
4 4
2 3
1 6
Output
NO
Input
8
3 5
1 2
6 1
Output
NO
Note
In the diagrams below, the squares controlled by the black queen are marked red, and the target square is marked blue.
In the first case, the king can move, for instance, via the squares (2, 3) and (3, 2). Note that the direct route through (2, 2) goes through check.
<image>
In the second case, the queen watches the fourth rank, and the king has no means of crossing it.
<image>
In the third case, the queen watches the third file.
<image>
Submitted Solution:
```
x=int(input())
a=[int(i)for i in input().split()]
b=[int(i)for i in input().split()]
c=[int(i)for i in input().split()]
if b[0]<a[0] and b[1]<a[1]:
if c[0]<a[0] and c[1]<a[1]:
print("YES")
else:
print("NO")
elif b[0]<a[0] and b[1]>a[1]:
if c[0]<a[0] and c[1]>a[1]:
print("YES")
else:
print("NO")
elif b[0]>a[0] and b[1]>a[1]:
if c[0]>a[0] and c[1]>a[1]:
print("YES")
else:
print("NO")
elif b[0]>a[0] and b[1]<a[1]:
if c[0]>a[0] and c[1]<a[1]:
print("YES")
else:
print("NO")
``` | instruction | 0 | 56,418 | 15 | 112,836 |
Yes | output | 1 | 56,418 | 15 | 112,837 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice and Bob are playing chess on a huge chessboard with dimensions n Γ n. Alice has a single piece left β a queen, located at (a_x, a_y), while Bob has only the king standing at (b_x, b_y). Alice thinks that as her queen is dominating the chessboard, victory is hers.
But Bob has made a devious plan to seize the victory for himself β he needs to march his king to (c_x, c_y) in order to claim the victory for himself. As Alice is distracted by her sense of superiority, she no longer moves any pieces around, and it is only Bob who makes any turns.
Bob will win if he can move his king from (b_x, b_y) to (c_x, c_y) without ever getting in check. Remember that a king can move to any of the 8 adjacent squares. A king is in check if it is on the same rank (i.e. row), file (i.e. column), or diagonal as the enemy queen.
Find whether Bob can win or not.
Input
The first line contains a single integer n (3 β€ n β€ 1000) β the dimensions of the chessboard.
The second line contains two integers a_x and a_y (1 β€ a_x, a_y β€ n) β the coordinates of Alice's queen.
The third line contains two integers b_x and b_y (1 β€ b_x, b_y β€ n) β the coordinates of Bob's king.
The fourth line contains two integers c_x and c_y (1 β€ c_x, c_y β€ n) β the coordinates of the location that Bob wants to get to.
It is guaranteed that Bob's king is currently not in check and the target location is not in check either.
Furthermore, the king is not located on the same square as the queen (i.e. a_x β b_x or a_y β b_y), and the target does coincide neither with the queen's position (i.e. c_x β a_x or c_y β a_y) nor with the king's position (i.e. c_x β b_x or c_y β b_y).
Output
Print "YES" (without quotes) if Bob can get from (b_x, b_y) to (c_x, c_y) without ever getting in check, otherwise print "NO".
You can print each letter in any case (upper or lower).
Examples
Input
8
4 4
1 3
3 1
Output
YES
Input
8
4 4
2 3
1 6
Output
NO
Input
8
3 5
1 2
6 1
Output
NO
Note
In the diagrams below, the squares controlled by the black queen are marked red, and the target square is marked blue.
In the first case, the king can move, for instance, via the squares (2, 3) and (3, 2). Note that the direct route through (2, 2) goes through check.
<image>
In the second case, the queen watches the fourth rank, and the king has no means of crossing it.
<image>
In the third case, the queen watches the third file.
<image>
Submitted Solution:
```
n=int(input())
a1,a2=list(map(int,input().split()))
b1,b2=list(map(int,input().split()))
c1,c2=list(map(int,input().split()))
if b2>c2:
a2,c2=c2,a2
if b1>c1:
a1,c1=c1,a1
if c1<a1 and c2<a2:
print("YES")
else:
print("NO")
``` | instruction | 0 | 56,419 | 15 | 112,838 |
No | output | 1 | 56,419 | 15 | 112,839 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice and Bob are playing chess on a huge chessboard with dimensions n Γ n. Alice has a single piece left β a queen, located at (a_x, a_y), while Bob has only the king standing at (b_x, b_y). Alice thinks that as her queen is dominating the chessboard, victory is hers.
But Bob has made a devious plan to seize the victory for himself β he needs to march his king to (c_x, c_y) in order to claim the victory for himself. As Alice is distracted by her sense of superiority, she no longer moves any pieces around, and it is only Bob who makes any turns.
Bob will win if he can move his king from (b_x, b_y) to (c_x, c_y) without ever getting in check. Remember that a king can move to any of the 8 adjacent squares. A king is in check if it is on the same rank (i.e. row), file (i.e. column), or diagonal as the enemy queen.
Find whether Bob can win or not.
Input
The first line contains a single integer n (3 β€ n β€ 1000) β the dimensions of the chessboard.
The second line contains two integers a_x and a_y (1 β€ a_x, a_y β€ n) β the coordinates of Alice's queen.
The third line contains two integers b_x and b_y (1 β€ b_x, b_y β€ n) β the coordinates of Bob's king.
The fourth line contains two integers c_x and c_y (1 β€ c_x, c_y β€ n) β the coordinates of the location that Bob wants to get to.
It is guaranteed that Bob's king is currently not in check and the target location is not in check either.
Furthermore, the king is not located on the same square as the queen (i.e. a_x β b_x or a_y β b_y), and the target does coincide neither with the queen's position (i.e. c_x β a_x or c_y β a_y) nor with the king's position (i.e. c_x β b_x or c_y β b_y).
Output
Print "YES" (without quotes) if Bob can get from (b_x, b_y) to (c_x, c_y) without ever getting in check, otherwise print "NO".
You can print each letter in any case (upper or lower).
Examples
Input
8
4 4
1 3
3 1
Output
YES
Input
8
4 4
2 3
1 6
Output
NO
Input
8
3 5
1 2
6 1
Output
NO
Note
In the diagrams below, the squares controlled by the black queen are marked red, and the target square is marked blue.
In the first case, the king can move, for instance, via the squares (2, 3) and (3, 2). Note that the direct route through (2, 2) goes through check.
<image>
In the second case, the queen watches the fourth rank, and the king has no means of crossing it.
<image>
In the third case, the queen watches the third file.
<image>
Submitted Solution:
```
n = int(input())
a1,a2 = [int(i) for i in input().split()]
b1,b2 = [int(i) for i in input().split()]
c1,c2 = [int(i) for i in input().split()]
if c1 == a1+1 and c2 == a2 or c1 ==a1-1 and c2==a2 or c1 == a1-1 and c2 == a2+1 or c1 == a1-1 and c2 == a2-1 or c1 == a1+1 and c2 == a2+1 or c1 == a1+1 and c2 == a2-1 or c1 == a1 and c2 == a2+1 or c1 == a1 and c2 == a2-1:
print('NO')
elif c2 == a2 or c1 == a1:
print('NO')
else:
if b1 < a1 and c1<a1 and c2 > a2 and b2 > a1:
print('YES')
exit()
if b1 < a1 and c1<a1 and c2 < a2 and b2 < a1:
print('YES')
exit()
if b1 > a1 and c1>a1 and c2 > a2 and b2 > a1:
print('YES')
exit()
if b1 > a1 and c1>a1 and c2 < a2 and b2 < a1:
print('YES')
exit()
print('NO')
``` | instruction | 0 | 56,420 | 15 | 112,840 |
No | output | 1 | 56,420 | 15 | 112,841 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice and Bob are playing chess on a huge chessboard with dimensions n Γ n. Alice has a single piece left β a queen, located at (a_x, a_y), while Bob has only the king standing at (b_x, b_y). Alice thinks that as her queen is dominating the chessboard, victory is hers.
But Bob has made a devious plan to seize the victory for himself β he needs to march his king to (c_x, c_y) in order to claim the victory for himself. As Alice is distracted by her sense of superiority, she no longer moves any pieces around, and it is only Bob who makes any turns.
Bob will win if he can move his king from (b_x, b_y) to (c_x, c_y) without ever getting in check. Remember that a king can move to any of the 8 adjacent squares. A king is in check if it is on the same rank (i.e. row), file (i.e. column), or diagonal as the enemy queen.
Find whether Bob can win or not.
Input
The first line contains a single integer n (3 β€ n β€ 1000) β the dimensions of the chessboard.
The second line contains two integers a_x and a_y (1 β€ a_x, a_y β€ n) β the coordinates of Alice's queen.
The third line contains two integers b_x and b_y (1 β€ b_x, b_y β€ n) β the coordinates of Bob's king.
The fourth line contains two integers c_x and c_y (1 β€ c_x, c_y β€ n) β the coordinates of the location that Bob wants to get to.
It is guaranteed that Bob's king is currently not in check and the target location is not in check either.
Furthermore, the king is not located on the same square as the queen (i.e. a_x β b_x or a_y β b_y), and the target does coincide neither with the queen's position (i.e. c_x β a_x or c_y β a_y) nor with the king's position (i.e. c_x β b_x or c_y β b_y).
Output
Print "YES" (without quotes) if Bob can get from (b_x, b_y) to (c_x, c_y) without ever getting in check, otherwise print "NO".
You can print each letter in any case (upper or lower).
Examples
Input
8
4 4
1 3
3 1
Output
YES
Input
8
4 4
2 3
1 6
Output
NO
Input
8
3 5
1 2
6 1
Output
NO
Note
In the diagrams below, the squares controlled by the black queen are marked red, and the target square is marked blue.
In the first case, the king can move, for instance, via the squares (2, 3) and (3, 2). Note that the direct route through (2, 2) goes through check.
<image>
In the second case, the queen watches the fourth rank, and the king has no means of crossing it.
<image>
In the third case, the queen watches the third file.
<image>
Submitted Solution:
```
n = int(input())
a_x,a_y = map(int, input().split(' '))
b_x,b_y = map(int, input().split(' '))
c_x,c_y = map(int, input().split(' '))
def get_quadrant(x,y):
if x < c_x:
if y < c_y:
return 1
else:
return 2
else:
if y < c_y:
return 3
else:
return 4
if get_quadrant(a_x, a_y) == get_quadrant(b_x, b_y):
print('YES')
else:
print('NO')
``` | instruction | 0 | 56,421 | 15 | 112,842 |
No | output | 1 | 56,421 | 15 | 112,843 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Alice and Bob are playing chess on a huge chessboard with dimensions n Γ n. Alice has a single piece left β a queen, located at (a_x, a_y), while Bob has only the king standing at (b_x, b_y). Alice thinks that as her queen is dominating the chessboard, victory is hers.
But Bob has made a devious plan to seize the victory for himself β he needs to march his king to (c_x, c_y) in order to claim the victory for himself. As Alice is distracted by her sense of superiority, she no longer moves any pieces around, and it is only Bob who makes any turns.
Bob will win if he can move his king from (b_x, b_y) to (c_x, c_y) without ever getting in check. Remember that a king can move to any of the 8 adjacent squares. A king is in check if it is on the same rank (i.e. row), file (i.e. column), or diagonal as the enemy queen.
Find whether Bob can win or not.
Input
The first line contains a single integer n (3 β€ n β€ 1000) β the dimensions of the chessboard.
The second line contains two integers a_x and a_y (1 β€ a_x, a_y β€ n) β the coordinates of Alice's queen.
The third line contains two integers b_x and b_y (1 β€ b_x, b_y β€ n) β the coordinates of Bob's king.
The fourth line contains two integers c_x and c_y (1 β€ c_x, c_y β€ n) β the coordinates of the location that Bob wants to get to.
It is guaranteed that Bob's king is currently not in check and the target location is not in check either.
Furthermore, the king is not located on the same square as the queen (i.e. a_x β b_x or a_y β b_y), and the target does coincide neither with the queen's position (i.e. c_x β a_x or c_y β a_y) nor with the king's position (i.e. c_x β b_x or c_y β b_y).
Output
Print "YES" (without quotes) if Bob can get from (b_x, b_y) to (c_x, c_y) without ever getting in check, otherwise print "NO".
You can print each letter in any case (upper or lower).
Examples
Input
8
4 4
1 3
3 1
Output
YES
Input
8
4 4
2 3
1 6
Output
NO
Input
8
3 5
1 2
6 1
Output
NO
Note
In the diagrams below, the squares controlled by the black queen are marked red, and the target square is marked blue.
In the first case, the king can move, for instance, via the squares (2, 3) and (3, 2). Note that the direct route through (2, 2) goes through check.
<image>
In the second case, the queen watches the fourth rank, and the king has no means of crossing it.
<image>
In the third case, the queen watches the third file.
<image>
Submitted Solution:
```
from collections import defaultdict as defdic
from collections import deque
def check(i,n,q,k):
if i<0:
return False
if i>n*n:
return False
ix=i//n
iy=i%n
kx=k//n
ky=k%n
qx=q//n
qy=q%n
if kx>qx and ix>qx:
if ky>qy and iy>qy:
return True
if ky<qy and iy<qy:
return True
if kx<qx and ix<qx:
if ky>qy and iy>qy:
return True
if ky<qy and iy<qy:
return True
return False
class Graph:
def __init__(self):
self.graph = defdic(list)
def addedge(self,u,v):
self.graph[u].append(v)
def BFS(self,u):
visited = defdic(int)
qu=deque()
qu.append(u)
visited[u]=True
x=[]
while qu:
s=qu.popleft()
x.append(s)
for i in self.graph[s]:
if visited[i]==0:
qu.append(i)
visited[i]=True
#print(x)
def BFSsem(self,u,q,c,n,k):
visited = defdic(int)
qu=deque()
qu.append(u)
visited[u]=True
x=[]
while qu:
s=qu.popleft()
x.append(s)
if s==c:
return "Yes"
if s-n>0 and check(s,n,q,k)==True:
g.addedge(s,s-n)
if check(s,n,q,k)==True and s%n!=1:
g.addedge(s,s-n-1)
if check(s,n,q,k)==True and s%n!=0:
g.addedge(s,s-n+1)
if s+n<=n*n and check(s,n,q,k)==True:
g.addedge(s,s+n)
if check(s,n,q,k)==True and s%n!=1:
g.addedge(s,s+n-1)
if check(s,n,q,k)==True and s%n!=0:
g.addedge(s,s+n+1)
if s%n!=1 and check(s,n,q,k)==True:
g.addedge(s,s-1)
if s%n!=0 and check(s,n,q,k)==True:
g.addedge(s,s+1)
for i in self.graph[s]:
if i==c:
return "Yes"
if visited[i]==0:
qu.append(i)
visited[i]=True
return "No"
def DFSUtil(self, s, visited,k):
stack = []
dpt={}
stack.append(s)
while (len(stack) != 0):
s = stack[-1]
if (not visited[s]):
k.add(s)
visited[s] = True
dpt[s]=len(k)
else :
s=stack.pop()
k.remove(s)
continue
if s==self.lens:
k=sorted(k)
for j in k:
print(j,end=" ")
return 0
l=0
for i in self.graph[s]:
if not visited[i]:
stack.append(i)
l+=1
return 1
def DFS(self):
k=set()
visited = defdic(int)
for i in self.graph:
if (not visited[i]):
x=self.DFSUtil(i, visited,k)
if x==0:
return
#print(k)
n=int(input())
qx,qy=map(int,input().split())
kx,ky=map(int,input().split())
cx,cy=map(int,input().split())
g=Graph()
kx-=1
ky-=1
qx-=1
qy-=1
cx-=1
cy-=1
print(g.BFSsem(((kx-1)*n)+ky,((qx-1)*n)+qy,((cx-1)*n)+cy,n,((kx-1)*n)+ky))
g.lens=n
'''1000
400 500
32 796
415 888'''
``` | instruction | 0 | 56,422 | 15 | 112,844 |
No | output | 1 | 56,422 | 15 | 112,845 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Scrooge McDuck keeps his most treasured savings in a home safe with a combination lock. Each time he wants to put there the treasures that he's earned fair and square, he has to open the lock.
<image>
The combination lock is represented by n rotating disks with digits from 0 to 9 written on them. Scrooge McDuck has to turn some disks so that the combination of digits on the disks forms a secret combination. In one move, he can rotate one disk one digit forwards or backwards. In particular, in one move he can go from digit 0 to digit 9 and vice versa. What minimum number of actions does he need for that?
Input
The first line contains a single integer n (1 β€ n β€ 1000) β the number of disks on the combination lock.
The second line contains a string of n digits β the original state of the disks.
The third line contains a string of n digits β Scrooge McDuck's combination that opens the lock.
Output
Print a single integer β the minimum number of moves Scrooge McDuck needs to open the lock.
Examples
Input
5
82195
64723
Output
13
Note
In the sample he needs 13 moves:
* 1 disk: <image>
* 2 disk: <image>
* 3 disk: <image>
* 4 disk: <image>
* 5 disk: <image> | instruction | 0 | 56,711 | 15 | 113,422 |
Tags: implementation
Correct Solution:
```
input(); cnt = 0
for i, j in (zip(input(), input())):
i, j = int(i), int(j)
cnt+=(abs(i-j), 10+min(i, j)-max(i, j))[abs(i-j)>5]
print(cnt)
#-------------------------------code_marshal--------------------------------
``` | output | 1 | 56,711 | 15 | 113,423 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Scrooge McDuck keeps his most treasured savings in a home safe with a combination lock. Each time he wants to put there the treasures that he's earned fair and square, he has to open the lock.
<image>
The combination lock is represented by n rotating disks with digits from 0 to 9 written on them. Scrooge McDuck has to turn some disks so that the combination of digits on the disks forms a secret combination. In one move, he can rotate one disk one digit forwards or backwards. In particular, in one move he can go from digit 0 to digit 9 and vice versa. What minimum number of actions does he need for that?
Input
The first line contains a single integer n (1 β€ n β€ 1000) β the number of disks on the combination lock.
The second line contains a string of n digits β the original state of the disks.
The third line contains a string of n digits β Scrooge McDuck's combination that opens the lock.
Output
Print a single integer β the minimum number of moves Scrooge McDuck needs to open the lock.
Examples
Input
5
82195
64723
Output
13
Note
In the sample he needs 13 moves:
* 1 disk: <image>
* 2 disk: <image>
* 3 disk: <image>
* 4 disk: <image>
* 5 disk: <image> | instruction | 0 | 56,712 | 15 | 113,424 |
Tags: implementation
Correct Solution:
```
n=int(input())
ex=input()
out=input()
mo=0
for i in range(n):
s=abs(int(ex[i])-int(out[i]))
if(s>5):
s=10-s
mo+=s
print(mo)
``` | output | 1 | 56,712 | 15 | 113,425 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Scrooge McDuck keeps his most treasured savings in a home safe with a combination lock. Each time he wants to put there the treasures that he's earned fair and square, he has to open the lock.
<image>
The combination lock is represented by n rotating disks with digits from 0 to 9 written on them. Scrooge McDuck has to turn some disks so that the combination of digits on the disks forms a secret combination. In one move, he can rotate one disk one digit forwards or backwards. In particular, in one move he can go from digit 0 to digit 9 and vice versa. What minimum number of actions does he need for that?
Input
The first line contains a single integer n (1 β€ n β€ 1000) β the number of disks on the combination lock.
The second line contains a string of n digits β the original state of the disks.
The third line contains a string of n digits β Scrooge McDuck's combination that opens the lock.
Output
Print a single integer β the minimum number of moves Scrooge McDuck needs to open the lock.
Examples
Input
5
82195
64723
Output
13
Note
In the sample he needs 13 moves:
* 1 disk: <image>
* 2 disk: <image>
* 3 disk: <image>
* 4 disk: <image>
* 5 disk: <image> | instruction | 0 | 56,713 | 15 | 113,426 |
Tags: implementation
Correct Solution:
```
n=int(input())
m1=input()
m2=input()
x=0
for i in range(len(m1)):
p=abs(int(m1[i])-int(m2[i]))
q=10-int(m1[i])+int(m2[i])
s=10-int(m2[i])+int(m1[i])
x=x+min(p,q,s)
print(x)
``` | output | 1 | 56,713 | 15 | 113,427 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Scrooge McDuck keeps his most treasured savings in a home safe with a combination lock. Each time he wants to put there the treasures that he's earned fair and square, he has to open the lock.
<image>
The combination lock is represented by n rotating disks with digits from 0 to 9 written on them. Scrooge McDuck has to turn some disks so that the combination of digits on the disks forms a secret combination. In one move, he can rotate one disk one digit forwards or backwards. In particular, in one move he can go from digit 0 to digit 9 and vice versa. What minimum number of actions does he need for that?
Input
The first line contains a single integer n (1 β€ n β€ 1000) β the number of disks on the combination lock.
The second line contains a string of n digits β the original state of the disks.
The third line contains a string of n digits β Scrooge McDuck's combination that opens the lock.
Output
Print a single integer β the minimum number of moves Scrooge McDuck needs to open the lock.
Examples
Input
5
82195
64723
Output
13
Note
In the sample he needs 13 moves:
* 1 disk: <image>
* 2 disk: <image>
* 3 disk: <image>
* 4 disk: <image>
* 5 disk: <image> | instruction | 0 | 56,714 | 15 | 113,428 |
Tags: implementation
Correct Solution:
```
# f = open('input')
n = int(input())
num1 = [int(x) for x in str(input().strip())]
num2 = [int(x) for x in str(input().strip())]
# print(num1)
# print(num2)
print(sum([min(abs(num1[x] - num2[x]), (min(num1[x], num2[x]) + 10 - max(num1[x],num2[x]))) for x in range(n)]))
``` | output | 1 | 56,714 | 15 | 113,429 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Scrooge McDuck keeps his most treasured savings in a home safe with a combination lock. Each time he wants to put there the treasures that he's earned fair and square, he has to open the lock.
<image>
The combination lock is represented by n rotating disks with digits from 0 to 9 written on them. Scrooge McDuck has to turn some disks so that the combination of digits on the disks forms a secret combination. In one move, he can rotate one disk one digit forwards or backwards. In particular, in one move he can go from digit 0 to digit 9 and vice versa. What minimum number of actions does he need for that?
Input
The first line contains a single integer n (1 β€ n β€ 1000) β the number of disks on the combination lock.
The second line contains a string of n digits β the original state of the disks.
The third line contains a string of n digits β Scrooge McDuck's combination that opens the lock.
Output
Print a single integer β the minimum number of moves Scrooge McDuck needs to open the lock.
Examples
Input
5
82195
64723
Output
13
Note
In the sample he needs 13 moves:
* 1 disk: <image>
* 2 disk: <image>
* 3 disk: <image>
* 4 disk: <image>
* 5 disk: <image> | instruction | 0 | 56,715 | 15 | 113,430 |
Tags: implementation
Correct Solution:
```
n=int(input())
a=input()
b=input()
turns=0
for i in range(n):
if (int(a[i])-int(b[i]))>0:
turns=turns+min( int(a[i]) - int(b[i]), 10 - (int(a[i]) - int(b[i])) )
else:
turns=turns+min( int(b[i])-int(a[i]), 10 + int(a[i]) - int(b[i]) )
print(turns)
``` | output | 1 | 56,715 | 15 | 113,431 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Scrooge McDuck keeps his most treasured savings in a home safe with a combination lock. Each time he wants to put there the treasures that he's earned fair and square, he has to open the lock.
<image>
The combination lock is represented by n rotating disks with digits from 0 to 9 written on them. Scrooge McDuck has to turn some disks so that the combination of digits on the disks forms a secret combination. In one move, he can rotate one disk one digit forwards or backwards. In particular, in one move he can go from digit 0 to digit 9 and vice versa. What minimum number of actions does he need for that?
Input
The first line contains a single integer n (1 β€ n β€ 1000) β the number of disks on the combination lock.
The second line contains a string of n digits β the original state of the disks.
The third line contains a string of n digits β Scrooge McDuck's combination that opens the lock.
Output
Print a single integer β the minimum number of moves Scrooge McDuck needs to open the lock.
Examples
Input
5
82195
64723
Output
13
Note
In the sample he needs 13 moves:
* 1 disk: <image>
* 2 disk: <image>
* 3 disk: <image>
* 4 disk: <image>
* 5 disk: <image> | instruction | 0 | 56,716 | 15 | 113,432 |
Tags: implementation
Correct Solution:
```
n = int(input())
disc_1 = input()
disc_2 = input()
s = 0
for i in range(n):
s += min(abs(int(disc_1[i])-int(disc_2[i])), 10-abs(int(disc_1[i])-int(disc_2[i])))
print(s)
``` | output | 1 | 56,716 | 15 | 113,433 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Scrooge McDuck keeps his most treasured savings in a home safe with a combination lock. Each time he wants to put there the treasures that he's earned fair and square, he has to open the lock.
<image>
The combination lock is represented by n rotating disks with digits from 0 to 9 written on them. Scrooge McDuck has to turn some disks so that the combination of digits on the disks forms a secret combination. In one move, he can rotate one disk one digit forwards or backwards. In particular, in one move he can go from digit 0 to digit 9 and vice versa. What minimum number of actions does he need for that?
Input
The first line contains a single integer n (1 β€ n β€ 1000) β the number of disks on the combination lock.
The second line contains a string of n digits β the original state of the disks.
The third line contains a string of n digits β Scrooge McDuck's combination that opens the lock.
Output
Print a single integer β the minimum number of moves Scrooge McDuck needs to open the lock.
Examples
Input
5
82195
64723
Output
13
Note
In the sample he needs 13 moves:
* 1 disk: <image>
* 2 disk: <image>
* 3 disk: <image>
* 4 disk: <image>
* 5 disk: <image> | instruction | 0 | 56,717 | 15 | 113,434 |
Tags: implementation
Correct Solution:
```
n = int(input())
a = input()
b = input()
rotation = 0
for i in range(len(a)):
if abs(int(a[i]) - int(b[i])) <= 5:
rotation = rotation + abs(int(a[i]) - int(b[i]))
else:
rotation = rotation + (9 - max(int(a[i]), int(b[i])) + 1 + min(int(a[i]), int(b[i])))
print(rotation)
``` | output | 1 | 56,717 | 15 | 113,435 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Scrooge McDuck keeps his most treasured savings in a home safe with a combination lock. Each time he wants to put there the treasures that he's earned fair and square, he has to open the lock.
<image>
The combination lock is represented by n rotating disks with digits from 0 to 9 written on them. Scrooge McDuck has to turn some disks so that the combination of digits on the disks forms a secret combination. In one move, he can rotate one disk one digit forwards or backwards. In particular, in one move he can go from digit 0 to digit 9 and vice versa. What minimum number of actions does he need for that?
Input
The first line contains a single integer n (1 β€ n β€ 1000) β the number of disks on the combination lock.
The second line contains a string of n digits β the original state of the disks.
The third line contains a string of n digits β Scrooge McDuck's combination that opens the lock.
Output
Print a single integer β the minimum number of moves Scrooge McDuck needs to open the lock.
Examples
Input
5
82195
64723
Output
13
Note
In the sample he needs 13 moves:
* 1 disk: <image>
* 2 disk: <image>
* 3 disk: <image>
* 4 disk: <image>
* 5 disk: <image> | instruction | 0 | 56,718 | 15 | 113,436 |
Tags: implementation
Correct Solution:
```
e = int(input())
a = input()
b = input()
res = 0
i = 0
while i != e:
res += min(abs(max(int(a[i]),int(b[i]))-min(int(a[i]),int(b[i]))), abs(10-max(int(a[i]),int(b[i]))+min(int(a[i]),int(b[i]))))
i += 1
print(res)
``` | output | 1 | 56,718 | 15 | 113,437 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Scrooge McDuck keeps his most treasured savings in a home safe with a combination lock. Each time he wants to put there the treasures that he's earned fair and square, he has to open the lock.
<image>
The combination lock is represented by n rotating disks with digits from 0 to 9 written on them. Scrooge McDuck has to turn some disks so that the combination of digits on the disks forms a secret combination. In one move, he can rotate one disk one digit forwards or backwards. In particular, in one move he can go from digit 0 to digit 9 and vice versa. What minimum number of actions does he need for that?
Input
The first line contains a single integer n (1 β€ n β€ 1000) β the number of disks on the combination lock.
The second line contains a string of n digits β the original state of the disks.
The third line contains a string of n digits β Scrooge McDuck's combination that opens the lock.
Output
Print a single integer β the minimum number of moves Scrooge McDuck needs to open the lock.
Examples
Input
5
82195
64723
Output
13
Note
In the sample he needs 13 moves:
* 1 disk: <image>
* 2 disk: <image>
* 3 disk: <image>
* 4 disk: <image>
* 5 disk: <image>
Submitted Solution:
```
n=int(input())
a=list(map(int,list(input())))
b=list(map(int,list(input())))
moves=0
for i in range(n):
clockwise=abs(a[i]-b[i])
counterClockwise=abs(9-max([ a[i],b[i] ]) + min([ a[i],b[i] ]) +1 )
if clockwise<=counterClockwise:
moves=moves+clockwise
print(moves)
else:
moves=moves+counterClockwise
print(moves)
``` | instruction | 0 | 56,724 | 15 | 113,448 |
No | output | 1 | 56,724 | 15 | 113,449 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Theater stage is a rectangular field of size n Γ m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.
You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) β left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.
A position is good if two conditions hold:
* there is no actor in the cell the spotlight is placed to;
* there is at least one actor in the direction the spotlight projects.
Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
Input
The first line contains two positive integers n and m (1 β€ n, m β€ 1000) β the number of rows and the number of columns in the plan.
The next n lines contain m integers, 0 or 1 each β the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
Output
Print one integer β the number of good positions for placing the spotlight.
Examples
Input
2 4
0 1 0 0
1 0 1 0
Output
9
Input
4 4
0 0 0 0
1 0 0 1
0 1 1 0
0 1 0 0
Output
20
Note
In the first example the following positions are good:
1. the (1, 1) cell and right direction;
2. the (1, 1) cell and down direction;
3. the (1, 3) cell and left direction;
4. the (1, 3) cell and down direction;
5. the (1, 4) cell and left direction;
6. the (2, 2) cell and left direction;
7. the (2, 2) cell and up direction;
8. the (2, 2) and right direction;
9. the (2, 4) cell and left direction.
Therefore, there are 9 good positions in this example. | instruction | 0 | 56,784 | 15 | 113,568 |
Tags: dp, implementation
Correct Solution:
```
n,m = list(map(int,input().split()))
ss = [list(map(int,input().split())) for _ in range(n)]
ll = []
ll.append([ss[i][j] for i in range(n) for j in range(m)]) #(t-d)(l-r)
ll.append([ss[i][j] for i in range(n) for j in range(m-1,-1,-1)]) #(t-d)(r-l)
ll.append([ss[i][j] for j in range(m) for i in range(n)]) #(l-r)(t-d)
ll.append([ss[i][j] for j in range(m) for i in range(n-1,-1,-1)]) #(l-r)(b-t)
cnt = 0
for t in range(4):
l = ll[t]
run,nor = (m,n) if t<2 else (n,m)
for r in range(nor):
rs = r*run
ho = 0 #0 for no object!
for i in range(run):
if l[rs+i] == 1:
ho = 1
continue
cnt += ho
print(cnt)
``` | output | 1 | 56,784 | 15 | 113,569 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Theater stage is a rectangular field of size n Γ m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.
You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) β left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.
A position is good if two conditions hold:
* there is no actor in the cell the spotlight is placed to;
* there is at least one actor in the direction the spotlight projects.
Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
Input
The first line contains two positive integers n and m (1 β€ n, m β€ 1000) β the number of rows and the number of columns in the plan.
The next n lines contain m integers, 0 or 1 each β the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
Output
Print one integer β the number of good positions for placing the spotlight.
Examples
Input
2 4
0 1 0 0
1 0 1 0
Output
9
Input
4 4
0 0 0 0
1 0 0 1
0 1 1 0
0 1 0 0
Output
20
Note
In the first example the following positions are good:
1. the (1, 1) cell and right direction;
2. the (1, 1) cell and down direction;
3. the (1, 3) cell and left direction;
4. the (1, 3) cell and down direction;
5. the (1, 4) cell and left direction;
6. the (2, 2) cell and left direction;
7. the (2, 2) cell and up direction;
8. the (2, 2) and right direction;
9. the (2, 4) cell and left direction.
Therefore, there are 9 good positions in this example. | instruction | 0 | 56,785 | 15 | 113,570 |
Tags: dp, implementation
Correct Solution:
```
num_input = input()
num_input = num_input.split(" ")
n = int(num_input[0])
m = int(num_input[1])
arr = []
for i in range(0, n):
arr.append(input().split(" "))
t_arr = list(map(list, zip(*arr)))
res = 0
for i in range(0, n):
num_1 = arr[i].count("1")
if num_1 > 0:
l = arr[i].index("1")
r = m - 1 - arr[i][::-1].index("1")
res += l
res += (m - 1 - r)
res += (r - l + 1 - num_1) * 2
for i in range(0, m):
num_1 = t_arr[i].count("1")
if num_1 > 0:
l = t_arr[i].index("1")
r = n - 1 - t_arr[i][::-1].index("1")
res += l
res += (n - 1 - r)
res += (r - l + 1 - num_1) * 2
print(res)
``` | output | 1 | 56,785 | 15 | 113,571 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Theater stage is a rectangular field of size n Γ m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.
You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) β left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.
A position is good if two conditions hold:
* there is no actor in the cell the spotlight is placed to;
* there is at least one actor in the direction the spotlight projects.
Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
Input
The first line contains two positive integers n and m (1 β€ n, m β€ 1000) β the number of rows and the number of columns in the plan.
The next n lines contain m integers, 0 or 1 each β the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
Output
Print one integer β the number of good positions for placing the spotlight.
Examples
Input
2 4
0 1 0 0
1 0 1 0
Output
9
Input
4 4
0 0 0 0
1 0 0 1
0 1 1 0
0 1 0 0
Output
20
Note
In the first example the following positions are good:
1. the (1, 1) cell and right direction;
2. the (1, 1) cell and down direction;
3. the (1, 3) cell and left direction;
4. the (1, 3) cell and down direction;
5. the (1, 4) cell and left direction;
6. the (2, 2) cell and left direction;
7. the (2, 2) cell and up direction;
8. the (2, 2) and right direction;
9. the (2, 4) cell and left direction.
Therefore, there are 9 good positions in this example. | instruction | 0 | 56,786 | 15 | 113,572 |
Tags: dp, implementation
Correct Solution:
```
n,m = list(map(int,input().split()))
arr = []
for i in range(n):
arr.append(list(map(int,input().split())))
c = 0
for i in range(n):
was = 0
for g in range(m):
if not was:
was = arr[i][g]
else:
c+=arr[i][g]==0
was = 0
for g in range(m-1,-1,-1):
if not was:
was = arr[i][g]
else:
c += arr[i][g] == 0
for i in range(m):
was = 0
for g in range(n):
if not was:
was = arr[g][i]
else:
c+=arr[g][i]==0
was = 0
for g in range(n-1,-1,-1):
if not was:
was = arr[g][i]
else:
c += arr[g][i] == 0
print(c)
``` | output | 1 | 56,786 | 15 | 113,573 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Theater stage is a rectangular field of size n Γ m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.
You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) β left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.
A position is good if two conditions hold:
* there is no actor in the cell the spotlight is placed to;
* there is at least one actor in the direction the spotlight projects.
Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
Input
The first line contains two positive integers n and m (1 β€ n, m β€ 1000) β the number of rows and the number of columns in the plan.
The next n lines contain m integers, 0 or 1 each β the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
Output
Print one integer β the number of good positions for placing the spotlight.
Examples
Input
2 4
0 1 0 0
1 0 1 0
Output
9
Input
4 4
0 0 0 0
1 0 0 1
0 1 1 0
0 1 0 0
Output
20
Note
In the first example the following positions are good:
1. the (1, 1) cell and right direction;
2. the (1, 1) cell and down direction;
3. the (1, 3) cell and left direction;
4. the (1, 3) cell and down direction;
5. the (1, 4) cell and left direction;
6. the (2, 2) cell and left direction;
7. the (2, 2) cell and up direction;
8. the (2, 2) and right direction;
9. the (2, 4) cell and left direction.
Therefore, there are 9 good positions in this example. | instruction | 0 | 56,787 | 15 | 113,574 |
Tags: dp, implementation
Correct Solution:
```
class CodeforcesTask729BSolution:
def __init__(self):
self.result = ''
self.n_m = []
self.plan = []
def read_input(self):
self.n_m = [int(x) for x in input().split(" ")]
for x in range(self.n_m[0]):
self.plan.append([int(y) for y in input().split(" ")])
def process_task(self):
left_rows = [[0] * self.n_m[1] for x in range(self.n_m[0])]
right_rows = [[0] * self.n_m[1] for x in range(self.n_m[0])]
top_cols = [[0] * self.n_m[1] for x in range(self.n_m[0])]
bottom_cols = [[0] * self.n_m[1] for x in range(self.n_m[0])]
for y in range(self.n_m[0]):
left_rows[y][0] = self.plan[y][0]
for x in range(1, self.n_m[1]):
for y in range(self.n_m[0]):
left_rows[y][x] = self.plan[y][x] + left_rows[y][x - 1]
for y in range(self.n_m[0]):
right_rows[y][-1] = self.plan[y][-1]
for x in range(self.n_m[1] - 2, -1, -1):
for y in range(self.n_m[0]):
right_rows[y][x] = self.plan[y][x] + right_rows[y][x + 1]
for x in range(self.n_m[1]):
top_cols[0][x] = self.plan[0][x]
for x in range(self.n_m[1]):
for y in range(1, self.n_m[0]):
top_cols[y][x] = self.plan[y][x] + top_cols[y - 1][x]
for x in range(self.n_m[1]):
bottom_cols[-1][x] = self.plan[-1][x]
for x in range(self.n_m[1]):
for y in range(self.n_m[0] - 2, -1, -1):
bottom_cols[y][x] = self.plan[y][x] + bottom_cols[y + 1][x]
"""print("left")
for r in left_rows:
print(r)
print("right")
for r in right_rows:
print(r)
print("top")
for r in top_cols:
print(r)
print("bottom")
for r in bottom_cols:
print(r)"""
cnt = 0
ways = [[0] * self.n_m[1] for x in range(self.n_m[0])]
for x in range(self.n_m[1]):
for y in range(self.n_m[0]):
if not self.plan[y][x]:
if left_rows[y][x]:
cnt += 1
ways[y][x] += 1
if right_rows[y][x]:
cnt += 1
ways[y][x] += 1
if top_cols[y][x]:
cnt += 1
ways[y][x] += 1
if bottom_cols[y][x]:
cnt += 1
ways[y][x] += 1
self.result = str(cnt)
#print("ways")
#for r in ways:
# print(r)
def get_result(self):
return self.result
if __name__ == "__main__":
Solution = CodeforcesTask729BSolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result())
``` | output | 1 | 56,787 | 15 | 113,575 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Theater stage is a rectangular field of size n Γ m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.
You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) β left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.
A position is good if two conditions hold:
* there is no actor in the cell the spotlight is placed to;
* there is at least one actor in the direction the spotlight projects.
Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
Input
The first line contains two positive integers n and m (1 β€ n, m β€ 1000) β the number of rows and the number of columns in the plan.
The next n lines contain m integers, 0 or 1 each β the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
Output
Print one integer β the number of good positions for placing the spotlight.
Examples
Input
2 4
0 1 0 0
1 0 1 0
Output
9
Input
4 4
0 0 0 0
1 0 0 1
0 1 1 0
0 1 0 0
Output
20
Note
In the first example the following positions are good:
1. the (1, 1) cell and right direction;
2. the (1, 1) cell and down direction;
3. the (1, 3) cell and left direction;
4. the (1, 3) cell and down direction;
5. the (1, 4) cell and left direction;
6. the (2, 2) cell and left direction;
7. the (2, 2) cell and up direction;
8. the (2, 2) and right direction;
9. the (2, 4) cell and left direction.
Therefore, there are 9 good positions in this example. | instruction | 0 | 56,788 | 15 | 113,576 |
Tags: dp, implementation
Correct Solution:
```
n,m = list(map(int,input().split()))
mat = [list(map(int,input().split())) for i in range(n)]
ans = [[0]*m for i in range(n)]
# left to right traversal on row and right to left on row
for i in range(n):
f = 0
for j in range(m):
if mat[i][j]==0:
ans[i][j]+=f
if mat[i][j]==1:
f = 1
f = 0
for j in range(m-1,-1,-1):
if mat[i][j]==0:
ans[i][j]+=f
if mat[i][j]==1:
f = 1
# top to bottom and bottom to top traversal on columns
for i in range(m):
f = 0
for j in range(n):
if mat[j][i]==0:
ans[j][i]+=f
if mat[j][i]==1:
f = 1
f = 0
for j in range(n-1,-1,-1):
if mat[j][i]==0:
ans[j][i]+=f
if mat[j][i]==1:
f = 1
cnt = 0
for i in ans:
cnt+=sum(i)
print(cnt)
``` | output | 1 | 56,788 | 15 | 113,577 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Theater stage is a rectangular field of size n Γ m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.
You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) β left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.
A position is good if two conditions hold:
* there is no actor in the cell the spotlight is placed to;
* there is at least one actor in the direction the spotlight projects.
Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
Input
The first line contains two positive integers n and m (1 β€ n, m β€ 1000) β the number of rows and the number of columns in the plan.
The next n lines contain m integers, 0 or 1 each β the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
Output
Print one integer β the number of good positions for placing the spotlight.
Examples
Input
2 4
0 1 0 0
1 0 1 0
Output
9
Input
4 4
0 0 0 0
1 0 0 1
0 1 1 0
0 1 0 0
Output
20
Note
In the first example the following positions are good:
1. the (1, 1) cell and right direction;
2. the (1, 1) cell and down direction;
3. the (1, 3) cell and left direction;
4. the (1, 3) cell and down direction;
5. the (1, 4) cell and left direction;
6. the (2, 2) cell and left direction;
7. the (2, 2) cell and up direction;
8. the (2, 2) and right direction;
9. the (2, 4) cell and left direction.
Therefore, there are 9 good positions in this example. | instruction | 0 | 56,789 | 15 | 113,578 |
Tags: dp, implementation
Correct Solution:
```
n, m = map(int, input().split())
a = []
t_a = []
ans = 0
for i in range(n):
x = list(map(int, input().split()))
a.append(x)
t_a = list(map(list, zip(*a)))
#print(a)
#print(t_a)
for i in range(n):
c = a[i].count(1)
if (c > 1):
l = a[i].index(1)
r = m - a[i][::-1].index(1) - 1
#print(l, r, ((m - c - (l + (m - r - 1)))))
ans += l + (m - r - 1) + ((m - c - (l + (m - r - 1))) * 2)
elif (c == 1):
ans += m - 1
#print(ans)
for i in range(m):
c = t_a[i].count(1)
if (c > 1):
l = t_a[i].index(1)
r = n - t_a[i][::-1].index(1) - 1
ans += l + (n - r - 1) + ((n - c - (l + (n - r - 1))) * 2)
elif (c == 1):
ans += n - 1
#print(ans)
print(ans)
``` | output | 1 | 56,789 | 15 | 113,579 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Theater stage is a rectangular field of size n Γ m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.
You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) β left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.
A position is good if two conditions hold:
* there is no actor in the cell the spotlight is placed to;
* there is at least one actor in the direction the spotlight projects.
Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
Input
The first line contains two positive integers n and m (1 β€ n, m β€ 1000) β the number of rows and the number of columns in the plan.
The next n lines contain m integers, 0 or 1 each β the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
Output
Print one integer β the number of good positions for placing the spotlight.
Examples
Input
2 4
0 1 0 0
1 0 1 0
Output
9
Input
4 4
0 0 0 0
1 0 0 1
0 1 1 0
0 1 0 0
Output
20
Note
In the first example the following positions are good:
1. the (1, 1) cell and right direction;
2. the (1, 1) cell and down direction;
3. the (1, 3) cell and left direction;
4. the (1, 3) cell and down direction;
5. the (1, 4) cell and left direction;
6. the (2, 2) cell and left direction;
7. the (2, 2) cell and up direction;
8. the (2, 2) and right direction;
9. the (2, 4) cell and left direction.
Therefore, there are 9 good positions in this example. | instruction | 0 | 56,790 | 15 | 113,580 |
Tags: dp, implementation
Correct Solution:
```
n,m=map(int,input().split())
ans=0
a=[]
for i in range(n):
a.append(list(map(int,input().split())))
for i in range(n):
for j in range(m):
if a[i][j]:
ans+=a[i][j:].count(0)
break
for j in range(m-1,-1,-1):
if a[i][j]:
ans+=a[i][:j].count(0)
break
for j in range(m):
for i in range(n):
if a[i][j]:
for k in range(i,n):
if a[k][j]==0:
ans+=1
break
for i in range(n-1,0,-1):
if a[i][j]:
for k in range(i):
if a[k][j]==0:
ans+=1
break
print(ans)
``` | output | 1 | 56,790 | 15 | 113,581 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Theater stage is a rectangular field of size n Γ m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.
You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) β left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.
A position is good if two conditions hold:
* there is no actor in the cell the spotlight is placed to;
* there is at least one actor in the direction the spotlight projects.
Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
Input
The first line contains two positive integers n and m (1 β€ n, m β€ 1000) β the number of rows and the number of columns in the plan.
The next n lines contain m integers, 0 or 1 each β the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
Output
Print one integer β the number of good positions for placing the spotlight.
Examples
Input
2 4
0 1 0 0
1 0 1 0
Output
9
Input
4 4
0 0 0 0
1 0 0 1
0 1 1 0
0 1 0 0
Output
20
Note
In the first example the following positions are good:
1. the (1, 1) cell and right direction;
2. the (1, 1) cell and down direction;
3. the (1, 3) cell and left direction;
4. the (1, 3) cell and down direction;
5. the (1, 4) cell and left direction;
6. the (2, 2) cell and left direction;
7. the (2, 2) cell and up direction;
8. the (2, 2) and right direction;
9. the (2, 4) cell and left direction.
Therefore, there are 9 good positions in this example. | instruction | 0 | 56,791 | 15 | 113,582 |
Tags: dp, implementation
Correct Solution:
```
#
# Created by Polusummator on 25.10.2020
# --------- Little PyPy Squad ---------
# Verdict: TLE
#
n, m = [int(i) for i in input().split()]
A = [0] * n
ones = 0
for i in range(n):
a = [int(i) for i in input().split()]
A[i] = a
ones += a.count(1)
ans = 0
for i in range(n):
for j in range(m):
if A[i][j] == 1:
ans += m - j
break
for j in range(m - 1, -1, -1):
if A[i][j] == 1:
ans += j + 1
break
for i in range(m):
for j in range(n):
if A[j][i] == 1:
ans += n - j
break
for j in range(n - 1, -1, -1):
if A[j][i] == 1:
ans += j + 1
break
print(ans - 4 * ones)
``` | output | 1 | 56,791 | 15 | 113,583 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Theater stage is a rectangular field of size n Γ m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.
You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) β left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.
A position is good if two conditions hold:
* there is no actor in the cell the spotlight is placed to;
* there is at least one actor in the direction the spotlight projects.
Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
Input
The first line contains two positive integers n and m (1 β€ n, m β€ 1000) β the number of rows and the number of columns in the plan.
The next n lines contain m integers, 0 or 1 each β the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
Output
Print one integer β the number of good positions for placing the spotlight.
Examples
Input
2 4
0 1 0 0
1 0 1 0
Output
9
Input
4 4
0 0 0 0
1 0 0 1
0 1 1 0
0 1 0 0
Output
20
Note
In the first example the following positions are good:
1. the (1, 1) cell and right direction;
2. the (1, 1) cell and down direction;
3. the (1, 3) cell and left direction;
4. the (1, 3) cell and down direction;
5. the (1, 4) cell and left direction;
6. the (2, 2) cell and left direction;
7. the (2, 2) cell and up direction;
8. the (2, 2) and right direction;
9. the (2, 4) cell and left direction.
Therefore, there are 9 good positions in this example.
Submitted Solution:
```
I = lambda: input().split()
n, m = map(int, I())
A, p = [I() for _ in range(n)], 0
for x in range(n):
for y in range(m):
if A[x][y] == '1':
for dx, dy in ((-1, 0), (1, 0), (0, -1), (0, 1)):
k = 0
while (0 <= x+(k+1)*dx < n and 0 <= y+(k+1)*dy < m
and A[x+(k+1)*dx][y+(k+1)*dy] == '0'):
k += 1
p += k
print(p)
``` | instruction | 0 | 56,792 | 15 | 113,584 |
Yes | output | 1 | 56,792 | 15 | 113,585 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Theater stage is a rectangular field of size n Γ m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.
You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) β left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.
A position is good if two conditions hold:
* there is no actor in the cell the spotlight is placed to;
* there is at least one actor in the direction the spotlight projects.
Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
Input
The first line contains two positive integers n and m (1 β€ n, m β€ 1000) β the number of rows and the number of columns in the plan.
The next n lines contain m integers, 0 or 1 each β the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
Output
Print one integer β the number of good positions for placing the spotlight.
Examples
Input
2 4
0 1 0 0
1 0 1 0
Output
9
Input
4 4
0 0 0 0
1 0 0 1
0 1 1 0
0 1 0 0
Output
20
Note
In the first example the following positions are good:
1. the (1, 1) cell and right direction;
2. the (1, 1) cell and down direction;
3. the (1, 3) cell and left direction;
4. the (1, 3) cell and down direction;
5. the (1, 4) cell and left direction;
6. the (2, 2) cell and left direction;
7. the (2, 2) cell and up direction;
8. the (2, 2) and right direction;
9. the (2, 4) cell and left direction.
Therefore, there are 9 good positions in this example.
Submitted Solution:
```
from sys import stdin,stdout
input = stdin.readline
n,m = map(int, input().split())
arr = []
for i in range(n):
arr.append(list(map(int, input().split())))
dp = [[0 for i in range(m)] for j in range(n)]
for i in range(n):
t = 0
for j in range(m):
if arr[i][j] == 0: dp[i][j] += t
else: t = 1
t = 0
for j in range(m-1,-1,-1):
if arr[i][j] == 0: dp[i][j] += t
else: t = 1
for i in range(m):
t = 0
for j in range(n):
if arr[j][i] == 0: dp[j][i] += t
else: t = 1
t = 0
for j in range(n-1,-1,-1):
if arr[j][i] == 0: dp[j][i] += t
else: t = 1
ans = 0
for i in dp:
ans += sum(i)
print(ans)
``` | instruction | 0 | 56,793 | 15 | 113,586 |
Yes | output | 1 | 56,793 | 15 | 113,587 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Theater stage is a rectangular field of size n Γ m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.
You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) β left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.
A position is good if two conditions hold:
* there is no actor in the cell the spotlight is placed to;
* there is at least one actor in the direction the spotlight projects.
Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
Input
The first line contains two positive integers n and m (1 β€ n, m β€ 1000) β the number of rows and the number of columns in the plan.
The next n lines contain m integers, 0 or 1 each β the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
Output
Print one integer β the number of good positions for placing the spotlight.
Examples
Input
2 4
0 1 0 0
1 0 1 0
Output
9
Input
4 4
0 0 0 0
1 0 0 1
0 1 1 0
0 1 0 0
Output
20
Note
In the first example the following positions are good:
1. the (1, 1) cell and right direction;
2. the (1, 1) cell and down direction;
3. the (1, 3) cell and left direction;
4. the (1, 3) cell and down direction;
5. the (1, 4) cell and left direction;
6. the (2, 2) cell and left direction;
7. the (2, 2) cell and up direction;
8. the (2, 2) and right direction;
9. the (2, 4) cell and left direction.
Therefore, there are 9 good positions in this example.
Submitted Solution:
```
# aadiupadhyay
import os.path
from math import gcd, floor, ceil
from collections import *
import sys
mod = 1000000007
INF = float('inf')
def st(): return list(sys.stdin.readline().strip())
def li(): return list(map(int, sys.stdin.readline().split()))
def mp(): return map(int, sys.stdin.readline().split())
def inp(): return int(sys.stdin.readline())
def pr(n): return sys.stdout.write(str(n)+"\n")
def prl(n): return sys.stdout.write(str(n)+" ")
if os.path.exists('input.txt'):
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
def solve():
n, m = mp()
l = []
up = []
down = []
left = []
right = []
zero = [0]*m
for i in range(n):
x = li()
l.append(x)
up.append(list(zero))
down.append(list(zero))
left.append(list(zero))
right.append(list(zero))
left[i][0] = x[0]
right[i][-1] = x[-1]
for j in range(1, m):
left[i][j] = x[j]+left[i][j-1]
right[i][m-1-j] = x[m-1-j] + right[i][m-j]
for i in range(m):
up[0][i] = l[0][i]
down[-1][i] = l[-1][i]
for j in range(1, n):
up[j][i] = l[j][i] + up[j-1][i]
down[n-j-1][i] = l[n-1-j][i] + down[n-j][i]
ans = 0
for i in range(n):
for j in range(m):
if l[i][j] != 1:
c = 0
c += min(1, up[i][j])
c += min(1, down[i][j])
c += min(1, left[i][j])
c += min(1, right[i][j])
ans += c
pr(ans)
for _ in range(1):
solve()
``` | instruction | 0 | 56,794 | 15 | 113,588 |
Yes | output | 1 | 56,794 | 15 | 113,589 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Theater stage is a rectangular field of size n Γ m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.
You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) β left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.
A position is good if two conditions hold:
* there is no actor in the cell the spotlight is placed to;
* there is at least one actor in the direction the spotlight projects.
Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
Input
The first line contains two positive integers n and m (1 β€ n, m β€ 1000) β the number of rows and the number of columns in the plan.
The next n lines contain m integers, 0 or 1 each β the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
Output
Print one integer β the number of good positions for placing the spotlight.
Examples
Input
2 4
0 1 0 0
1 0 1 0
Output
9
Input
4 4
0 0 0 0
1 0 0 1
0 1 1 0
0 1 0 0
Output
20
Note
In the first example the following positions are good:
1. the (1, 1) cell and right direction;
2. the (1, 1) cell and down direction;
3. the (1, 3) cell and left direction;
4. the (1, 3) cell and down direction;
5. the (1, 4) cell and left direction;
6. the (2, 2) cell and left direction;
7. the (2, 2) cell and up direction;
8. the (2, 2) and right direction;
9. the (2, 4) cell and left direction.
Therefore, there are 9 good positions in this example.
Submitted Solution:
```
n,m = map(int,input().split())
t=[]
for k in range(n):
t.append(list(map(int,input().split())))
T=[]
TB=[]
for i in range(n):
u=[]
for j in range(m):
if j==0:
u.append(t[i][j])
else:
u.append(u[-1]+t[i][j])
T.append(u)
v=[]
for s in range(m-1,-1,-1):
if s == m-1:
v.append(t[i][s])
else:
v.append(v[-1]+t[i][s])
TB.append(v[::-1])
f=[]
for j in range(m):
u=[]
for i in range(n):
u.append(t[i][j])
f.append(u)
UT=[]
UB=[]
for i in range(m):
u=[]
for j in range(n):
if j==0:
u.append(f[i][j])
else:
u.append(u[-1]+f[i][j])
UT.append(u)
v=[]
for k in range(n-1,-1,-1):
if k == n-1:
v.append(f[i][k])
else:
v.append(v[-1]+f[i][k])
UB.append(v[::-1])
p=0
for i in range(n):
for j in range(m):
if t[i][j]==0:
if T[i][j]>0:
p+=1
if TB[i][j]>0:
p+=1
if UT[j][i]>0:
p+=1
if UB[j][i]>0:
p+=1
print(p)
``` | instruction | 0 | 56,795 | 15 | 113,590 |
Yes | output | 1 | 56,795 | 15 | 113,591 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Theater stage is a rectangular field of size n Γ m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.
You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) β left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.
A position is good if two conditions hold:
* there is no actor in the cell the spotlight is placed to;
* there is at least one actor in the direction the spotlight projects.
Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
Input
The first line contains two positive integers n and m (1 β€ n, m β€ 1000) β the number of rows and the number of columns in the plan.
The next n lines contain m integers, 0 or 1 each β the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
Output
Print one integer β the number of good positions for placing the spotlight.
Examples
Input
2 4
0 1 0 0
1 0 1 0
Output
9
Input
4 4
0 0 0 0
1 0 0 1
0 1 1 0
0 1 0 0
Output
20
Note
In the first example the following positions are good:
1. the (1, 1) cell and right direction;
2. the (1, 1) cell and down direction;
3. the (1, 3) cell and left direction;
4. the (1, 3) cell and down direction;
5. the (1, 4) cell and left direction;
6. the (2, 2) cell and left direction;
7. the (2, 2) cell and up direction;
8. the (2, 2) and right direction;
9. the (2, 4) cell and left direction.
Therefore, there are 9 good positions in this example.
Submitted Solution:
```
arr = []
yx = []
for s in input():
if s != ' ':
yx += [int(s)]
for _ in range(yx[0]):
for s in input():
if s != ' ':
arr += [int(s)]
i = 0
res = 0
up = 0
for n in arr:
if n == 0:
g = up * yx[1]
for k in arr[up * yx[1]:up * yx[1] + yx[1]]:
if i + 1 == g:
res += 1
if i - 1 == g:
res += 1
g += 1
g = up * yx[1]
for k in arr[up * yx[1]:len(arr):yx[1]]:
if i + yx[1] == g:
res += 1
if i - yx[1] == g:
res += 1
g += 1
i += 1
up = i // yx[1]
print(res + yx[0])
``` | instruction | 0 | 56,796 | 15 | 113,592 |
No | output | 1 | 56,796 | 15 | 113,593 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Theater stage is a rectangular field of size n Γ m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.
You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) β left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.
A position is good if two conditions hold:
* there is no actor in the cell the spotlight is placed to;
* there is at least one actor in the direction the spotlight projects.
Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
Input
The first line contains two positive integers n and m (1 β€ n, m β€ 1000) β the number of rows and the number of columns in the plan.
The next n lines contain m integers, 0 or 1 each β the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
Output
Print one integer β the number of good positions for placing the spotlight.
Examples
Input
2 4
0 1 0 0
1 0 1 0
Output
9
Input
4 4
0 0 0 0
1 0 0 1
0 1 1 0
0 1 0 0
Output
20
Note
In the first example the following positions are good:
1. the (1, 1) cell and right direction;
2. the (1, 1) cell and down direction;
3. the (1, 3) cell and left direction;
4. the (1, 3) cell and down direction;
5. the (1, 4) cell and left direction;
6. the (2, 2) cell and left direction;
7. the (2, 2) cell and up direction;
8. the (2, 2) and right direction;
9. the (2, 4) cell and left direction.
Therefore, there are 9 good positions in this example.
Submitted Solution:
```
n, m = map(lambda x: int(x), input().split())
arr = []
for q in range(n):
arr.append(list(map(lambda x: int(x), input().split())))
counterLights = 0
'''for i in range(n):
if 1 in arr[i]:
for j in range(m):
rightCounter = 0
leftCounter = 0
upCounter = 0
bottomCounter = 0
if arr[i][j] == 0:
for p in range(j, len(arr[i])):
if arr[i][p] == 1:
rightCounter += 1
break
for p in range(0, j):
if arr[i][p] == 1:
leftCounter += 1
break
for p in range(i, len(arr)):
if arr[p][j] == 1:
bottomCounter += 1
break
for p in range(0, i):
if arr[p][j] == 1:
upCounter += 1
break
else:
continue
c = 0
c += 1 if rightCounter != 0 else 0
c += 1 if leftCounter != 0 else 0
c += 1 if bottomCounter != 0 else 0
c += 1 if upCounter != 0 else 0
counterLights += c
else:
continue
print(counterLights)'''
glCounter = 0
for i in range(n):
if 1 in arr[i]:
for j in range(m):
if arr[i][j] == 1:
#print('wrk', i, j)
rightCounter = 0
leftCounter = 0
upCounter = 0
bottomCounter = 0
for p in range(j+1, len(arr[i])):
if arr[i][p] == 0:
rightCounter += 1
else:
break
for p in range(0, j):
if arr[i][p] == 0:
leftCounter += 1
for p in range(i+1, len(arr)):
if arr[p][j] == 0:
bottomCounter += 1
else:
break
for p in range(0, i):
if arr[p][j] == 0:
upCounter += 1
else:
break
#print(upCounter, ' ', rightCounter, ' ', bottomCounter, ' ', leftCounter)
glCounter += rightCounter + leftCounter + upCounter + bottomCounter
else:
continue
else:
continue
print(glCounter)
``` | instruction | 0 | 56,797 | 15 | 113,594 |
No | output | 1 | 56,797 | 15 | 113,595 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Theater stage is a rectangular field of size n Γ m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.
You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) β left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.
A position is good if two conditions hold:
* there is no actor in the cell the spotlight is placed to;
* there is at least one actor in the direction the spotlight projects.
Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
Input
The first line contains two positive integers n and m (1 β€ n, m β€ 1000) β the number of rows and the number of columns in the plan.
The next n lines contain m integers, 0 or 1 each β the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
Output
Print one integer β the number of good positions for placing the spotlight.
Examples
Input
2 4
0 1 0 0
1 0 1 0
Output
9
Input
4 4
0 0 0 0
1 0 0 1
0 1 1 0
0 1 0 0
Output
20
Note
In the first example the following positions are good:
1. the (1, 1) cell and right direction;
2. the (1, 1) cell and down direction;
3. the (1, 3) cell and left direction;
4. the (1, 3) cell and down direction;
5. the (1, 4) cell and left direction;
6. the (2, 2) cell and left direction;
7. the (2, 2) cell and up direction;
8. the (2, 2) and right direction;
9. the (2, 4) cell and left direction.
Therefore, there are 9 good positions in this example.
Submitted Solution:
```
n,m=map(int,input().split())
mat=[list(map(int,input().split())) for i in range(n)]
cnt=0
l=[['x']*(max(n,m)) for i in range(max(n,m))]
for i in range(n):
for j in range(m):
l[i][j]=mat[i][j]
for i in range(max(n,m)):
for j in range(max(n,m)):
if l[i][j]!=1 and l[i][j]==0:
for x in range(j, max(n,m)):
if l[i][x] == 1:
cnt += 1
break
for x in range(j, -1, -1):
if l[i][x] == 1:
cnt += 1
break
for x in range(i, max(n,m)):
if l[j][x] == 1:
cnt += 1
break
for x in range(i, -1, -1):
if l[j][x] == 1:
cnt += 1
break
print(cnt)
``` | instruction | 0 | 56,798 | 15 | 113,596 |
No | output | 1 | 56,798 | 15 | 113,597 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Theater stage is a rectangular field of size n Γ m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.
You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) β left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.
A position is good if two conditions hold:
* there is no actor in the cell the spotlight is placed to;
* there is at least one actor in the direction the spotlight projects.
Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
Input
The first line contains two positive integers n and m (1 β€ n, m β€ 1000) β the number of rows and the number of columns in the plan.
The next n lines contain m integers, 0 or 1 each β the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
Output
Print one integer β the number of good positions for placing the spotlight.
Examples
Input
2 4
0 1 0 0
1 0 1 0
Output
9
Input
4 4
0 0 0 0
1 0 0 1
0 1 1 0
0 1 0 0
Output
20
Note
In the first example the following positions are good:
1. the (1, 1) cell and right direction;
2. the (1, 1) cell and down direction;
3. the (1, 3) cell and left direction;
4. the (1, 3) cell and down direction;
5. the (1, 4) cell and left direction;
6. the (2, 2) cell and left direction;
7. the (2, 2) cell and up direction;
8. the (2, 2) and right direction;
9. the (2, 4) cell and left direction.
Therefore, there are 9 good positions in this example.
Submitted Solution:
```
n, m = map(int, input().split())
a = [0]*n
for i in range(n):
a[i] = [0]*m
for i in range(n):
inp = input().split()
for j in range(m):
a[i][j] = int(inp[j])
res = 0
for i in range(n):
for j in range(m):
if (a[i][j] == 0):
if (sum(a[i][j:m]) >= 1):
res+=1
if (sum(a[i][j:-1:-1]) >= 1):
res+=1
s3 = 0
s4 = 0
for x in range(i, -1, -1):
s3+=a[x][j]
for y in range(i, n):
s4+=a[y][j]
if (s3 >= 1):
res+=1
if (s4 >= 1):
res+=1
print(res)
``` | instruction | 0 | 56,799 | 15 | 113,598 |
No | output | 1 | 56,799 | 15 | 113,599 |
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,380 | 15 | 114,760 |
Tags: dfs and similar, graphs, hashing, shortest paths
Correct Solution:
```
from collections import deque
x0,y0,x1,y1 = list(map(int, input().split()))
n = int(input())
allowed = {}
for i in range(n):
r,a,b = list(map(int, input().split()))
for j in range(a,b+1):
allowed[(r,j)] = True
visited = {}
q = deque()
q.append((x0, y0))
visited[(x0, y0)] = 0
dir = [(-1, 0), (1, 0), (0, -1), (0, 1), (-1, -1), (-1, 1), (1, -1), (1, 1)]
result = -1
while len(q) > 0:
x,y = q.popleft()
original_dist = visited[(x, y)]
if x == x1 and y == y1:
result = original_dist
break
for i in range(len(dir)):
dx, dy = dir[i]
nx, ny = x+dx, y+dy
if (nx, ny) in allowed and (nx, ny) not in visited:
q.append((nx,ny))
visited[(nx, ny)] = original_dist + 1
print(result)
``` | output | 1 | 57,380 | 15 | 114,761 |
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,381 | 15 | 114,762 |
Tags: dfs and similar, graphs, hashing, shortest paths
Correct Solution:
```
from collections import deque
x0,y0,x1,y1=list(map(int, input().split()))
n=int(input())
allowed={}
for i in range(n):
r,a,b=list(map(int,input().split()))
for j in range(a,b+1):
allowed[(r,j)]=True
visited={}
q=deque()
q.append((x0,y0))
visited[(x0,y0)]=0
dire=[(-1,0),(1,0),(0,-1),(0,1),(-1,-1),(-1,1),(1,1),(1,-1)]
result=-1;
while len(q)>0:
x,y=q.popleft()
original_dist=visited[(x,y)]
if x==x1 and y==y1:
result=original_dist;
break;
for i in range(len(dire)):
dx,dy=dire[i]
nx,ny=x+dx,y+dy
if (nx,ny) in allowed and (nx,ny) not in visited:
q.append((nx,ny))
visited[(nx,ny)]=original_dist+1
print(result)
``` | output | 1 | 57,381 | 15 | 114,763 |
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,382 | 15 | 114,764 |
Tags: dfs and similar, graphs, hashing, shortest paths
Correct Solution:
```
def bfs(a,b):
global segment
q = [(a,b)]
while q:
a, b = q.pop(0)
x = [0,0,-1,1,-1,1,-1,1]
y = [1,-1,0,0,-1,1,1,-1]
for i in range(8):
a_new, b_new = a+x[i], b+y[i]
if (a_new,b_new) in segment and segment[(a_new,b_new)] == -1:
segment[(a_new,b_new)] = segment[(a,b)] + 1
q.append((a_new,b_new))
x_start, y_start, x_end, y_end = map(int, input().split())
n = int(input())
segment = {}
for i in range(n):
r, a, b = map(int,input().split())
for i in range(a,b+1):
segment[(r,i)] = -1
if (x_end,y_end) not in segment:
print(-1)
quit()
segment[(x_start,y_start)] = 0
bfs(x_start,y_start)
print(segment[(x_end,y_end)])
``` | output | 1 | 57,382 | 15 | 114,765 |
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,383 | 15 | 114,766 |
Tags: dfs and similar, graphs, hashing, shortest paths
Correct Solution:
```
def connect_point(graph, r, j, deg) :
if (r - 1, j) in graph and deg < graph[r - 1, j][0]:
graph[r - 1, j][0] = deg
graph[r, j].append((r - 1, j))
if (r + 1, j) in graph and deg < graph[r + 1, j][0]:
graph[r + 1, j][0] = deg
graph[r, j].append((r + 1, j))
if (r, j - 1) in graph and deg < graph[r, j - 1][0]:
graph[r, j - 1][0] = deg
graph[r, j].append((r, j - 1))
if (r, j + 1) in graph and deg < graph[r, j + 1][0]:
graph[r, j + 1][0] = deg
graph[r, j].append((r, j + 1))
if (r - 1, j - 1) in graph and deg < graph[r - 1, j - 1][0]:
graph[r - 1, j - 1][0] = deg
graph[r, j].append((r - 1, j - 1))
if (r - 1, j + 1) in graph and deg < graph[r - 1, j + 1][0]:
graph[r - 1, j + 1][0] = deg
graph[r, j].append((r - 1, j + 1))
if (r + 1, j - 1) in graph and deg < graph[r + 1, j - 1][0]:
graph[r + 1, j - 1][0] = deg
graph[r, j].append((r + 1, j - 1))
if (r + 1, j + 1) in graph and deg < graph[r + 1, j + 1][0]:
graph[r + 1, j + 1][0] = deg
graph[r, j].append((r + 1, j + 1))
def bfs(graph):
res = -1
deg = 1
queue = []
queue.append((sr, sc, 0))
while queue :
item = queue.pop(0)
if item[0] == fr and item[1] == fc:
res = item[2]
break
connect_point(graph, item[0], item[1], item[2] + 1)
for i in graph[item[0], item[1]]:
if type(i) == int:
continue
queue.append((i[0], i[1], item[2] + 1))
deg += 1
return res
sr, sc, fr, fc = [int(i) for i in input().split()]
n = int(input())
graph = {}
graph[sr, sc] = [0]
graph[fr, fc] = [100000000000]
for i in range(n) :
r, a, b = [int(i) for i in input().split()]
for j in range(a, b + 1) :
if (r, j) not in graph:
graph[r, j] = [100000000000]
ret = bfs(graph)
#print(graph)
print(ret)
``` | output | 1 | 57,383 | 15 | 114,767 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.