message stringlengths 2 28.7k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 21 109k | cluster float64 7 7 | __index_level_0__ int64 42 217k |
|---|---|---|---|---|---|
Provide a correct Python 3 solution for this coding contest problem.
We have a grid of H rows and W columns of squares. The color of the square at the i-th row from the top and the j-th column from the left (1 \leq i \leq H, 1 \leq j \leq W) is given to you as a character c_{i,j}: the square is white if c_{i,j} is `.`, and black if c_{i,j} is `#`.
Consider doing the following operation:
* Choose some number of rows (possibly zero), and some number of columns (possibly zero). Then, paint red all squares in the chosen rows and all squares in the chosen columns.
You are given a positive integer K. How many choices of rows and columns result in exactly K black squares remaining after the operation? Here, we consider two choices different when there is a row or column chosen in only one of those choices.
Constraints
* 1 \leq H, W \leq 6
* 1 \leq K \leq HW
* c_{i,j} is `.` or `#`.
Input
Input is given from Standard Input in the following format:
H W K
c_{1,1}c_{1,2}...c_{1,W}
c_{2,1}c_{2,2}...c_{2,W}
:
c_{H,1}c_{H,2}...c_{H,W}
Output
Print an integer representing the number of choices of rows and columns satisfying the condition.
Examples
Input
2 3 2
..#
###
Output
5
Input
2 3 2
..#
Output
5
Input
2 3 4
..#
Output
1
Input
2 2 3
Output
0
Input
6 6 8
..##..
.#..#.
....#
....#
....#
Output
208 | instruction | 0 | 19,128 | 7 | 38,256 |
"Correct Solution:
```
H, W, K = map(int, input().split())
C = [input() for _ in range(H)]
ans = 0
for r in range(1<<H):
for c in range(1<<W):
k = 0
for h in range(H):
if (r>>h)&1: continue
for w in range(W):
if (c>>w)&1: continue
if C[h][w]=='#': k+=1
if k==K: ans+=1
print(ans)
``` | output | 1 | 19,128 | 7 | 38,257 |
Provide a correct Python 3 solution for this coding contest problem.
We have a grid of H rows and W columns of squares. The color of the square at the i-th row from the top and the j-th column from the left (1 \leq i \leq H, 1 \leq j \leq W) is given to you as a character c_{i,j}: the square is white if c_{i,j} is `.`, and black if c_{i,j} is `#`.
Consider doing the following operation:
* Choose some number of rows (possibly zero), and some number of columns (possibly zero). Then, paint red all squares in the chosen rows and all squares in the chosen columns.
You are given a positive integer K. How many choices of rows and columns result in exactly K black squares remaining after the operation? Here, we consider two choices different when there is a row or column chosen in only one of those choices.
Constraints
* 1 \leq H, W \leq 6
* 1 \leq K \leq HW
* c_{i,j} is `.` or `#`.
Input
Input is given from Standard Input in the following format:
H W K
c_{1,1}c_{1,2}...c_{1,W}
c_{2,1}c_{2,2}...c_{2,W}
:
c_{H,1}c_{H,2}...c_{H,W}
Output
Print an integer representing the number of choices of rows and columns satisfying the condition.
Examples
Input
2 3 2
..#
###
Output
5
Input
2 3 2
..#
Output
5
Input
2 3 4
..#
Output
1
Input
2 2 3
Output
0
Input
6 6 8
..##..
.#..#.
....#
....#
....#
Output
208 | instruction | 0 | 19,129 | 7 | 38,258 |
"Correct Solution:
```
h,*c=open(0)
h,w,k=map(int,h.split())
print(sum(k==sum(y>>w+z//w&y>>z%w&1and'.'>c[z//w][z%w]for z in range(h*w))for y in range(1<<h+w)))
``` | output | 1 | 19,129 | 7 | 38,259 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a grid of H rows and W columns of squares. The color of the square at the i-th row from the top and the j-th column from the left (1 \leq i \leq H, 1 \leq j \leq W) is given to you as a character c_{i,j}: the square is white if c_{i,j} is `.`, and black if c_{i,j} is `#`.
Consider doing the following operation:
* Choose some number of rows (possibly zero), and some number of columns (possibly zero). Then, paint red all squares in the chosen rows and all squares in the chosen columns.
You are given a positive integer K. How many choices of rows and columns result in exactly K black squares remaining after the operation? Here, we consider two choices different when there is a row or column chosen in only one of those choices.
Constraints
* 1 \leq H, W \leq 6
* 1 \leq K \leq HW
* c_{i,j} is `.` or `#`.
Input
Input is given from Standard Input in the following format:
H W K
c_{1,1}c_{1,2}...c_{1,W}
c_{2,1}c_{2,2}...c_{2,W}
:
c_{H,1}c_{H,2}...c_{H,W}
Output
Print an integer representing the number of choices of rows and columns satisfying the condition.
Examples
Input
2 3 2
..#
###
Output
5
Input
2 3 2
..#
Output
5
Input
2 3 4
..#
Output
1
Input
2 2 3
Output
0
Input
6 6 8
..##..
.#..#.
....#
....#
....#
Output
208
Submitted Solution:
```
h,w,K = map(int,input().split())
masu = [input() for _ in range(h)]
ans = 0
for i in range(2**h):
for j in range(2**w):
cnt = 0
for k in range(h):
for l in range(w):
if ((i >> k) & 1) and ((j >> l) & 1) and masu[k][l] == "#":
cnt += 1
if cnt == K:
ans += 1
print(ans)
``` | instruction | 0 | 19,130 | 7 | 38,260 |
Yes | output | 1 | 19,130 | 7 | 38,261 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a grid of H rows and W columns of squares. The color of the square at the i-th row from the top and the j-th column from the left (1 \leq i \leq H, 1 \leq j \leq W) is given to you as a character c_{i,j}: the square is white if c_{i,j} is `.`, and black if c_{i,j} is `#`.
Consider doing the following operation:
* Choose some number of rows (possibly zero), and some number of columns (possibly zero). Then, paint red all squares in the chosen rows and all squares in the chosen columns.
You are given a positive integer K. How many choices of rows and columns result in exactly K black squares remaining after the operation? Here, we consider two choices different when there is a row or column chosen in only one of those choices.
Constraints
* 1 \leq H, W \leq 6
* 1 \leq K \leq HW
* c_{i,j} is `.` or `#`.
Input
Input is given from Standard Input in the following format:
H W K
c_{1,1}c_{1,2}...c_{1,W}
c_{2,1}c_{2,2}...c_{2,W}
:
c_{H,1}c_{H,2}...c_{H,W}
Output
Print an integer representing the number of choices of rows and columns satisfying the condition.
Examples
Input
2 3 2
..#
###
Output
5
Input
2 3 2
..#
Output
5
Input
2 3 4
..#
Output
1
Input
2 2 3
Output
0
Input
6 6 8
..##..
.#..#.
....#
....#
....#
Output
208
Submitted Solution:
```
h,w,k = map(int, input().split())
import sys
c = [[str(a) for a in l.strip()] for l in sys.stdin]
ans = 0
b = 2 ** (h + w)
for i in range(b):
for j in range(h):
for q in range(w):
if ((i >> j) & 1) and (i >> (q+h)) & 1:
if c[j][q] == "#":
sum += 1
if sum == k:
ans += 1
sum = 0
print(ans)
``` | instruction | 0 | 19,131 | 7 | 38,262 |
Yes | output | 1 | 19,131 | 7 | 38,263 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a grid of H rows and W columns of squares. The color of the square at the i-th row from the top and the j-th column from the left (1 \leq i \leq H, 1 \leq j \leq W) is given to you as a character c_{i,j}: the square is white if c_{i,j} is `.`, and black if c_{i,j} is `#`.
Consider doing the following operation:
* Choose some number of rows (possibly zero), and some number of columns (possibly zero). Then, paint red all squares in the chosen rows and all squares in the chosen columns.
You are given a positive integer K. How many choices of rows and columns result in exactly K black squares remaining after the operation? Here, we consider two choices different when there is a row or column chosen in only one of those choices.
Constraints
* 1 \leq H, W \leq 6
* 1 \leq K \leq HW
* c_{i,j} is `.` or `#`.
Input
Input is given from Standard Input in the following format:
H W K
c_{1,1}c_{1,2}...c_{1,W}
c_{2,1}c_{2,2}...c_{2,W}
:
c_{H,1}c_{H,2}...c_{H,W}
Output
Print an integer representing the number of choices of rows and columns satisfying the condition.
Examples
Input
2 3 2
..#
###
Output
5
Input
2 3 2
..#
Output
5
Input
2 3 4
..#
Output
1
Input
2 2 3
Output
0
Input
6 6 8
..##..
.#..#.
....#
....#
....#
Output
208
Submitted Solution:
```
from copy import deepcopy
H,W,K = map(int,input().split())
c = [input() for _ in range(H)]
ans = 0
for i in range(2**H):
for j in range(2**W):
r = 0
for k in range(H):
if ((i >> k) & 1):
for l in range(W):
if ((j >> l) & 1):
if c[k][l] == "#":
r += 1
if r == K:
ans += 1
print(ans)
``` | instruction | 0 | 19,132 | 7 | 38,264 |
Yes | output | 1 | 19,132 | 7 | 38,265 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a grid of H rows and W columns of squares. The color of the square at the i-th row from the top and the j-th column from the left (1 \leq i \leq H, 1 \leq j \leq W) is given to you as a character c_{i,j}: the square is white if c_{i,j} is `.`, and black if c_{i,j} is `#`.
Consider doing the following operation:
* Choose some number of rows (possibly zero), and some number of columns (possibly zero). Then, paint red all squares in the chosen rows and all squares in the chosen columns.
You are given a positive integer K. How many choices of rows and columns result in exactly K black squares remaining after the operation? Here, we consider two choices different when there is a row or column chosen in only one of those choices.
Constraints
* 1 \leq H, W \leq 6
* 1 \leq K \leq HW
* c_{i,j} is `.` or `#`.
Input
Input is given from Standard Input in the following format:
H W K
c_{1,1}c_{1,2}...c_{1,W}
c_{2,1}c_{2,2}...c_{2,W}
:
c_{H,1}c_{H,2}...c_{H,W}
Output
Print an integer representing the number of choices of rows and columns satisfying the condition.
Examples
Input
2 3 2
..#
###
Output
5
Input
2 3 2
..#
Output
5
Input
2 3 4
..#
Output
1
Input
2 2 3
Output
0
Input
6 6 8
..##..
.#..#.
....#
....#
....#
Output
208
Submitted Solution:
```
H,W,K=map(int,input().split())
C=[input() for _ in range(H)]
ans=0
for i in range(2**H):
for j in range(2**W):
c=0
for ii in range(H):
for jj in range(W):
if (i>>ii)&(j>>jj)&1:
if C[ii][jj]=='#':
c+=1
if c==K:
ans+=1
print(ans)
``` | instruction | 0 | 19,133 | 7 | 38,266 |
Yes | output | 1 | 19,133 | 7 | 38,267 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a grid of H rows and W columns of squares. The color of the square at the i-th row from the top and the j-th column from the left (1 \leq i \leq H, 1 \leq j \leq W) is given to you as a character c_{i,j}: the square is white if c_{i,j} is `.`, and black if c_{i,j} is `#`.
Consider doing the following operation:
* Choose some number of rows (possibly zero), and some number of columns (possibly zero). Then, paint red all squares in the chosen rows and all squares in the chosen columns.
You are given a positive integer K. How many choices of rows and columns result in exactly K black squares remaining after the operation? Here, we consider two choices different when there is a row or column chosen in only one of those choices.
Constraints
* 1 \leq H, W \leq 6
* 1 \leq K \leq HW
* c_{i,j} is `.` or `#`.
Input
Input is given from Standard Input in the following format:
H W K
c_{1,1}c_{1,2}...c_{1,W}
c_{2,1}c_{2,2}...c_{2,W}
:
c_{H,1}c_{H,2}...c_{H,W}
Output
Print an integer representing the number of choices of rows and columns satisfying the condition.
Examples
Input
2 3 2
..#
###
Output
5
Input
2 3 2
..#
Output
5
Input
2 3 4
..#
Output
1
Input
2 2 3
Output
0
Input
6 6 8
..##..
.#..#.
....#
....#
....#
Output
208
Submitted Solution:
```
import itertools
import copy
import numpy as np
H, W, K = map(int, input().split())
C = []
for i in range(H):
# C.append(list(input()))
C.append(input())
# print(C)
total_b = 0
# for m in C:
# for n in m:
# if(n == '#'):
# total_b += 1
for m in C:
total_b += m.count('#')
# print(total_b)
diff = total_b - K
row_cnt = []
for i in range(H):
if(C[i].count('#') <= diff):
# row_cnt[i] = C[i].count('#')
row_cnt.append(i)
col_cnt = []
for i in range(W):
cnt_temp = 0
for n in C:
if n[i] == '#':
cnt_temp += 1
if(cnt_temp <= diff):
# col_cnt[i] = cnt_temp
col_cnt.append(i)
# print(row_cnt)
# print(col_cnt)
row_list = []
col_list = []
for i in range(len(row_cnt) + 1):
row_list.append(list(itertools.combinations(row_cnt, i)))
for i in range(len(col_cnt) + 1):
col_list.append(list(itertools.combinations(col_cnt, i)))
temp = copy.deepcopy(C)
C.clear()
for x in temp:
C.append(list(x))
arr=np.array(C,dtype=str)
ans_cnt = 0
if diff == 0:
ans_cnt = 1
for row in row_list:
for col in col_list:
temp_arr = copy.deepcopy(arr)
# print(temp_arr)
y = np.delete(temp_arr, row, axis = 0)
y = np.delete(y, col, axis = 1)
cnt = np.count_nonzero((y == '#'))
if cnt == K:
ans_cnt += 1
print(ans_cnt)
``` | instruction | 0 | 19,134 | 7 | 38,268 |
No | output | 1 | 19,134 | 7 | 38,269 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a grid of H rows and W columns of squares. The color of the square at the i-th row from the top and the j-th column from the left (1 \leq i \leq H, 1 \leq j \leq W) is given to you as a character c_{i,j}: the square is white if c_{i,j} is `.`, and black if c_{i,j} is `#`.
Consider doing the following operation:
* Choose some number of rows (possibly zero), and some number of columns (possibly zero). Then, paint red all squares in the chosen rows and all squares in the chosen columns.
You are given a positive integer K. How many choices of rows and columns result in exactly K black squares remaining after the operation? Here, we consider two choices different when there is a row or column chosen in only one of those choices.
Constraints
* 1 \leq H, W \leq 6
* 1 \leq K \leq HW
* c_{i,j} is `.` or `#`.
Input
Input is given from Standard Input in the following format:
H W K
c_{1,1}c_{1,2}...c_{1,W}
c_{2,1}c_{2,2}...c_{2,W}
:
c_{H,1}c_{H,2}...c_{H,W}
Output
Print an integer representing the number of choices of rows and columns satisfying the condition.
Examples
Input
2 3 2
..#
###
Output
5
Input
2 3 2
..#
Output
5
Input
2 3 4
..#
Output
1
Input
2 2 3
Output
0
Input
6 6 8
..##..
.#..#.
....#
....#
....#
Output
208
Submitted Solution:
```
umber=list(map(int,input().split()))
H,W,K=number[0],number[1],number[2]
answer=0
nums=[]
for i in range(H):
tmp=list(str(input()))
nums.append(tmp)
count=0
for cowmask in range(1<<H):
for rowmask in range(1<<W):
count=0
for x in range(H):
for y in range(W):
if (cowmask>>x)&1==0 and (rowmask>>y)&1==0 and nums[x][y]=="#":
count+=1
if count==K:
answer+=1
print(answer)
``` | instruction | 0 | 19,135 | 7 | 38,270 |
No | output | 1 | 19,135 | 7 | 38,271 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a grid of H rows and W columns of squares. The color of the square at the i-th row from the top and the j-th column from the left (1 \leq i \leq H, 1 \leq j \leq W) is given to you as a character c_{i,j}: the square is white if c_{i,j} is `.`, and black if c_{i,j} is `#`.
Consider doing the following operation:
* Choose some number of rows (possibly zero), and some number of columns (possibly zero). Then, paint red all squares in the chosen rows and all squares in the chosen columns.
You are given a positive integer K. How many choices of rows and columns result in exactly K black squares remaining after the operation? Here, we consider two choices different when there is a row or column chosen in only one of those choices.
Constraints
* 1 \leq H, W \leq 6
* 1 \leq K \leq HW
* c_{i,j} is `.` or `#`.
Input
Input is given from Standard Input in the following format:
H W K
c_{1,1}c_{1,2}...c_{1,W}
c_{2,1}c_{2,2}...c_{2,W}
:
c_{H,1}c_{H,2}...c_{H,W}
Output
Print an integer representing the number of choices of rows and columns satisfying the condition.
Examples
Input
2 3 2
..#
###
Output
5
Input
2 3 2
..#
Output
5
Input
2 3 4
..#
Output
1
Input
2 2 3
Output
0
Input
6 6 8
..##..
.#..#.
....#
....#
....#
Output
208
Submitted Solution:
```
import sys
def count(bd, H, W, K):
cnt = 0
for i in range(H):
for j in range(W):
if bd[i][j] == '#':
cnt += 1
return cnt
def main():
H, W, K = (int(x) for x in input().split())
b = [input().split() for i in range(H)]
count = 0
bd = b
if K == count(bd, H, W, K):
count += 1
for j in range(W):
bd = b
for i in range(H):
bd[i][j] = '.'
if K == count(bd, H, W, K):
count += 1
for i in range(H):
bd = b
for j in range(W):
bd[i][j] = '.'
if K == count(bd, H, W, K):
count += 1
for j in range(W):
bd = b
for i in range(H):
bd[i][j] = '.'
for jm in range(W):
bd[i][jm] = '.'
if K == count(bd, H, W, K):
count += 1
print(count)
if __name__ == "__main__":
main()
``` | instruction | 0 | 19,136 | 7 | 38,272 |
No | output | 1 | 19,136 | 7 | 38,273 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have a grid of H rows and W columns of squares. The color of the square at the i-th row from the top and the j-th column from the left (1 \leq i \leq H, 1 \leq j \leq W) is given to you as a character c_{i,j}: the square is white if c_{i,j} is `.`, and black if c_{i,j} is `#`.
Consider doing the following operation:
* Choose some number of rows (possibly zero), and some number of columns (possibly zero). Then, paint red all squares in the chosen rows and all squares in the chosen columns.
You are given a positive integer K. How many choices of rows and columns result in exactly K black squares remaining after the operation? Here, we consider two choices different when there is a row or column chosen in only one of those choices.
Constraints
* 1 \leq H, W \leq 6
* 1 \leq K \leq HW
* c_{i,j} is `.` or `#`.
Input
Input is given from Standard Input in the following format:
H W K
c_{1,1}c_{1,2}...c_{1,W}
c_{2,1}c_{2,2}...c_{2,W}
:
c_{H,1}c_{H,2}...c_{H,W}
Output
Print an integer representing the number of choices of rows and columns satisfying the condition.
Examples
Input
2 3 2
..#
###
Output
5
Input
2 3 2
..#
Output
5
Input
2 3 4
..#
Output
1
Input
2 2 3
Output
0
Input
6 6 8
..##..
.#..#.
....#
....#
....#
Output
208
Submitted Solution:
```
h,w,k = map(int,input().split())
c = []
for _ in range(h):
cc = input()
c.append(cc)
d = 0
kk = 0
g= []
for i in range(2 ** h):
gg=[]
for j in range(h):
if ((i>>j) &1):
gg.append(j)
g.append(gg)
import copy
gb = copy.deepcopy(g)
for lis in g:
ii = lis
for lisj in gb:
jj = lisj
for i in range(h):
if i in ii:
continue
for j in range(w):
if j in jj:
continue
if c[i][j]=='#':
kk+=1
if kk==k:
d+=1
kk = 0
print(d)
``` | instruction | 0 | 19,137 | 7 | 38,274 |
No | output | 1 | 19,137 | 7 | 38,275 |
Provide a correct Python 3 solution for this coding contest problem.
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles.
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.
There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.
* '.' - a black tile
* '#' - a red tile
* '@' - a man on a black tile(appears exactly once in a data set)
The end of the input is indicated by a line consisting of two zeros.
Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
Examples
Input
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
Output
45
59
6
13
Input
6 9
....#.
.....#
......
......
......
......
......
@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
.###
...@...
.###
..#.#..
..#.#..
0 0
Output
45
59
6
13 | instruction | 0 | 20,208 | 7 | 40,416 |
"Correct Solution:
```
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def dfs(s,Map,H,W):
dxdy = [(1,0),(-1,0),(0,1),(0,-1)]
for i in range(4):
Map[s[0]][s[1]] = '*'
nextPos = (s[0]+dxdy[i][0],s[1]+dxdy[i][1])
if nextPos[0] >= 0 and nextPos[1] >= 0 and nextPos[0] < H and nextPos[1] < W:
if Map[nextPos[0]][nextPos[1]] == '.':
dfs(nextPos,Map,H,W)
while True:
W,H = map(int,input().split(" "))
if W == 0 and H == 0:
break
Map = [list(input()) for i in range(0,H)]
for i in range(H):
for j in range(W):
if Map[i][j] == '@':
S = (i,j)
dfs(S,Map,H,W)
print(sum([1 if Map[i][j] == '*' else 0 for i in range(H) for j in range(W)]))
``` | output | 1 | 20,208 | 7 | 40,417 |
Provide a correct Python 3 solution for this coding contest problem.
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles.
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.
There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.
* '.' - a black tile
* '#' - a red tile
* '@' - a man on a black tile(appears exactly once in a data set)
The end of the input is indicated by a line consisting of two zeros.
Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
Examples
Input
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
Output
45
59
6
13
Input
6 9
....#.
.....#
......
......
......
......
......
@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
.###
...@...
.###
..#.#..
..#.#..
0 0
Output
45
59
6
13 | instruction | 0 | 20,209 | 7 | 40,418 |
"Correct Solution:
```
def f(h,w):
global W,H,S,A
if h<0 or h>=H or w<0 or w>=W or S[h][w]=="#" or A[h][w]==1:
return
A[h][w] = 1
f(h-1,w)
f(h+1,w)
f(h,w-1)
f(h,w+1)
while True:
W,H = map(int,input().split())
if W+H == 0:
break
S = [list(input()) for h in range(H)]
A = [[0 for w in range(W)] for h in range(H)]
for h in range(H):
for w in range(W):
if S[h][w] == "@":
f(h,w)
break
ans = 0
for i in A:
ans += sum(i)
print(ans)
``` | output | 1 | 20,209 | 7 | 40,419 |
Provide a correct Python 3 solution for this coding contest problem.
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles.
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.
There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.
* '.' - a black tile
* '#' - a red tile
* '@' - a man on a black tile(appears exactly once in a data set)
The end of the input is indicated by a line consisting of two zeros.
Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
Examples
Input
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
Output
45
59
6
13
Input
6 9
....#.
.....#
......
......
......
......
......
@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
.###
...@...
.###
..#.#..
..#.#..
0 0
Output
45
59
6
13 | instruction | 0 | 20,210 | 7 | 40,420 |
"Correct Solution:
```
import queue
dxy=[(1,0),(0,1),(-1,0),(0,-1)]
def bfs(sx,sy,H,W):#幅優先探索
q=queue.Queue()
visited=[[False]*W for _ in range(H)]
q.put((sx,sy))
visited[sx][sy]=True
ans=1
while not q.empty():
px,py=q.get()
for dx,dy in dxy:
nx=px+dx
ny=py+dy
if 0<=nx<H and 0<=ny<W:
if c[nx][ny]=='.' and visited[nx][ny]==False:
visited[nx][ny]=True
q.put((nx,ny))
ans+=1
return ans
while True:
W,H=map(int,input().split())
if(W==0 and H==0):
break
c=[input() for _ in range(H)]
for i in range(H):
if "@" in c[i]:
sx=i
sy=c[i].index("@")
print(bfs(sx,sy,H,W))
``` | output | 1 | 20,210 | 7 | 40,421 |
Provide a correct Python 3 solution for this coding contest problem.
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles.
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.
There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.
* '.' - a black tile
* '#' - a red tile
* '@' - a man on a black tile(appears exactly once in a data set)
The end of the input is indicated by a line consisting of two zeros.
Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
Examples
Input
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
Output
45
59
6
13
Input
6 9
....#.
.....#
......
......
......
......
......
@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
.###
...@...
.###
..#.#..
..#.#..
0 0
Output
45
59
6
13 | instruction | 0 | 20,211 | 7 | 40,422 |
"Correct Solution:
```
while True:
W, H = map(int, input().split())
if W == 0 and H == 0:
break
maze = []
for _ in range(H):
maze.append(list(input()))
visited = [[False] * W for _ in range(H)]
dxy = [(1, 0), (0, 1), (-1, 0), (0, -1)]
ans = 0
def rec(x, y):
global ans
visited[x][y] = True
ans += 1
for (dx, dy) in dxy: # 4方向の移動を試す
x2 = x + dx
y2 = y + dy
if 0 <= x2 < H and 0 <= y2 < W:
if maze[x2][y2] == '.':
if not visited[x2][y2]:
rec(x2, y2) # 再帰呼び出し
for h in range(H):
for w in range(W):
if maze[h][w] == '@':
rec(h, w)
break
else:
continue
break
print(ans)
``` | output | 1 | 20,211 | 7 | 40,423 |
Provide a correct Python 3 solution for this coding contest problem.
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles.
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.
There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.
* '.' - a black tile
* '#' - a red tile
* '@' - a man on a black tile(appears exactly once in a data set)
The end of the input is indicated by a line consisting of two zeros.
Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
Examples
Input
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
Output
45
59
6
13
Input
6 9
....#.
.....#
......
......
......
......
......
@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
.###
...@...
.###
..#.#..
..#.#..
0 0
Output
45
59
6
13 | instruction | 0 | 20,212 | 7 | 40,424 |
"Correct Solution:
```
def count_area(h,w):
count = 0
if (h>0) and (s[h-1][w]== "."):
s[h-1][w] = "#"
count += count_area(h-1,w)
if (h<H-1) and (s[h+1][w]== "."):
s[h+1][w] = "#"
count += count_area(h+1,w)
if (w>0) and (s[h][w-1]== "."):
s[h][w-1] = "#"
count += count_area(h,w-1)
if (w<W-1) and (s[h][w+1]== "."):
s[h][w+1] = "#"
count += count_area(h,w+1)
return count+1
while(True):
W, H = map(int, input().split())
if H==0&W==0:break
s = [list(input()) for i in range(H)]
for h in range(H):
for w in range(W):
if s[h][w] == "@":
s[h][w] = "#"
print(count_area(h,w))
break
``` | output | 1 | 20,212 | 7 | 40,425 |
Provide a correct Python 3 solution for this coding contest problem.
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles.
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.
There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.
* '.' - a black tile
* '#' - a red tile
* '@' - a man on a black tile(appears exactly once in a data set)
The end of the input is indicated by a line consisting of two zeros.
Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
Examples
Input
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
Output
45
59
6
13
Input
6 9
....#.
.....#
......
......
......
......
......
@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
.###
...@...
.###
..#.#..
..#.#..
0 0
Output
45
59
6
13 | instruction | 0 | 20,213 | 7 | 40,426 |
"Correct Solution:
```
I=input
def f(x,y,r):
if not(0<=x<len(r[0])and(0<=y<len(r)))or r[y][x]=='#':return 0
r[y][x]='#'
return 1+sum(f(x+dx,y+dy,r)for dx,dy in[[-1,0],[1,0],[0,-1],[0,1]])
while 1:
w,h=map(int,I().split())
if w==0:break
r=[list(input()) for _ in[0]*h]
for a,b in enumerate(r):
if '@'in b:y=a;x=b.index('@');break
print(f(x,y,r))
``` | output | 1 | 20,213 | 7 | 40,427 |
Provide a correct Python 3 solution for this coding contest problem.
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles.
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.
There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.
* '.' - a black tile
* '#' - a red tile
* '@' - a man on a black tile(appears exactly once in a data set)
The end of the input is indicated by a line consisting of two zeros.
Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
Examples
Input
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
Output
45
59
6
13
Input
6 9
....#.
.....#
......
......
......
......
......
@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
.###
...@...
.###
..#.#..
..#.#..
0 0
Output
45
59
6
13 | instruction | 0 | 20,214 | 7 | 40,428 |
"Correct Solution:
```
from queue import Queue
def calcReachableTileNumByBFSearch(room, start_pos):
q = Queue()
q.put(start_pos)
count = 1
while not q.empty():
cur_pos = q.get()
D0 = [1, 0, -1, 0]
D1 = [0, -1, 0, 1]
for d in range(4):
nc = [0,0]
nc[0] = cur_pos[0]+D0[d]
nc[1] = cur_pos[1]+D1[d]
if room[nc[0]][nc[1]] == '.':
room[nc[0]][nc[1]] = '*'
q.put([nc[0],nc[1]])
count +=1
return count
def solve(width, height):
room = [['#' for i in range(width+2)] for j in range(height+2)]
for i in range(height):
row = input()
for j in range(width):
room[i+1][j+1] = row[j]
if room[i+1][j+1] == '@':
start = [i+1,j+1]
return calcReachableTileNumByBFSearch(room, start)
def main():
while True:
width, height = map(int, input().split())
if width == 0 and height == 0:
break
print(solve(width, height))
if __name__ == "__main__":
main()
``` | output | 1 | 20,214 | 7 | 40,429 |
Provide a correct Python 3 solution for this coding contest problem.
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles.
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.
There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.
* '.' - a black tile
* '#' - a red tile
* '@' - a man on a black tile(appears exactly once in a data set)
The end of the input is indicated by a line consisting of two zeros.
Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
Examples
Input
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
Output
45
59
6
13
Input
6 9
....#.
.....#
......
......
......
......
......
@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
.###
...@...
.###
..#.#..
..#.#..
0 0
Output
45
59
6
13 | instruction | 0 | 20,215 | 7 | 40,430 |
"Correct Solution:
```
(lambda f:f(f))(lambda T,A=lambda L,yp,ri=lambda l:R.append([{xi:x}for xi,x in enumerate(l)]):[P.__setitem__("x",L.index("@")),P.__setitem__("y",yp),ri(L)]if "@"in L else ri(L),S=lambda X,Y,s,u:[R[Y][X].__setitem__(X,"@"),s(X-1,Y,s,u)+s(X+1,Y,s,u)+s(X,Y+1,s,u)+s(X,Y-1,s,u)+1][1]if 0<=X<W[0]and 0<=Y<W[1]and u(X,Y)else 0,u=lambda X,Y:R[Y][X].get(X)is ".",g=globals:[g().__setitem__("W",list(map(int,input().split()))),g().__setitem__("R",[]),g().__setitem__("P",{"x":9,"y":9}),[A(input(),Y)for Y in range(W[1])],[R[P["y"]][P["x"]].__setitem__(P["x"],"."),print(S(P["x"],P["y"],S,u)),T(T)]if R else 9])
``` | output | 1 | 20,215 | 7 | 40,431 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles.
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.
There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.
* '.' - a black tile
* '#' - a red tile
* '@' - a man on a black tile(appears exactly once in a data set)
The end of the input is indicated by a line consisting of two zeros.
Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
Examples
Input
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
Output
45
59
6
13
Input
6 9
....#.
.....#
......
......
......
......
......
@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
.###
...@...
.###
..#.#..
..#.#..
0 0
Output
45
59
6
13
Submitted Solution:
```
from collections import deque
def bfs():
d = [[False] * w for i in range(h)]
dx = [1,0,-1,0]
dy = [0,1,0,-1]
que = deque([])
que.append((sx,sy))
d[sx][sy] = True
cnt = 1
while que:
p = que.popleft()
for i in range(4):
nx = p[0] + dx[i]
ny = p[1] + dy[i]
if 0 <= nx < h and 0 <= ny < w and Map[nx][ny] != "#" and d[nx][ny] == False:
cnt += 1
que.append((nx,ny))
d[nx][ny] = True
return cnt
while 1:
w,h = map(int,input().split())
if w == 0 and h == 0:
break
Map = [str(input()) for i in range(h)]
for i in range(h):
for j in range(w):
if Map[i][j] == "@":
sx = i
sy = j
print(bfs())
``` | instruction | 0 | 20,216 | 7 | 40,432 |
Yes | output | 1 | 20,216 | 7 | 40,433 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles.
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.
There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.
* '.' - a black tile
* '#' - a red tile
* '@' - a man on a black tile(appears exactly once in a data set)
The end of the input is indicated by a line consisting of two zeros.
Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
Examples
Input
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
Output
45
59
6
13
Input
6 9
....#.
.....#
......
......
......
......
......
@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
.###
...@...
.###
..#.#..
..#.#..
0 0
Output
45
59
6
13
Submitted Solution:
```
def main():
while 1:
w, h = map(int, input().split())
if w == 0: break
fld = [[] for i in range(h)]
for i in range(h):
readline = input()
for j in range(w):
item = readline[j]
fld[i].append(item)
if item == '@': sy, sx = i, j
solve(fld, sy, sx)
count = 0
for i in range(h):
for j in range(w):
if fld[i][j] == 'A': count += 1
print(count)
def solve(fld, cy, cx):
vy = [1,-1,0,0]
vx = [0,0,1,-1]
fld[cy][cx] = 'A'
for i in range(4):
ny = cy + vy[i]
nx = cx + vx[i]
if ny < 0 or nx < 0 or ny >= len(fld) or nx >= len(fld[0]):continue
elif fld[ny][nx] == '.': solve(fld, ny, nx)
main()
``` | instruction | 0 | 20,217 | 7 | 40,434 |
Yes | output | 1 | 20,217 | 7 | 40,435 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles.
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.
There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.
* '.' - a black tile
* '#' - a red tile
* '@' - a man on a black tile(appears exactly once in a data set)
The end of the input is indicated by a line consisting of two zeros.
Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
Examples
Input
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
Output
45
59
6
13
Input
6 9
....#.
.....#
......
......
......
......
......
@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
.###
...@...
.###
..#.#..
..#.#..
0 0
Output
45
59
6
13
Submitted Solution:
```
def dfs(now,origin,drawn): # 深さ優先探索 再帰関数 originは変更しない drawnは訪れた場所に1それ以外は0
positive = '.' # 進める場所
negative = '#' # 進めない場所(壁)
direct = [0,0,0,0] # [上,下,左,右]を表す
height = len(origin)
width = len(origin[0])
now_h = now[0]
now_w = now[1]
### それぞれの方向について、originにおいてpositiveかつdrawnにおいて0(つまり訪れたことがある)なら進める
if now_h != 0 and origin[now_h-1][now_w] == positive and drawn[now_h-1][now_w] == 0:
direct[0] = 1
if now_h != height-1 and origin[now_h+1][now_w] == positive and drawn[now_h+1][now_w] == 0:
direct[1] = 1
if now_w != 0 and origin[now_h][now_w-1] == positive and drawn[now_h][now_w-1] == 0:
direct[2] = 1
if now_w != width-1 and origin[now_h][now_w+1] == positive and drawn[now_h][now_w+1] == 0:
direct[3] = 1
if sum(direct) == 0: # 進む先がなければ終了
return 0
d = {0:[now_h-1,now_w],1:[now_h+1,now_w],2:[now_h,now_w-1],3:[now_h,now_w+1]} # 進む先の座標を辞書にしておく
for i in range(4):
if direct[i] == 1:
drawn[d[i][0]][d[i][1]] = 1 # d[i]の座標に移動したのでdrawnで訪れたことを記録した
dfs(d[i],origin,drawn) # 再帰
return 0
while True:
W, H = map(int,input().split()) # 入力を受け取る
if W == 0 and H == 0: # 両方0だったらbreak
break
tiles = [list(input()) for i in range(H)] # 入力
possible_tiles = [[0 for i in range(W)] for j in range(H)] # 訪れたことがある場所を記憶する
for h in range(H):
for w in range(W):
if tiles[h][w] == '@': # 現在地
now = [h,w]
break
possible_tiles[now[0]][now[1]] = 1 # 現在地は既に訪れている
dfs(now,tiles,possible_tiles) # 探索
ans = 0
for h in range(H):
for w in range(W):
if possible_tiles[h][w] == 1:
ans += 1
print(ans)
``` | instruction | 0 | 20,218 | 7 | 40,436 |
Yes | output | 1 | 20,218 | 7 | 40,437 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles.
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.
There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.
* '.' - a black tile
* '#' - a red tile
* '@' - a man on a black tile(appears exactly once in a data set)
The end of the input is indicated by a line consisting of two zeros.
Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
Examples
Input
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
Output
45
59
6
13
Input
6 9
....#.
.....#
......
......
......
......
......
@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
.###
...@...
.###
..#.#..
..#.#..
0 0
Output
45
59
6
13
Submitted Solution:
```
import copy
w, h = map(int,input().split())
while [h, w] != [0, 0]:
area = []
a = 1
for i in range(h):
area.append(list(input()))
area[i].insert(0,"#")
area[i].append("#")
area.insert(0,["#" for i in range(w + 2)])
area.append(["#" for i in range(w + 2)])
que = []
for i in range(1, h + 1):
for j in range(1, w + 1):
if area[i][j] == "@":
que.append([i, j])
break
else:
continue
break
while True:
nque = []
for i in que:
h1 = i[0]
w1 = i[1]
if area[h1 - 1][w1] == ".":
area[h1 - 1][w1] = "*"
nque.append([h1 - 1, w1])
a += 1
if area[h1 + 1][w1] == ".":
area[h1 + 1][w1] = "*"
nque.append([h1 + 1, w1])
a += 1
if area[h1][w1 - 1] == ".":
area[h1][w1 - 1] = "*"
nque.append([h1, w1 - 1])
a += 1
if area[h1][w1 + 1] == ".":
area[h1][w1 + 1] = "*"
nque.append([h1, w1 + 1])
a += 1
if nque == []:
break
que = copy.deepcopy(nque)
print(a)
w, h = map(int,input().split())
``` | instruction | 0 | 20,219 | 7 | 40,438 |
Yes | output | 1 | 20,219 | 7 | 40,439 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles.
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.
There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.
* '.' - a black tile
* '#' - a red tile
* '@' - a man on a black tile(appears exactly once in a data set)
The end of the input is indicated by a line consisting of two zeros.
Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
Examples
Input
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
Output
45
59
6
13
Input
6 9
....#.
.....#
......
......
......
......
......
@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
.###
...@...
.###
..#.#..
..#.#..
0 0
Output
45
59
6
13
Submitted Solution:
```
data, res = [], 0
def valid(x, y):
global data
return 0 <= x < W and 0 <= y < H and data[y][x] != '#'
def dfs(x, y):
global data, res
if valid(x, y):
res += 1
data[y][x] = '#'
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
for i in range(4):
dfs(x + dx[i], y + dy[i])
else:
return
while True:
global data, res
W, H = map(int, input().split())
if not W:
break
data = [list(input()) for _ in range(H)]
res = 0
for i in range(W):
for j in range(H):
if data[j][i] == '@':
dfs(i, j)
break
print(res)
``` | instruction | 0 | 20,220 | 7 | 40,440 |
No | output | 1 | 20,220 | 7 | 40,441 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles.
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.
There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.
* '.' - a black tile
* '#' - a red tile
* '@' - a man on a black tile(appears exactly once in a data set)
The end of the input is indicated by a line consisting of two zeros.
Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
Examples
Input
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
Output
45
59
6
13
Input
6 9
....#.
.....#
......
......
......
......
......
@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
.###
...@...
.###
..#.#..
..#.#..
0 0
Output
45
59
6
13
Submitted Solution:
```
while True:
W,H = [int(x) for x in input().split()]
if W==0 and H==0:
exit()
tiles = []
X = 0
Y = 0
for i in range(H):
tiles.append(input())
if '@' in tiles[i]:
Y = i
X = tiles[i].index('@')
d = [[9999 for j in range(H*W)] for i in range(H*W)]
for i in range(H*W):
d[i][i] = 0
for i in range(H):
for j in range(W):
if j>0:
if tiles[i][j] in '.@' and tiles[i][j-1] in '.@':
d[W*i+j][W*i+j-1] = 1
if j<W-1:
if tiles[i][j] in '.@' and tiles[i][j+1] in '.@':
d[W*i+j][W*i+j+1] = 1
if i>0:
if tiles[i][j] in '.@' and tiles[i-1][j] in '.@':
d[W*i+j][W*i+j-W] = 1
if i<H-1:
if tiles[i][j] in '.@' and tiles[i+1][j] in '.@':
d[W*i+j][W*i+j+W] = 1
for k in range(H*W):
for i in range(H*W):
for j in range(H*W):
if d[i][j] > d[i][k]+d[k][j]:
d[i][j] = d[i][k]+d[k][j]
cnt = 0
for i in range(H*W):
if d[X+Y*W][i]<9999:
cnt += 1
print(cnt)
``` | instruction | 0 | 20,221 | 7 | 40,442 |
No | output | 1 | 20,221 | 7 | 40,443 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles.
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.
There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.
* '.' - a black tile
* '#' - a red tile
* '@' - a man on a black tile(appears exactly once in a data set)
The end of the input is indicated by a line consisting of two zeros.
Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
Examples
Input
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
Output
45
59
6
13
Input
6 9
....#.
.....#
......
......
......
......
......
@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
.###
...@...
.###
..#.#..
..#.#..
0 0
Output
45
59
6
13
Submitted Solution:
```
while 1:
x,y = list(map(int,input()split()))
if x == 0:
break
pp = [1,-1,0,0]
qq = [0,0,1,-1]
m = []
for i in range(y):
m.append(list(input()))
if '@' in m[-1]:
ss = m[-1].index('@')
tt = i
cnt = 0
def move (cx,cy):
global cnt
if cy >= y or cy < 0 or cx >= x or cx < 0:
return 0
if m[cy][cx] = '.'v or m[cy][cx] = '@'
cnt += 1
m[cy][cx] = '0'
for i in range(4):
move(cx + pp[i],cy + qq[i])
move(ss,tt)
print(cnt)
``` | instruction | 0 | 20,222 | 7 | 40,444 |
No | output | 1 | 20,222 | 7 | 40,445 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles.
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.
There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.
* '.' - a black tile
* '#' - a red tile
* '@' - a man on a black tile(appears exactly once in a data set)
The end of the input is indicated by a line consisting of two zeros.
Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
Examples
Input
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
Output
45
59
6
13
Input
6 9
....#.
.....#
......
......
......
......
......
@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
.###
...@...
.###
..#.#..
..#.#..
0 0
Output
45
59
6
13
Submitted Solution:
```
import queue
def solve(w, h):
q = queue.Queue()
m = [['#' for i in range(w+2)] for j in range(h+2)]
for i in range(h):
inp = input()
for j in range(w):
m[i+1][j+1] = inp[j]
if m[i+1][j+1] == '@':
start = [i+1,j+1]
q.put(start)
c = 1
while not q.empty():
cur = q.get()
XY = [(1,0), (-1, 0), (0, 1), (0, -1)]
for x, y in XY:
cx = cur[0]+x
cy = cur[1]+y
if m[cx][cy] == '.':
m[cx][cy] = '#'
q.put([cx, cy])
c+=1
return c
while True:
w, h = [int(i) for i in input().split()]
if w == 0 and h == 0:
break
print solve(w,h)
``` | instruction | 0 | 20,223 | 7 | 40,446 |
No | output | 1 | 20,223 | 7 | 40,447 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Paul is at the orchestra. The string section is arranged in an r × c rectangular grid and is filled with violinists with the exception of n violists. Paul really likes violas, so he would like to take a picture including at least k of them. Paul can take a picture of any axis-parallel rectangle in the orchestra. Count the number of possible pictures that Paul can take.
Two pictures are considered to be different if the coordinates of corresponding rectangles are different.
Input
The first line of input contains four space-separated integers r, c, n, k (1 ≤ r, c, n ≤ 10, 1 ≤ k ≤ n) — the number of rows and columns of the string section, the total number of violas, and the minimum number of violas Paul would like in his photograph, respectively.
The next n lines each contain two integers xi and yi (1 ≤ xi ≤ r, 1 ≤ yi ≤ c): the position of the i-th viola. It is guaranteed that no location appears more than once in the input.
Output
Print a single integer — the number of photographs Paul can take which include at least k violas.
Examples
Input
2 2 1 1
1 2
Output
4
Input
3 2 3 3
1 1
3 1
2 2
Output
1
Input
3 2 3 2
1 1
3 1
2 2
Output
4
Note
We will use '*' to denote violinists and '#' to denote violists.
In the first sample, the orchestra looks as follows
*#
**
Paul can take a photograph of just the viola, the 1 × 2 column containing the viola, the 2 × 1 row containing the viola, or the entire string section, for 4 pictures total.
In the second sample, the orchestra looks as follows
#*
*#
#*
Paul must take a photograph of the entire section.
In the third sample, the orchestra looks the same as in the second sample. | instruction | 0 | 20,737 | 7 | 41,474 |
Tags: brute force, implementation
Correct Solution:
```
r, c, n, k = list(map(int, input().split()))
alt = []
count = 0
for i in range(n):
a, b = list(map(int, input().split()))
alt.append([a - 1, b - 1])
for x1 in range(r):
for x2 in range(x1 + 1):
for y1 in range(c):
for y2 in range(y1 + 1):
d = 0
for i in range(n):
if (alt[i][0] <= x1 and alt[i][0] >= x2 and alt[i][1] <= y1 and alt[i][1] >= y2):
d += 1
if d >= k:
count += 1
print(count)
``` | output | 1 | 20,737 | 7 | 41,475 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Paul is at the orchestra. The string section is arranged in an r × c rectangular grid and is filled with violinists with the exception of n violists. Paul really likes violas, so he would like to take a picture including at least k of them. Paul can take a picture of any axis-parallel rectangle in the orchestra. Count the number of possible pictures that Paul can take.
Two pictures are considered to be different if the coordinates of corresponding rectangles are different.
Input
The first line of input contains four space-separated integers r, c, n, k (1 ≤ r, c, n ≤ 10, 1 ≤ k ≤ n) — the number of rows and columns of the string section, the total number of violas, and the minimum number of violas Paul would like in his photograph, respectively.
The next n lines each contain two integers xi and yi (1 ≤ xi ≤ r, 1 ≤ yi ≤ c): the position of the i-th viola. It is guaranteed that no location appears more than once in the input.
Output
Print a single integer — the number of photographs Paul can take which include at least k violas.
Examples
Input
2 2 1 1
1 2
Output
4
Input
3 2 3 3
1 1
3 1
2 2
Output
1
Input
3 2 3 2
1 1
3 1
2 2
Output
4
Note
We will use '*' to denote violinists and '#' to denote violists.
In the first sample, the orchestra looks as follows
*#
**
Paul can take a photograph of just the viola, the 1 × 2 column containing the viola, the 2 × 1 row containing the viola, or the entire string section, for 4 pictures total.
In the second sample, the orchestra looks as follows
#*
*#
#*
Paul must take a photograph of the entire section.
In the third sample, the orchestra looks the same as in the second sample. | instruction | 0 | 20,738 | 7 | 41,476 |
Tags: brute force, implementation
Correct Solution:
```
r, c, n, k = map(int, input().split())
A = [[0] * c for i in range(r)]
for i in range(n):
a, b = map(int, input().split())
A[a - 1][b - 1] = 1
f = 0
g = 0
ans = 0
for i in range(r):
for j in range(c):
for i2 in range(r - i):
for j2 in range(c - j):
cnt = 0
for i3 in range(i, i + i2 + 1):
for j3 in range(j, j + j2 + 1):
#print(i3, j3)
cnt += int(A[i3][j3] == 1)
if cnt >= k:
ans += 1
print(ans)
``` | output | 1 | 20,738 | 7 | 41,477 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Paul is at the orchestra. The string section is arranged in an r × c rectangular grid and is filled with violinists with the exception of n violists. Paul really likes violas, so he would like to take a picture including at least k of them. Paul can take a picture of any axis-parallel rectangle in the orchestra. Count the number of possible pictures that Paul can take.
Two pictures are considered to be different if the coordinates of corresponding rectangles are different.
Input
The first line of input contains four space-separated integers r, c, n, k (1 ≤ r, c, n ≤ 10, 1 ≤ k ≤ n) — the number of rows and columns of the string section, the total number of violas, and the minimum number of violas Paul would like in his photograph, respectively.
The next n lines each contain two integers xi and yi (1 ≤ xi ≤ r, 1 ≤ yi ≤ c): the position of the i-th viola. It is guaranteed that no location appears more than once in the input.
Output
Print a single integer — the number of photographs Paul can take which include at least k violas.
Examples
Input
2 2 1 1
1 2
Output
4
Input
3 2 3 3
1 1
3 1
2 2
Output
1
Input
3 2 3 2
1 1
3 1
2 2
Output
4
Note
We will use '*' to denote violinists and '#' to denote violists.
In the first sample, the orchestra looks as follows
*#
**
Paul can take a photograph of just the viola, the 1 × 2 column containing the viola, the 2 × 1 row containing the viola, or the entire string section, for 4 pictures total.
In the second sample, the orchestra looks as follows
#*
*#
#*
Paul must take a photograph of the entire section.
In the third sample, the orchestra looks the same as in the second sample. | instruction | 0 | 20,739 | 7 | 41,478 |
Tags: brute force, implementation
Correct Solution:
```
read = lambda: map(int, input().split())
r, c, n, k = read()
a = [[0] * (c + 1) for i in range(r + 1)]
for i in range(n):
x, y = read()
a[x][y] = 1
d = [[0] * (c + 1) for i in range(r + 1)]
for i in range(1, r + 1):
for j in range(1, c + 1):
if a[i][j]: d[i][j] += 1
if i == j == 0: continue
p1 = d[i - 1][j] if i else 0
p2 = d[i][j - 1] if j else 0
p3 = d[i - 1][j - 1] if i and j else 0
d[i][j] += p1 + p2 - p3
cnt = 0
for x1 in range(r + 1):
for y1 in range(c + 1):
for x2 in range(x1 + 1, r + 1):
for y2 in range(y1 + 1, c + 1):
cur = d[x2][y2] - d[x2][y1] - d[x1][y2] + d[x1][y1]
if cur >= k: cnt += 1
print(cnt)
``` | output | 1 | 20,739 | 7 | 41,479 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Paul is at the orchestra. The string section is arranged in an r × c rectangular grid and is filled with violinists with the exception of n violists. Paul really likes violas, so he would like to take a picture including at least k of them. Paul can take a picture of any axis-parallel rectangle in the orchestra. Count the number of possible pictures that Paul can take.
Two pictures are considered to be different if the coordinates of corresponding rectangles are different.
Input
The first line of input contains four space-separated integers r, c, n, k (1 ≤ r, c, n ≤ 10, 1 ≤ k ≤ n) — the number of rows and columns of the string section, the total number of violas, and the minimum number of violas Paul would like in his photograph, respectively.
The next n lines each contain two integers xi and yi (1 ≤ xi ≤ r, 1 ≤ yi ≤ c): the position of the i-th viola. It is guaranteed that no location appears more than once in the input.
Output
Print a single integer — the number of photographs Paul can take which include at least k violas.
Examples
Input
2 2 1 1
1 2
Output
4
Input
3 2 3 3
1 1
3 1
2 2
Output
1
Input
3 2 3 2
1 1
3 1
2 2
Output
4
Note
We will use '*' to denote violinists and '#' to denote violists.
In the first sample, the orchestra looks as follows
*#
**
Paul can take a photograph of just the viola, the 1 × 2 column containing the viola, the 2 × 1 row containing the viola, or the entire string section, for 4 pictures total.
In the second sample, the orchestra looks as follows
#*
*#
#*
Paul must take a photograph of the entire section.
In the third sample, the orchestra looks the same as in the second sample. | instruction | 0 | 20,740 | 7 | 41,480 |
Tags: brute force, implementation
Correct Solution:
```
r,c,n,k = map(int, input().split())
G = [[0]*c for i in range(0,r)]
for i in range(0, n):
a,b = map(int, input().split())
a-=1
b-=1
G[a][b] = True
ans = 0
for x in range(0,r):
for y in range(0,c):
for y1 in range(y + 1, c + 1):
for x1 in range(x + 1, r + 1):
num = 0;
for cl in range(y, y1):
for rw in range(x, x1):
if G[rw][cl]:
num += 1
if num >= k:
ans+=1
print(ans)
``` | output | 1 | 20,740 | 7 | 41,481 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Paul is at the orchestra. The string section is arranged in an r × c rectangular grid and is filled with violinists with the exception of n violists. Paul really likes violas, so he would like to take a picture including at least k of them. Paul can take a picture of any axis-parallel rectangle in the orchestra. Count the number of possible pictures that Paul can take.
Two pictures are considered to be different if the coordinates of corresponding rectangles are different.
Input
The first line of input contains four space-separated integers r, c, n, k (1 ≤ r, c, n ≤ 10, 1 ≤ k ≤ n) — the number of rows and columns of the string section, the total number of violas, and the minimum number of violas Paul would like in his photograph, respectively.
The next n lines each contain two integers xi and yi (1 ≤ xi ≤ r, 1 ≤ yi ≤ c): the position of the i-th viola. It is guaranteed that no location appears more than once in the input.
Output
Print a single integer — the number of photographs Paul can take which include at least k violas.
Examples
Input
2 2 1 1
1 2
Output
4
Input
3 2 3 3
1 1
3 1
2 2
Output
1
Input
3 2 3 2
1 1
3 1
2 2
Output
4
Note
We will use '*' to denote violinists and '#' to denote violists.
In the first sample, the orchestra looks as follows
*#
**
Paul can take a photograph of just the viola, the 1 × 2 column containing the viola, the 2 × 1 row containing the viola, or the entire string section, for 4 pictures total.
In the second sample, the orchestra looks as follows
#*
*#
#*
Paul must take a photograph of the entire section.
In the third sample, the orchestra looks the same as in the second sample. | instruction | 0 | 20,741 | 7 | 41,482 |
Tags: brute force, implementation
Correct Solution:
```
#!/usr/bin/env python3
r, c, n, k = [int(x) for x in input().split()]
min_x, min_y, max_x, max_y = r, c, 1, 1
alts = []
for i in range(0, n):
x, y = [int(i) for i in input().split()]
alts.append((x, y))
photo_count = 0
for x in range(1, r + 1):
for y in range(1, c + 1):
for w in range(1, r - (x - 1) + 1):
for h in range(1, c - (y - 1) + 1):
# print('x == {0}, y = {1}, w = {2}, l = {3}'.format(x, y, w, h))
alt_count = 0
for alt in alts:
if x <= alt[0] <= x + w - 1 and y <= alt[1] <= y + h - 1:
alt_count += 1
# print('yay!')
if alt_count >= k:
photo_count += 1
break
print(photo_count)
``` | output | 1 | 20,741 | 7 | 41,483 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Paul is at the orchestra. The string section is arranged in an r × c rectangular grid and is filled with violinists with the exception of n violists. Paul really likes violas, so he would like to take a picture including at least k of them. Paul can take a picture of any axis-parallel rectangle in the orchestra. Count the number of possible pictures that Paul can take.
Two pictures are considered to be different if the coordinates of corresponding rectangles are different.
Input
The first line of input contains four space-separated integers r, c, n, k (1 ≤ r, c, n ≤ 10, 1 ≤ k ≤ n) — the number of rows and columns of the string section, the total number of violas, and the minimum number of violas Paul would like in his photograph, respectively.
The next n lines each contain two integers xi and yi (1 ≤ xi ≤ r, 1 ≤ yi ≤ c): the position of the i-th viola. It is guaranteed that no location appears more than once in the input.
Output
Print a single integer — the number of photographs Paul can take which include at least k violas.
Examples
Input
2 2 1 1
1 2
Output
4
Input
3 2 3 3
1 1
3 1
2 2
Output
1
Input
3 2 3 2
1 1
3 1
2 2
Output
4
Note
We will use '*' to denote violinists and '#' to denote violists.
In the first sample, the orchestra looks as follows
*#
**
Paul can take a photograph of just the viola, the 1 × 2 column containing the viola, the 2 × 1 row containing the viola, or the entire string section, for 4 pictures total.
In the second sample, the orchestra looks as follows
#*
*#
#*
Paul must take a photograph of the entire section.
In the third sample, the orchestra looks the same as in the second sample. | instruction | 0 | 20,742 | 7 | 41,484 |
Tags: brute force, implementation
Correct Solution:
```
def check(x1, y1, x2, y2):
global dp
cnt = 0
for i in range(x2, x1 + 1):
for j in range(y2, y1 + 1):
cnt += dp[i][j]
return cnt
#n = int(input())
r, c, n, k = map(int, input().split())
#s = list(map(int, input().split()))
dp = [[0] * c for i in range(r)]
for i in range(n):
a, b = map(int, input().split())
dp[a - 1][b - 1] = 1
answer = 0
for x1 in range(r):
for y1 in range(c):
for x2 in range(x1 + 1):
for y2 in range(y1 + 1):
cnt = check(x1, y1, x2, y2)
#print(cnt, x1, y1, x2, y2)
if cnt >= k:
answer += 1
print(answer)
``` | output | 1 | 20,742 | 7 | 41,485 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Paul is at the orchestra. The string section is arranged in an r × c rectangular grid and is filled with violinists with the exception of n violists. Paul really likes violas, so he would like to take a picture including at least k of them. Paul can take a picture of any axis-parallel rectangle in the orchestra. Count the number of possible pictures that Paul can take.
Two pictures are considered to be different if the coordinates of corresponding rectangles are different.
Input
The first line of input contains four space-separated integers r, c, n, k (1 ≤ r, c, n ≤ 10, 1 ≤ k ≤ n) — the number of rows and columns of the string section, the total number of violas, and the minimum number of violas Paul would like in his photograph, respectively.
The next n lines each contain two integers xi and yi (1 ≤ xi ≤ r, 1 ≤ yi ≤ c): the position of the i-th viola. It is guaranteed that no location appears more than once in the input.
Output
Print a single integer — the number of photographs Paul can take which include at least k violas.
Examples
Input
2 2 1 1
1 2
Output
4
Input
3 2 3 3
1 1
3 1
2 2
Output
1
Input
3 2 3 2
1 1
3 1
2 2
Output
4
Note
We will use '*' to denote violinists and '#' to denote violists.
In the first sample, the orchestra looks as follows
*#
**
Paul can take a photograph of just the viola, the 1 × 2 column containing the viola, the 2 × 1 row containing the viola, or the entire string section, for 4 pictures total.
In the second sample, the orchestra looks as follows
#*
*#
#*
Paul must take a photograph of the entire section.
In the third sample, the orchestra looks the same as in the second sample. | instruction | 0 | 20,743 | 7 | 41,486 |
Tags: brute force, implementation
Correct Solution:
```
r, c, n, k = map(int, input().split())
g = set([tuple(map(int, input().split())) for _ in range(n)])
ret = 0
for i in range(r):
for j in range(c):
for l in range(1, r-i+1):
for w in range(1, c-j+1):
count = 0
for a in range(i, i+l):
for b in range(j, j+w):
if (a+1, b+1) in g:
count += 1
if count >= k:
ret += 1
print(ret)
``` | output | 1 | 20,743 | 7 | 41,487 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Paul is at the orchestra. The string section is arranged in an r × c rectangular grid and is filled with violinists with the exception of n violists. Paul really likes violas, so he would like to take a picture including at least k of them. Paul can take a picture of any axis-parallel rectangle in the orchestra. Count the number of possible pictures that Paul can take.
Two pictures are considered to be different if the coordinates of corresponding rectangles are different.
Input
The first line of input contains four space-separated integers r, c, n, k (1 ≤ r, c, n ≤ 10, 1 ≤ k ≤ n) — the number of rows and columns of the string section, the total number of violas, and the minimum number of violas Paul would like in his photograph, respectively.
The next n lines each contain two integers xi and yi (1 ≤ xi ≤ r, 1 ≤ yi ≤ c): the position of the i-th viola. It is guaranteed that no location appears more than once in the input.
Output
Print a single integer — the number of photographs Paul can take which include at least k violas.
Examples
Input
2 2 1 1
1 2
Output
4
Input
3 2 3 3
1 1
3 1
2 2
Output
1
Input
3 2 3 2
1 1
3 1
2 2
Output
4
Note
We will use '*' to denote violinists and '#' to denote violists.
In the first sample, the orchestra looks as follows
*#
**
Paul can take a photograph of just the viola, the 1 × 2 column containing the viola, the 2 × 1 row containing the viola, or the entire string section, for 4 pictures total.
In the second sample, the orchestra looks as follows
#*
*#
#*
Paul must take a photograph of the entire section.
In the third sample, the orchestra looks the same as in the second sample. | instruction | 0 | 20,744 | 7 | 41,488 |
Tags: brute force, implementation
Correct Solution:
```
import io
import sys
import time
import random
#~ start = time.clock()
#~ test = '''2 2 1 1
#~ 1 2'''
#~ test = '''3 2 3 3
#~ 1 1
#~ 3 1
#~ 2 2'''
#~ test = '''3 2 3 2
#~ 1 1
#~ 3 1
#~ 2 2'''
#~ sys.stdin = io.StringIO(test)
r,c,n,k = map(int,input().split()) # row column number-of-violists
data = [ [0 for i in range(c)] for j in range(r) ]
#~ print(r,c,n,k)
for i in range(n):
x,y = map(int,input().split()) # row column
data[x-1][y-1] = 1
nviolists = [ [0 for i in range(c)] for j in range(r) ]
for r1 in range(r):
count = 0
for c1 in range(c):
count += data[r1][c1]
nviolists[r1][c1] = (nviolists[r1-1][c1] if r1>0 else 0) \
+ count
#~ print(r1,c1,nviolists[r1][c1],nviolists[r1][c1-1])
#~ print(nviolists)
count = 0
for r1 in range(r):
for r2 in range(r1,r):
for c1 in range(c):
for c2 in range(c1,c):
# between (r1,c1) and (r2,c2)
vcount = nviolists[r2][c2] \
- (nviolists[r1-1][c2] if r1>0 else 0) \
- (nviolists[r2][c1-1] if c1>0 else 0) \
+ (nviolists[r1-1][c1-1] if r1*c1>0 else 0)
if vcount>=k:
#~ print( (r1,c1), (r2,c2) )
count +=1
print(count)
#~ dur = time.clock()-start
#~ print("Time:",dur)
``` | output | 1 | 20,744 | 7 | 41,489 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Paul is at the orchestra. The string section is arranged in an r × c rectangular grid and is filled with violinists with the exception of n violists. Paul really likes violas, so he would like to take a picture including at least k of them. Paul can take a picture of any axis-parallel rectangle in the orchestra. Count the number of possible pictures that Paul can take.
Two pictures are considered to be different if the coordinates of corresponding rectangles are different.
Input
The first line of input contains four space-separated integers r, c, n, k (1 ≤ r, c, n ≤ 10, 1 ≤ k ≤ n) — the number of rows and columns of the string section, the total number of violas, and the minimum number of violas Paul would like in his photograph, respectively.
The next n lines each contain two integers xi and yi (1 ≤ xi ≤ r, 1 ≤ yi ≤ c): the position of the i-th viola. It is guaranteed that no location appears more than once in the input.
Output
Print a single integer — the number of photographs Paul can take which include at least k violas.
Examples
Input
2 2 1 1
1 2
Output
4
Input
3 2 3 3
1 1
3 1
2 2
Output
1
Input
3 2 3 2
1 1
3 1
2 2
Output
4
Note
We will use '*' to denote violinists and '#' to denote violists.
In the first sample, the orchestra looks as follows
*#
**
Paul can take a photograph of just the viola, the 1 × 2 column containing the viola, the 2 × 1 row containing the viola, or the entire string section, for 4 pictures total.
In the second sample, the orchestra looks as follows
#*
*#
#*
Paul must take a photograph of the entire section.
In the third sample, the orchestra looks the same as in the second sample.
Submitted Solution:
```
r,c,n,k = map(int,input().split())
xy = [list(map(int,input().split())) for i in range(n)]
field = [[0]*c for i in range(r)]
for i in range(n):
field[xy[i][0]-1][xy[i][1]-1] = 1
num = 0
for i in range(r):
for j in range(c):
for i2 in range(i,r):
for j2 in range(j,c):
num2 = 0
for i3 in range(i,i2+1):
for j3 in range(j,j2+1):
if field[i3][j3] == 1:
num2 += 1
if num2 >= k:
num += 1
print(num)
``` | instruction | 0 | 20,745 | 7 | 41,490 |
Yes | output | 1 | 20,745 | 7 | 41,491 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Paul is at the orchestra. The string section is arranged in an r × c rectangular grid and is filled with violinists with the exception of n violists. Paul really likes violas, so he would like to take a picture including at least k of them. Paul can take a picture of any axis-parallel rectangle in the orchestra. Count the number of possible pictures that Paul can take.
Two pictures are considered to be different if the coordinates of corresponding rectangles are different.
Input
The first line of input contains four space-separated integers r, c, n, k (1 ≤ r, c, n ≤ 10, 1 ≤ k ≤ n) — the number of rows and columns of the string section, the total number of violas, and the minimum number of violas Paul would like in his photograph, respectively.
The next n lines each contain two integers xi and yi (1 ≤ xi ≤ r, 1 ≤ yi ≤ c): the position of the i-th viola. It is guaranteed that no location appears more than once in the input.
Output
Print a single integer — the number of photographs Paul can take which include at least k violas.
Examples
Input
2 2 1 1
1 2
Output
4
Input
3 2 3 3
1 1
3 1
2 2
Output
1
Input
3 2 3 2
1 1
3 1
2 2
Output
4
Note
We will use '*' to denote violinists and '#' to denote violists.
In the first sample, the orchestra looks as follows
*#
**
Paul can take a photograph of just the viola, the 1 × 2 column containing the viola, the 2 × 1 row containing the viola, or the entire string section, for 4 pictures total.
In the second sample, the orchestra looks as follows
#*
*#
#*
Paul must take a photograph of the entire section.
In the third sample, the orchestra looks the same as in the second sample.
Submitted Solution:
```
r, c, n, k = map(int, input().split())
board = [[0 for col in range(c)] for row in range(r)]
for i in range(n):
x, y = map(lambda x: int(x)-1, input().split())
board[x][y] = 1
photos = 0
for x1 in range(r):
for y1 in range(c):
for x2 in range(x1, r):
for y2 in range(y1, c):
s = 0
for row in range(x1, x2+1):
s += sum(board[row][y1:y2+1])
if s >= k:
photos += 1
print(photos)
``` | instruction | 0 | 20,746 | 7 | 41,492 |
Yes | output | 1 | 20,746 | 7 | 41,493 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Paul is at the orchestra. The string section is arranged in an r × c rectangular grid and is filled with violinists with the exception of n violists. Paul really likes violas, so he would like to take a picture including at least k of them. Paul can take a picture of any axis-parallel rectangle in the orchestra. Count the number of possible pictures that Paul can take.
Two pictures are considered to be different if the coordinates of corresponding rectangles are different.
Input
The first line of input contains four space-separated integers r, c, n, k (1 ≤ r, c, n ≤ 10, 1 ≤ k ≤ n) — the number of rows and columns of the string section, the total number of violas, and the minimum number of violas Paul would like in his photograph, respectively.
The next n lines each contain two integers xi and yi (1 ≤ xi ≤ r, 1 ≤ yi ≤ c): the position of the i-th viola. It is guaranteed that no location appears more than once in the input.
Output
Print a single integer — the number of photographs Paul can take which include at least k violas.
Examples
Input
2 2 1 1
1 2
Output
4
Input
3 2 3 3
1 1
3 1
2 2
Output
1
Input
3 2 3 2
1 1
3 1
2 2
Output
4
Note
We will use '*' to denote violinists and '#' to denote violists.
In the first sample, the orchestra looks as follows
*#
**
Paul can take a photograph of just the viola, the 1 × 2 column containing the viola, the 2 × 1 row containing the viola, or the entire string section, for 4 pictures total.
In the second sample, the orchestra looks as follows
#*
*#
#*
Paul must take a photograph of the entire section.
In the third sample, the orchestra looks the same as in the second sample.
Submitted Solution:
```
r, c, n, k = map(int, input().split())
M = [['*'] * c for i in range(r)]
for i in range(n):
x, y = map(int, input().split())
M[x-1][y-1] = '#'
def contain(M, k, x1, y1, x2, y2):
for i in range(x1, x2+1):
for j in range(y1, y2+1):
if M[i][j] == '#':
k -= 1
return k <= 0
ans = 0
for x1 in range(r):
for y1 in range(c):
for x2 in range(x1, r):
for y2 in range(y1, c):
if contain(M, k, x1, y1, x2, y2):
ans += 1
print(ans)
``` | instruction | 0 | 20,747 | 7 | 41,494 |
Yes | output | 1 | 20,747 | 7 | 41,495 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Paul is at the orchestra. The string section is arranged in an r × c rectangular grid and is filled with violinists with the exception of n violists. Paul really likes violas, so he would like to take a picture including at least k of them. Paul can take a picture of any axis-parallel rectangle in the orchestra. Count the number of possible pictures that Paul can take.
Two pictures are considered to be different if the coordinates of corresponding rectangles are different.
Input
The first line of input contains four space-separated integers r, c, n, k (1 ≤ r, c, n ≤ 10, 1 ≤ k ≤ n) — the number of rows and columns of the string section, the total number of violas, and the minimum number of violas Paul would like in his photograph, respectively.
The next n lines each contain two integers xi and yi (1 ≤ xi ≤ r, 1 ≤ yi ≤ c): the position of the i-th viola. It is guaranteed that no location appears more than once in the input.
Output
Print a single integer — the number of photographs Paul can take which include at least k violas.
Examples
Input
2 2 1 1
1 2
Output
4
Input
3 2 3 3
1 1
3 1
2 2
Output
1
Input
3 2 3 2
1 1
3 1
2 2
Output
4
Note
We will use '*' to denote violinists and '#' to denote violists.
In the first sample, the orchestra looks as follows
*#
**
Paul can take a photograph of just the viola, the 1 × 2 column containing the viola, the 2 × 1 row containing the viola, or the entire string section, for 4 pictures total.
In the second sample, the orchestra looks as follows
#*
*#
#*
Paul must take a photograph of the entire section.
In the third sample, the orchestra looks the same as in the second sample.
Submitted Solution:
```
def read_ints():
return map(int, input().split())
r, c, n, k = read_ints()
s = [[False] * (c+1) for _ in range(r+1)]
for _ in range(n):
x, y = read_ints()
assert x <= r, x
assert y <= c, y
s[x][y] = True
ss = [[0] * (c+1) for _ in range(r+1)]
for y in range(1, r+1):
for x in range(1, c+1):
ss[y][x] = ss[y-1][x] + ss[y][x-1] - ss[y-1][x-1] + int(s[y][x])
def count(lx, ly, rx, ry, ss):
assert 0 < lx <= c, lx
assert 0 < rx <= c, rx
assert 0 < ly <= r, ly
assert 0 < ry <= r, ry
return ss[ry][rx] - ss[ly-1][rx] - ss[ry][lx-1] + ss[ly-1][lx-1]
ccc = 0
for ly in range(1, r+1):
for lx in range(1, c+1):
for ry in range(ly, r+1):
for rx in range(lx, c+1):
if count(lx, ly, rx, ry, ss) >= k:
ccc += 1
print(ccc)
``` | instruction | 0 | 20,748 | 7 | 41,496 |
Yes | output | 1 | 20,748 | 7 | 41,497 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Paul is at the orchestra. The string section is arranged in an r × c rectangular grid and is filled with violinists with the exception of n violists. Paul really likes violas, so he would like to take a picture including at least k of them. Paul can take a picture of any axis-parallel rectangle in the orchestra. Count the number of possible pictures that Paul can take.
Two pictures are considered to be different if the coordinates of corresponding rectangles are different.
Input
The first line of input contains four space-separated integers r, c, n, k (1 ≤ r, c, n ≤ 10, 1 ≤ k ≤ n) — the number of rows and columns of the string section, the total number of violas, and the minimum number of violas Paul would like in his photograph, respectively.
The next n lines each contain two integers xi and yi (1 ≤ xi ≤ r, 1 ≤ yi ≤ c): the position of the i-th viola. It is guaranteed that no location appears more than once in the input.
Output
Print a single integer — the number of photographs Paul can take which include at least k violas.
Examples
Input
2 2 1 1
1 2
Output
4
Input
3 2 3 3
1 1
3 1
2 2
Output
1
Input
3 2 3 2
1 1
3 1
2 2
Output
4
Note
We will use '*' to denote violinists and '#' to denote violists.
In the first sample, the orchestra looks as follows
*#
**
Paul can take a photograph of just the viola, the 1 × 2 column containing the viola, the 2 × 1 row containing the viola, or the entire string section, for 4 pictures total.
In the second sample, the orchestra looks as follows
#*
*#
#*
Paul must take a photograph of the entire section.
In the third sample, the orchestra looks the same as in the second sample.
Submitted Solution:
```
#!/usr/bin/python3
tmp = input().split(" ");
r = int(tmp[0])
c = int(tmp[1])
n = int(tmp[2])
k = int(tmp[3])
tab = [[0]]
for j in range(1,c+1):
tab[0].append(0)
for i in range(1,r+1):
tab.append([0])
for j in range(1,c+1):
tab[i].append(0)
for i in range(0,n):
tmp = input().split(" ")
tab[ int(tmp[0]) ][ int(tmp[1]) ] = 1
pre = tab
for i in range(1,r+1):
for j in range(1,c+1):
pre[i][j] = pre[i-1][j] + pre[i][j-1] - pre[i-1][j-1] + tab[i][j]
wynik = 0
for i in range(1,r+1):
for j in range(1,c+1):
for ii in range(i,r+1):
for jj in range(j,c+1):
if tab[ii][jj] - tab[i-1][jj] - tab[ii][j-1] - tab[i-1][j-1] >= k:
wynik = wynik + 1
print ( wynik )
``` | instruction | 0 | 20,749 | 7 | 41,498 |
No | output | 1 | 20,749 | 7 | 41,499 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Paul is at the orchestra. The string section is arranged in an r × c rectangular grid and is filled with violinists with the exception of n violists. Paul really likes violas, so he would like to take a picture including at least k of them. Paul can take a picture of any axis-parallel rectangle in the orchestra. Count the number of possible pictures that Paul can take.
Two pictures are considered to be different if the coordinates of corresponding rectangles are different.
Input
The first line of input contains four space-separated integers r, c, n, k (1 ≤ r, c, n ≤ 10, 1 ≤ k ≤ n) — the number of rows and columns of the string section, the total number of violas, and the minimum number of violas Paul would like in his photograph, respectively.
The next n lines each contain two integers xi and yi (1 ≤ xi ≤ r, 1 ≤ yi ≤ c): the position of the i-th viola. It is guaranteed that no location appears more than once in the input.
Output
Print a single integer — the number of photographs Paul can take which include at least k violas.
Examples
Input
2 2 1 1
1 2
Output
4
Input
3 2 3 3
1 1
3 1
2 2
Output
1
Input
3 2 3 2
1 1
3 1
2 2
Output
4
Note
We will use '*' to denote violinists and '#' to denote violists.
In the first sample, the orchestra looks as follows
*#
**
Paul can take a photograph of just the viola, the 1 × 2 column containing the viola, the 2 × 1 row containing the viola, or the entire string section, for 4 pictures total.
In the second sample, the orchestra looks as follows
#*
*#
#*
Paul must take a photograph of the entire section.
In the third sample, the orchestra looks the same as in the second sample.
Submitted Solution:
```
r, c, n, k = map(int, input().split(' '))
p = []
def findIn(z, x, y, s):
global n
ret = 0
for i in range(n):
if z <= p[i][0] and y >= p[i][0] and x <= p[i][1] and s >= p[i][1]:
ret = ret + 1
return ret
for i in range(n):
x, y = map(int, input().split(' '))
p.append((x, y))
ans = 0
for z in range(1, c + 1):
for x in range(1, r + 1):
for y in range(z, c + 1):
for s in range(x, r + 1):
if findIn(z, x, y, s) >= k:
ans = ans + 1
print (ans);
``` | instruction | 0 | 20,750 | 7 | 41,500 |
No | output | 1 | 20,750 | 7 | 41,501 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Paul is at the orchestra. The string section is arranged in an r × c rectangular grid and is filled with violinists with the exception of n violists. Paul really likes violas, so he would like to take a picture including at least k of them. Paul can take a picture of any axis-parallel rectangle in the orchestra. Count the number of possible pictures that Paul can take.
Two pictures are considered to be different if the coordinates of corresponding rectangles are different.
Input
The first line of input contains four space-separated integers r, c, n, k (1 ≤ r, c, n ≤ 10, 1 ≤ k ≤ n) — the number of rows and columns of the string section, the total number of violas, and the minimum number of violas Paul would like in his photograph, respectively.
The next n lines each contain two integers xi and yi (1 ≤ xi ≤ r, 1 ≤ yi ≤ c): the position of the i-th viola. It is guaranteed that no location appears more than once in the input.
Output
Print a single integer — the number of photographs Paul can take which include at least k violas.
Examples
Input
2 2 1 1
1 2
Output
4
Input
3 2 3 3
1 1
3 1
2 2
Output
1
Input
3 2 3 2
1 1
3 1
2 2
Output
4
Note
We will use '*' to denote violinists and '#' to denote violists.
In the first sample, the orchestra looks as follows
*#
**
Paul can take a photograph of just the viola, the 1 × 2 column containing the viola, the 2 × 1 row containing the viola, or the entire string section, for 4 pictures total.
In the second sample, the orchestra looks as follows
#*
*#
#*
Paul must take a photograph of the entire section.
In the third sample, the orchestra looks the same as in the second sample.
Submitted Solution:
```
#!/usr/bin/env python3
r, c, n, k = [int(x) for x in input().split()]
min_x, min_y, max_x, max_y = r, c, 1, 1
first = True
for i in range(0, n):
x, y = [int(x) for x in input().split()]
if first:
min_x = x
min_y = y
max_x = x
max_y = y
else:
min_x = min(x, min_x)
min_y = min(y, min_y)
max_x = max(x, max_x)
max_y = max(y, max_y)
print(min_x * min_y * (r - max_x + 1) * (c - max_y + 1))
``` | instruction | 0 | 20,751 | 7 | 41,502 |
No | output | 1 | 20,751 | 7 | 41,503 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Paul is at the orchestra. The string section is arranged in an r × c rectangular grid and is filled with violinists with the exception of n violists. Paul really likes violas, so he would like to take a picture including at least k of them. Paul can take a picture of any axis-parallel rectangle in the orchestra. Count the number of possible pictures that Paul can take.
Two pictures are considered to be different if the coordinates of corresponding rectangles are different.
Input
The first line of input contains four space-separated integers r, c, n, k (1 ≤ r, c, n ≤ 10, 1 ≤ k ≤ n) — the number of rows and columns of the string section, the total number of violas, and the minimum number of violas Paul would like in his photograph, respectively.
The next n lines each contain two integers xi and yi (1 ≤ xi ≤ r, 1 ≤ yi ≤ c): the position of the i-th viola. It is guaranteed that no location appears more than once in the input.
Output
Print a single integer — the number of photographs Paul can take which include at least k violas.
Examples
Input
2 2 1 1
1 2
Output
4
Input
3 2 3 3
1 1
3 1
2 2
Output
1
Input
3 2 3 2
1 1
3 1
2 2
Output
4
Note
We will use '*' to denote violinists and '#' to denote violists.
In the first sample, the orchestra looks as follows
*#
**
Paul can take a photograph of just the viola, the 1 × 2 column containing the viola, the 2 × 1 row containing the viola, or the entire string section, for 4 pictures total.
In the second sample, the orchestra looks as follows
#*
*#
#*
Paul must take a photograph of the entire section.
In the third sample, the orchestra looks the same as in the second sample.
Submitted Solution:
```
r, c, n, k = map(int, input().split(' '))
p = []
def findIn(z, x, y, s):
global n
ret = 0
for i in range(n):
if z <= p[0][0] and y >= p[0][0] and x <= p[0][1] and s >= p[0][1]:
ret = ret + 1
return ret
for i in range(n):
x, y = map(int, input().split(' '))
p.append((x, y))
ans = 0
for z in range(1, c + 1):
for x in range(1, r + 1):
for y in range(z, c + 1):
for s in range(x, r + 1):
if findIn(z, x, y, s) >= k:
ans = ans + 1
print (ans);
``` | instruction | 0 | 20,752 | 7 | 41,504 |
No | output | 1 | 20,752 | 7 | 41,505 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A group of n dancers rehearses a performance for the closing ceremony. The dancers are arranged in a row, they've studied their dancing moves and can't change positions. For some of them, a white dancing suit is already bought, for some of them — a black one, and for the rest the suit will be bought in the future.
On the day when the suits were to be bought, the director was told that the participants of the olympiad will be happy if the colors of the suits on the scene will form a palindrome. A palindrome is a sequence that is the same when read from left to right and when read from right to left. The director liked the idea, and she wants to buy suits so that the color of the leftmost dancer's suit is the same as the color of the rightmost dancer's suit, the 2nd left is the same as 2nd right, and so on.
The director knows how many burls it costs to buy a white suit, and how many burls to buy a black suit. You need to find out whether it is possible to buy suits to form a palindrome, and if it's possible, what's the minimal cost of doing so. Remember that dancers can not change positions, and due to bureaucratic reasons it is not allowed to buy new suits for the dancers who already have suits, even if it reduces the overall spending.
Input
The first line contains three integers n, a, and b (1 ≤ n ≤ 20, 1 ≤ a, b ≤ 100) — the number of dancers, the cost of a white suit, and the cost of a black suit.
The next line contains n numbers c_i, i-th of which denotes the color of the suit of the i-th dancer. Number 0 denotes the white color, 1 — the black color, and 2 denotes that a suit for this dancer is still to be bought.
Output
If it is not possible to form a palindrome without swapping dancers and buying new suits for those who have one, then output -1. Otherwise, output the minimal price to get the desired visual effect.
Examples
Input
5 100 1
0 1 2 1 2
Output
101
Input
3 10 12
1 2 0
Output
-1
Input
3 12 1
0 1 0
Output
0
Note
In the first sample, the cheapest way to obtain palindromic colors is to buy a black suit for the third from left dancer and a white suit for the rightmost dancer.
In the second sample, the leftmost dancer's suit already differs from the rightmost dancer's suit so there is no way to obtain the desired coloring.
In the third sample, all suits are already bought and their colors form a palindrome. | instruction | 0 | 21,181 | 7 | 42,362 |
Tags: greedy
Correct Solution:
```
N = [int(x) for x in input().strip().split()]
arr = [int(x) for x in input().strip().split()]
i = 0
j = N[0]-1
cost = 0
mini = min(N[1],N[2])
while(i<j):
if(arr[i] == 2 and arr[j] == 2):
cost+=(2*mini)
elif(arr[i] == 2):
if(arr[j] == 0):
cost+=N[1]
else:
cost+=N[2]
elif(arr[j] == 2):
if(arr[i] == 0):
cost+=N[1]
else:
cost+=N[2]
elif(arr[i] != arr[j]):
cost = -1
break
else:
pass
i+=1
j-=1
if(i == j and arr[i] == 2):
cost+=mini
print(cost)
``` | output | 1 | 21,181 | 7 | 42,363 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A group of n dancers rehearses a performance for the closing ceremony. The dancers are arranged in a row, they've studied their dancing moves and can't change positions. For some of them, a white dancing suit is already bought, for some of them — a black one, and for the rest the suit will be bought in the future.
On the day when the suits were to be bought, the director was told that the participants of the olympiad will be happy if the colors of the suits on the scene will form a palindrome. A palindrome is a sequence that is the same when read from left to right and when read from right to left. The director liked the idea, and she wants to buy suits so that the color of the leftmost dancer's suit is the same as the color of the rightmost dancer's suit, the 2nd left is the same as 2nd right, and so on.
The director knows how many burls it costs to buy a white suit, and how many burls to buy a black suit. You need to find out whether it is possible to buy suits to form a palindrome, and if it's possible, what's the minimal cost of doing so. Remember that dancers can not change positions, and due to bureaucratic reasons it is not allowed to buy new suits for the dancers who already have suits, even if it reduces the overall spending.
Input
The first line contains three integers n, a, and b (1 ≤ n ≤ 20, 1 ≤ a, b ≤ 100) — the number of dancers, the cost of a white suit, and the cost of a black suit.
The next line contains n numbers c_i, i-th of which denotes the color of the suit of the i-th dancer. Number 0 denotes the white color, 1 — the black color, and 2 denotes that a suit for this dancer is still to be bought.
Output
If it is not possible to form a palindrome without swapping dancers and buying new suits for those who have one, then output -1. Otherwise, output the minimal price to get the desired visual effect.
Examples
Input
5 100 1
0 1 2 1 2
Output
101
Input
3 10 12
1 2 0
Output
-1
Input
3 12 1
0 1 0
Output
0
Note
In the first sample, the cheapest way to obtain palindromic colors is to buy a black suit for the third from left dancer and a white suit for the rightmost dancer.
In the second sample, the leftmost dancer's suit already differs from the rightmost dancer's suit so there is no way to obtain the desired coloring.
In the third sample, all suits are already bought and their colors form a palindrome. | instruction | 0 | 21,182 | 7 | 42,364 |
Tags: greedy
Correct Solution:
```
n, a, b = map(int, input().split())
c = list(map(int, input().split()))
flag = True
res = 0
mn = min(a, b)
for i in range((n + 1) // 2):
fst = c[i]
snd = c[n - i - 1]
if fst == 2 and snd == 2:
if i == n // 2:
res += mn
else:
res += 2 * mn
elif fst == 2:
if snd == 0:
res += a
else:
res += b
elif snd == 2:
if fst == 0:
res += a
else:
res += b
elif fst != snd:
flag = False
break
if not flag:
res = -1
print(res)
``` | output | 1 | 21,182 | 7 | 42,365 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A group of n dancers rehearses a performance for the closing ceremony. The dancers are arranged in a row, they've studied their dancing moves and can't change positions. For some of them, a white dancing suit is already bought, for some of them — a black one, and for the rest the suit will be bought in the future.
On the day when the suits were to be bought, the director was told that the participants of the olympiad will be happy if the colors of the suits on the scene will form a palindrome. A palindrome is a sequence that is the same when read from left to right and when read from right to left. The director liked the idea, and she wants to buy suits so that the color of the leftmost dancer's suit is the same as the color of the rightmost dancer's suit, the 2nd left is the same as 2nd right, and so on.
The director knows how many burls it costs to buy a white suit, and how many burls to buy a black suit. You need to find out whether it is possible to buy suits to form a palindrome, and if it's possible, what's the minimal cost of doing so. Remember that dancers can not change positions, and due to bureaucratic reasons it is not allowed to buy new suits for the dancers who already have suits, even if it reduces the overall spending.
Input
The first line contains three integers n, a, and b (1 ≤ n ≤ 20, 1 ≤ a, b ≤ 100) — the number of dancers, the cost of a white suit, and the cost of a black suit.
The next line contains n numbers c_i, i-th of which denotes the color of the suit of the i-th dancer. Number 0 denotes the white color, 1 — the black color, and 2 denotes that a suit for this dancer is still to be bought.
Output
If it is not possible to form a palindrome without swapping dancers and buying new suits for those who have one, then output -1. Otherwise, output the minimal price to get the desired visual effect.
Examples
Input
5 100 1
0 1 2 1 2
Output
101
Input
3 10 12
1 2 0
Output
-1
Input
3 12 1
0 1 0
Output
0
Note
In the first sample, the cheapest way to obtain palindromic colors is to buy a black suit for the third from left dancer and a white suit for the rightmost dancer.
In the second sample, the leftmost dancer's suit already differs from the rightmost dancer's suit so there is no way to obtain the desired coloring.
In the third sample, all suits are already bought and their colors form a palindrome. | instruction | 0 | 21,183 | 7 | 42,366 |
Tags: greedy
Correct Solution:
```
#codeforces_1040A_live
gi = lambda: list(map(int,input().split()))
n,a,b = gi()
l = gi()
ans = 0
for k in range(n//2):
if l[k] == 2 and l[-k-1] == 2:
ans += 2*min(a,b)
elif l[k] == 2 or l[-k-1] == 2:
if l[k] == 2:
ans += [a,b][l[-k-1]]
else:
ans += [a,b][l[k]]
else:
if l[k] != l[-1-k]:
print(-1)
exit();
if n%2 == 1 and l[n//2] == 2:
ans += min(a,b)
print(ans)
``` | output | 1 | 21,183 | 7 | 42,367 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A group of n dancers rehearses a performance for the closing ceremony. The dancers are arranged in a row, they've studied their dancing moves and can't change positions. For some of them, a white dancing suit is already bought, for some of them — a black one, and for the rest the suit will be bought in the future.
On the day when the suits were to be bought, the director was told that the participants of the olympiad will be happy if the colors of the suits on the scene will form a palindrome. A palindrome is a sequence that is the same when read from left to right and when read from right to left. The director liked the idea, and she wants to buy suits so that the color of the leftmost dancer's suit is the same as the color of the rightmost dancer's suit, the 2nd left is the same as 2nd right, and so on.
The director knows how many burls it costs to buy a white suit, and how many burls to buy a black suit. You need to find out whether it is possible to buy suits to form a palindrome, and if it's possible, what's the minimal cost of doing so. Remember that dancers can not change positions, and due to bureaucratic reasons it is not allowed to buy new suits for the dancers who already have suits, even if it reduces the overall spending.
Input
The first line contains three integers n, a, and b (1 ≤ n ≤ 20, 1 ≤ a, b ≤ 100) — the number of dancers, the cost of a white suit, and the cost of a black suit.
The next line contains n numbers c_i, i-th of which denotes the color of the suit of the i-th dancer. Number 0 denotes the white color, 1 — the black color, and 2 denotes that a suit for this dancer is still to be bought.
Output
If it is not possible to form a palindrome without swapping dancers and buying new suits for those who have one, then output -1. Otherwise, output the minimal price to get the desired visual effect.
Examples
Input
5 100 1
0 1 2 1 2
Output
101
Input
3 10 12
1 2 0
Output
-1
Input
3 12 1
0 1 0
Output
0
Note
In the first sample, the cheapest way to obtain palindromic colors is to buy a black suit for the third from left dancer and a white suit for the rightmost dancer.
In the second sample, the leftmost dancer's suit already differs from the rightmost dancer's suit so there is no way to obtain the desired coloring.
In the third sample, all suits are already bought and their colors form a palindrome. | instruction | 0 | 21,184 | 7 | 42,368 |
Tags: greedy
Correct Solution:
```
line1 = input()
n ,pwhite,pblack = line1.split(' ')
line2 = input()
mylist = line2.split(' ')
intlist = list()
for x in mylist:
intlist.append(int(x))
totalcost = 0
#0 white
cheaper = ((0,pwhite) if (int(pwhite) <= int(pblack)) else (1,pblack))
if (not len(intlist)%2):
pass
else:
if(intlist[int((len(intlist)-1)/2)]) ==2:
totalcost += int(cheaper[1])
intlist[int((len(intlist) - 1) / 2)] = int(cheaper[0])
for x in range(int(len(intlist)/2)):
if (((intlist[x] == 0 )and (intlist[len(intlist)-x-1] == 1)) or ((intlist[x] == 1 )and (intlist[len(intlist)-x-1] == 0))):
totalcost = -1
break
elif ( (intlist[x] == 2 ) and (intlist[len(intlist)-x-1] == 2)):
intlist[x] = cheaper[0]
intlist[len(intlist) - x - 1] = cheaper[0]
totalcost += (2*int(cheaper[1]))
elif ( (intlist[x] == 2 ) and (intlist[len(intlist)-x-1] != 2)):
intlist[x] = intlist[len(intlist)-x-1]
if(intlist[x]):
totalcost += int(pblack)
else:
totalcost += int(pwhite)
elif ((intlist[x] != 2) and (intlist[len(intlist) - x - 1] == 2)):
intlist[len(intlist) - x - 1] = intlist[x]
if (intlist[x]):
totalcost += int(pblack)
else:
totalcost += int(pwhite)
else:
k =1
print(totalcost)
``` | output | 1 | 21,184 | 7 | 42,369 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A group of n dancers rehearses a performance for the closing ceremony. The dancers are arranged in a row, they've studied their dancing moves and can't change positions. For some of them, a white dancing suit is already bought, for some of them — a black one, and for the rest the suit will be bought in the future.
On the day when the suits were to be bought, the director was told that the participants of the olympiad will be happy if the colors of the suits on the scene will form a palindrome. A palindrome is a sequence that is the same when read from left to right and when read from right to left. The director liked the idea, and she wants to buy suits so that the color of the leftmost dancer's suit is the same as the color of the rightmost dancer's suit, the 2nd left is the same as 2nd right, and so on.
The director knows how many burls it costs to buy a white suit, and how many burls to buy a black suit. You need to find out whether it is possible to buy suits to form a palindrome, and if it's possible, what's the minimal cost of doing so. Remember that dancers can not change positions, and due to bureaucratic reasons it is not allowed to buy new suits for the dancers who already have suits, even if it reduces the overall spending.
Input
The first line contains three integers n, a, and b (1 ≤ n ≤ 20, 1 ≤ a, b ≤ 100) — the number of dancers, the cost of a white suit, and the cost of a black suit.
The next line contains n numbers c_i, i-th of which denotes the color of the suit of the i-th dancer. Number 0 denotes the white color, 1 — the black color, and 2 denotes that a suit for this dancer is still to be bought.
Output
If it is not possible to form a palindrome without swapping dancers and buying new suits for those who have one, then output -1. Otherwise, output the minimal price to get the desired visual effect.
Examples
Input
5 100 1
0 1 2 1 2
Output
101
Input
3 10 12
1 2 0
Output
-1
Input
3 12 1
0 1 0
Output
0
Note
In the first sample, the cheapest way to obtain palindromic colors is to buy a black suit for the third from left dancer and a white suit for the rightmost dancer.
In the second sample, the leftmost dancer's suit already differs from the rightmost dancer's suit so there is no way to obtain the desired coloring.
In the third sample, all suits are already bought and their colors form a palindrome. | instruction | 0 | 21,185 | 7 | 42,370 |
Tags: greedy
Correct Solution:
```
n, a, b = map(int, input().split())
c = [int(x) for x in input().split()]
if a < b:
min = a
else:
min = b
middle = n // 2
if n % 2 != 0 and c[middle] == 2:
s = min
else:
s = 0
for i in range(middle):
if c[i] == c[n - i - 1] and c[i] != 2:
pass
else:
if (c[i] == 1 and c[n - i - 1] == 0) or (c[i] == 0 and c[n - i - 1] == 1):
s = -1
break
elif (c[i] == 1 and c[n - i - 1] == 2) or (c[i] == 2 and c[n - i - 1] == 1):
s += b
elif (c[i] == 0 and c[n - i - 1] == 2) or (c[i] == 2 and c[n - i - 1] == 0):
s += a
elif c[i] == 2 and c[n - i - 1] == 2:
s += 2 * min
print(s)
``` | output | 1 | 21,185 | 7 | 42,371 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A group of n dancers rehearses a performance for the closing ceremony. The dancers are arranged in a row, they've studied their dancing moves and can't change positions. For some of them, a white dancing suit is already bought, for some of them — a black one, and for the rest the suit will be bought in the future.
On the day when the suits were to be bought, the director was told that the participants of the olympiad will be happy if the colors of the suits on the scene will form a palindrome. A palindrome is a sequence that is the same when read from left to right and when read from right to left. The director liked the idea, and she wants to buy suits so that the color of the leftmost dancer's suit is the same as the color of the rightmost dancer's suit, the 2nd left is the same as 2nd right, and so on.
The director knows how many burls it costs to buy a white suit, and how many burls to buy a black suit. You need to find out whether it is possible to buy suits to form a palindrome, and if it's possible, what's the minimal cost of doing so. Remember that dancers can not change positions, and due to bureaucratic reasons it is not allowed to buy new suits for the dancers who already have suits, even if it reduces the overall spending.
Input
The first line contains three integers n, a, and b (1 ≤ n ≤ 20, 1 ≤ a, b ≤ 100) — the number of dancers, the cost of a white suit, and the cost of a black suit.
The next line contains n numbers c_i, i-th of which denotes the color of the suit of the i-th dancer. Number 0 denotes the white color, 1 — the black color, and 2 denotes that a suit for this dancer is still to be bought.
Output
If it is not possible to form a palindrome without swapping dancers and buying new suits for those who have one, then output -1. Otherwise, output the minimal price to get the desired visual effect.
Examples
Input
5 100 1
0 1 2 1 2
Output
101
Input
3 10 12
1 2 0
Output
-1
Input
3 12 1
0 1 0
Output
0
Note
In the first sample, the cheapest way to obtain palindromic colors is to buy a black suit for the third from left dancer and a white suit for the rightmost dancer.
In the second sample, the leftmost dancer's suit already differs from the rightmost dancer's suit so there is no way to obtain the desired coloring.
In the third sample, all suits are already bought and their colors form a palindrome. | instruction | 0 | 21,186 | 7 | 42,372 |
Tags: greedy
Correct Solution:
```
n, a, b = map(int, input().split())
c = input().split()
l = len(c)
summe = 0
if l % 2:
if c[l//2] == '2':
summe += min(a, b)
l = l//2
for x, y in zip(c[:l], reversed(c)):
if (x == '2') and (y == '2'):
summe += 2*min(a, b)
elif (x in '01') and (y in '01'):
if x != y:
print('-1')
summe = None
break
else:
continue
elif ((x != '2') and (y == '2')):
summe += {'0': a, '1': b}[x]
elif ((y != '2') and (x == '2')):
summe += {'0': a, '1': b}[y]
if not summe is None:
print(summe)
``` | output | 1 | 21,186 | 7 | 42,373 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A group of n dancers rehearses a performance for the closing ceremony. The dancers are arranged in a row, they've studied their dancing moves and can't change positions. For some of them, a white dancing suit is already bought, for some of them — a black one, and for the rest the suit will be bought in the future.
On the day when the suits were to be bought, the director was told that the participants of the olympiad will be happy if the colors of the suits on the scene will form a palindrome. A palindrome is a sequence that is the same when read from left to right and when read from right to left. The director liked the idea, and she wants to buy suits so that the color of the leftmost dancer's suit is the same as the color of the rightmost dancer's suit, the 2nd left is the same as 2nd right, and so on.
The director knows how many burls it costs to buy a white suit, and how many burls to buy a black suit. You need to find out whether it is possible to buy suits to form a palindrome, and if it's possible, what's the minimal cost of doing so. Remember that dancers can not change positions, and due to bureaucratic reasons it is not allowed to buy new suits for the dancers who already have suits, even if it reduces the overall spending.
Input
The first line contains three integers n, a, and b (1 ≤ n ≤ 20, 1 ≤ a, b ≤ 100) — the number of dancers, the cost of a white suit, and the cost of a black suit.
The next line contains n numbers c_i, i-th of which denotes the color of the suit of the i-th dancer. Number 0 denotes the white color, 1 — the black color, and 2 denotes that a suit for this dancer is still to be bought.
Output
If it is not possible to form a palindrome without swapping dancers and buying new suits for those who have one, then output -1. Otherwise, output the minimal price to get the desired visual effect.
Examples
Input
5 100 1
0 1 2 1 2
Output
101
Input
3 10 12
1 2 0
Output
-1
Input
3 12 1
0 1 0
Output
0
Note
In the first sample, the cheapest way to obtain palindromic colors is to buy a black suit for the third from left dancer and a white suit for the rightmost dancer.
In the second sample, the leftmost dancer's suit already differs from the rightmost dancer's suit so there is no way to obtain the desired coloring.
In the third sample, all suits are already bought and their colors form a palindrome. | instruction | 0 | 21,187 | 7 | 42,374 |
Tags: greedy
Correct Solution:
```
n, a, b = map(int, input().split())
c = list(map(int, input().split()))
cost = 0
for i in range(n // 2):
if c[i] != c[n - i - 1] and c[i] != 2 and c[n - i - 1] != 2:
print(-1)
exit()
elif c[i] == c[n - i - 1] and c[i] == 2:
cost += min(a, b) * 2
elif c[i] == c[n - i - 1]:
continue
else:
if c[i] == 0:
cost += a
elif c[i] == 1:
cost += b
elif c[n - i - 1] == 0:
cost += a
elif c[n - i - 1] == 1:
cost += b
if n % 2 and c[n // 2] == 2:
cost += min(a, b)
print(cost)
``` | output | 1 | 21,187 | 7 | 42,375 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A group of n dancers rehearses a performance for the closing ceremony. The dancers are arranged in a row, they've studied their dancing moves and can't change positions. For some of them, a white dancing suit is already bought, for some of them — a black one, and for the rest the suit will be bought in the future.
On the day when the suits were to be bought, the director was told that the participants of the olympiad will be happy if the colors of the suits on the scene will form a palindrome. A palindrome is a sequence that is the same when read from left to right and when read from right to left. The director liked the idea, and she wants to buy suits so that the color of the leftmost dancer's suit is the same as the color of the rightmost dancer's suit, the 2nd left is the same as 2nd right, and so on.
The director knows how many burls it costs to buy a white suit, and how many burls to buy a black suit. You need to find out whether it is possible to buy suits to form a palindrome, and if it's possible, what's the minimal cost of doing so. Remember that dancers can not change positions, and due to bureaucratic reasons it is not allowed to buy new suits for the dancers who already have suits, even if it reduces the overall spending.
Input
The first line contains three integers n, a, and b (1 ≤ n ≤ 20, 1 ≤ a, b ≤ 100) — the number of dancers, the cost of a white suit, and the cost of a black suit.
The next line contains n numbers c_i, i-th of which denotes the color of the suit of the i-th dancer. Number 0 denotes the white color, 1 — the black color, and 2 denotes that a suit for this dancer is still to be bought.
Output
If it is not possible to form a palindrome without swapping dancers and buying new suits for those who have one, then output -1. Otherwise, output the minimal price to get the desired visual effect.
Examples
Input
5 100 1
0 1 2 1 2
Output
101
Input
3 10 12
1 2 0
Output
-1
Input
3 12 1
0 1 0
Output
0
Note
In the first sample, the cheapest way to obtain palindromic colors is to buy a black suit for the third from left dancer and a white suit for the rightmost dancer.
In the second sample, the leftmost dancer's suit already differs from the rightmost dancer's suit so there is no way to obtain the desired coloring.
In the third sample, all suits are already bought and their colors form a palindrome. | instruction | 0 | 21,188 | 7 | 42,376 |
Tags: greedy
Correct Solution:
```
import math
from operator import itemgetter
from builtins import input
from difflib import SequenceMatcher
def reduce(list):
r = 1
for x in list:
r *= x
return r
import sys
def TwoArrays(a, b, k):
"""
:param a:list
:param b: list
:return: int
"""
count = 0
for m in range(k, len(a) + 1):
for i in range(len(a) - m + 1):
atemp = a[i:i + m]
for kk in range(len(b) - m + 1):
btemp = b[kk:kk + m]
print(atemp, btemp)
lol = len(list(set(atemp).intersection(btemp)))
if lol >= k:
count += 1
return count
from collections import defaultdict
from collections import Counter
class Graphs:
def __init__(self, vertices):
self.V = vertices
self.graph = []
def append(self, u, v, w):
self.graph.append([u, v, w])
def findParent(self, parent, i):
if parent[i] == i:
return i
return self.findParent(parent, parent[i])
def Union(self, parent, rank, xroot, yroot):
x = self.findParent(parent, xroot)
y = self.findParent(parent, yroot)
if rank[x] < rank[y]:
parent[x] = y
elif rank[x] > rank[y]:
parent[y] = x
else:
parent[x] = y
rank[y] += 1
def Kruskal(self):
i = 0
e = 0;
ans = []
self.graph = sorted(self.graph, key=lambda item: item[2])
parent = [];
rank = []
for node in range(self.V):
parent.append(node)
rank.append(0)
while e < self.V - 1:
u, v, w = self.graph[i]
i += 1
x = self.findParent(parent, u)
y = self.findParent(parent, v)
if x != y:
e += 1
ans.append([u, v, w])
self.Union(parent, rank, x, y)
return sum(row[2] for row in ans)
"""
if __name__ == '__main__':
v,edge = map(int, input().split(' '))
graphs = Graphs(v)
for _ in range(edge):
u,v,w = map(int, input().split(' '))
graphs.append(u-1,v-1,w)
#print(graphs.graph)
print(graphs.Kruskal())
"""
# import numpy as np
def Exchange(rates, x, s, f, m):
"""
:param rates: list
:param x: int
:param s: int
:param f: int
:param m: int
:return: int
x = initial money
s = starting currency
f = final currency
m = moves allowed
"""
while m > 0:
# print(x)
maxExchange = max(rates[s])
temp_s = s
s = rates[s].index(maxExchange)
# print(rates[s], maxExchange,temp_s,s,x)
if m != 2:
x *= maxExchange
m -= 1
else:
if s == f:
temp = sorted(rates[temp_s])[-2]
s = rates[temp_s].index(temp)
x *= temp
m -= 1
else:
s = rates[s].index(maxExchange)
x *= maxExchange
m -= 1
# print(rates[s], maxExchange)
return x
"""
currencies = int(input())
x,s,f,m = map(int, input().split(' '))
rates = []
for _ in range(currencies):
rates.append([int(x) for x in input().strip().split(' ')])
print(Exchange(rates,x,s,f,m))
"""
def delArray(a):
c = Counter(a)
counts = sorted([x for x in c.values() if x >= 2])
steps = 0
for i in range(len(counts) - 1):
steps += counts[i]
counts[i + 1] -= counts[i]
print(steps)
if steps == 0:
return len(a)
if (len(a) - len(counts) + 1) % 2 == 0:
return steps + ((len(a) - len(counts) + 1) // 2)
else:
return (steps + (len(a) - len(counts) + 1) // 2 + 1)
def makeCalendar(n, start):
weeks = [4] * 7
days = ['mon', 'tues', 'wed', 'thurs', 'fri', 'sat', 'sun']
rem = n % 7;
start = days.index(start)
while rem > 0:
rem -= 1
weeks[start % 7] += 1
start += 1
return weeks
def modulo(x, y, p=9):
res = 1
x = x % p
while y > 0:
if y & 1:
res = (res * x) % p
y = y >> 1
x = (x * x) % p
return res
def countSubs(a):
c = Counter(a)
if len(c) == 1:
return len(c)
else:
vals = sorted(c.values());
tot = sum(vals);
temp = vals[0];
total = 0
print(vals)
for i in range(len(vals) - 1):
temp2 = tot - temp
total += vals[i] * (2 ** (temp2) - 1)
# print(total)
temp += vals[i + 1]
return total
from functools import reduce
import sys
import fractions
import math
def gcd(L):
return (reduce(fractions.gcd, L))
def maximumValue(a):
# Return the maximum value of f among all subsegments [l..r].
# print(a[0:1])
maxi = -sys.maxsize
for i in range(len(a)):
for j in range(i, len(a)):
temp = [int(math.fabs(x)) for x in a[i:j + 1]]
# temp = map(int, temp)
# print(temp, i, j+1)
formula = gcd(temp) * (sum(a[i:j + 1]) - max(a[i:j + 1]))
# print(i,j+1, formula)
maxi = max(formula, maxi)
# print(formula)
# maxi = max(formula, maxi)
return maxi
def match(first, second):
if len(first) == 0 and len(second) == 0:
return True
if len(first) > 1 and first[0] == '*' and len(second) == 0:
return False
if (len(first) > 1 and first[0] == '\\') or (len(first) != 0 and len(second) != 0 and first[0] == second[0]):
return match(first[1:], second[1:]);
if len(first) != 0 and first[0] == '*':
return match(first[1:], second) or match(first, second[1:])
return False
def countEquals(a):
count = 0
for i in range(len(a) - 1):
if a[i] != a[i + 1]:
count += 1
return count + 1
def stringMerging():
size1, size2 = map(int, input().split(' '));
a = input()
b = input()
case1 = a[0];
case2 = b[0];
i = 1;
j = 0;
while i < size1:
# print(i,j)
if case1[-1] == b[j] and j < size2:
case1 += b[j];
j += 1
else:
case1 += a[i]
i += 1
while j < size2:
case1 += b[j];
j += 1
i = 0;
j = 1;
while j < size2:
if case2[-1] == a[i] and i < size1:
case2 += a[i];
i += 1
else:
case2 += b[j]
j += 1
while i < size1:
case2 += a[i];
i += 1
print(case1, case2)
print(min(countEquals(case1), countEquals(case2)))
chars = 256
def Char_countingArray(string):
count = [0] * chars
for i in string:
count[ord(i)] += 1
return count
def Fibo():
def modiFibo(n):
if n == 0:
return 1
if n == 1:
return 1
return (5 * (modiFibo(n - 1)) + 3 * modiFibo(n - 2))
size = int(input())
a = [int(x) for x in input().strip().split(' ')]
num = 1
for x in a:
num *= x
num %= 100
print(modiFibo(num) % 1000)
def firstNonRepeating(string):
count = Char_countingArray(string)
ind = -1
k = 0
for i in string:
if count[ord(i)] == 1:
ind = k
break
k += 1
return ind
def rescueCat():
rows, cols = map(int, input().split(' '))
specials = int(input())
actual = [[0 for _ in range(cols)] for _ in range(rows)]
copy = [[0 for _ in range(cols)] for _ in range(rows)]
for _ in range(specials):
x, y, c = map(int, input().split(' '))
x = rows - x - 1
actual[x][y] = c
for i in range(rows):
copy[i][0] += 1
if i + actual[i][0] < rows:
if actual[i][0] > 0:
copy[i + actual[i][0]][0] += 1
if actual[i][0] == -1:
copy[i][0] = 0
# print(actual)
# print(copy)
for j in range(1, cols):
copy[0][j] += 1
if j + actual[0][j] < cols:
if actual[0][j] > 0:
copy[j + actual[0][j]][0] += 1
if actual[0][j] == -1:
copy[0][j] = 0
for i in range(1, rows):
for j in range(1, cols):
if j + actual[i][j] < cols:
copy[i][j + actual[i][j]] += 1
if i + actual[i][j] < rows:
copy[i + actual[i][j]][j] += 1
if actual[i][j] == -1:
copy[i][j] = 0
else:
copy[i][j] = copy[i - 1][j] + copy[i][j - 1]
# print(copy)
print(copy[-1][-1])
def pairs():
n, k = map(int, input().split(' '))
count = 0
for i in range(1, n - k + 1):
count += (n - i) // k
# ((n)**2 - (n)*k)//(2*k) + (k+1)//2 - ((n-k)(n-k+1))/(2*k)
temp = ((n) ** 2 - (n) * k) // (2 * k) + ((k) // 2 - (n % k) // 2)
print(count, " ", temp, " ", count - temp)
def voting():
n = int(input())
a = [int(x) for x in input().strip().split(' ')]
presum = [];
presum.append(a[0])
for i in range(1, n):
presum.append(a[i] + presum[-1])
votes = [0] * n
"""
for i in range(n):
if i==0 or i==n-1:
votes[i]+=1
else:
votes[i]+=2"""
for i in range(n - 1):
for j in range(i + 1, n):
temp = presum[j - 1] - presum[i]
if temp <= a[i]:
votes[j] += 1
if temp <= a[j]:
votes[i] += 1
print(*votes)
def mineat():
n, h = map(int, input().split(' '))
a = [int(x) for x in input().strip().split(' ')]
a = sorted(a)
if n == h:
print(a[-1])
else:
hours_left = 2 * (h - n)
start = 2 * n - h
i = 0
for _ in range(n):
if a[i] != 0:
break
i += 1
a = a[i:]
for i in range(1, a[-1] + 1):
temp_sum = 0
for j in range(len(a)):
temp_sum += math.ceil(a[j] / i)
if temp_sum <= h:
break
print(i)
def universe(n):
d, s = map(str, input().split(' '))
d = int(d)
binary = [0] * len(s)
if 'C' not in s:
if len(s) <= d:
print("Case #", n, ":0")
else:
print("Case #", n, ":IMPOSSIBLE")
else:
swaps = 0;
curr = 1
for i in range(len(s)):
if s[i] == 'C':
binary[i] = 0
curr *= 2
else:
binary[i] = curr
i = len(s) - 2
while i > -1:
if sum(binary) <= d:
# print(sum(binary))
break
if binary[i] == 0:
binary[i + 1], binary[i] = binary[i], binary[i + 1]
swaps += 1
binary[i] /= 2
i -= 1
print("Case #", n, ":", swaps)
def board():
n,k = map(int, input().split(' '))
s = input()
start = s[:k]
su = 0
for i in range(1,n-k+1):
temp = s[i:i+k]
for i,val in enumerate(temp):
if val != start[i]:
su+=1
start = temp
return su
# your code goes here
def generate_primes():
n = 10 ** 5
a = []
nNew = (n - 2) // 2
marked = [False] * (nNew + 1)
for i in range(1, nNew + 1):
j = i
while (i + j + 2 * i * j) <= nNew:
marked[i + j + 2 * i * j] = True
j += 1
if n > 2:
a.append(2)
for i in range(1, nNew + 1):
if marked[i] == False:
a.append(2 * i + 1)
return a
# @param A : list of integers
# @return an integer
def solve(A):
a = generate_primes()
#print(a)
prod = 1
for num in A:
prod = prod * num
ptr = 0;
unique = 1
i = 0; factors = []
while prod > 1:
if prod % a[i] == 0:
prod /= a[i]
if i not in factors:
factors.append(i)
else:
i += 1
return len(factors)
import bisect
def required_primes(n):
a = generate_primes()
temp = n//2
pos_left = bisect.bisect_left(a,temp)
left = pos_left; right = left+1
while (left<0 and right<len(a)-1):
if a[left]+a[right]<n:
right+=1
elif a[left]+a[right]>n:
left-=1
else:
return left,right
def dance_pal():
a,white,black = map(int, input().strip().split(' '))
dress = [int(x) for x in input().strip().split(' ')]
flag = True; cost = 0
for i in range(a//2):
#print(dress[i], dress[a-1-i])
if dress[i]!=2 and dress[a-1-i]!=2:
if dress[i]!= dress[a-1-i]:
#print("hwjk")
flag = False
break
else:
if dress[i]==2 and dress[a-1-i]==2:
cost += 2*min(white,black)
else:
if dress[i]==2:
if dress[a-1-i]==0:
cost += white
else:
cost+= black
elif dress[a-1-i]==2:
if dress[i]==0:
cost += white
else:
cost+= black
if a%2!=0 and dress[a//2]==2:
cost+=min(black,white)
if flag:
print(cost)
else:
print("-1")
if __name__ == '__main__':
t = 1
#t = int(input())
while t > 0:
t -= 1
dance_pal()
``` | output | 1 | 21,188 | 7 | 42,377 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.