message stringlengths 2 19.9k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 322 108k | cluster float64 15 15 | __index_level_0__ int64 644 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
All of us love treasures, right? That's why young Vasya is heading for a Treasure Island.
Treasure Island may be represented as a rectangular table n Γ m which is surrounded by the ocean. Let us number rows of the field with consecutive integers from 1 to n from top to bottom and columns with consecutive integers from 1 to m from left to right. Denote the cell in r-th row and c-th column as (r, c). Some of the island cells contain impassable forests, and some cells are free and passable. Treasure is hidden in cell (n, m).
Vasya got off the ship in cell (1, 1). Now he wants to reach the treasure. He is hurrying up, so he can move only from cell to the cell in next row (downwards) or next column (rightwards), i.e. from cell (x, y) he can move only to cells (x+1, y) and (x, y+1). Of course Vasya can't move through cells with impassable forests.
Evil Witch is aware of Vasya's journey and she is going to prevent him from reaching the treasure. Before Vasya's first move she is able to grow using her evil magic impassable forests in previously free cells. Witch is able to grow a forest in any number of any free cells except cells (1, 1) where Vasya got off his ship and (n, m) where the treasure is hidden.
Help Evil Witch by finding out the minimum number of cells she has to turn into impassable forests so that Vasya is no longer able to reach the treasure.
Input
First line of input contains two positive integers n, m (3 β€ n β
m β€ 1 000 000), sizes of the island.
Following n lines contains strings s_i of length m describing the island, j-th character of string s_i equals "#" if cell (i, j) contains an impassable forest and "." if the cell is free and passable. Let us remind you that Vasya gets of his ship at the cell (1, 1), i.e. the first cell of the first row, and he wants to reach cell (n, m), i.e. the last cell of the last row.
It's guaranteed, that cells (1, 1) and (n, m) are empty.
Output
Print the only integer k, which is the minimum number of cells Evil Witch has to turn into impassable forest in order to prevent Vasya from reaching the treasure.
Examples
Input
2 2
..
..
Output
2
Input
4 4
....
#.#.
....
.#..
Output
1
Input
3 4
....
.##.
....
Output
2
Note
The following picture illustrates the island in the third example. Blue arrows show possible paths Vasya may use to go from (1, 1) to (n, m). Red illustrates one possible set of cells for the Witch to turn into impassable forest to make Vasya's trip from (1, 1) to (n, m) impossible.
<image> | instruction | 0 | 107,937 | 15 | 215,874 |
Tags: dfs and similar, dp, flows, hashing
Correct Solution:
```
import sys
input = sys.stdin.readline
n,m=map(int,input().split())
MAP=[list(input().strip()) for i in range(n)]
from collections import deque
Q=deque()
Q.append([0,0])
while Q:
x,y=Q.pop()
if x+1<n and MAP[x+1][y]==".":
MAP[x+1][y]=1
Q.append([x+1,y])
if y+1<m and MAP[x][y+1]==".":
MAP[x][y+1]=1
Q.append([x,y+1])
Q.append([n-1,m-1])
while Q:
x,y=Q.pop()
if x-1>=0 and MAP[x-1][y]==1:
MAP[x-1][y]=0
Q.append([x-1,y])
if y-1>=0 and MAP[x][y-1]==1:
MAP[x][y-1]=0
Q.append([x,y-1])
if MAP[n-1][m-1]!=1:
print(0)
sys.exit()
SCORE=[0]*(n+m+5)
for i in range(n):
for j in range(m):
if MAP[i][j]==0:
SCORE[i+j]+=1
if 1 in SCORE:
print(1)
else:
print(2)
``` | output | 1 | 107,937 | 15 | 215,875 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
All of us love treasures, right? That's why young Vasya is heading for a Treasure Island.
Treasure Island may be represented as a rectangular table n Γ m which is surrounded by the ocean. Let us number rows of the field with consecutive integers from 1 to n from top to bottom and columns with consecutive integers from 1 to m from left to right. Denote the cell in r-th row and c-th column as (r, c). Some of the island cells contain impassable forests, and some cells are free and passable. Treasure is hidden in cell (n, m).
Vasya got off the ship in cell (1, 1). Now he wants to reach the treasure. He is hurrying up, so he can move only from cell to the cell in next row (downwards) or next column (rightwards), i.e. from cell (x, y) he can move only to cells (x+1, y) and (x, y+1). Of course Vasya can't move through cells with impassable forests.
Evil Witch is aware of Vasya's journey and she is going to prevent him from reaching the treasure. Before Vasya's first move she is able to grow using her evil magic impassable forests in previously free cells. Witch is able to grow a forest in any number of any free cells except cells (1, 1) where Vasya got off his ship and (n, m) where the treasure is hidden.
Help Evil Witch by finding out the minimum number of cells she has to turn into impassable forests so that Vasya is no longer able to reach the treasure.
Input
First line of input contains two positive integers n, m (3 β€ n β
m β€ 1 000 000), sizes of the island.
Following n lines contains strings s_i of length m describing the island, j-th character of string s_i equals "#" if cell (i, j) contains an impassable forest and "." if the cell is free and passable. Let us remind you that Vasya gets of his ship at the cell (1, 1), i.e. the first cell of the first row, and he wants to reach cell (n, m), i.e. the last cell of the last row.
It's guaranteed, that cells (1, 1) and (n, m) are empty.
Output
Print the only integer k, which is the minimum number of cells Evil Witch has to turn into impassable forest in order to prevent Vasya from reaching the treasure.
Examples
Input
2 2
..
..
Output
2
Input
4 4
....
#.#.
....
.#..
Output
1
Input
3 4
....
.##.
....
Output
2
Note
The following picture illustrates the island in the third example. Blue arrows show possible paths Vasya may use to go from (1, 1) to (n, m). Red illustrates one possible set of cells for the Witch to turn into impassable forest to make Vasya's trip from (1, 1) to (n, m) impossible.
<image>
Submitted Solution:
```
import sys
input = sys.stdin.readline
n,m=map(int,input().split())
MAP=[list(input().strip()) for i in range(n)]
from collections import deque
Q=deque()
Q.append([0,0])
while Q:
x,y=Q.pop()
if x+1<n and MAP[x+1][y]==".":
MAP[x+1][y]=1
Q.append([x+1,y])
if y+1<m and MAP[x][y+1]==".":
MAP[x][y+1]=1
Q.append([x,y+1])
#print(Q,MAP)
Q.append([n-1,m-1])
#print('1',Q,MAP)
while Q:
x,y=Q.pop()
if x-1>=0 and MAP[x-1][y]==1:
MAP[x-1][y]=0
Q.append([x-1,y])
if y-1>=0 and MAP[x][y-1]==1:
MAP[x][y-1]=0
Q.append([x,y-1])
#print(Q,MAP)
#print('2',Q,MAP)
if MAP[n-1][m-1]!=1:
print(0)
sys.exit()
SCORE=[0]*(n+m+5)
for i in range(n):
for j in range(m):
if MAP[i][j]==0:
SCORE[i+j]+=1
#print(SCORE)
if 1 in SCORE:
print(1)
else:
print(2)
``` | instruction | 0 | 107,938 | 15 | 215,876 |
Yes | output | 1 | 107,938 | 15 | 215,877 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
All of us love treasures, right? That's why young Vasya is heading for a Treasure Island.
Treasure Island may be represented as a rectangular table n Γ m which is surrounded by the ocean. Let us number rows of the field with consecutive integers from 1 to n from top to bottom and columns with consecutive integers from 1 to m from left to right. Denote the cell in r-th row and c-th column as (r, c). Some of the island cells contain impassable forests, and some cells are free and passable. Treasure is hidden in cell (n, m).
Vasya got off the ship in cell (1, 1). Now he wants to reach the treasure. He is hurrying up, so he can move only from cell to the cell in next row (downwards) or next column (rightwards), i.e. from cell (x, y) he can move only to cells (x+1, y) and (x, y+1). Of course Vasya can't move through cells with impassable forests.
Evil Witch is aware of Vasya's journey and she is going to prevent him from reaching the treasure. Before Vasya's first move she is able to grow using her evil magic impassable forests in previously free cells. Witch is able to grow a forest in any number of any free cells except cells (1, 1) where Vasya got off his ship and (n, m) where the treasure is hidden.
Help Evil Witch by finding out the minimum number of cells she has to turn into impassable forests so that Vasya is no longer able to reach the treasure.
Input
First line of input contains two positive integers n, m (3 β€ n β
m β€ 1 000 000), sizes of the island.
Following n lines contains strings s_i of length m describing the island, j-th character of string s_i equals "#" if cell (i, j) contains an impassable forest and "." if the cell is free and passable. Let us remind you that Vasya gets of his ship at the cell (1, 1), i.e. the first cell of the first row, and he wants to reach cell (n, m), i.e. the last cell of the last row.
It's guaranteed, that cells (1, 1) and (n, m) are empty.
Output
Print the only integer k, which is the minimum number of cells Evil Witch has to turn into impassable forest in order to prevent Vasya from reaching the treasure.
Examples
Input
2 2
..
..
Output
2
Input
4 4
....
#.#.
....
.#..
Output
1
Input
3 4
....
.##.
....
Output
2
Note
The following picture illustrates the island in the third example. Blue arrows show possible paths Vasya may use to go from (1, 1) to (n, m). Red illustrates one possible set of cells for the Witch to turn into impassable forest to make Vasya's trip from (1, 1) to (n, m) impossible.
<image>
Submitted Solution:
```
import sys
n, m = map(int, input().split())
o_map = ['.'*(m+2)]
o_map.extend(['.' + sys.stdin.readline() + '.' for i in range(n)])
o_map.append(['.'*(m+2)])
# forward method num
f_method = [[0]*(m+2) for i in range(n+2)]
# reverse method num
r_method = [[0]*(m+2) for i in range(n+2)]
MOD = 10**9 + 93
# forward calc
f_method[1][1] = 1
for i in range(1, n+1):
for j in range(1, m+1):
if o_map[i][j] == '.' and i + j != 2:
f_method[i][j] = (f_method[i-1][j] + f_method[i][j-1]) % MOD
r_method[n][m] = 1
for i in range(n, 0, -1):
for j in range(m, 0, -1):
if o_map[i][j] == '.' and i + j != m + n:
r_method[i][j] = (r_method[i+1][j] + r_method[i][j+1]) % MOD
if f_method[n][m] == 0:
#if n == 175 and m == 119:
# print(f_method[n-1][m], f_method[n][m-1])
print(0)
else:
for i in range(1, n+1):
for j in range(1, m+1):
if i + j in [2, m+n]:
continue
if (f_method[i][j] * r_method[i][j]) % MOD == f_method[n][m]:
print(1)
exit()
print(2)
``` | instruction | 0 | 107,939 | 15 | 215,878 |
Yes | output | 1 | 107,939 | 15 | 215,879 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
All of us love treasures, right? That's why young Vasya is heading for a Treasure Island.
Treasure Island may be represented as a rectangular table n Γ m which is surrounded by the ocean. Let us number rows of the field with consecutive integers from 1 to n from top to bottom and columns with consecutive integers from 1 to m from left to right. Denote the cell in r-th row and c-th column as (r, c). Some of the island cells contain impassable forests, and some cells are free and passable. Treasure is hidden in cell (n, m).
Vasya got off the ship in cell (1, 1). Now he wants to reach the treasure. He is hurrying up, so he can move only from cell to the cell in next row (downwards) or next column (rightwards), i.e. from cell (x, y) he can move only to cells (x+1, y) and (x, y+1). Of course Vasya can't move through cells with impassable forests.
Evil Witch is aware of Vasya's journey and she is going to prevent him from reaching the treasure. Before Vasya's first move she is able to grow using her evil magic impassable forests in previously free cells. Witch is able to grow a forest in any number of any free cells except cells (1, 1) where Vasya got off his ship and (n, m) where the treasure is hidden.
Help Evil Witch by finding out the minimum number of cells she has to turn into impassable forests so that Vasya is no longer able to reach the treasure.
Input
First line of input contains two positive integers n, m (3 β€ n β
m β€ 1 000 000), sizes of the island.
Following n lines contains strings s_i of length m describing the island, j-th character of string s_i equals "#" if cell (i, j) contains an impassable forest and "." if the cell is free and passable. Let us remind you that Vasya gets of his ship at the cell (1, 1), i.e. the first cell of the first row, and he wants to reach cell (n, m), i.e. the last cell of the last row.
It's guaranteed, that cells (1, 1) and (n, m) are empty.
Output
Print the only integer k, which is the minimum number of cells Evil Witch has to turn into impassable forest in order to prevent Vasya from reaching the treasure.
Examples
Input
2 2
..
..
Output
2
Input
4 4
....
#.#.
....
.#..
Output
1
Input
3 4
....
.##.
....
Output
2
Note
The following picture illustrates the island in the third example. Blue arrows show possible paths Vasya may use to go from (1, 1) to (n, m). Red illustrates one possible set of cells for the Witch to turn into impassable forest to make Vasya's trip from (1, 1) to (n, m) impossible.
<image>
Submitted Solution:
```
from sys import stdin
def main():
n, m = map(int, input().split())
field = stdin.read()
if n == 1 or m == 1:
return print('10'['#' in field])
field = [c == '.' for c in field]
field += (False,) * (m + 2)
visited1, visited2 = ([False] * len(field) for _ in '12')
cnt = [0] * (n + m)
m += 1
visited1[0] = visited2[n * m - 2] = True
for i in range(n * m - 2, 0, -1):
if field[i] and (visited2[i + 1] or visited2[i + m]):
visited2[i] = True
for i in range(n * m):
if field[i] and (visited1[i - 1] or visited1[i - m]):
visited1[i] = True
if visited2[i]:
cnt[i // m + i % m] += 1
print(min(cnt[1:-2]))
if __name__ == '__main__':
main()
``` | instruction | 0 | 107,940 | 15 | 215,880 |
Yes | output | 1 | 107,940 | 15 | 215,881 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
All of us love treasures, right? That's why young Vasya is heading for a Treasure Island.
Treasure Island may be represented as a rectangular table n Γ m which is surrounded by the ocean. Let us number rows of the field with consecutive integers from 1 to n from top to bottom and columns with consecutive integers from 1 to m from left to right. Denote the cell in r-th row and c-th column as (r, c). Some of the island cells contain impassable forests, and some cells are free and passable. Treasure is hidden in cell (n, m).
Vasya got off the ship in cell (1, 1). Now he wants to reach the treasure. He is hurrying up, so he can move only from cell to the cell in next row (downwards) or next column (rightwards), i.e. from cell (x, y) he can move only to cells (x+1, y) and (x, y+1). Of course Vasya can't move through cells with impassable forests.
Evil Witch is aware of Vasya's journey and she is going to prevent him from reaching the treasure. Before Vasya's first move she is able to grow using her evil magic impassable forests in previously free cells. Witch is able to grow a forest in any number of any free cells except cells (1, 1) where Vasya got off his ship and (n, m) where the treasure is hidden.
Help Evil Witch by finding out the minimum number of cells she has to turn into impassable forests so that Vasya is no longer able to reach the treasure.
Input
First line of input contains two positive integers n, m (3 β€ n β
m β€ 1 000 000), sizes of the island.
Following n lines contains strings s_i of length m describing the island, j-th character of string s_i equals "#" if cell (i, j) contains an impassable forest and "." if the cell is free and passable. Let us remind you that Vasya gets of his ship at the cell (1, 1), i.e. the first cell of the first row, and he wants to reach cell (n, m), i.e. the last cell of the last row.
It's guaranteed, that cells (1, 1) and (n, m) are empty.
Output
Print the only integer k, which is the minimum number of cells Evil Witch has to turn into impassable forest in order to prevent Vasya from reaching the treasure.
Examples
Input
2 2
..
..
Output
2
Input
4 4
....
#.#.
....
.#..
Output
1
Input
3 4
....
.##.
....
Output
2
Note
The following picture illustrates the island in the third example. Blue arrows show possible paths Vasya may use to go from (1, 1) to (n, m). Red illustrates one possible set of cells for the Witch to turn into impassable forest to make Vasya's trip from (1, 1) to (n, m) impossible.
<image>
Submitted Solution:
```
import sys
input = sys.stdin.readline
n,m=map(int,input().split())
if n==1:
f=input().strip()
if '#' in f:
print(0)
else:print(1)
sys.exit()
if m==1:
f=input().strip()
for i in range(n):
if '#'==f:
print(0)
break
f=input().strip()
else:print(1)
sys.exit()
a=[]
for i in range(n):
b=input().strip()
d=[]
for j in range(m):
if b[j]=='.':d.append(1)
else:d.append(0)
a.append(d)
gr=[[] for i in range(n*m+5)]
for i in range(2,n+1):
for j in range(2,m+1):
if a[i-2][j-1]==1:
gr[(i-1)*m+j].append((i-2)*m+j)
if a[i-1][j-2]==1:
gr[(i-1)*m+j].append((i-1)*m+j-1)
for i in range(2,n+1):
if a[i-2][0]==1:
gr[(i-1)*m+1].append((i-2)*m+1)
for j in range(2,m+1):
if a[0][j-2]==1:
gr[j].append(j-1)
if a[n-1][m-2]==1:gr[n*m].append(n*m-1)
if a[n-2][m-1]==1:gr[n*m].append(n*m-m)
v=[False for i in range(n*m+6)]
if a[n-1][m-2]==1:
s=[n*m-1]
t=True
while s and t:
x=s.pop()
if x==1:t=False
v[x]=True
for j in gr[x]:
if v[j]:continue
s.append(j)
ans=0
if v[1]:ans+=1
v[1]=False
if a[n-2][m-1]==1:
s=[n*m-m]
while s:
x=s.pop()
v[x]=True
for j in gr[x]:
if v[j]:continue
s.append(j)
if v[1]:ans+=1
print(ans)
``` | instruction | 0 | 107,941 | 15 | 215,882 |
Yes | output | 1 | 107,941 | 15 | 215,883 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
All of us love treasures, right? That's why young Vasya is heading for a Treasure Island.
Treasure Island may be represented as a rectangular table n Γ m which is surrounded by the ocean. Let us number rows of the field with consecutive integers from 1 to n from top to bottom and columns with consecutive integers from 1 to m from left to right. Denote the cell in r-th row and c-th column as (r, c). Some of the island cells contain impassable forests, and some cells are free and passable. Treasure is hidden in cell (n, m).
Vasya got off the ship in cell (1, 1). Now he wants to reach the treasure. He is hurrying up, so he can move only from cell to the cell in next row (downwards) or next column (rightwards), i.e. from cell (x, y) he can move only to cells (x+1, y) and (x, y+1). Of course Vasya can't move through cells with impassable forests.
Evil Witch is aware of Vasya's journey and she is going to prevent him from reaching the treasure. Before Vasya's first move she is able to grow using her evil magic impassable forests in previously free cells. Witch is able to grow a forest in any number of any free cells except cells (1, 1) where Vasya got off his ship and (n, m) where the treasure is hidden.
Help Evil Witch by finding out the minimum number of cells she has to turn into impassable forests so that Vasya is no longer able to reach the treasure.
Input
First line of input contains two positive integers n, m (3 β€ n β
m β€ 1 000 000), sizes of the island.
Following n lines contains strings s_i of length m describing the island, j-th character of string s_i equals "#" if cell (i, j) contains an impassable forest and "." if the cell is free and passable. Let us remind you that Vasya gets of his ship at the cell (1, 1), i.e. the first cell of the first row, and he wants to reach cell (n, m), i.e. the last cell of the last row.
It's guaranteed, that cells (1, 1) and (n, m) are empty.
Output
Print the only integer k, which is the minimum number of cells Evil Witch has to turn into impassable forest in order to prevent Vasya from reaching the treasure.
Examples
Input
2 2
..
..
Output
2
Input
4 4
....
#.#.
....
.#..
Output
1
Input
3 4
....
.##.
....
Output
2
Note
The following picture illustrates the island in the third example. Blue arrows show possible paths Vasya may use to go from (1, 1) to (n, m). Red illustrates one possible set of cells for the Witch to turn into impassable forest to make Vasya's trip from (1, 1) to (n, m) impossible.
<image>
Submitted Solution:
```
N,M = map(int,input().split())
mp = [input() for i in range(N)]
cnt = [0]*(N+M)
for i in range(N):
for j in range(M):
if mp[i][j]=='.':cnt[i+j]+=1
if cnt[0]==0 or cnt[N+M-2]==0:print(0)
elif cnt.count(1)>2:print(1)
else :print(2)
``` | instruction | 0 | 107,942 | 15 | 215,884 |
No | output | 1 | 107,942 | 15 | 215,885 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
All of us love treasures, right? That's why young Vasya is heading for a Treasure Island.
Treasure Island may be represented as a rectangular table n Γ m which is surrounded by the ocean. Let us number rows of the field with consecutive integers from 1 to n from top to bottom and columns with consecutive integers from 1 to m from left to right. Denote the cell in r-th row and c-th column as (r, c). Some of the island cells contain impassable forests, and some cells are free and passable. Treasure is hidden in cell (n, m).
Vasya got off the ship in cell (1, 1). Now he wants to reach the treasure. He is hurrying up, so he can move only from cell to the cell in next row (downwards) or next column (rightwards), i.e. from cell (x, y) he can move only to cells (x+1, y) and (x, y+1). Of course Vasya can't move through cells with impassable forests.
Evil Witch is aware of Vasya's journey and she is going to prevent him from reaching the treasure. Before Vasya's first move she is able to grow using her evil magic impassable forests in previously free cells. Witch is able to grow a forest in any number of any free cells except cells (1, 1) where Vasya got off his ship and (n, m) where the treasure is hidden.
Help Evil Witch by finding out the minimum number of cells she has to turn into impassable forests so that Vasya is no longer able to reach the treasure.
Input
First line of input contains two positive integers n, m (3 β€ n β
m β€ 1 000 000), sizes of the island.
Following n lines contains strings s_i of length m describing the island, j-th character of string s_i equals "#" if cell (i, j) contains an impassable forest and "." if the cell is free and passable. Let us remind you that Vasya gets of his ship at the cell (1, 1), i.e. the first cell of the first row, and he wants to reach cell (n, m), i.e. the last cell of the last row.
It's guaranteed, that cells (1, 1) and (n, m) are empty.
Output
Print the only integer k, which is the minimum number of cells Evil Witch has to turn into impassable forest in order to prevent Vasya from reaching the treasure.
Examples
Input
2 2
..
..
Output
2
Input
4 4
....
#.#.
....
.#..
Output
1
Input
3 4
....
.##.
....
Output
2
Note
The following picture illustrates the island in the third example. Blue arrows show possible paths Vasya may use to go from (1, 1) to (n, m). Red illustrates one possible set of cells for the Witch to turn into impassable forest to make Vasya's trip from (1, 1) to (n, m) impossible.
<image>
Submitted Solution:
```
import sys
input = sys.stdin.readline
def getInt(): return int(input())
def getVars(): return map(int, input().split())
def getList(): return list(map(int, input().split()))
def getStr(): return input().strip()
## -------------------------------
def addDictList(d, key, val):
if key not in d: d[key] = []
d[key].append(val)
def addDictInt(d, key, val):
if key not in d: d[key] = 0
d[key] = val
def addDictCount(d, key):
if key not in d: d[key] = 0
d[key] += 1
def addDictSum(d, key, val):
if key not in d: d[key] = 0
d[key] += val
## -------------------------------
n, m = getVars()
matrix = []
for i in range(n):
matrix.append(getStr())
stack = []
stack.append([0, 0])
maxP = min(2, n, m)
while len(stack) > 0:
elem = stack.pop(0)
p = 0
if elem == [n-1, m-1]: break
if elem[0] < n-1 and matrix[elem[0]+1][elem[1]] == '.' and [elem[0]+1, elem[1]] not in stack:
stack.append([elem[0]+1, elem[1]])
p += 1
if elem[1] < m-1 and matrix[elem[0]][elem[1]+1] == '.' and [elem[0], elem[1]+1] not in stack:
stack.append([elem[0], elem[1]+1])
p += 1
if len(stack) == 1 and (stack[0] != [n-1, m-1] or p > 0):
maxP = 1
if len(stack) == 0:
maxP = 0
break
print(maxP)
``` | instruction | 0 | 107,943 | 15 | 215,886 |
No | output | 1 | 107,943 | 15 | 215,887 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
All of us love treasures, right? That's why young Vasya is heading for a Treasure Island.
Treasure Island may be represented as a rectangular table n Γ m which is surrounded by the ocean. Let us number rows of the field with consecutive integers from 1 to n from top to bottom and columns with consecutive integers from 1 to m from left to right. Denote the cell in r-th row and c-th column as (r, c). Some of the island cells contain impassable forests, and some cells are free and passable. Treasure is hidden in cell (n, m).
Vasya got off the ship in cell (1, 1). Now he wants to reach the treasure. He is hurrying up, so he can move only from cell to the cell in next row (downwards) or next column (rightwards), i.e. from cell (x, y) he can move only to cells (x+1, y) and (x, y+1). Of course Vasya can't move through cells with impassable forests.
Evil Witch is aware of Vasya's journey and she is going to prevent him from reaching the treasure. Before Vasya's first move she is able to grow using her evil magic impassable forests in previously free cells. Witch is able to grow a forest in any number of any free cells except cells (1, 1) where Vasya got off his ship and (n, m) where the treasure is hidden.
Help Evil Witch by finding out the minimum number of cells she has to turn into impassable forests so that Vasya is no longer able to reach the treasure.
Input
First line of input contains two positive integers n, m (3 β€ n β
m β€ 1 000 000), sizes of the island.
Following n lines contains strings s_i of length m describing the island, j-th character of string s_i equals "#" if cell (i, j) contains an impassable forest and "." if the cell is free and passable. Let us remind you that Vasya gets of his ship at the cell (1, 1), i.e. the first cell of the first row, and he wants to reach cell (n, m), i.e. the last cell of the last row.
It's guaranteed, that cells (1, 1) and (n, m) are empty.
Output
Print the only integer k, which is the minimum number of cells Evil Witch has to turn into impassable forest in order to prevent Vasya from reaching the treasure.
Examples
Input
2 2
..
..
Output
2
Input
4 4
....
#.#.
....
.#..
Output
1
Input
3 4
....
.##.
....
Output
2
Note
The following picture illustrates the island in the third example. Blue arrows show possible paths Vasya may use to go from (1, 1) to (n, m). Red illustrates one possible set of cells for the Witch to turn into impassable forest to make Vasya's trip from (1, 1) to (n, m) impossible.
<image>
Submitted Solution:
```
n, m = map(int,input().split())
a = []
ans = float('inf')
for i in range(n):
a.append(input())
for line in range(2,n+m-1):
act_cnt = 0
start_col = max(0,line-n)
count = min(line, m-start_col, n)
for j in range(0,count):
act_cnt+=int(a[min(n,line)-j-1][start_col+j]=='.')
ans = min(act_cnt,ans)
for r in range(1,n-1):
act_cnt = 0
for j in range(m):
act_cnt+= int(a[r][j]=='.')
ans = min(act_cnt,ans)
for c in range(1,m-1):
act_cnt = 0
for i in range(n):
act_cnt+= int(a[i][c]=='.')
ans = min(act_cnt,ans)
print(ans)
``` | instruction | 0 | 107,944 | 15 | 215,888 |
No | output | 1 | 107,944 | 15 | 215,889 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
All of us love treasures, right? That's why young Vasya is heading for a Treasure Island.
Treasure Island may be represented as a rectangular table n Γ m which is surrounded by the ocean. Let us number rows of the field with consecutive integers from 1 to n from top to bottom and columns with consecutive integers from 1 to m from left to right. Denote the cell in r-th row and c-th column as (r, c). Some of the island cells contain impassable forests, and some cells are free and passable. Treasure is hidden in cell (n, m).
Vasya got off the ship in cell (1, 1). Now he wants to reach the treasure. He is hurrying up, so he can move only from cell to the cell in next row (downwards) or next column (rightwards), i.e. from cell (x, y) he can move only to cells (x+1, y) and (x, y+1). Of course Vasya can't move through cells with impassable forests.
Evil Witch is aware of Vasya's journey and she is going to prevent him from reaching the treasure. Before Vasya's first move she is able to grow using her evil magic impassable forests in previously free cells. Witch is able to grow a forest in any number of any free cells except cells (1, 1) where Vasya got off his ship and (n, m) where the treasure is hidden.
Help Evil Witch by finding out the minimum number of cells she has to turn into impassable forests so that Vasya is no longer able to reach the treasure.
Input
First line of input contains two positive integers n, m (3 β€ n β
m β€ 1 000 000), sizes of the island.
Following n lines contains strings s_i of length m describing the island, j-th character of string s_i equals "#" if cell (i, j) contains an impassable forest and "." if the cell is free and passable. Let us remind you that Vasya gets of his ship at the cell (1, 1), i.e. the first cell of the first row, and he wants to reach cell (n, m), i.e. the last cell of the last row.
It's guaranteed, that cells (1, 1) and (n, m) are empty.
Output
Print the only integer k, which is the minimum number of cells Evil Witch has to turn into impassable forest in order to prevent Vasya from reaching the treasure.
Examples
Input
2 2
..
..
Output
2
Input
4 4
....
#.#.
....
.#..
Output
1
Input
3 4
....
.##.
....
Output
2
Note
The following picture illustrates the island in the third example. Blue arrows show possible paths Vasya may use to go from (1, 1) to (n, m). Red illustrates one possible set of cells for the Witch to turn into impassable forest to make Vasya's trip from (1, 1) to (n, m) impossible.
<image>
Submitted Solution:
```
n,m=map(int,input().split())
a=[]
for i in range(n):
a.append(input()+'#')
a.append('#'*(m+1))
#print(a)
dp=[[0 for i in range(m+1)] for j in range(n+1)]
dp[n-1][m-1]=1
for i in range(n):
for j in range(m):
if(i!=0 or j!=0):
if(a[n-1-i][m-1-j]=='#'):
dp[i][j]=0
else:
dp[n-1-i][m-1-j]=dp[n-1-i+1][m-1-j]+dp[n-1-i][m-1-j+1]
print(dp[0][0])
#print(dp)
``` | instruction | 0 | 107,945 | 15 | 215,890 |
No | output | 1 | 107,945 | 15 | 215,891 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Consider a square grid with h rows and w columns with some dominoes on it. Each domino covers exactly two cells of the grid that share a common side. Every cell is covered by at most one domino.
Let's call a placement of dominoes on the grid perfectly balanced if no row and no column contains a pair of cells covered by two different dominoes. In other words, every row and column may contain no covered cells, one covered cell, or two covered cells that belong to the same domino.
You are given a perfectly balanced placement of dominoes on a grid. Find the number of ways to place zero or more extra dominoes on this grid to keep the placement perfectly balanced. Output this number modulo 998 244 353.
Input
The first line contains three integers h, w, and n (1 β€ h, w β€ 3600; 0 β€ n β€ 2400), denoting the dimensions of the grid and the number of already placed dominoes. The rows are numbered from 1 to h, and the columns are numbered from 1 to w.
Each of the next n lines contains four integers r_{i, 1}, c_{i, 1}, r_{i, 2}, c_{i, 2} (1 β€ r_{i, 1} β€ r_{i, 2} β€ h; 1 β€ c_{i, 1} β€ c_{i, 2} β€ w), denoting the row id and the column id of the cells covered by the i-th domino. Cells (r_{i, 1}, c_{i, 1}) and (r_{i, 2}, c_{i, 2}) are distinct and share a common side.
The given domino placement is perfectly balanced.
Output
Output the number of ways to place zero or more extra dominoes on the grid to keep the placement perfectly balanced, modulo 998 244 353.
Examples
Input
5 7 2
3 1 3 2
4 4 4 5
Output
8
Input
5 4 2
1 2 2 2
4 3 4 4
Output
1
Input
23 42 0
Output
102848351
Note
In the first example, the initial grid looks like this:
<image>
Here are 8 ways to place zero or more extra dominoes to keep the placement perfectly balanced:
<image>
In the second example, the initial grid looks like this:
<image>
No extra dominoes can be placed here.
Submitted Solution:
```
i,j,k=input(),input(),input(),
print(i,i,k)
``` | instruction | 0 | 107,946 | 15 | 215,892 |
No | output | 1 | 107,946 | 15 | 215,893 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this task Anna and Maria play a game with a very unpleasant rival. Anna and Maria are in the opposite squares of a chessboard (8 Γ 8): Anna is in the upper right corner, and Maria is in the lower left one. Apart from them, the board has several statues. Each statue occupies exactly one square. A square that contains a statue cannot have anything or anyone β neither any other statues, nor Anna, nor Maria.
Anna is present on the board as a figurant (she stands still and never moves), and Maria has been actively involved in the game. Her goal is β to come to Anna's square. Maria and statues move in turn, Maria moves first. During one move Maria can go to any adjacent on the side or diagonal cell in which there is no statue, or she can stay in the cell where she is. The statues during their move must go one square down simultaneously, and those statues that were in the bottom row fall from the board and are no longer appeared.
At that moment, when one of the statues is in the cell in which the Maria is, the statues are declared winners. At the moment when Maria comes into the cell where Anna has been waiting, Maria is declared the winner.
Obviously, nothing depends on the statues, so it all depends on Maria. Determine who will win, if Maria does not make a strategic error.
Input
You are given the 8 strings whose length equals 8, describing the initial position on the board. The first line represents the top row of the board, the next one β for the second from the top, and so on, the last line represents the bottom row. Each character string matches a single cell board in the appropriate row, and the characters are in the same manner as that of the corresponding cell. If the cell is empty, the corresponding character is ".". If a cell has Maria, then it is represented by character "M". If a cell has Anna, it is represented by the character "A". If a cell has a statue, then the cell is represented by character "S".
It is guaranteed that the last character of the first row is always "A", the first character of the last line is always "M". The remaining characters are "." or "S".
Output
If Maria wins, print string "WIN". If the statues win, print string "LOSE".
Examples
Input
.......A
........
........
........
........
........
........
M.......
Output
WIN
Input
.......A
........
........
........
........
........
SS......
M.......
Output
LOSE
Input
.......A
........
........
........
........
.S......
S.......
MS......
Output
LOSE | instruction | 0 | 107,968 | 15 | 215,936 |
Tags: dfs and similar
Correct Solution:
```
from itertools import product
from queue import Queue
BOARD_SIZE = 10
OCC, EMPTY = 1, 0
VISITED = 2
board = [input() for _ in range(8)]
graph = []
for _ in range(BOARD_SIZE):
graph += [[[EMPTY for _ in range(BOARD_SIZE)] for _ in range(BOARD_SIZE)]]
for i, j in product(range(8), repeat = 2):
if board[i][j] == 'S':
graph[0][i + 1][j + 1] = OCC
for i in range(BOARD_SIZE):
graph[0][i][0] = OCC
graph[0][i][BOARD_SIZE - 1] = OCC
graph[0][0][i] = OCC
graph[0][BOARD_SIZE - 1][i] = OCC
for time in range(1, BOARD_SIZE):
for i, j in product(range(2, 9), range(1, 9)):
graph[time][i][j] = graph[time - 1][i - 1][j]
for i in range(BOARD_SIZE):
graph[time][i][0] = OCC
graph[time][i][BOARD_SIZE - 1] = OCC
graph[time][0][i] = OCC
graph[time][BOARD_SIZE - 1][i] = OCC
Q = Queue()
Q.put((0, 8, 1))
pos_moves = [
[-1, -1],
[-1, 0],
[-1, 1],
[0, -1],
[0, 0],
[0, 1],
[1, -1],
[1, 0],
[1, 1]
]
while not Q.empty():
time, i, j = Q.get()
if time < BOARD_SIZE - 1:
for d in pos_moves:
if graph[time][i + d[0]][j + d[1]] != OCC and \
graph[time + 1][i + d[0]][j + d[1]] == EMPTY:
graph[time + 1][i + d[0]][j + d[1]] = VISITED
Q.put((time + 1, i + d[0], j + d[1]))
any_pos = False
for i, j in product(range(1, 9), repeat = 2):
if graph[BOARD_SIZE - 1][i][j] == VISITED:
any_pos = True
# WA on some 19 test
if any_pos:
print("WIN")
else:
print("LOSE")
``` | output | 1 | 107,968 | 15 | 215,937 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this task Anna and Maria play a game with a very unpleasant rival. Anna and Maria are in the opposite squares of a chessboard (8 Γ 8): Anna is in the upper right corner, and Maria is in the lower left one. Apart from them, the board has several statues. Each statue occupies exactly one square. A square that contains a statue cannot have anything or anyone β neither any other statues, nor Anna, nor Maria.
Anna is present on the board as a figurant (she stands still and never moves), and Maria has been actively involved in the game. Her goal is β to come to Anna's square. Maria and statues move in turn, Maria moves first. During one move Maria can go to any adjacent on the side or diagonal cell in which there is no statue, or she can stay in the cell where she is. The statues during their move must go one square down simultaneously, and those statues that were in the bottom row fall from the board and are no longer appeared.
At that moment, when one of the statues is in the cell in which the Maria is, the statues are declared winners. At the moment when Maria comes into the cell where Anna has been waiting, Maria is declared the winner.
Obviously, nothing depends on the statues, so it all depends on Maria. Determine who will win, if Maria does not make a strategic error.
Input
You are given the 8 strings whose length equals 8, describing the initial position on the board. The first line represents the top row of the board, the next one β for the second from the top, and so on, the last line represents the bottom row. Each character string matches a single cell board in the appropriate row, and the characters are in the same manner as that of the corresponding cell. If the cell is empty, the corresponding character is ".". If a cell has Maria, then it is represented by character "M". If a cell has Anna, it is represented by the character "A". If a cell has a statue, then the cell is represented by character "S".
It is guaranteed that the last character of the first row is always "A", the first character of the last line is always "M". The remaining characters are "." or "S".
Output
If Maria wins, print string "WIN". If the statues win, print string "LOSE".
Examples
Input
.......A
........
........
........
........
........
........
M.......
Output
WIN
Input
.......A
........
........
........
........
........
SS......
M.......
Output
LOSE
Input
.......A
........
........
........
........
.S......
S.......
MS......
Output
LOSE | instruction | 0 | 107,969 | 15 | 215,938 |
Tags: dfs and similar
Correct Solution:
```
start=[]
for i in range(8): start.append(input())
a=[start]
#start=[".......A","........","........","........","........",".SSSSSSS","S.......","M......."]
#a=[start]
for i in range(10):
tmp=a[-1]
tmp=[".......A"]+tmp
tmp[1]=tmp[1][:-1]+"."
a.append(tmp[:-1])
dx=[-1,1,0,0,0,1,1,-1,-1]
dy=[0,0,-1,1,0,-1,1,-1,1]
def chk(x,y,step):
if a[step][y][x]=="S": return False
if step==9:return True
for i in range(8):
x_,y_=x+dx[i],y+dy[i]
if min(x_,y_)<0 or max(x_,y_)>7:continue
if a[step][y_][x_]!='S' and chk(x_,y_,step+1): return True
return False
if chk(0,7,0):
print("WIN")
else:
print("LOSE")
``` | output | 1 | 107,969 | 15 | 215,939 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this task Anna and Maria play a game with a very unpleasant rival. Anna and Maria are in the opposite squares of a chessboard (8 Γ 8): Anna is in the upper right corner, and Maria is in the lower left one. Apart from them, the board has several statues. Each statue occupies exactly one square. A square that contains a statue cannot have anything or anyone β neither any other statues, nor Anna, nor Maria.
Anna is present on the board as a figurant (she stands still and never moves), and Maria has been actively involved in the game. Her goal is β to come to Anna's square. Maria and statues move in turn, Maria moves first. During one move Maria can go to any adjacent on the side or diagonal cell in which there is no statue, or she can stay in the cell where she is. The statues during their move must go one square down simultaneously, and those statues that were in the bottom row fall from the board and are no longer appeared.
At that moment, when one of the statues is in the cell in which the Maria is, the statues are declared winners. At the moment when Maria comes into the cell where Anna has been waiting, Maria is declared the winner.
Obviously, nothing depends on the statues, so it all depends on Maria. Determine who will win, if Maria does not make a strategic error.
Input
You are given the 8 strings whose length equals 8, describing the initial position on the board. The first line represents the top row of the board, the next one β for the second from the top, and so on, the last line represents the bottom row. Each character string matches a single cell board in the appropriate row, and the characters are in the same manner as that of the corresponding cell. If the cell is empty, the corresponding character is ".". If a cell has Maria, then it is represented by character "M". If a cell has Anna, it is represented by the character "A". If a cell has a statue, then the cell is represented by character "S".
It is guaranteed that the last character of the first row is always "A", the first character of the last line is always "M". The remaining characters are "." or "S".
Output
If Maria wins, print string "WIN". If the statues win, print string "LOSE".
Examples
Input
.......A
........
........
........
........
........
........
M.......
Output
WIN
Input
.......A
........
........
........
........
........
SS......
M.......
Output
LOSE
Input
.......A
........
........
........
........
.S......
S.......
MS......
Output
LOSE | instruction | 0 | 107,970 | 15 | 215,940 |
Tags: dfs and similar
Correct Solution:
```
from sys import stdin
from collections import *
from copy import deepcopy
def valid(i, j):
n, m = [8] * 2
return i > -1 and i < n and j > -1 and j < m
def str_inp(n):
return list(reversed(list((list(stdin.readline()[:-1]) for x in range(n)))))
def check(x, y, step):
if step + 1 < 8 and all[step + 1][x][y] == 'S' or step < 8 and all[step][x][y] == 'S':
# print(all[step][x][y], x, y)
return False
return True
def print_maze(x):
for i in range(8):
print(*all[x][i], sep='')
print(end='\n')
def dfs(x, y):
stack, visit, step = [[x, y, -1]], defaultdict(int), -1
while (stack):
x, y, step = stack.pop()
step += 1
if chess[x][y] == 'A':
exit(print('WIN'))
st = 0
for i in range(9):
nx, ny = [x + dx[i], y + dy[i]]
if i == 0:
if step >= 8:
continue
else:
visit[nx, ny] = 0
if valid(nx, ny) and check(nx, ny, step) and not visit[nx, ny]:
stack.append([nx, ny, step])
# print(nx, ny, step, check(nx, ny, step))
# print_maze(min(step, 7))
visit[nx, ny] = 1
else:
st += 1
# print(stack, step)
if st == 9:
step -= 1
visit[x, y] = 0
dx, dy, chess = [0, -1, 0, 1, 0, 1, -1, 1, -1], [0, 0, 1, 0, -1, 1, -1, -1, 1], str_inp(8)
all, ini = [chess], [['.' for i in range(8)] for j in range(8)]
for k in range(1, 9):
tem = deepcopy(ini)
for i in range(8):
for j in range(8):
if chess[i][j] == 'S' and i - k > -1:
tem[i - k][j] = 'S'
all.append(tem)
dfs(0, 0)
print('LOSE')
``` | output | 1 | 107,970 | 15 | 215,941 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this task Anna and Maria play a game with a very unpleasant rival. Anna and Maria are in the opposite squares of a chessboard (8 Γ 8): Anna is in the upper right corner, and Maria is in the lower left one. Apart from them, the board has several statues. Each statue occupies exactly one square. A square that contains a statue cannot have anything or anyone β neither any other statues, nor Anna, nor Maria.
Anna is present on the board as a figurant (she stands still and never moves), and Maria has been actively involved in the game. Her goal is β to come to Anna's square. Maria and statues move in turn, Maria moves first. During one move Maria can go to any adjacent on the side or diagonal cell in which there is no statue, or she can stay in the cell where she is. The statues during their move must go one square down simultaneously, and those statues that were in the bottom row fall from the board and are no longer appeared.
At that moment, when one of the statues is in the cell in which the Maria is, the statues are declared winners. At the moment when Maria comes into the cell where Anna has been waiting, Maria is declared the winner.
Obviously, nothing depends on the statues, so it all depends on Maria. Determine who will win, if Maria does not make a strategic error.
Input
You are given the 8 strings whose length equals 8, describing the initial position on the board. The first line represents the top row of the board, the next one β for the second from the top, and so on, the last line represents the bottom row. Each character string matches a single cell board in the appropriate row, and the characters are in the same manner as that of the corresponding cell. If the cell is empty, the corresponding character is ".". If a cell has Maria, then it is represented by character "M". If a cell has Anna, it is represented by the character "A". If a cell has a statue, then the cell is represented by character "S".
It is guaranteed that the last character of the first row is always "A", the first character of the last line is always "M". The remaining characters are "." or "S".
Output
If Maria wins, print string "WIN". If the statues win, print string "LOSE".
Examples
Input
.......A
........
........
........
........
........
........
M.......
Output
WIN
Input
.......A
........
........
........
........
........
SS......
M.......
Output
LOSE
Input
.......A
........
........
........
........
.S......
S.......
MS......
Output
LOSE | instruction | 0 | 107,971 | 15 | 215,942 |
Tags: dfs and similar
Correct Solution:
```
f = []
for i in range(8):
f.append(input())
d = [[[0 for i in range(8)] for j in range(8)] for k in range(100)]
d[0][7][0] = 1
dx = [1, 1, 1, 0, 0, -1, -1, -1, 0]
dy = [1, 0, -1, 1, -1, 1, 0, -1, 0]
ans = 'LOSE'
for i in range(99):
for x in range(8):
for y in range(8):
if not d[i][x][y]:
continue
for j in range(9):
nx = x + dx[j]
ny = y + dy[j]
if not(0 <= nx < 8 and 0 <= ny < 8):
continue
valid = True
if 0 <= nx - i < 8 and f[nx - i][ny] == 'S':
valid = False
if 0 <= nx - i - 1 < 8 and f[nx - i - 1][ny] == 'S':
valid = False
if not valid:
continue
d[i + 1][nx][ny] = 1
if nx == 0 and ny == 7:
ans = 'WIN'
print(ans)
``` | output | 1 | 107,971 | 15 | 215,943 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this task Anna and Maria play a game with a very unpleasant rival. Anna and Maria are in the opposite squares of a chessboard (8 Γ 8): Anna is in the upper right corner, and Maria is in the lower left one. Apart from them, the board has several statues. Each statue occupies exactly one square. A square that contains a statue cannot have anything or anyone β neither any other statues, nor Anna, nor Maria.
Anna is present on the board as a figurant (she stands still and never moves), and Maria has been actively involved in the game. Her goal is β to come to Anna's square. Maria and statues move in turn, Maria moves first. During one move Maria can go to any adjacent on the side or diagonal cell in which there is no statue, or she can stay in the cell where she is. The statues during their move must go one square down simultaneously, and those statues that were in the bottom row fall from the board and are no longer appeared.
At that moment, when one of the statues is in the cell in which the Maria is, the statues are declared winners. At the moment when Maria comes into the cell where Anna has been waiting, Maria is declared the winner.
Obviously, nothing depends on the statues, so it all depends on Maria. Determine who will win, if Maria does not make a strategic error.
Input
You are given the 8 strings whose length equals 8, describing the initial position on the board. The first line represents the top row of the board, the next one β for the second from the top, and so on, the last line represents the bottom row. Each character string matches a single cell board in the appropriate row, and the characters are in the same manner as that of the corresponding cell. If the cell is empty, the corresponding character is ".". If a cell has Maria, then it is represented by character "M". If a cell has Anna, it is represented by the character "A". If a cell has a statue, then the cell is represented by character "S".
It is guaranteed that the last character of the first row is always "A", the first character of the last line is always "M". The remaining characters are "." or "S".
Output
If Maria wins, print string "WIN". If the statues win, print string "LOSE".
Examples
Input
.......A
........
........
........
........
........
........
M.......
Output
WIN
Input
.......A
........
........
........
........
........
SS......
M.......
Output
LOSE
Input
.......A
........
........
........
........
.S......
S.......
MS......
Output
LOSE | instruction | 0 | 107,972 | 15 | 215,944 |
Tags: dfs and similar
Correct Solution:
```
grid = [ list(input().strip()) for r in range(8) ]
grid[0][7] = '.'
grid[7][0] = '.'
mark = [ [ False for c in range(8) ] for r in range(8) ]
mark[7][0] = True
def display_grid():
print('\n'.join(map(lambda row: ''.join(row), grid)))
print()
def display_mark():
for row in mark:
row = map(lambda a: 'X' if a else '_', row)
print(''.join(row))
print()
for i in range(8):
moved = False
new_mark = [ [ False for c in range(8) ] for r in range(8) ]
for r in range(8):
for c in range(8):
if not mark[r][c]:
continue
for R in range(max(0, r - 1), min(r + 2, 8)):
for C in range(max(0, c - 1), min(c + 2, 8)):
if grid[R][C] == 'S':
continue
if R - 1 >= 0 and grid[R - 1][C] == 'S':
continue
new_mark[R][C] = True
moved = True
mark = new_mark
grid.insert(0, 8 * [ '.' ])
grid.pop()
if not moved:
break
if moved:
print('WIN')
else:
print('LOSE')
``` | output | 1 | 107,972 | 15 | 215,945 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In this task Anna and Maria play a game with a very unpleasant rival. Anna and Maria are in the opposite squares of a chessboard (8 Γ 8): Anna is in the upper right corner, and Maria is in the lower left one. Apart from them, the board has several statues. Each statue occupies exactly one square. A square that contains a statue cannot have anything or anyone β neither any other statues, nor Anna, nor Maria.
Anna is present on the board as a figurant (she stands still and never moves), and Maria has been actively involved in the game. Her goal is β to come to Anna's square. Maria and statues move in turn, Maria moves first. During one move Maria can go to any adjacent on the side or diagonal cell in which there is no statue, or she can stay in the cell where she is. The statues during their move must go one square down simultaneously, and those statues that were in the bottom row fall from the board and are no longer appeared.
At that moment, when one of the statues is in the cell in which the Maria is, the statues are declared winners. At the moment when Maria comes into the cell where Anna has been waiting, Maria is declared the winner.
Obviously, nothing depends on the statues, so it all depends on Maria. Determine who will win, if Maria does not make a strategic error.
Input
You are given the 8 strings whose length equals 8, describing the initial position on the board. The first line represents the top row of the board, the next one β for the second from the top, and so on, the last line represents the bottom row. Each character string matches a single cell board in the appropriate row, and the characters are in the same manner as that of the corresponding cell. If the cell is empty, the corresponding character is ".". If a cell has Maria, then it is represented by character "M". If a cell has Anna, it is represented by the character "A". If a cell has a statue, then the cell is represented by character "S".
It is guaranteed that the last character of the first row is always "A", the first character of the last line is always "M". The remaining characters are "." or "S".
Output
If Maria wins, print string "WIN". If the statues win, print string "LOSE".
Examples
Input
.......A
........
........
........
........
........
........
M.......
Output
WIN
Input
.......A
........
........
........
........
........
SS......
M.......
Output
LOSE
Input
.......A
........
........
........
........
.S......
S.......
MS......
Output
LOSE | instruction | 0 | 107,973 | 15 | 215,946 |
Tags: dfs and similar
Correct Solution:
```
r, s = [63], ''.join(input() + 'T' for i in range(8)) + 'T' * 9
for i in range(0, 72, 9):
t = set()
for x in r:
for y in (x, x - 1, x + 1, x - 9, x + 9, x - 10, x - 8, x + 10, x + 8):
if s[y] == 'T': continue
if (y < i or s[y - i] != 'S') and (y < i + 9 or s[y - i - 9] != 'S'): t.add(y)
r = t
print('WIN' if r else 'LOSE')
# Made By Mostafa_Khaled
``` | output | 1 | 107,973 | 15 | 215,947 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In this task Anna and Maria play a game with a very unpleasant rival. Anna and Maria are in the opposite squares of a chessboard (8 Γ 8): Anna is in the upper right corner, and Maria is in the lower left one. Apart from them, the board has several statues. Each statue occupies exactly one square. A square that contains a statue cannot have anything or anyone β neither any other statues, nor Anna, nor Maria.
Anna is present on the board as a figurant (she stands still and never moves), and Maria has been actively involved in the game. Her goal is β to come to Anna's square. Maria and statues move in turn, Maria moves first. During one move Maria can go to any adjacent on the side or diagonal cell in which there is no statue, or she can stay in the cell where she is. The statues during their move must go one square down simultaneously, and those statues that were in the bottom row fall from the board and are no longer appeared.
At that moment, when one of the statues is in the cell in which the Maria is, the statues are declared winners. At the moment when Maria comes into the cell where Anna has been waiting, Maria is declared the winner.
Obviously, nothing depends on the statues, so it all depends on Maria. Determine who will win, if Maria does not make a strategic error.
Input
You are given the 8 strings whose length equals 8, describing the initial position on the board. The first line represents the top row of the board, the next one β for the second from the top, and so on, the last line represents the bottom row. Each character string matches a single cell board in the appropriate row, and the characters are in the same manner as that of the corresponding cell. If the cell is empty, the corresponding character is ".". If a cell has Maria, then it is represented by character "M". If a cell has Anna, it is represented by the character "A". If a cell has a statue, then the cell is represented by character "S".
It is guaranteed that the last character of the first row is always "A", the first character of the last line is always "M". The remaining characters are "." or "S".
Output
If Maria wins, print string "WIN". If the statues win, print string "LOSE".
Examples
Input
.......A
........
........
........
........
........
........
M.......
Output
WIN
Input
.......A
........
........
........
........
........
SS......
M.......
Output
LOSE
Input
.......A
........
........
........
........
.S......
S.......
MS......
Output
LOSE
Submitted Solution:
```
from itertools import product
from queue import Queue
BOARD_SIZE = 10
OCC_OR_VISITED, EMPTY = 1, 0
board = [input() for _ in range(8)]
graph = []
for _ in range(BOARD_SIZE):
graph += [[[EMPTY for _ in range(BOARD_SIZE)] for _ in range(BOARD_SIZE)]]
for i, j in product(range(8), repeat = 2):
if board[i][j] == 'S':
graph[0][i + 1][j + 1] = OCC_OR_VISITED
for i in range(BOARD_SIZE):
graph[0][i][0] = OCC_OR_VISITED
graph[0][i][BOARD_SIZE - 1] = OCC_OR_VISITED
graph[0][0][i] = OCC_OR_VISITED
graph[0][BOARD_SIZE - 1][i] = OCC_OR_VISITED
for time in range(1, BOARD_SIZE):
for i, j in product(range(2, 9), range(1, 9)):
graph[time][i][j] = graph[time - 1][i - 1][j]
for i in range(BOARD_SIZE):
graph[time][i][0] = OCC_OR_VISITED
graph[time][i][BOARD_SIZE - 1] = OCC_OR_VISITED
graph[time][0][i] = OCC_OR_VISITED
graph[time][BOARD_SIZE - 1][i] = OCC_OR_VISITED
Q = Queue()
Q.put((0, 8, 1))
pos_moves = [
[-1, -1],
[-1, 0],
[-1, 1],
[0, -1],
[0, 0],
[0, 1],
[1, -1],
[1, 0],
[1, 1]
]
while not Q.empty():
time, i, j = Q.get()
if time < BOARD_SIZE - 1:
for d in pos_moves:
if graph[time][i + d[0]][j + d[1]] == EMPTY and \
graph[time + 1][i + d[0]][j + d[1]] == EMPTY:
graph[time + 1][i + d[0]][j + d[1]] = OCC_OR_VISITED
Q.put((time + 1, i + d[0], j + d[1]))
any_pos = False
for i, j in product(range(1, 9), repeat = 2):
if graph[BOARD_SIZE - 1][i][j] == OCC_OR_VISITED:
any_pos = True
if any_pos:
print("WIN")
else:
print("LOSE")
``` | instruction | 0 | 107,974 | 15 | 215,948 |
No | output | 1 | 107,974 | 15 | 215,949 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In this task Anna and Maria play a game with a very unpleasant rival. Anna and Maria are in the opposite squares of a chessboard (8 Γ 8): Anna is in the upper right corner, and Maria is in the lower left one. Apart from them, the board has several statues. Each statue occupies exactly one square. A square that contains a statue cannot have anything or anyone β neither any other statues, nor Anna, nor Maria.
Anna is present on the board as a figurant (she stands still and never moves), and Maria has been actively involved in the game. Her goal is β to come to Anna's square. Maria and statues move in turn, Maria moves first. During one move Maria can go to any adjacent on the side or diagonal cell in which there is no statue, or she can stay in the cell where she is. The statues during their move must go one square down simultaneously, and those statues that were in the bottom row fall from the board and are no longer appeared.
At that moment, when one of the statues is in the cell in which the Maria is, the statues are declared winners. At the moment when Maria comes into the cell where Anna has been waiting, Maria is declared the winner.
Obviously, nothing depends on the statues, so it all depends on Maria. Determine who will win, if Maria does not make a strategic error.
Input
You are given the 8 strings whose length equals 8, describing the initial position on the board. The first line represents the top row of the board, the next one β for the second from the top, and so on, the last line represents the bottom row. Each character string matches a single cell board in the appropriate row, and the characters are in the same manner as that of the corresponding cell. If the cell is empty, the corresponding character is ".". If a cell has Maria, then it is represented by character "M". If a cell has Anna, it is represented by the character "A". If a cell has a statue, then the cell is represented by character "S".
It is guaranteed that the last character of the first row is always "A", the first character of the last line is always "M". The remaining characters are "." or "S".
Output
If Maria wins, print string "WIN". If the statues win, print string "LOSE".
Examples
Input
.......A
........
........
........
........
........
........
M.......
Output
WIN
Input
.......A
........
........
........
........
........
SS......
M.......
Output
LOSE
Input
.......A
........
........
........
........
.S......
S.......
MS......
Output
LOSE
Submitted Solution:
```
from sys import stdin
from collections import *
from copy import deepcopy
def valid(i, j):
n, m = [8] * 2
return i > -1 and i < n and j > -1 and j < m
def str_inp(n):
return list(reversed(list((list(stdin.readline()[:-1]) for x in range(n)))))
def check(x, y, step):
if step - 1 < 7 and all[step - 1][x][y] == 'S' or step < 8 and all[step][x][y] == 'S':
# print(all[step][x][y])
return False
return True
def dfs(x, y):
stack, visit, step = [[x, y]], defaultdict(int), 0
while (stack):
x, y = stack.pop()
step += 1
if chess[x][y] == 'A':
exit(print('WIN'))
if not visit[x, y]:
# print(x, y, step)
visit[x, y] = 1
st = 0
for i in range(8):
nx, ny = [x + dx[i], y + dy[i]]
if valid(nx, ny) and check(nx, ny, step) and not visit[nx, ny]:
stack.append([nx, ny])
else:
st += 1
if st == 8:
step -= 1
dx, dy, chess = [-1, 0, 1, 0, 1, -1, 1, -1], [0, 1, 0, -1, 1, -1, -1, 1], str_inp(8)
all, ini = [chess], [['.' for i in range(8)] for j in range(8)]
for k in range(1, 9):
tem = deepcopy(ini)
for i in range(8):
for j in range(8):
if chess[i][j] == 'S' and i - k > -1:
tem[i - k][j] = 'S'
all.append(tem)
#print
# for i in range(8):
# for j in range(8):
# print(*all[i][j])
# print(end='\n')
dfs(0, 0)
print('LOSE')
``` | instruction | 0 | 107,975 | 15 | 215,950 |
No | output | 1 | 107,975 | 15 | 215,951 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In this task Anna and Maria play a game with a very unpleasant rival. Anna and Maria are in the opposite squares of a chessboard (8 Γ 8): Anna is in the upper right corner, and Maria is in the lower left one. Apart from them, the board has several statues. Each statue occupies exactly one square. A square that contains a statue cannot have anything or anyone β neither any other statues, nor Anna, nor Maria.
Anna is present on the board as a figurant (she stands still and never moves), and Maria has been actively involved in the game. Her goal is β to come to Anna's square. Maria and statues move in turn, Maria moves first. During one move Maria can go to any adjacent on the side or diagonal cell in which there is no statue, or she can stay in the cell where she is. The statues during their move must go one square down simultaneously, and those statues that were in the bottom row fall from the board and are no longer appeared.
At that moment, when one of the statues is in the cell in which the Maria is, the statues are declared winners. At the moment when Maria comes into the cell where Anna has been waiting, Maria is declared the winner.
Obviously, nothing depends on the statues, so it all depends on Maria. Determine who will win, if Maria does not make a strategic error.
Input
You are given the 8 strings whose length equals 8, describing the initial position on the board. The first line represents the top row of the board, the next one β for the second from the top, and so on, the last line represents the bottom row. Each character string matches a single cell board in the appropriate row, and the characters are in the same manner as that of the corresponding cell. If the cell is empty, the corresponding character is ".". If a cell has Maria, then it is represented by character "M". If a cell has Anna, it is represented by the character "A". If a cell has a statue, then the cell is represented by character "S".
It is guaranteed that the last character of the first row is always "A", the first character of the last line is always "M". The remaining characters are "." or "S".
Output
If Maria wins, print string "WIN". If the statues win, print string "LOSE".
Examples
Input
.......A
........
........
........
........
........
........
M.......
Output
WIN
Input
.......A
........
........
........
........
........
SS......
M.......
Output
LOSE
Input
.......A
........
........
........
........
.S......
S.......
MS......
Output
LOSE
Submitted Solution:
```
f = []
for i in range(8):
f.append(input())
d = [[[0 for i in range(8)] for j in range(8)] for k in range(100)]
d[0][7][0] = 1
dx = [1, 1, 1, 0, 0, -1, -1, -1]
dy = [1, 0, -1, 1, -1, 1, 0, -1]
ans = 'LOSE'
for i in range(99):
for x in range(8):
for y in range(8):
if not d[i][x][y]:
continue
for j in range(8):
nx = x + dx[j]
ny = y + dy[j]
if not(0 <= nx < 8 and 0 <= ny < 8):
continue
valid = True
if 0 <= nx - i < 8 and f[nx - i][ny] == 'S':
valid = False
if 0 <= nx - i - 1 < 8 and f[nx - i - 1][ny] == 'S':
valid = False
if not valid:
continue
d[i + 1][nx][ny] = 1
if nx == 0 and ny == 7:
ans = 'WIN'
print(ans)
``` | instruction | 0 | 107,976 | 15 | 215,952 |
No | output | 1 | 107,976 | 15 | 215,953 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Smart Beaver from ABBYY got hooked on square matrices. Now he is busy studying an n Γ n size matrix, where n is odd. The Smart Beaver considers the following matrix elements good:
* Elements of the main diagonal.
* Elements of the secondary diagonal.
* Elements of the "middle" row β the row which has exactly <image> rows above it and the same number of rows below it.
* Elements of the "middle" column β the column that has exactly <image> columns to the left of it and the same number of columns to the right of it.
<image> The figure shows a 5 Γ 5 matrix. The good elements are marked with green.
Help the Smart Beaver count the sum of good elements of the given matrix.
Input
The first line of input data contains a single odd integer n. Each of the next n lines contains n integers aij (0 β€ aij β€ 100) separated by single spaces β the elements of the given matrix.
The input limitations for getting 30 points are:
* 1 β€ n β€ 5
The input limitations for getting 100 points are:
* 1 β€ n β€ 101
Output
Print a single integer β the sum of good matrix elements.
Examples
Input
3
1 2 3
4 5 6
7 8 9
Output
45
Input
5
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
Output
17
Note
In the first sample all matrix elements will be good. Good elements in the second sample are shown on the figure. | instruction | 0 | 108,114 | 15 | 216,228 |
Tags: implementation
Correct Solution:
```
n = int(input())
matrix = [[0] * n] * n
for i in range(n):
a = list(map(int, input().split(' ')))
matrix[i] = a
ans = 0
for i in range(n):
for j in range(n):
if i == n // 2 or j == n // 2 or i == j or i + j == n - 1:
ans += matrix[i][j]
print(ans)
``` | output | 1 | 108,114 | 15 | 216,229 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Smart Beaver from ABBYY got hooked on square matrices. Now he is busy studying an n Γ n size matrix, where n is odd. The Smart Beaver considers the following matrix elements good:
* Elements of the main diagonal.
* Elements of the secondary diagonal.
* Elements of the "middle" row β the row which has exactly <image> rows above it and the same number of rows below it.
* Elements of the "middle" column β the column that has exactly <image> columns to the left of it and the same number of columns to the right of it.
<image> The figure shows a 5 Γ 5 matrix. The good elements are marked with green.
Help the Smart Beaver count the sum of good elements of the given matrix.
Input
The first line of input data contains a single odd integer n. Each of the next n lines contains n integers aij (0 β€ aij β€ 100) separated by single spaces β the elements of the given matrix.
The input limitations for getting 30 points are:
* 1 β€ n β€ 5
The input limitations for getting 100 points are:
* 1 β€ n β€ 101
Output
Print a single integer β the sum of good matrix elements.
Examples
Input
3
1 2 3
4 5 6
7 8 9
Output
45
Input
5
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
Output
17
Note
In the first sample all matrix elements will be good. Good elements in the second sample are shown on the figure. | instruction | 0 | 108,115 | 15 | 216,230 |
Tags: implementation
Correct Solution:
```
n=int(input())
if n==1:
print(int(input()))
else:
g=[]
for i in range(n):
g.append(list(map(int,input().split())))
x=n//2
count=sum(g[x])
for i in range(n):
count+=g[i][x]
count+=g[i][i]+g[i][n-1-i]
count-=g[x][x]*3
print(count)
``` | output | 1 | 108,115 | 15 | 216,231 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Smart Beaver from ABBYY got hooked on square matrices. Now he is busy studying an n Γ n size matrix, where n is odd. The Smart Beaver considers the following matrix elements good:
* Elements of the main diagonal.
* Elements of the secondary diagonal.
* Elements of the "middle" row β the row which has exactly <image> rows above it and the same number of rows below it.
* Elements of the "middle" column β the column that has exactly <image> columns to the left of it and the same number of columns to the right of it.
<image> The figure shows a 5 Γ 5 matrix. The good elements are marked with green.
Help the Smart Beaver count the sum of good elements of the given matrix.
Input
The first line of input data contains a single odd integer n. Each of the next n lines contains n integers aij (0 β€ aij β€ 100) separated by single spaces β the elements of the given matrix.
The input limitations for getting 30 points are:
* 1 β€ n β€ 5
The input limitations for getting 100 points are:
* 1 β€ n β€ 101
Output
Print a single integer β the sum of good matrix elements.
Examples
Input
3
1 2 3
4 5 6
7 8 9
Output
45
Input
5
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
Output
17
Note
In the first sample all matrix elements will be good. Good elements in the second sample are shown on the figure. | instruction | 0 | 108,116 | 15 | 216,232 |
Tags: implementation
Correct Solution:
```
n = int(input())
a = [list(map(int, input().split())) for _ in range(n)]
s1 = sum(a[i][i] for i in range(n))
s2 = sum(a[i][n - 1 - i] for i in range(n))
s3 = sum(a[i][n // 2] for i in range(n))
s4 = sum(a[n // 2][i] for i in range(n))
print(s1 + s2 + s3 + s4 - 3 * a[n //2][n // 2])
``` | output | 1 | 108,116 | 15 | 216,233 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Smart Beaver from ABBYY got hooked on square matrices. Now he is busy studying an n Γ n size matrix, where n is odd. The Smart Beaver considers the following matrix elements good:
* Elements of the main diagonal.
* Elements of the secondary diagonal.
* Elements of the "middle" row β the row which has exactly <image> rows above it and the same number of rows below it.
* Elements of the "middle" column β the column that has exactly <image> columns to the left of it and the same number of columns to the right of it.
<image> The figure shows a 5 Γ 5 matrix. The good elements are marked with green.
Help the Smart Beaver count the sum of good elements of the given matrix.
Input
The first line of input data contains a single odd integer n. Each of the next n lines contains n integers aij (0 β€ aij β€ 100) separated by single spaces β the elements of the given matrix.
The input limitations for getting 30 points are:
* 1 β€ n β€ 5
The input limitations for getting 100 points are:
* 1 β€ n β€ 101
Output
Print a single integer β the sum of good matrix elements.
Examples
Input
3
1 2 3
4 5 6
7 8 9
Output
45
Input
5
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
Output
17
Note
In the first sample all matrix elements will be good. Good elements in the second sample are shown on the figure. | instruction | 0 | 108,117 | 15 | 216,234 |
Tags: implementation
Correct Solution:
```
n = int(input())
lst = []
for i in range(n):
a = list(map(int, input().split()))
lst.append(a)
middle = n // 2
first = 0
second = 0
for i in range(0, n):
for j in range(0, n):
if (i == j):
first += lst[i][j]
if (i == n - j - 1):
second += lst[i][j]
midrow = sum(lst[middle])
midcol = 0
for i in range(n):
midcol = midcol + lst[i][middle]
print(midcol + midrow + first + second -3*(lst[n//2][n//2]))
``` | output | 1 | 108,117 | 15 | 216,235 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Smart Beaver from ABBYY got hooked on square matrices. Now he is busy studying an n Γ n size matrix, where n is odd. The Smart Beaver considers the following matrix elements good:
* Elements of the main diagonal.
* Elements of the secondary diagonal.
* Elements of the "middle" row β the row which has exactly <image> rows above it and the same number of rows below it.
* Elements of the "middle" column β the column that has exactly <image> columns to the left of it and the same number of columns to the right of it.
<image> The figure shows a 5 Γ 5 matrix. The good elements are marked with green.
Help the Smart Beaver count the sum of good elements of the given matrix.
Input
The first line of input data contains a single odd integer n. Each of the next n lines contains n integers aij (0 β€ aij β€ 100) separated by single spaces β the elements of the given matrix.
The input limitations for getting 30 points are:
* 1 β€ n β€ 5
The input limitations for getting 100 points are:
* 1 β€ n β€ 101
Output
Print a single integer β the sum of good matrix elements.
Examples
Input
3
1 2 3
4 5 6
7 8 9
Output
45
Input
5
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
Output
17
Note
In the first sample all matrix elements will be good. Good elements in the second sample are shown on the figure. | instruction | 0 | 108,118 | 15 | 216,236 |
Tags: implementation
Correct Solution:
```
n = int(input())
a = [list(map(int, input().split())) for i in range(n)]
print(sum(a[i][i] + a[i][n - i - 1] + a[n // 2][i] + a[i][n // 2] for i in range(n)) - 3 * a[n // 2][n // 2])
``` | output | 1 | 108,118 | 15 | 216,237 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Smart Beaver from ABBYY got hooked on square matrices. Now he is busy studying an n Γ n size matrix, where n is odd. The Smart Beaver considers the following matrix elements good:
* Elements of the main diagonal.
* Elements of the secondary diagonal.
* Elements of the "middle" row β the row which has exactly <image> rows above it and the same number of rows below it.
* Elements of the "middle" column β the column that has exactly <image> columns to the left of it and the same number of columns to the right of it.
<image> The figure shows a 5 Γ 5 matrix. The good elements are marked with green.
Help the Smart Beaver count the sum of good elements of the given matrix.
Input
The first line of input data contains a single odd integer n. Each of the next n lines contains n integers aij (0 β€ aij β€ 100) separated by single spaces β the elements of the given matrix.
The input limitations for getting 30 points are:
* 1 β€ n β€ 5
The input limitations for getting 100 points are:
* 1 β€ n β€ 101
Output
Print a single integer β the sum of good matrix elements.
Examples
Input
3
1 2 3
4 5 6
7 8 9
Output
45
Input
5
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
Output
17
Note
In the first sample all matrix elements will be good. Good elements in the second sample are shown on the figure. | instruction | 0 | 108,119 | 15 | 216,238 |
Tags: implementation
Correct Solution:
```
n=int(input())
are=[]
suma=0
for k in range (n):
a=input()
a=a.split()
are.append(a)
espaciovacio=n-3
for k in range ( n):
for r in range (n):
if k==((n)//2):
suma=suma+int(are[k][r])
are[k][r]="0"
elif r==((n)//2):
suma=suma+int(are[k][r])
are[k][r]="0"
elif k==r:
suma=suma+int(are[k][r])
are[k][r]="0"
elif (k+r==n-1):
suma=suma+int(are[k][r])
are[k][r]="0"
print(suma)
``` | output | 1 | 108,119 | 15 | 216,239 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Smart Beaver from ABBYY got hooked on square matrices. Now he is busy studying an n Γ n size matrix, where n is odd. The Smart Beaver considers the following matrix elements good:
* Elements of the main diagonal.
* Elements of the secondary diagonal.
* Elements of the "middle" row β the row which has exactly <image> rows above it and the same number of rows below it.
* Elements of the "middle" column β the column that has exactly <image> columns to the left of it and the same number of columns to the right of it.
<image> The figure shows a 5 Γ 5 matrix. The good elements are marked with green.
Help the Smart Beaver count the sum of good elements of the given matrix.
Input
The first line of input data contains a single odd integer n. Each of the next n lines contains n integers aij (0 β€ aij β€ 100) separated by single spaces β the elements of the given matrix.
The input limitations for getting 30 points are:
* 1 β€ n β€ 5
The input limitations for getting 100 points are:
* 1 β€ n β€ 101
Output
Print a single integer β the sum of good matrix elements.
Examples
Input
3
1 2 3
4 5 6
7 8 9
Output
45
Input
5
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
Output
17
Note
In the first sample all matrix elements will be good. Good elements in the second sample are shown on the figure. | instruction | 0 | 108,120 | 15 | 216,240 |
Tags: implementation
Correct Solution:
```
"""
instagram : essipoortahmasb2018
telegram channel : essi_python
"""
Sum = 0
i = input
n = int(i())
l = []
for i in range(n):
l.append([*map(int,input().split())])
for i in range(n):
Sum+= l[i][i]
l[i][i] = 0
Sum+=l[i][n//2]
l[i][n//2] = 0
a = 0
b = n - 1
for i in range(n):
Sum+=l[a][b]
l[a][b]=0
Sum+=l[n//2][b]
l[n//2][b] = 0
a+=1
b-=1
print(Sum)
``` | output | 1 | 108,120 | 15 | 216,241 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Smart Beaver from ABBYY got hooked on square matrices. Now he is busy studying an n Γ n size matrix, where n is odd. The Smart Beaver considers the following matrix elements good:
* Elements of the main diagonal.
* Elements of the secondary diagonal.
* Elements of the "middle" row β the row which has exactly <image> rows above it and the same number of rows below it.
* Elements of the "middle" column β the column that has exactly <image> columns to the left of it and the same number of columns to the right of it.
<image> The figure shows a 5 Γ 5 matrix. The good elements are marked with green.
Help the Smart Beaver count the sum of good elements of the given matrix.
Input
The first line of input data contains a single odd integer n. Each of the next n lines contains n integers aij (0 β€ aij β€ 100) separated by single spaces β the elements of the given matrix.
The input limitations for getting 30 points are:
* 1 β€ n β€ 5
The input limitations for getting 100 points are:
* 1 β€ n β€ 101
Output
Print a single integer β the sum of good matrix elements.
Examples
Input
3
1 2 3
4 5 6
7 8 9
Output
45
Input
5
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
Output
17
Note
In the first sample all matrix elements will be good. Good elements in the second sample are shown on the figure. | instruction | 0 | 108,121 | 15 | 216,242 |
Tags: implementation
Correct Solution:
```
n = int(input())
ans = 0
for i in range(n):
a = [int(i) for i in input().split()]
if i != n // 2:
ans += a[i] + a[n//2] +a[n-1-i]
else:
for j in range(n):
ans += a[j]
print(ans)
``` | output | 1 | 108,121 | 15 | 216,243 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Olya loves energy drinks. She loves them so much that her room is full of empty cans from energy drinks.
Formally, her room can be represented as a field of n Γ m cells, each cell of which is empty or littered with cans.
Olya drank a lot of energy drink, so now she can run k meters per second. Each second she chooses one of the four directions (up, down, left or right) and runs from 1 to k meters in this direction. Of course, she can only run through empty cells.
Now Olya needs to get from cell (x1, y1) to cell (x2, y2). How many seconds will it take her if she moves optimally?
It's guaranteed that cells (x1, y1) and (x2, y2) are empty. These cells can coincide.
Input
The first line contains three integers n, m and k (1 β€ n, m, k β€ 1000) β the sizes of the room and Olya's speed.
Then n lines follow containing m characters each, the i-th of them contains on j-th position "#", if the cell (i, j) is littered with cans, and "." otherwise.
The last line contains four integers x1, y1, x2, y2 (1 β€ x1, x2 β€ n, 1 β€ y1, y2 β€ m) β the coordinates of the first and the last cells.
Output
Print a single integer β the minimum time it will take Olya to get from (x1, y1) to (x2, y2).
If it's impossible to get from (x1, y1) to (x2, y2), print -1.
Examples
Input
3 4 4
....
###.
....
1 1 3 1
Output
3
Input
3 4 1
....
###.
....
1 1 3 1
Output
8
Input
2 2 1
.#
#.
1 1 2 2
Output
-1
Note
In the first sample Olya should run 3 meters to the right in the first second, 2 meters down in the second second and 3 meters to the left in the third second.
In second sample Olya should run to the right for 3 seconds, then down for 2 seconds and then to the left for 3 seconds.
Olya does not recommend drinking energy drinks and generally believes that this is bad. | instruction | 0 | 108,347 | 15 | 216,694 |
Tags: data structures, dfs and similar, graphs, shortest paths
Correct Solution:
```
from sys import *
f = lambda: map(int, input().split())
n, m, k = f()
t = [[1e9 * (q == '.') for q in stdin.readline()] for i in range(n)]
t.append([0] * m)
a, b, c, d = [q - 1 for q in f()]
u = [(a, b)]
t[a][b] = l = 0
def g(i, x, y):
if i > k or t[x][y] < l: return 0
if t[x][y] > l:
t[x][y] = l
v.append((x, y))
return 1
while u and t[c][d] == 1e9:
l += 1
v = []
for x, y in u:
i = j = 1
while g(i, x - i, y): i += 1
while g(j, x + j, y): j += 1
i = j = 1
while g(i, x, y - i): i += 1
while g(j, x, y + j): j += 1
u = v
print(l if t[c][d] < 1e9 else -1)
``` | output | 1 | 108,347 | 15 | 216,695 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Olya loves energy drinks. She loves them so much that her room is full of empty cans from energy drinks.
Formally, her room can be represented as a field of n Γ m cells, each cell of which is empty or littered with cans.
Olya drank a lot of energy drink, so now she can run k meters per second. Each second she chooses one of the four directions (up, down, left or right) and runs from 1 to k meters in this direction. Of course, she can only run through empty cells.
Now Olya needs to get from cell (x1, y1) to cell (x2, y2). How many seconds will it take her if she moves optimally?
It's guaranteed that cells (x1, y1) and (x2, y2) are empty. These cells can coincide.
Input
The first line contains three integers n, m and k (1 β€ n, m, k β€ 1000) β the sizes of the room and Olya's speed.
Then n lines follow containing m characters each, the i-th of them contains on j-th position "#", if the cell (i, j) is littered with cans, and "." otherwise.
The last line contains four integers x1, y1, x2, y2 (1 β€ x1, x2 β€ n, 1 β€ y1, y2 β€ m) β the coordinates of the first and the last cells.
Output
Print a single integer β the minimum time it will take Olya to get from (x1, y1) to (x2, y2).
If it's impossible to get from (x1, y1) to (x2, y2), print -1.
Examples
Input
3 4 4
....
###.
....
1 1 3 1
Output
3
Input
3 4 1
....
###.
....
1 1 3 1
Output
8
Input
2 2 1
.#
#.
1 1 2 2
Output
-1
Note
In the first sample Olya should run 3 meters to the right in the first second, 2 meters down in the second second and 3 meters to the left in the third second.
In second sample Olya should run to the right for 3 seconds, then down for 2 seconds and then to the left for 3 seconds.
Olya does not recommend drinking energy drinks and generally believes that this is bad. | instruction | 0 | 108,348 | 15 | 216,696 |
Tags: data structures, dfs and similar, graphs, shortest paths
Correct 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')
dx = [0, 0, -1, 1]
dy = [1, -1, 0, 0]
n, m, k = mp()
l = []
now = [INF for i in range(m)]
distance = []
for i in range(n):
cur = list(now)
s = st()
l.append(s)
distance.append(cur)
a, b, c, d = mp()
a, b, c, d = a-1, b-1, c-1, d-1
q = deque()
q.append((a, b))
distance[a][b] = 0
ans = -1
while q:
x, y = q.popleft()
if (x, y) == (c, d):
ans = distance[x][y]
break
for i in range(4):
for j in range(1, k+1):
x1, x2 = x+j*dx[i], y+j*dy[i]
if x1 < 0 or x2 < 0 or x1 >= n or x2 >= m or l[x1][x2] == '#' or distance[x1][x2] < 1+distance[x][y]:
break
else:
if distance[x1][x2] > 1 + distance[x][y]:
distance[x1][x2] = 1+distance[x][y]
q.append((x1, x2))
pr(ans)
``` | output | 1 | 108,348 | 15 | 216,697 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Olya loves energy drinks. She loves them so much that her room is full of empty cans from energy drinks.
Formally, her room can be represented as a field of n Γ m cells, each cell of which is empty or littered with cans.
Olya drank a lot of energy drink, so now she can run k meters per second. Each second she chooses one of the four directions (up, down, left or right) and runs from 1 to k meters in this direction. Of course, she can only run through empty cells.
Now Olya needs to get from cell (x1, y1) to cell (x2, y2). How many seconds will it take her if she moves optimally?
It's guaranteed that cells (x1, y1) and (x2, y2) are empty. These cells can coincide.
Input
The first line contains three integers n, m and k (1 β€ n, m, k β€ 1000) β the sizes of the room and Olya's speed.
Then n lines follow containing m characters each, the i-th of them contains on j-th position "#", if the cell (i, j) is littered with cans, and "." otherwise.
The last line contains four integers x1, y1, x2, y2 (1 β€ x1, x2 β€ n, 1 β€ y1, y2 β€ m) β the coordinates of the first and the last cells.
Output
Print a single integer β the minimum time it will take Olya to get from (x1, y1) to (x2, y2).
If it's impossible to get from (x1, y1) to (x2, y2), print -1.
Examples
Input
3 4 4
....
###.
....
1 1 3 1
Output
3
Input
3 4 1
....
###.
....
1 1 3 1
Output
8
Input
2 2 1
.#
#.
1 1 2 2
Output
-1
Note
In the first sample Olya should run 3 meters to the right in the first second, 2 meters down in the second second and 3 meters to the left in the third second.
In second sample Olya should run to the right for 3 seconds, then down for 2 seconds and then to the left for 3 seconds.
Olya does not recommend drinking energy drinks and generally believes that this is bad. | instruction | 0 | 108,349 | 15 | 216,698 |
Tags: data structures, dfs and similar, graphs, shortest paths
Correct Solution:
```
import sys
import io, os
input = sys.stdin.readline
INF = 10**18
def main():
h, w, k = map(int, input().split())
C = [input().rstrip() for i in range(h)]
x1, y1, x2, y2 = map(int, input().split())
x1, y1, x2, y2 = x1-1, y1-1, x2-1, y2-1
#print(C)
from collections import deque
q = deque()
q.append((x1, y1))
dist = [[INF]*w for i in range(h)]
dist[x1][y1] = 0
while q:
x, y = q.popleft()
if x == x2 and y == y2:
print(dist[x][y])
exit()
for dx, dy in (0, 1), (1, 0), (0, -1), (-1, 0):
for i in range(1, k+1):
nx, ny = x+dx*i, y+dy*i
if nx < 0 or h <= nx:
break
if ny < 0 or w <= ny:
break
if C[nx][ny] == '#':
break
if dist[nx][ny] <= dist[x][y]:
break
if dist[nx][ny] == dist[x][y]+1:
continue
dist[nx][ny] = dist[x][y]+1
q.append((nx, ny))
#print(dist)
print(-1)
if __name__ == '__main__':
main()
``` | output | 1 | 108,349 | 15 | 216,699 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Olya loves energy drinks. She loves them so much that her room is full of empty cans from energy drinks.
Formally, her room can be represented as a field of n Γ m cells, each cell of which is empty or littered with cans.
Olya drank a lot of energy drink, so now she can run k meters per second. Each second she chooses one of the four directions (up, down, left or right) and runs from 1 to k meters in this direction. Of course, she can only run through empty cells.
Now Olya needs to get from cell (x1, y1) to cell (x2, y2). How many seconds will it take her if she moves optimally?
It's guaranteed that cells (x1, y1) and (x2, y2) are empty. These cells can coincide.
Input
The first line contains three integers n, m and k (1 β€ n, m, k β€ 1000) β the sizes of the room and Olya's speed.
Then n lines follow containing m characters each, the i-th of them contains on j-th position "#", if the cell (i, j) is littered with cans, and "." otherwise.
The last line contains four integers x1, y1, x2, y2 (1 β€ x1, x2 β€ n, 1 β€ y1, y2 β€ m) β the coordinates of the first and the last cells.
Output
Print a single integer β the minimum time it will take Olya to get from (x1, y1) to (x2, y2).
If it's impossible to get from (x1, y1) to (x2, y2), print -1.
Examples
Input
3 4 4
....
###.
....
1 1 3 1
Output
3
Input
3 4 1
....
###.
....
1 1 3 1
Output
8
Input
2 2 1
.#
#.
1 1 2 2
Output
-1
Note
In the first sample Olya should run 3 meters to the right in the first second, 2 meters down in the second second and 3 meters to the left in the third second.
In second sample Olya should run to the right for 3 seconds, then down for 2 seconds and then to the left for 3 seconds.
Olya does not recommend drinking energy drinks and generally believes that this is bad. | instruction | 0 | 108,350 | 15 | 216,700 |
Tags: data structures, dfs and similar, graphs, shortest paths
Correct Solution:
```
from sys import *
f = lambda: map(int, input().split())
n, m, k = f()
t = [[1e9 * (q == '.') for q in stdin.readline()] for i in range(n)]
t.append([0] * m)
a, b, c, d = [q - 1 for q in f()]
u = [(a, b)]
t[a][b] = l = 0
def g(x, y):
if t[x][y] > l:
t[x][y] = l
v.append((x, y))
while u and t[c][d] == 1e9:
l += 1
v = []
for x, y in u:
i = 1
while i <= k and t[x - i][y] >= l:
g(x - i, y)
i += 1
i = 1
while i <= k and t[x + i][y] >= l:
g(x + i, y)
i += 1
j = 1
while j <= k and t[x][y - j] >= l:
g(x, y - j)
j += 1
j = 1
while j <= k and t[x][y + j] >= l:
g(x, y + j)
j += 1
u = v
print(l if t[c][d] < 1e9 else -1)
``` | output | 1 | 108,350 | 15 | 216,701 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Olya loves energy drinks. She loves them so much that her room is full of empty cans from energy drinks.
Formally, her room can be represented as a field of n Γ m cells, each cell of which is empty or littered with cans.
Olya drank a lot of energy drink, so now she can run k meters per second. Each second she chooses one of the four directions (up, down, left or right) and runs from 1 to k meters in this direction. Of course, she can only run through empty cells.
Now Olya needs to get from cell (x1, y1) to cell (x2, y2). How many seconds will it take her if she moves optimally?
It's guaranteed that cells (x1, y1) and (x2, y2) are empty. These cells can coincide.
Input
The first line contains three integers n, m and k (1 β€ n, m, k β€ 1000) β the sizes of the room and Olya's speed.
Then n lines follow containing m characters each, the i-th of them contains on j-th position "#", if the cell (i, j) is littered with cans, and "." otherwise.
The last line contains four integers x1, y1, x2, y2 (1 β€ x1, x2 β€ n, 1 β€ y1, y2 β€ m) β the coordinates of the first and the last cells.
Output
Print a single integer β the minimum time it will take Olya to get from (x1, y1) to (x2, y2).
If it's impossible to get from (x1, y1) to (x2, y2), print -1.
Examples
Input
3 4 4
....
###.
....
1 1 3 1
Output
3
Input
3 4 1
....
###.
....
1 1 3 1
Output
8
Input
2 2 1
.#
#.
1 1 2 2
Output
-1
Note
In the first sample Olya should run 3 meters to the right in the first second, 2 meters down in the second second and 3 meters to the left in the third second.
In second sample Olya should run to the right for 3 seconds, then down for 2 seconds and then to the left for 3 seconds.
Olya does not recommend drinking energy drinks and generally believes that this is bad. | instruction | 0 | 108,351 | 15 | 216,702 |
Tags: data structures, dfs and similar, graphs, shortest paths
Correct Solution:
```
# ------------------- fast io --------------------
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# ------------------- fast io --------------------
from math import ceil
def prod(a, mod=10 ** 9 + 7):
ans = 1
for each in a:
ans = (ans * each) % mod
return ans
def gcd(x, y):
while y:
x, y = y, x % y
return x
def lcm(a, b): return a * b // gcd(a, b)
def binary(x, length=16):
y = bin(x)[2:]
return y if len(y) >= length else "0" * (length - len(y)) + y
from math import inf
from collections import deque
for _ in range(int(input()) if not True else 1):
#n = int(input())
n, m, k = map(int, input().split())
# a, b = map(int, input().split())
# c, d = map(int, input().split())
# a = list(map(int, input().split()))
# b = list(map(int, input().split()))
a = []
for i in range(n):
a += [input()]
x1, y1, x2, y2 = map(int, input().split())
x1, y1 = x1-1, y1-1
x2, y2 = x2-1, y2-1
queue = deque([(x1, y1)])
dist = [[inf] * m for i in range(n)]
dist[x1][y1] = 0
while queue:
x, y = queue.popleft()
for i in range(x+1, x + k + 1):
if i >= n or dist[i][y] <= dist[x][y] or a[i][y]=='#': break
if dist[i][y] == inf:
queue += [(i, y)]
dist[i][y] = dist[x][y] + 1
for i in range(x - 1, x - k - 1, -1):
if i < 0 or dist[i][y] <= dist[x][y] or a[i][y]=='#': break
if dist[i][y] == inf:
queue += [(i, y)]
dist[i][y] = dist[x][y] + 1
for j in range(y + 1, y + k + 1):
if j >= m or dist[x][j] <= dist[x][y] or a[x][j]=='#': break
if dist[x][j] == inf:
queue += [(x, j)]
dist[x][j] = dist[x][y] + 1
for j in range(y - 1, y - k - 1, -1):
if j < 0 or dist[x][j] <= dist[x][y] or a[x][j]=='#': break
if dist[x][j] == inf:
queue += [(x, j)]
dist[x][j] = dist[x][y] + 1
print(-1 if dist[x2][y2] == inf else dist[x2][y2])
``` | output | 1 | 108,351 | 15 | 216,703 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Olya loves energy drinks. She loves them so much that her room is full of empty cans from energy drinks.
Formally, her room can be represented as a field of n Γ m cells, each cell of which is empty or littered with cans.
Olya drank a lot of energy drink, so now she can run k meters per second. Each second she chooses one of the four directions (up, down, left or right) and runs from 1 to k meters in this direction. Of course, she can only run through empty cells.
Now Olya needs to get from cell (x1, y1) to cell (x2, y2). How many seconds will it take her if she moves optimally?
It's guaranteed that cells (x1, y1) and (x2, y2) are empty. These cells can coincide.
Input
The first line contains three integers n, m and k (1 β€ n, m, k β€ 1000) β the sizes of the room and Olya's speed.
Then n lines follow containing m characters each, the i-th of them contains on j-th position "#", if the cell (i, j) is littered with cans, and "." otherwise.
The last line contains four integers x1, y1, x2, y2 (1 β€ x1, x2 β€ n, 1 β€ y1, y2 β€ m) β the coordinates of the first and the last cells.
Output
Print a single integer β the minimum time it will take Olya to get from (x1, y1) to (x2, y2).
If it's impossible to get from (x1, y1) to (x2, y2), print -1.
Examples
Input
3 4 4
....
###.
....
1 1 3 1
Output
3
Input
3 4 1
....
###.
....
1 1 3 1
Output
8
Input
2 2 1
.#
#.
1 1 2 2
Output
-1
Note
In the first sample Olya should run 3 meters to the right in the first second, 2 meters down in the second second and 3 meters to the left in the third second.
In second sample Olya should run to the right for 3 seconds, then down for 2 seconds and then to the left for 3 seconds.
Olya does not recommend drinking energy drinks and generally believes that this is bad. | instruction | 0 | 108,352 | 15 | 216,704 |
Tags: data structures, dfs and similar, graphs, shortest paths
Correct Solution:
```
import sys
from collections import deque
input=sys.stdin.readline
n,m,k=map(int,input().split())
grid=[]
grid1=[]
grid2=[]
leftyes=[]
rightyes=[]
upyes=[]
downyes=[]
for i in range(n):
grid.append(input())
grid1.append([-1]*m)
grid2.append([0]*m)
leftyes.append([0]*m)
rightyes.append([0]*m)
downyes.append([0]*m)
upyes.append([0]*m)
for i in range(n):
count=0
for j in range(m):
if grid[i][j]=='.':
count+=1
else:
count=0
if count>k:
leftyes[i][j]=1
rightyes[i][j-k]=1
for i in range(m):
count=0
for j in range(n):
if grid[j][i]=='.':
count+=1
else:
count=0
if count>k:
upyes[j][i]=1
downyes[j-k][i]=1
x1,y1,x2,y2=map(int,input().split())
que=deque([(x1-1,y1-1,0)])
grid1[x1-1][y1-1]=0
while que:
x,y,step=que.popleft()
if grid2[x][y]:
continue
grid2[x][y]=1
if not(x<n-1 and grid1[x+1][y]!=-1 and grid1[x+1][y]<=step):
curr=x-1
count=0
while count<k and curr>=0 and grid[curr][y]=='.':
if grid1[curr][y]!=-1:
curr-=1
count+=1
continue
que.append((curr,y,step+1))
grid1[curr][y]=step+1
curr-=1
count+=1
else:
if upyes[x][y] and grid1[x-k][y]==-1:
que.append((x-k,y,step+1))
grid1[x-k][y]=step+1
if not(x>0 and grid1[x-1][y]!=-1 and grid1[x-1][y]<=step):
curr=x+1
count=0
while count<k and curr<=n-1 and grid[curr][y]=='.':
if grid1[curr][y]!=-1:
curr+=1
count+=1
continue
que.append((curr,y,step+1))
grid1[curr][y]=step+1
curr+=1
count+=1
else:
if downyes[x][y] and grid1[x+k][y]==-1:
que.append((x+k,y,step+1))
grid1[x+k][y]=step+1
if not(y<m-1 and grid1[x][y+1]!=-1 and grid1[x][y+1]<=step):
curr=y-1
count=0
while count<k and curr>=0 and grid[x][curr]=='.':
if grid1[x][curr]!=-1:
curr-=1
count+=1
continue
que.append((x,curr,step+1))
grid1[x][curr]=step+1
curr-=1
count+=1
else:
if leftyes[x][y] and grid1[x][y-k]==-1:
que.append((x,y-k,step+1))
grid1[x][y-k]=step+1
if not(y>0 and grid1[x][y-1]!=-1 and grid1[x][y-1]<=step):
curr=y+1
count=0
while count<k and curr<=m-1 and grid[x][curr]=='.':
if grid1[x][curr]!=-1:
curr+=1
count+=1
continue
que.append((x,curr,step+1))
grid1[x][curr]=step+1
curr+=1
count+=1
else:
if rightyes[x][y] and grid1[x][y+k]==-1:
que.append((x,y+k,step+1))
grid1[x][y+k]=step+1
print(grid1[x2-1][y2-1])
``` | output | 1 | 108,352 | 15 | 216,705 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Olya loves energy drinks. She loves them so much that her room is full of empty cans from energy drinks.
Formally, her room can be represented as a field of n Γ m cells, each cell of which is empty or littered with cans.
Olya drank a lot of energy drink, so now she can run k meters per second. Each second she chooses one of the four directions (up, down, left or right) and runs from 1 to k meters in this direction. Of course, she can only run through empty cells.
Now Olya needs to get from cell (x1, y1) to cell (x2, y2). How many seconds will it take her if she moves optimally?
It's guaranteed that cells (x1, y1) and (x2, y2) are empty. These cells can coincide.
Input
The first line contains three integers n, m and k (1 β€ n, m, k β€ 1000) β the sizes of the room and Olya's speed.
Then n lines follow containing m characters each, the i-th of them contains on j-th position "#", if the cell (i, j) is littered with cans, and "." otherwise.
The last line contains four integers x1, y1, x2, y2 (1 β€ x1, x2 β€ n, 1 β€ y1, y2 β€ m) β the coordinates of the first and the last cells.
Output
Print a single integer β the minimum time it will take Olya to get from (x1, y1) to (x2, y2).
If it's impossible to get from (x1, y1) to (x2, y2), print -1.
Examples
Input
3 4 4
....
###.
....
1 1 3 1
Output
3
Input
3 4 1
....
###.
....
1 1 3 1
Output
8
Input
2 2 1
.#
#.
1 1 2 2
Output
-1
Note
In the first sample Olya should run 3 meters to the right in the first second, 2 meters down in the second second and 3 meters to the left in the third second.
In second sample Olya should run to the right for 3 seconds, then down for 2 seconds and then to the left for 3 seconds.
Olya does not recommend drinking energy drinks and generally believes that this is bad. | instruction | 0 | 108,353 | 15 | 216,706 |
Tags: data structures, dfs and similar, graphs, shortest paths
Correct Solution:
```
from collections import deque
n, m, k = map(int, input().split())
INF = float("inf")
d = [[INF] * m for _ in range(n)]
t = [[] for i in range(n)]
for i in range(n):
a = list(input())
t[i] = a
sx, sy, gx, gy = map(int, input().split())
sx, sy, gx, gy = sx - 1, sy - 1, gx - 1, gy - 1
def bfs():
que = deque()
que.append((sx, sy))
d[sx][sy] = 0
while len(que):
x, y = que.popleft()
if x == gx and y == gy:
break
for dx, dy in [(0, 1), (1, 0), (-1, 0), (0, -1)]:
for i in range(1, k + 1):
nx, ny = x + i * dx, y + i * dy
if not 0 <= nx < n or not 0 <= ny < m or t[nx][ny] != "." or d[nx][ny] <= d[x][y]:
break
else:
if d[nx][ny] > d[x][y] + 1:
d[nx][ny] = d[x][y] + 1
que.append((nx, ny))
return d[gx][gy]
ans = bfs()
if ans == INF:
print(-1)
else:
print(ans)
``` | output | 1 | 108,353 | 15 | 216,707 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Olya loves energy drinks. She loves them so much that her room is full of empty cans from energy drinks.
Formally, her room can be represented as a field of n Γ m cells, each cell of which is empty or littered with cans.
Olya drank a lot of energy drink, so now she can run k meters per second. Each second she chooses one of the four directions (up, down, left or right) and runs from 1 to k meters in this direction. Of course, she can only run through empty cells.
Now Olya needs to get from cell (x1, y1) to cell (x2, y2). How many seconds will it take her if she moves optimally?
It's guaranteed that cells (x1, y1) and (x2, y2) are empty. These cells can coincide.
Input
The first line contains three integers n, m and k (1 β€ n, m, k β€ 1000) β the sizes of the room and Olya's speed.
Then n lines follow containing m characters each, the i-th of them contains on j-th position "#", if the cell (i, j) is littered with cans, and "." otherwise.
The last line contains four integers x1, y1, x2, y2 (1 β€ x1, x2 β€ n, 1 β€ y1, y2 β€ m) β the coordinates of the first and the last cells.
Output
Print a single integer β the minimum time it will take Olya to get from (x1, y1) to (x2, y2).
If it's impossible to get from (x1, y1) to (x2, y2), print -1.
Examples
Input
3 4 4
....
###.
....
1 1 3 1
Output
3
Input
3 4 1
....
###.
....
1 1 3 1
Output
8
Input
2 2 1
.#
#.
1 1 2 2
Output
-1
Note
In the first sample Olya should run 3 meters to the right in the first second, 2 meters down in the second second and 3 meters to the left in the third second.
In second sample Olya should run to the right for 3 seconds, then down for 2 seconds and then to the left for 3 seconds.
Olya does not recommend drinking energy drinks and generally believes that this is bad. | instruction | 0 | 108,354 | 15 | 216,708 |
Tags: data structures, dfs and similar, graphs, shortest paths
Correct Solution:
```
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**15
mod = 10**9+7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def main():
n,m,k = LI()
a = [[-1] * (m+2)]
a += [[-1] + [inf if c=='.' else -1 for c in S()] + [-1] for _ in range(n)]
a += [[-1] * (m+2)]
x1,y1,x2,y2 = LI()
a[x1][y1] = 0
q = [(x1,y1)]
qi = 0
dy = [-1,1,0,0]
dx = [0,0,-1,1]
while qi < len(q):
x,y = q[qi]
qi += 1
nd = a[x][y] + 1
for di in range(4):
for j in range(1,k+1):
ny = y + dy[di] * j
nx = x + dx[di] * j
if a[nx][ny] > nd:
if ny == y2 and nx == x2:
return nd
a[nx][ny] = nd
q.append((nx,ny))
elif a[nx][ny] < nd:
break
if a[x2][y2] < inf:
return a[x2][y2]
return -1
print(main())
``` | output | 1 | 108,354 | 15 | 216,709 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Olya loves energy drinks. She loves them so much that her room is full of empty cans from energy drinks.
Formally, her room can be represented as a field of n Γ m cells, each cell of which is empty or littered with cans.
Olya drank a lot of energy drink, so now she can run k meters per second. Each second she chooses one of the four directions (up, down, left or right) and runs from 1 to k meters in this direction. Of course, she can only run through empty cells.
Now Olya needs to get from cell (x1, y1) to cell (x2, y2). How many seconds will it take her if she moves optimally?
It's guaranteed that cells (x1, y1) and (x2, y2) are empty. These cells can coincide.
Input
The first line contains three integers n, m and k (1 β€ n, m, k β€ 1000) β the sizes of the room and Olya's speed.
Then n lines follow containing m characters each, the i-th of them contains on j-th position "#", if the cell (i, j) is littered with cans, and "." otherwise.
The last line contains four integers x1, y1, x2, y2 (1 β€ x1, x2 β€ n, 1 β€ y1, y2 β€ m) β the coordinates of the first and the last cells.
Output
Print a single integer β the minimum time it will take Olya to get from (x1, y1) to (x2, y2).
If it's impossible to get from (x1, y1) to (x2, y2), print -1.
Examples
Input
3 4 4
....
###.
....
1 1 3 1
Output
3
Input
3 4 1
....
###.
....
1 1 3 1
Output
8
Input
2 2 1
.#
#.
1 1 2 2
Output
-1
Note
In the first sample Olya should run 3 meters to the right in the first second, 2 meters down in the second second and 3 meters to the left in the third second.
In second sample Olya should run to the right for 3 seconds, then down for 2 seconds and then to the left for 3 seconds.
Olya does not recommend drinking energy drinks and generally believes that this is bad.
Submitted Solution:
```
from sys import stdin
input=lambda : stdin.readline().strip()
from math import ceil,sqrt,factorial,gcd
from collections import deque
n,m,k=map(int,input().split())
dp=[[float("infinity") for i in range(m)] for i in range(n)]
l=[]
for i in range(n):
l.append(list(input()))
x1,y1,x2,y2=map(lambda a:int(a)-1,input().split())
t=[(1,0),(0,1),(0,-1),(-1,0)]
q=deque()
q.append((x1,y1))
dp[x1][y1]=0
while q:
x,y=q.popleft()
if x==x2 and y==y2:
# print(90)
break
for a,b in t:
for i in range(1,k+1):
e=x+i*a
f=y+i*b
if e<0 or e>=n or f>=m or f<0 or l[e][f]!="." or dp[e][f]<dp[x][y]+1:
# print(e,f)
break
else:
if dp[e][f]>dp[x][y]+1:
dp[e][f]=dp[x][y]+1
q.append((e,f))
# print(q)
ans=dp[x2][y2]
if ans==float("infinity"):
ans=-1
print(ans)
``` | instruction | 0 | 108,355 | 15 | 216,710 |
Yes | output | 1 | 108,355 | 15 | 216,711 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Olya loves energy drinks. She loves them so much that her room is full of empty cans from energy drinks.
Formally, her room can be represented as a field of n Γ m cells, each cell of which is empty or littered with cans.
Olya drank a lot of energy drink, so now she can run k meters per second. Each second she chooses one of the four directions (up, down, left or right) and runs from 1 to k meters in this direction. Of course, she can only run through empty cells.
Now Olya needs to get from cell (x1, y1) to cell (x2, y2). How many seconds will it take her if she moves optimally?
It's guaranteed that cells (x1, y1) and (x2, y2) are empty. These cells can coincide.
Input
The first line contains three integers n, m and k (1 β€ n, m, k β€ 1000) β the sizes of the room and Olya's speed.
Then n lines follow containing m characters each, the i-th of them contains on j-th position "#", if the cell (i, j) is littered with cans, and "." otherwise.
The last line contains four integers x1, y1, x2, y2 (1 β€ x1, x2 β€ n, 1 β€ y1, y2 β€ m) β the coordinates of the first and the last cells.
Output
Print a single integer β the minimum time it will take Olya to get from (x1, y1) to (x2, y2).
If it's impossible to get from (x1, y1) to (x2, y2), print -1.
Examples
Input
3 4 4
....
###.
....
1 1 3 1
Output
3
Input
3 4 1
....
###.
....
1 1 3 1
Output
8
Input
2 2 1
.#
#.
1 1 2 2
Output
-1
Note
In the first sample Olya should run 3 meters to the right in the first second, 2 meters down in the second second and 3 meters to the left in the third second.
In second sample Olya should run to the right for 3 seconds, then down for 2 seconds and then to the left for 3 seconds.
Olya does not recommend drinking energy drinks and generally believes that this is bad.
Submitted Solution:
```
# aadiupadhyay
import os.path
# testing extension
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')
dx = [0, 0, -1, 1]
dy = [1, -1, 0, 0]
n, m, k = mp()
l = []
now = [INF for i in range(m)]
distance = []
for i in range(n):
cur = list(now)
s = st()
l.append(s)
distance.append(cur)
a, b, c, d = mp()
a, b, c, d = a-1, b-1, c-1, d-1
q = deque()
q.append((a, b))
distance[a][b] = 0
ans = -1
while q:
x, y = q.popleft()
if (x, y) == (c, d):
ans = distance[x][y]
break
for i in range(4):
for j in range(1, k+1):
x1, x2 = x+j*dx[i], y+j*dy[i]
if x1 < 0 or x2 < 0 or x1 >= n or x2 >= m or l[x1][x2] == '#' or distance[x1][x2] < 1+distance[x][y]:
break
else:
if distance[x1][x2] > 1 + distance[x][y]:
distance[x1][x2] = 1+distance[x][y]
q.append((x1, x2))
pr(ans)
``` | instruction | 0 | 108,356 | 15 | 216,712 |
Yes | output | 1 | 108,356 | 15 | 216,713 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Olya loves energy drinks. She loves them so much that her room is full of empty cans from energy drinks.
Formally, her room can be represented as a field of n Γ m cells, each cell of which is empty or littered with cans.
Olya drank a lot of energy drink, so now she can run k meters per second. Each second she chooses one of the four directions (up, down, left or right) and runs from 1 to k meters in this direction. Of course, she can only run through empty cells.
Now Olya needs to get from cell (x1, y1) to cell (x2, y2). How many seconds will it take her if she moves optimally?
It's guaranteed that cells (x1, y1) and (x2, y2) are empty. These cells can coincide.
Input
The first line contains three integers n, m and k (1 β€ n, m, k β€ 1000) β the sizes of the room and Olya's speed.
Then n lines follow containing m characters each, the i-th of them contains on j-th position "#", if the cell (i, j) is littered with cans, and "." otherwise.
The last line contains four integers x1, y1, x2, y2 (1 β€ x1, x2 β€ n, 1 β€ y1, y2 β€ m) β the coordinates of the first and the last cells.
Output
Print a single integer β the minimum time it will take Olya to get from (x1, y1) to (x2, y2).
If it's impossible to get from (x1, y1) to (x2, y2), print -1.
Examples
Input
3 4 4
....
###.
....
1 1 3 1
Output
3
Input
3 4 1
....
###.
....
1 1 3 1
Output
8
Input
2 2 1
.#
#.
1 1 2 2
Output
-1
Note
In the first sample Olya should run 3 meters to the right in the first second, 2 meters down in the second second and 3 meters to the left in the third second.
In second sample Olya should run to the right for 3 seconds, then down for 2 seconds and then to the left for 3 seconds.
Olya does not recommend drinking energy drinks and generally believes that this is bad.
Submitted Solution:
```
import sys
from collections import deque
input=sys.stdin.readline
n,m,k=map(int,input().split())
grid=[]
grid1=[]
grid2=[]
leftyes=[]
rightyes=[]
upyes=[]
downyes=[]
for i in range(n):
grid.append(input())
grid1.append([-1]*m)
grid2.append([0]*m)
leftyes.append([0]*m)
rightyes.append([0]*m)
downyes.append([0]*m)
upyes.append([0]*m)
for i in range(n):
count=0
for j in range(m):
if grid[i][j]=='.':
count+=1
else:
count=0
if count>k:
leftyes[i][j]=1
rightyes[i][j-k]=1
for i in range(m):
count=0
for j in range(n):
if grid[j][i]=='.':
count+=1
else:
count=0
if count>k:
upyes[j][i]=1
downyes[j-k][i]=1
x1,y1,x2,y2=map(int,input().split())
que=deque([(x1-1,y1-1,0)])
grid1[x1-1][y1-1]=0
while que:
x,y,step=que.popleft()
if grid2[x][y]:
continue
grid2[x][y]=1
if not(x<n-1 and grid1[x+1][y]!=-1):
curr=x-1
count=0
while count<k and curr>=0 and grid[curr][y]=='.' and grid1[curr][y]==-1:
que.append((curr,y,step+1))
grid1[curr][y]=step+1
curr-=1
count+=1
else:
if upyes[x][y] and grid1[x-k][y]==-1:
que.append((x-k,y,step+1))
grid1[x-k][y]=step+1
if not(x>0 and grid1[x-1][y]!=-1):
curr=x+1
count=0
while count<k and curr<=n-1 and grid[curr][y]=='.' and grid1[curr][y]==-1:
que.append((curr,y,step+1))
grid1[curr][y]=step+1
curr+=1
count+=1
else:
if downyes[x][y] and grid1[x+k][y]==-1:
que.append((x+k,y,step+1))
grid1[x+k][y]=step+1
if not(y<m-1 and grid1[x][y+1]!=-1):
curr=y-1
count=0
while count<k and curr>=0 and grid[x][curr]=='.' and grid1[x][curr]==-1:
que.append((x,curr,step+1))
grid1[x][curr]=step+1
curr-=1
count+=1
else:
if leftyes[x][y] and grid1[x][y-k]==-1:
que.append((x,y-k,step+1))
grid1[x][y-k]=step+1
if not(y>0 and grid1[x][y-1]!=-1):
curr=y+1
count=0
while count<k and curr<=m-1 and grid[x][curr]=='.' and grid1[x][curr]==-1:
que.append((x,curr,step+1))
grid1[x][curr]=step+1
curr+=1
count+=1
else:
if rightyes[x][y] and grid1[x][y+k]==-1:
que.append((x,y+k,step+1))
grid1[x][y+k]=step+1
print(grid1[x2-1][y2-1])
``` | instruction | 0 | 108,357 | 15 | 216,714 |
No | output | 1 | 108,357 | 15 | 216,715 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Olya loves energy drinks. She loves them so much that her room is full of empty cans from energy drinks.
Formally, her room can be represented as a field of n Γ m cells, each cell of which is empty or littered with cans.
Olya drank a lot of energy drink, so now she can run k meters per second. Each second she chooses one of the four directions (up, down, left or right) and runs from 1 to k meters in this direction. Of course, she can only run through empty cells.
Now Olya needs to get from cell (x1, y1) to cell (x2, y2). How many seconds will it take her if she moves optimally?
It's guaranteed that cells (x1, y1) and (x2, y2) are empty. These cells can coincide.
Input
The first line contains three integers n, m and k (1 β€ n, m, k β€ 1000) β the sizes of the room and Olya's speed.
Then n lines follow containing m characters each, the i-th of them contains on j-th position "#", if the cell (i, j) is littered with cans, and "." otherwise.
The last line contains four integers x1, y1, x2, y2 (1 β€ x1, x2 β€ n, 1 β€ y1, y2 β€ m) β the coordinates of the first and the last cells.
Output
Print a single integer β the minimum time it will take Olya to get from (x1, y1) to (x2, y2).
If it's impossible to get from (x1, y1) to (x2, y2), print -1.
Examples
Input
3 4 4
....
###.
....
1 1 3 1
Output
3
Input
3 4 1
....
###.
....
1 1 3 1
Output
8
Input
2 2 1
.#
#.
1 1 2 2
Output
-1
Note
In the first sample Olya should run 3 meters to the right in the first second, 2 meters down in the second second and 3 meters to the left in the third second.
In second sample Olya should run to the right for 3 seconds, then down for 2 seconds and then to the left for 3 seconds.
Olya does not recommend drinking energy drinks and generally believes that this is bad.
Submitted Solution:
```
import sys
from collections import deque
input=sys.stdin.readline
n,m,k=map(int,input().split())
grid=[]
grid1=[]
grid2=[]
leftyes=[]
rightyes=[]
upyes=[]
downyes=[]
for i in range(n):
grid.append(input())
grid1.append([-1]*m)
grid2.append([0]*m)
leftyes.append([0]*m)
rightyes.append([0]*m)
downyes.append([0]*m)
upyes.append([0]*m)
for i in range(n):
count=0
for j in range(m):
if grid[i][j]=='.':
count+=1
else:
count=0
if count>k:
leftyes[i][j]=1
rightyes[i][j-k]=1
for i in range(m):
count=0
for j in range(n):
if grid[j][i]=='.':
count+=1
else:
count=0
if count>k:
upyes[j][i]=1
downyes[j-k][i]=1
x1,y1,x2,y2=map(int,input().split())
que=deque([(x1-1,y1-1,0)])
grid1[x1-1][y1-1]=0
while que:
x,y,step=que.popleft()
if grid2[x][y]:
continue
grid2[x][y]=1
if not(x<n-1 and grid1[x+1][y]!=-1):
curr=x-1
count=0
while count<k and curr>=0 and grid[curr][y]=='.' and grid1[curr][y]==-1:
que.append((curr,y,step+1))
grid1[curr][y]=step+1
curr-=1
count+=1
else:
if upyes[x][y]:
que.append((x-k,y,step+1))
grid1[x-k][y]=step+1
if not(x>0 and grid1[x-1][y]!=-1):
curr=x+1
count=0
while count<k and curr<=n-1 and grid[curr][y]=='.' and grid1[curr][y]==-1:
que.append((curr,y,step+1))
grid1[curr][y]=step+1
curr+=1
count+=1
else:
if downyes[x][y]:
que.append((x+k,y,step+1))
grid1[x+k][y]=step+1
if not(y<m-1 and grid1[x][y+1]!=-1):
curr=y-1
count=0
while count<k and curr>=0 and grid[x][curr]=='.' and grid1[x][curr]==-1:
que.append((x,curr,step+1))
grid1[x][curr]=step+1
curr-=1
count+=1
else:
if leftyes[x][y]:
que.append((x,y-k,step+1))
grid1[x][y-k]=step+1
if not(y>0 and grid1[x][y-1]!=-1):
curr=y+1
count=0
while count<k and curr<=m-1 and grid[x][curr]=='.' and grid1[x][curr]==-1:
que.append((x,curr,step+1))
grid1[x][curr]=step+1
curr+=1
count+=1
else:
if rightyes[x][y]:
que.append((x,y+k,step+1))
grid1[x][y+k]=step+1
print(grid1[x2-1][y2-1])
``` | instruction | 0 | 108,358 | 15 | 216,716 |
No | output | 1 | 108,358 | 15 | 216,717 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Olya loves energy drinks. She loves them so much that her room is full of empty cans from energy drinks.
Formally, her room can be represented as a field of n Γ m cells, each cell of which is empty or littered with cans.
Olya drank a lot of energy drink, so now she can run k meters per second. Each second she chooses one of the four directions (up, down, left or right) and runs from 1 to k meters in this direction. Of course, she can only run through empty cells.
Now Olya needs to get from cell (x1, y1) to cell (x2, y2). How many seconds will it take her if she moves optimally?
It's guaranteed that cells (x1, y1) and (x2, y2) are empty. These cells can coincide.
Input
The first line contains three integers n, m and k (1 β€ n, m, k β€ 1000) β the sizes of the room and Olya's speed.
Then n lines follow containing m characters each, the i-th of them contains on j-th position "#", if the cell (i, j) is littered with cans, and "." otherwise.
The last line contains four integers x1, y1, x2, y2 (1 β€ x1, x2 β€ n, 1 β€ y1, y2 β€ m) β the coordinates of the first and the last cells.
Output
Print a single integer β the minimum time it will take Olya to get from (x1, y1) to (x2, y2).
If it's impossible to get from (x1, y1) to (x2, y2), print -1.
Examples
Input
3 4 4
....
###.
....
1 1 3 1
Output
3
Input
3 4 1
....
###.
....
1 1 3 1
Output
8
Input
2 2 1
.#
#.
1 1 2 2
Output
-1
Note
In the first sample Olya should run 3 meters to the right in the first second, 2 meters down in the second second and 3 meters to the left in the third second.
In second sample Olya should run to the right for 3 seconds, then down for 2 seconds and then to the left for 3 seconds.
Olya does not recommend drinking energy drinks and generally believes that this is bad.
Submitted Solution:
```
from collections import deque
n, m, k = map(int, input().split())
INF = float("inf")
d = [[INF] * (m + 1) for _ in range(n + 1)]
t = [[] for i in range(n + 1)]
for i in range(n):
a = list("#" + input())
t[i + 1] = a
sx, sy, gx, gy = map(int, input().split())
def bfs():
px, py, cnt = 0, 0, 1
que = deque()
que.append((sx, sy, px, py, cnt))
d[sx][sy] = 0
while len(que):
x, y, px, py, cnt = que.popleft()
if x == gx and y == gy:
break
for dx, dy in [(0, 1), (1, 0), (-1, 0), (0, -1)]:
nx, ny = x + dx, y + dy
if 0 < nx <= n and 0 < ny <= m and t[nx][ny] != "#" and d[nx][ny] - 1 > d[x][y]:
if px == dx and py == dy and cnt < k:
d[nx][ny] = d[x][y]
cnt += 1
que.append((nx, ny, dx, dy, cnt))
else:
d[nx][ny] = d[x][y] + 1
cnt = 1
que.append((nx, ny, dx, dy, cnt))
return d[gx][gy]
ans = bfs()
if ans == INF:
print(-1)
else:
print(ans)
``` | instruction | 0 | 108,359 | 15 | 216,718 |
No | output | 1 | 108,359 | 15 | 216,719 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Olya loves energy drinks. She loves them so much that her room is full of empty cans from energy drinks.
Formally, her room can be represented as a field of n Γ m cells, each cell of which is empty or littered with cans.
Olya drank a lot of energy drink, so now she can run k meters per second. Each second she chooses one of the four directions (up, down, left or right) and runs from 1 to k meters in this direction. Of course, she can only run through empty cells.
Now Olya needs to get from cell (x1, y1) to cell (x2, y2). How many seconds will it take her if she moves optimally?
It's guaranteed that cells (x1, y1) and (x2, y2) are empty. These cells can coincide.
Input
The first line contains three integers n, m and k (1 β€ n, m, k β€ 1000) β the sizes of the room and Olya's speed.
Then n lines follow containing m characters each, the i-th of them contains on j-th position "#", if the cell (i, j) is littered with cans, and "." otherwise.
The last line contains four integers x1, y1, x2, y2 (1 β€ x1, x2 β€ n, 1 β€ y1, y2 β€ m) β the coordinates of the first and the last cells.
Output
Print a single integer β the minimum time it will take Olya to get from (x1, y1) to (x2, y2).
If it's impossible to get from (x1, y1) to (x2, y2), print -1.
Examples
Input
3 4 4
....
###.
....
1 1 3 1
Output
3
Input
3 4 1
....
###.
....
1 1 3 1
Output
8
Input
2 2 1
.#
#.
1 1 2 2
Output
-1
Note
In the first sample Olya should run 3 meters to the right in the first second, 2 meters down in the second second and 3 meters to the left in the third second.
In second sample Olya should run to the right for 3 seconds, then down for 2 seconds and then to the left for 3 seconds.
Olya does not recommend drinking energy drinks and generally believes that this is bad.
Submitted Solution:
```
from collections import deque
n, m, k = map(int, input().split())
INF = float("inf")
d = [[INF] * (m + 1) for _ in range(n + 1)]
t = [[] for i in range(n + 1)]
for i in range(n):
a = list("#" + input())
t[i + 1] = a
sx, sy, gx, gy = map(int, input().split())
def bfs():
px, py, cnt = 0, 0, 0
que = deque()
que.append((sx, sy, px, py, cnt))
d[sx][sy] = 0
while len(que):
x, y, px, py, cnt = que.popleft()
# if x == gx and y == gy:
# break
for dx, dy in [(0, 1), (1, 0), (-1, 0), (0, -1)]:
nx, ny = x + dx, y + dy
if 0 < nx <= n and 0 < ny <= m and t[nx][ny] != "#" and d[nx][ny] - 1 > d[x][y]:
if px == dx and py == dy and cnt < k:
d[nx][ny] = d[x][y]
cnt += 1
else:
d[nx][ny] = d[x][y] + 1
cnt = 1
px, py = dx, dy
que.append((nx, ny, px, py, cnt))
return d[gx][gy]
ans = bfs()
if ans == INF:
print(-1)
else:
print(ans)
``` | instruction | 0 | 108,360 | 15 | 216,720 |
No | output | 1 | 108,360 | 15 | 216,721 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.