message stringlengths 2 44.5k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 276 109k | cluster float64 23 23 | __index_level_0__ int64 552 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Little Chris is a huge fan of linear algebra. This time he has been given a homework about the unusual square of a square matrix.
The dot product of two integer number vectors x and y of size n is the sum of the products of the corresponding components of the vectors. The unusual square of an n × n square matrix A is defined as the sum of n dot products. The i-th of them is the dot product of the i-th row vector and the i-th column vector in the matrix A.
Fortunately for Chris, he has to work only in GF(2)! This means that all operations (addition, multiplication) are calculated modulo 2. In fact, the matrix A is binary: each element of A is either 0 or 1. For example, consider the following matrix A:
<image>
The unusual square of A is equal to (1·1 + 1·0 + 1·1) + (0·1 + 1·1 + 1·0) + (1·1 + 0·1 + 0·0) = 0 + 1 + 1 = 0.
However, there is much more to the homework. Chris has to process q queries; each query can be one of the following:
1. given a row index i, flip all the values in the i-th row in A;
2. given a column index i, flip all the values in the i-th column in A;
3. find the unusual square of A.
To flip a bit value w means to change it to 1 - w, i.e., 1 changes to 0 and 0 changes to 1.
Given the initial matrix A, output the answers for each query of the third type! Can you solve Chris's homework?
Input
The first line of input contains an integer n (1 ≤ n ≤ 1000), the number of rows and the number of columns in the matrix A. The next n lines describe the matrix: the i-th line contains n space-separated bits and describes the i-th row of A. The j-th number of the i-th line aij (0 ≤ aij ≤ 1) is the element on the intersection of the i-th row and the j-th column of A.
The next line of input contains an integer q (1 ≤ q ≤ 106), the number of queries. Each of the next q lines describes a single query, which can be one of the following:
* 1 i — flip the values of the i-th row;
* 2 i — flip the values of the i-th column;
* 3 — output the unusual square of A.
Note: since the size of the input and output could be very large, don't use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++.
Output
Let the number of the 3rd type queries in the input be m. Output a single string s of length m, where the i-th symbol of s is the value of the unusual square of A for the i-th query of the 3rd type as it appears in the input.
Examples
Input
3
1 1 1
0 1 1
1 0 0
12
3
2 3
3
2 2
2 2
1 3
3
3
1 2
2 1
1 1
3
Output
01001 | instruction | 0 | 2,818 | 23 | 5,636 |
Tags: implementation, math
Correct Solution:
```
#------------------------template--------------------------#
import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import*
from io import BytesIO, IOBase
def vsInput():
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
def value():return tuple(map(int,input().split()))
def array():return [int(i) for i in input().split()]
def Int():return int(input())
def Str():return input()
def arrayS():return [i for i in input().split()]
#-------------------------code---------------------------#
#vsInput()
n=Int()
ans=0
for i in range(n):
a=array()
ans+=a[i]
#print(ans)
for _ in range(Int()):
s=input()
if(s=='3'):
print(ans%2,end="")
else:
t,c=map(int,s.split())
ans+=1
``` | output | 1 | 2,818 | 23 | 5,637 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Chris is a huge fan of linear algebra. This time he has been given a homework about the unusual square of a square matrix.
The dot product of two integer number vectors x and y of size n is the sum of the products of the corresponding components of the vectors. The unusual square of an n × n square matrix A is defined as the sum of n dot products. The i-th of them is the dot product of the i-th row vector and the i-th column vector in the matrix A.
Fortunately for Chris, he has to work only in GF(2)! This means that all operations (addition, multiplication) are calculated modulo 2. In fact, the matrix A is binary: each element of A is either 0 or 1. For example, consider the following matrix A:
<image>
The unusual square of A is equal to (1·1 + 1·0 + 1·1) + (0·1 + 1·1 + 1·0) + (1·1 + 0·1 + 0·0) = 0 + 1 + 1 = 0.
However, there is much more to the homework. Chris has to process q queries; each query can be one of the following:
1. given a row index i, flip all the values in the i-th row in A;
2. given a column index i, flip all the values in the i-th column in A;
3. find the unusual square of A.
To flip a bit value w means to change it to 1 - w, i.e., 1 changes to 0 and 0 changes to 1.
Given the initial matrix A, output the answers for each query of the third type! Can you solve Chris's homework?
Input
The first line of input contains an integer n (1 ≤ n ≤ 1000), the number of rows and the number of columns in the matrix A. The next n lines describe the matrix: the i-th line contains n space-separated bits and describes the i-th row of A. The j-th number of the i-th line aij (0 ≤ aij ≤ 1) is the element on the intersection of the i-th row and the j-th column of A.
The next line of input contains an integer q (1 ≤ q ≤ 106), the number of queries. Each of the next q lines describes a single query, which can be one of the following:
* 1 i — flip the values of the i-th row;
* 2 i — flip the values of the i-th column;
* 3 — output the unusual square of A.
Note: since the size of the input and output could be very large, don't use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++.
Output
Let the number of the 3rd type queries in the input be m. Output a single string s of length m, where the i-th symbol of s is the value of the unusual square of A for the i-th query of the 3rd type as it appears in the input.
Examples
Input
3
1 1 1
0 1 1
1 0 0
12
3
2 3
3
2 2
2 2
1 3
3
3
1 2
2 1
1 1
3
Output
01001
Submitted Solution:
```
from sys import stdin,stdout
cnt = 0
arr = []
for i in range(int(stdin.readline())):
if stdin.readline().split()[i]=='1':
arr.append(1)
cnt += 1
else:
arr.append(0)
stdin.readline()
for i in stdin:
if len(i)<3:
stdout.write(str(cnt%2))
else:
_,j = map(int,i.split())
j -= 1
if arr[j]:
arr[j] = 0
cnt -= 1
else:
arr[j] = 1
cnt += 1
``` | instruction | 0 | 2,819 | 23 | 5,638 |
Yes | output | 1 | 2,819 | 23 | 5,639 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Chris is a huge fan of linear algebra. This time he has been given a homework about the unusual square of a square matrix.
The dot product of two integer number vectors x and y of size n is the sum of the products of the corresponding components of the vectors. The unusual square of an n × n square matrix A is defined as the sum of n dot products. The i-th of them is the dot product of the i-th row vector and the i-th column vector in the matrix A.
Fortunately for Chris, he has to work only in GF(2)! This means that all operations (addition, multiplication) are calculated modulo 2. In fact, the matrix A is binary: each element of A is either 0 or 1. For example, consider the following matrix A:
<image>
The unusual square of A is equal to (1·1 + 1·0 + 1·1) + (0·1 + 1·1 + 1·0) + (1·1 + 0·1 + 0·0) = 0 + 1 + 1 = 0.
However, there is much more to the homework. Chris has to process q queries; each query can be one of the following:
1. given a row index i, flip all the values in the i-th row in A;
2. given a column index i, flip all the values in the i-th column in A;
3. find the unusual square of A.
To flip a bit value w means to change it to 1 - w, i.e., 1 changes to 0 and 0 changes to 1.
Given the initial matrix A, output the answers for each query of the third type! Can you solve Chris's homework?
Input
The first line of input contains an integer n (1 ≤ n ≤ 1000), the number of rows and the number of columns in the matrix A. The next n lines describe the matrix: the i-th line contains n space-separated bits and describes the i-th row of A. The j-th number of the i-th line aij (0 ≤ aij ≤ 1) is the element on the intersection of the i-th row and the j-th column of A.
The next line of input contains an integer q (1 ≤ q ≤ 106), the number of queries. Each of the next q lines describes a single query, which can be one of the following:
* 1 i — flip the values of the i-th row;
* 2 i — flip the values of the i-th column;
* 3 — output the unusual square of A.
Note: since the size of the input and output could be very large, don't use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++.
Output
Let the number of the 3rd type queries in the input be m. Output a single string s of length m, where the i-th symbol of s is the value of the unusual square of A for the i-th query of the 3rd type as it appears in the input.
Examples
Input
3
1 1 1
0 1 1
1 0 0
12
3
2 3
3
2 2
2 2
1 3
3
3
1 2
2 1
1 1
3
Output
01001
Submitted Solution:
```
from sys import stdin
input = stdin.readline
n = int(input())
lis=list(list(map(int, input().split())) for _ in range(n))
u=0
for i in range(n):
for j in range(n):
if i==j:
u^=lis[i][j]
ans =[]
k = int(input())
for i in range(k):
s = input()
if s[0]=='3':
ans.append(str(u))
else:
u^=1
print(''.join(ans))
``` | instruction | 0 | 2,820 | 23 | 5,640 |
Yes | output | 1 | 2,820 | 23 | 5,641 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Chris is a huge fan of linear algebra. This time he has been given a homework about the unusual square of a square matrix.
The dot product of two integer number vectors x and y of size n is the sum of the products of the corresponding components of the vectors. The unusual square of an n × n square matrix A is defined as the sum of n dot products. The i-th of them is the dot product of the i-th row vector and the i-th column vector in the matrix A.
Fortunately for Chris, he has to work only in GF(2)! This means that all operations (addition, multiplication) are calculated modulo 2. In fact, the matrix A is binary: each element of A is either 0 or 1. For example, consider the following matrix A:
<image>
The unusual square of A is equal to (1·1 + 1·0 + 1·1) + (0·1 + 1·1 + 1·0) + (1·1 + 0·1 + 0·0) = 0 + 1 + 1 = 0.
However, there is much more to the homework. Chris has to process q queries; each query can be one of the following:
1. given a row index i, flip all the values in the i-th row in A;
2. given a column index i, flip all the values in the i-th column in A;
3. find the unusual square of A.
To flip a bit value w means to change it to 1 - w, i.e., 1 changes to 0 and 0 changes to 1.
Given the initial matrix A, output the answers for each query of the third type! Can you solve Chris's homework?
Input
The first line of input contains an integer n (1 ≤ n ≤ 1000), the number of rows and the number of columns in the matrix A. The next n lines describe the matrix: the i-th line contains n space-separated bits and describes the i-th row of A. The j-th number of the i-th line aij (0 ≤ aij ≤ 1) is the element on the intersection of the i-th row and the j-th column of A.
The next line of input contains an integer q (1 ≤ q ≤ 106), the number of queries. Each of the next q lines describes a single query, which can be one of the following:
* 1 i — flip the values of the i-th row;
* 2 i — flip the values of the i-th column;
* 3 — output the unusual square of A.
Note: since the size of the input and output could be very large, don't use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++.
Output
Let the number of the 3rd type queries in the input be m. Output a single string s of length m, where the i-th symbol of s is the value of the unusual square of A for the i-th query of the 3rd type as it appears in the input.
Examples
Input
3
1 1 1
0 1 1
1 0 0
12
3
2 3
3
2 2
2 2
1 3
3
3
1 2
2 1
1 1
3
Output
01001
Submitted Solution:
```
from sys import stdin, stdout
from functools import reduce
from operator import xor
from threading import Thread, stack_size
def arr_inp(n):
return [int(x) for x in input().split()]
def main():
n = int(int(input()))
mat = [list(map(int, input().split())) for _ in range(n)]
ans, q, a = reduce(xor, [mat[i][i] for i in range(n)]), int(input()), []
queries = [stdin.readline() for i in range(q)]
for query in queries:
if query[0] == '3':
a.append(str(ans))
else:
ans ^= 1
print(''.join(a))
if __name__ == '__main__':
stack_size(102400000)
thread = Thread(target=main)
thread.start()
``` | instruction | 0 | 2,821 | 23 | 5,642 |
Yes | output | 1 | 2,821 | 23 | 5,643 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Chris is a huge fan of linear algebra. This time he has been given a homework about the unusual square of a square matrix.
The dot product of two integer number vectors x and y of size n is the sum of the products of the corresponding components of the vectors. The unusual square of an n × n square matrix A is defined as the sum of n dot products. The i-th of them is the dot product of the i-th row vector and the i-th column vector in the matrix A.
Fortunately for Chris, he has to work only in GF(2)! This means that all operations (addition, multiplication) are calculated modulo 2. In fact, the matrix A is binary: each element of A is either 0 or 1. For example, consider the following matrix A:
<image>
The unusual square of A is equal to (1·1 + 1·0 + 1·1) + (0·1 + 1·1 + 1·0) + (1·1 + 0·1 + 0·0) = 0 + 1 + 1 = 0.
However, there is much more to the homework. Chris has to process q queries; each query can be one of the following:
1. given a row index i, flip all the values in the i-th row in A;
2. given a column index i, flip all the values in the i-th column in A;
3. find the unusual square of A.
To flip a bit value w means to change it to 1 - w, i.e., 1 changes to 0 and 0 changes to 1.
Given the initial matrix A, output the answers for each query of the third type! Can you solve Chris's homework?
Input
The first line of input contains an integer n (1 ≤ n ≤ 1000), the number of rows and the number of columns in the matrix A. The next n lines describe the matrix: the i-th line contains n space-separated bits and describes the i-th row of A. The j-th number of the i-th line aij (0 ≤ aij ≤ 1) is the element on the intersection of the i-th row and the j-th column of A.
The next line of input contains an integer q (1 ≤ q ≤ 106), the number of queries. Each of the next q lines describes a single query, which can be one of the following:
* 1 i — flip the values of the i-th row;
* 2 i — flip the values of the i-th column;
* 3 — output the unusual square of A.
Note: since the size of the input and output could be very large, don't use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++.
Output
Let the number of the 3rd type queries in the input be m. Output a single string s of length m, where the i-th symbol of s is the value of the unusual square of A for the i-th query of the 3rd type as it appears in the input.
Examples
Input
3
1 1 1
0 1 1
1 0 0
12
3
2 3
3
2 2
2 2
1 3
3
3
1 2
2 1
1 1
3
Output
01001
Submitted Solution:
```
import os,sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
def print(*args, **kwargs):
"""Prints the values to a stream, or to sys.stdout by default."""
sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
at_start = True
for x in args:
if not at_start:
file.write(sep)
file.write(str(x))
at_start = False
file.write(kwargs.pop("end", "\n"))
if kwargs.pop("flush", False):
file.flush()
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
n=int(input())
mat=[]
for i in range(n):
l=list(map(int,input().split()))
mat.append(l)
summ=0
for i in range(n):
for j in range(n):
if(i==j):
summ+=mat[i][j]
q=int(input())
for i in range(q):
p=list(map(int,input().split()))
if(p[0]==3):
print(summ%2,end="")
else:
summ+=1
``` | instruction | 0 | 2,822 | 23 | 5,644 |
Yes | output | 1 | 2,822 | 23 | 5,645 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Chris is a huge fan of linear algebra. This time he has been given a homework about the unusual square of a square matrix.
The dot product of two integer number vectors x and y of size n is the sum of the products of the corresponding components of the vectors. The unusual square of an n × n square matrix A is defined as the sum of n dot products. The i-th of them is the dot product of the i-th row vector and the i-th column vector in the matrix A.
Fortunately for Chris, he has to work only in GF(2)! This means that all operations (addition, multiplication) are calculated modulo 2. In fact, the matrix A is binary: each element of A is either 0 or 1. For example, consider the following matrix A:
<image>
The unusual square of A is equal to (1·1 + 1·0 + 1·1) + (0·1 + 1·1 + 1·0) + (1·1 + 0·1 + 0·0) = 0 + 1 + 1 = 0.
However, there is much more to the homework. Chris has to process q queries; each query can be one of the following:
1. given a row index i, flip all the values in the i-th row in A;
2. given a column index i, flip all the values in the i-th column in A;
3. find the unusual square of A.
To flip a bit value w means to change it to 1 - w, i.e., 1 changes to 0 and 0 changes to 1.
Given the initial matrix A, output the answers for each query of the third type! Can you solve Chris's homework?
Input
The first line of input contains an integer n (1 ≤ n ≤ 1000), the number of rows and the number of columns in the matrix A. The next n lines describe the matrix: the i-th line contains n space-separated bits and describes the i-th row of A. The j-th number of the i-th line aij (0 ≤ aij ≤ 1) is the element on the intersection of the i-th row and the j-th column of A.
The next line of input contains an integer q (1 ≤ q ≤ 106), the number of queries. Each of the next q lines describes a single query, which can be one of the following:
* 1 i — flip the values of the i-th row;
* 2 i — flip the values of the i-th column;
* 3 — output the unusual square of A.
Note: since the size of the input and output could be very large, don't use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++.
Output
Let the number of the 3rd type queries in the input be m. Output a single string s of length m, where the i-th symbol of s is the value of the unusual square of A for the i-th query of the 3rd type as it appears in the input.
Examples
Input
3
1 1 1
0 1 1
1 0 0
12
3
2 3
3
2 2
2 2
1 3
3
3
1 2
2 1
1 1
3
Output
01001
Submitted Solution:
```
from operator import and_, xor
from functools import reduce
from itertools import chain
from sys import stdin
input = stdin.readline
n = int(input())
l = list(chain(*list(list(map(int, input().split())) for _ in range(n))))
q = int(input())
commands = list(list(map(int, input().split())) for _ in range(q))
output = []
for i in range(q):
if commands[i][0] == 3:
ans = 0
for i in range(n):
ans += sum([*map(and_, l[i*n:(1+i)*n], l[i::n])]) % 2
ans %= 2
output.append(ans)
if commands[i][0] == 2:
col = commands[i][1] - 1
l[col::n] = [*map(lambda v : 1 - v, l[col::n])]
if commands[i][0] == 1:
row = commands[i][1] - 1
l[row*n:(row+1)*n] = [*map(lambda v : 1 - v, l[row*n:(row+1)*n])]
print(''.join([*map(str, output)]))
``` | instruction | 0 | 2,823 | 23 | 5,646 |
No | output | 1 | 2,823 | 23 | 5,647 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Chris is a huge fan of linear algebra. This time he has been given a homework about the unusual square of a square matrix.
The dot product of two integer number vectors x and y of size n is the sum of the products of the corresponding components of the vectors. The unusual square of an n × n square matrix A is defined as the sum of n dot products. The i-th of them is the dot product of the i-th row vector and the i-th column vector in the matrix A.
Fortunately for Chris, he has to work only in GF(2)! This means that all operations (addition, multiplication) are calculated modulo 2. In fact, the matrix A is binary: each element of A is either 0 or 1. For example, consider the following matrix A:
<image>
The unusual square of A is equal to (1·1 + 1·0 + 1·1) + (0·1 + 1·1 + 1·0) + (1·1 + 0·1 + 0·0) = 0 + 1 + 1 = 0.
However, there is much more to the homework. Chris has to process q queries; each query can be one of the following:
1. given a row index i, flip all the values in the i-th row in A;
2. given a column index i, flip all the values in the i-th column in A;
3. find the unusual square of A.
To flip a bit value w means to change it to 1 - w, i.e., 1 changes to 0 and 0 changes to 1.
Given the initial matrix A, output the answers for each query of the third type! Can you solve Chris's homework?
Input
The first line of input contains an integer n (1 ≤ n ≤ 1000), the number of rows and the number of columns in the matrix A. The next n lines describe the matrix: the i-th line contains n space-separated bits and describes the i-th row of A. The j-th number of the i-th line aij (0 ≤ aij ≤ 1) is the element on the intersection of the i-th row and the j-th column of A.
The next line of input contains an integer q (1 ≤ q ≤ 106), the number of queries. Each of the next q lines describes a single query, which can be one of the following:
* 1 i — flip the values of the i-th row;
* 2 i — flip the values of the i-th column;
* 3 — output the unusual square of A.
Note: since the size of the input and output could be very large, don't use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++.
Output
Let the number of the 3rd type queries in the input be m. Output a single string s of length m, where the i-th symbol of s is the value of the unusual square of A for the i-th query of the 3rd type as it appears in the input.
Examples
Input
3
1 1 1
0 1 1
1 0 0
12
3
2 3
3
2 2
2 2
1 3
3
3
1 2
2 1
1 1
3
Output
01001
Submitted Solution:
```
from operator import and_, xor
from functools import reduce
from itertools import chain
from sys import stdin
input = stdin.readline
n = int(input())
l = list(chain(*list(list(map(int, input().split())) for _ in range(n))))
q = int(input())
commands = list(list(map(int, input().split())) for _ in range(q))
output = list()
for i in range(q):
if commands[i][0] == 3:
ans = 0
for i in range(n):
ans += sum([*map(and_, l[i*n:(1+i)*n], l[i::n])]) % 2
ans %= 2
output += [ans]
if commands[i][0] == 2:
col = commands[i][1] - 1
l[col::n] = [*map(lambda v : 1 - v, l[col::n])]
if commands[i][0] == 1:
row = commands[i][1] - 1
l[row*n:(row+1)*n] = [*map(lambda v : 1 - v, l[row*n:(row+1)*n])]
print(''.join([*map(str, l)]))
``` | instruction | 0 | 2,824 | 23 | 5,648 |
No | output | 1 | 2,824 | 23 | 5,649 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Chris is a huge fan of linear algebra. This time he has been given a homework about the unusual square of a square matrix.
The dot product of two integer number vectors x and y of size n is the sum of the products of the corresponding components of the vectors. The unusual square of an n × n square matrix A is defined as the sum of n dot products. The i-th of them is the dot product of the i-th row vector and the i-th column vector in the matrix A.
Fortunately for Chris, he has to work only in GF(2)! This means that all operations (addition, multiplication) are calculated modulo 2. In fact, the matrix A is binary: each element of A is either 0 or 1. For example, consider the following matrix A:
<image>
The unusual square of A is equal to (1·1 + 1·0 + 1·1) + (0·1 + 1·1 + 1·0) + (1·1 + 0·1 + 0·0) = 0 + 1 + 1 = 0.
However, there is much more to the homework. Chris has to process q queries; each query can be one of the following:
1. given a row index i, flip all the values in the i-th row in A;
2. given a column index i, flip all the values in the i-th column in A;
3. find the unusual square of A.
To flip a bit value w means to change it to 1 - w, i.e., 1 changes to 0 and 0 changes to 1.
Given the initial matrix A, output the answers for each query of the third type! Can you solve Chris's homework?
Input
The first line of input contains an integer n (1 ≤ n ≤ 1000), the number of rows and the number of columns in the matrix A. The next n lines describe the matrix: the i-th line contains n space-separated bits and describes the i-th row of A. The j-th number of the i-th line aij (0 ≤ aij ≤ 1) is the element on the intersection of the i-th row and the j-th column of A.
The next line of input contains an integer q (1 ≤ q ≤ 106), the number of queries. Each of the next q lines describes a single query, which can be one of the following:
* 1 i — flip the values of the i-th row;
* 2 i — flip the values of the i-th column;
* 3 — output the unusual square of A.
Note: since the size of the input and output could be very large, don't use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++.
Output
Let the number of the 3rd type queries in the input be m. Output a single string s of length m, where the i-th symbol of s is the value of the unusual square of A for the i-th query of the 3rd type as it appears in the input.
Examples
Input
3
1 1 1
0 1 1
1 0 0
12
3
2 3
3
2 2
2 2
1 3
3
3
1 2
2 1
1 1
3
Output
01001
Submitted Solution:
```
def main():
from sys import stdin
from operator import xor
from functools import reduce
x, res = reduce(xor, (input()[i] == '1' for i in range(0, int(input()) * 2, 2))), []
for s in stdin.read().splitlines():
if s == '3':
res.append("01"[x])
else:
x ^= True
print(''.join(res))
if __name__ == "__main__":
main()
``` | instruction | 0 | 2,825 | 23 | 5,650 |
No | output | 1 | 2,825 | 23 | 5,651 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Little Chris is a huge fan of linear algebra. This time he has been given a homework about the unusual square of a square matrix.
The dot product of two integer number vectors x and y of size n is the sum of the products of the corresponding components of the vectors. The unusual square of an n × n square matrix A is defined as the sum of n dot products. The i-th of them is the dot product of the i-th row vector and the i-th column vector in the matrix A.
Fortunately for Chris, he has to work only in GF(2)! This means that all operations (addition, multiplication) are calculated modulo 2. In fact, the matrix A is binary: each element of A is either 0 or 1. For example, consider the following matrix A:
<image>
The unusual square of A is equal to (1·1 + 1·0 + 1·1) + (0·1 + 1·1 + 1·0) + (1·1 + 0·1 + 0·0) = 0 + 1 + 1 = 0.
However, there is much more to the homework. Chris has to process q queries; each query can be one of the following:
1. given a row index i, flip all the values in the i-th row in A;
2. given a column index i, flip all the values in the i-th column in A;
3. find the unusual square of A.
To flip a bit value w means to change it to 1 - w, i.e., 1 changes to 0 and 0 changes to 1.
Given the initial matrix A, output the answers for each query of the third type! Can you solve Chris's homework?
Input
The first line of input contains an integer n (1 ≤ n ≤ 1000), the number of rows and the number of columns in the matrix A. The next n lines describe the matrix: the i-th line contains n space-separated bits and describes the i-th row of A. The j-th number of the i-th line aij (0 ≤ aij ≤ 1) is the element on the intersection of the i-th row and the j-th column of A.
The next line of input contains an integer q (1 ≤ q ≤ 106), the number of queries. Each of the next q lines describes a single query, which can be one of the following:
* 1 i — flip the values of the i-th row;
* 2 i — flip the values of the i-th column;
* 3 — output the unusual square of A.
Note: since the size of the input and output could be very large, don't use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++.
Output
Let the number of the 3rd type queries in the input be m. Output a single string s of length m, where the i-th symbol of s is the value of the unusual square of A for the i-th query of the 3rd type as it appears in the input.
Examples
Input
3
1 1 1
0 1 1
1 0 0
12
3
2 3
3
2 2
2 2
1 3
3
3
1 2
2 1
1 1
3
Output
01001
Submitted Solution:
```
current = False
n = int(input())
for i in range(n):
v = [bool for i in input().split()]
if v[i] == True:
current = not current
output = ""
q = int(input())
for i in range(q):
query = [int(x) for x in input().split()]
if query[0] == 3:
output += str(int(current))
# print(int(current))
else:
current = not current
print(output)
``` | instruction | 0 | 2,826 | 23 | 5,652 |
No | output | 1 | 2,826 | 23 | 5,653 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given set of n points in 5-dimensional space. The points are labeled from 1 to n. No two points coincide.
We will call point a bad if there are different points b and c, not equal to a, from the given set such that angle between vectors <image> and <image> is acute (i.e. strictly less than <image>). Otherwise, the point is called good.
The angle between vectors <image> and <image> in 5-dimensional space is defined as <image>, where <image> is the scalar product and <image> is length of <image>.
Given the list of points, print the indices of the good points in ascending order.
Input
The first line of input contains a single integer n (1 ≤ n ≤ 103) — the number of points.
The next n lines of input contain five integers ai, bi, ci, di, ei (|ai|, |bi|, |ci|, |di|, |ei| ≤ 103) — the coordinates of the i-th point. All points are distinct.
Output
First, print a single integer k — the number of good points.
Then, print k integers, each on their own line — the indices of the good points in ascending order.
Examples
Input
6
0 0 0 0 0
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
Output
1
1
Input
3
0 0 1 2 0
0 0 9 2 0
0 0 5 9 0
Output
0
Note
In the first sample, the first point forms exactly a <image> angle with all other pairs of points, so it is good.
In the second sample, along the cd plane, we can see the points look as follows:
<image>
We can see that all angles here are acute, so no points are good. | instruction | 0 | 3,031 | 23 | 6,062 |
Tags: brute force, geometry, math
Correct Solution:
```
n = int(input())
if n > 20:
print(0)
else:
s = []
for i in range(n):
s.append([int(i) for i in input().split()])
p = []
for i in range(n):
p.append(0)
for j in range(n):
for l in range(j):
b = 0
for k in range(5):
b += (s[i][k]-s[j][k])*(s[i][k]-s[l][k])
if b > 0:
p[-1] += 1
u = 0
ans = []
for i in range(n):
if p[i]==0:
u+=1
ans.append(i+1)
print(u)
for i in ans:
print(i)
``` | output | 1 | 3,031 | 23 | 6,063 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given set of n points in 5-dimensional space. The points are labeled from 1 to n. No two points coincide.
We will call point a bad if there are different points b and c, not equal to a, from the given set such that angle between vectors <image> and <image> is acute (i.e. strictly less than <image>). Otherwise, the point is called good.
The angle between vectors <image> and <image> in 5-dimensional space is defined as <image>, where <image> is the scalar product and <image> is length of <image>.
Given the list of points, print the indices of the good points in ascending order.
Input
The first line of input contains a single integer n (1 ≤ n ≤ 103) — the number of points.
The next n lines of input contain five integers ai, bi, ci, di, ei (|ai|, |bi|, |ci|, |di|, |ei| ≤ 103) — the coordinates of the i-th point. All points are distinct.
Output
First, print a single integer k — the number of good points.
Then, print k integers, each on their own line — the indices of the good points in ascending order.
Examples
Input
6
0 0 0 0 0
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
Output
1
1
Input
3
0 0 1 2 0
0 0 9 2 0
0 0 5 9 0
Output
0
Note
In the first sample, the first point forms exactly a <image> angle with all other pairs of points, so it is good.
In the second sample, along the cd plane, we can see the points look as follows:
<image>
We can see that all angles here are acute, so no points are good. | instruction | 0 | 3,032 | 23 | 6,064 |
Tags: brute force, geometry, math
Correct Solution:
```
# -*- coding: utf-8 -*-
import math
import collections
import bisect
import heapq
import time
import random
import itertools
"""
created by shhuan at 2017/10/19 17:35
"""
N = int(input())
P = []
for i in range(N):
P.append([int(x) for x in input().split()])
def length(a):
return math.sqrt(sum([x**2 for x in a]))
def angle(a, b):
c = sum(a[i]*b[i] for i in range(len(a)))
la = length(a)
lb = length(b)
if la and lb:
return math.acos(c/la/lb)
return -1
def cos(a, b, c):
x = [b[i]-a[i] for i in range(len(a))]
y = [c[i]-a[i] for i in range(len(a))]
c = sum(x[i] * y[i] for i in range(len(x)))
return c
ans = []
for i in range(N):
bad = False
for j in range(N):
if bad:
break
if i != j:
for k in range(N):
if k != i and k != j:
if cos(P[i], P[j], P[k]) > 0:
bad = True
break
if not bad:
ans.append(i+1)
print(len(ans))
if ans:
print(' '.join(map(str, ans)))
``` | output | 1 | 3,032 | 23 | 6,065 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given set of n points in 5-dimensional space. The points are labeled from 1 to n. No two points coincide.
We will call point a bad if there are different points b and c, not equal to a, from the given set such that angle between vectors <image> and <image> is acute (i.e. strictly less than <image>). Otherwise, the point is called good.
The angle between vectors <image> and <image> in 5-dimensional space is defined as <image>, where <image> is the scalar product and <image> is length of <image>.
Given the list of points, print the indices of the good points in ascending order.
Input
The first line of input contains a single integer n (1 ≤ n ≤ 103) — the number of points.
The next n lines of input contain five integers ai, bi, ci, di, ei (|ai|, |bi|, |ci|, |di|, |ei| ≤ 103) — the coordinates of the i-th point. All points are distinct.
Output
First, print a single integer k — the number of good points.
Then, print k integers, each on their own line — the indices of the good points in ascending order.
Examples
Input
6
0 0 0 0 0
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
Output
1
1
Input
3
0 0 1 2 0
0 0 9 2 0
0 0 5 9 0
Output
0
Note
In the first sample, the first point forms exactly a <image> angle with all other pairs of points, so it is good.
In the second sample, along the cd plane, we can see the points look as follows:
<image>
We can see that all angles here are acute, so no points are good. | instruction | 0 | 3,033 | 23 | 6,066 |
Tags: brute force, geometry, math
Correct Solution:
```
from math import *
n = int(input())
arr = [list(map(int, input().split())) for i in range(n)]
ans = [True] * n
for a in range(n):
for b in range(n):
bad = False
for c in range(n):
if arr[b] != arr[a] and arr[c] != arr[a] and arr[b] != arr[c]:
ab = [arr[b][i] - arr[a][i] for i in range(5)]
ac = [arr[c][i] - arr[a][i] for i in range(5)]
sc = sum([ab[i] * ac[i] for i in range(5)])
lab = (sum([ab[i] ** 2 for i in range(5)])) ** 0.5
lac = (sum([ac[i] ** 2 for i in range(5)])) ** 0.5
try:
if degrees(acos(sc / (lab * lac))) < 90:
ans[a] = False
bad = True
break
except:
pass
if bad:
break
print(sum(ans))
for i in range(n):
if ans[i]:
print(i + 1)
``` | output | 1 | 3,033 | 23 | 6,067 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given set of n points in 5-dimensional space. The points are labeled from 1 to n. No two points coincide.
We will call point a bad if there are different points b and c, not equal to a, from the given set such that angle between vectors <image> and <image> is acute (i.e. strictly less than <image>). Otherwise, the point is called good.
The angle between vectors <image> and <image> in 5-dimensional space is defined as <image>, where <image> is the scalar product and <image> is length of <image>.
Given the list of points, print the indices of the good points in ascending order.
Input
The first line of input contains a single integer n (1 ≤ n ≤ 103) — the number of points.
The next n lines of input contain five integers ai, bi, ci, di, ei (|ai|, |bi|, |ci|, |di|, |ei| ≤ 103) — the coordinates of the i-th point. All points are distinct.
Output
First, print a single integer k — the number of good points.
Then, print k integers, each on their own line — the indices of the good points in ascending order.
Examples
Input
6
0 0 0 0 0
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
Output
1
1
Input
3
0 0 1 2 0
0 0 9 2 0
0 0 5 9 0
Output
0
Note
In the first sample, the first point forms exactly a <image> angle with all other pairs of points, so it is good.
In the second sample, along the cd plane, we can see the points look as follows:
<image>
We can see that all angles here are acute, so no points are good. | instruction | 0 | 3,034 | 23 | 6,068 |
Tags: brute force, geometry, math
Correct Solution:
```
n = int(input())
dots = []
for i in range(n):
dots.append(list(map(int, input().split())))
good_dots = []
for a_index in range(n):
a_dot = dots[a_index]
a_is_good = True
for b_index in range(n):
for c_index in range(n):
if a_index != b_index and a_index != c_index and b_index < c_index:
b_dot = dots[b_index]
c_dot = dots[c_index]
ab_vec = [b_coord - a_coord for a_coord, b_coord in zip(a_dot, b_dot)]
ac_vec = [c_coord - a_coord for a_coord, c_coord in zip(a_dot, c_dot)]
# print(ab_vec)
# print(ac_vec)
prod = sum([a * b for a, b in zip(ab_vec, ac_vec)])
# print(prod)
if prod > 0:
a_is_good = False
break
if not a_is_good:
break
if a_is_good:
good_dots.append(a_index + 1)
print(len(good_dots))
for dot in good_dots:
print(dot)
``` | output | 1 | 3,034 | 23 | 6,069 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given set of n points in 5-dimensional space. The points are labeled from 1 to n. No two points coincide.
We will call point a bad if there are different points b and c, not equal to a, from the given set such that angle between vectors <image> and <image> is acute (i.e. strictly less than <image>). Otherwise, the point is called good.
The angle between vectors <image> and <image> in 5-dimensional space is defined as <image>, where <image> is the scalar product and <image> is length of <image>.
Given the list of points, print the indices of the good points in ascending order.
Input
The first line of input contains a single integer n (1 ≤ n ≤ 103) — the number of points.
The next n lines of input contain five integers ai, bi, ci, di, ei (|ai|, |bi|, |ci|, |di|, |ei| ≤ 103) — the coordinates of the i-th point. All points are distinct.
Output
First, print a single integer k — the number of good points.
Then, print k integers, each on their own line — the indices of the good points in ascending order.
Examples
Input
6
0 0 0 0 0
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
Output
1
1
Input
3
0 0 1 2 0
0 0 9 2 0
0 0 5 9 0
Output
0
Note
In the first sample, the first point forms exactly a <image> angle with all other pairs of points, so it is good.
In the second sample, along the cd plane, we can see the points look as follows:
<image>
We can see that all angles here are acute, so no points are good. | instruction | 0 | 3,035 | 23 | 6,070 |
Tags: brute force, geometry, math
Correct Solution:
```
import sys
from math import acos, sqrt, pi
n = int(input())
p = []
def get_angle(a, b, c):
v = [(b[i]-a[i], c[i]-a[i]) for i in range(5)]
sp = sum([v[i][0]*v[i][1] for i in range(5)])
sab = sqrt(sum([v[i][0]*v[i][0] for i in range(5)]))
sac = sqrt(sum([v[i][1]*v[i][1] for i in range(5)]))
if 2*acos(sp/(sab*sac))< pi:
return True
else:
return False
for i in range(n):
p.append(list(map(int, input().split())))
if n>38:
print('0')
sys.exit()
s = set()
t = [False]*n
for k in range(n):
if not t[k]:
for i in range(n):
if k != i:
for j in range(n):
if i != j and k != j:
if get_angle(p[k],p[i],p[j]):
s.add(k)
t[k] = True
if get_angle(p[i],p[k],p[j]):
s.add(i)
t[i] = True
if get_angle(p[j],p[k],p[i]):
s.add(j)
t[j] = True
if t[k]:
break
if t[k]:
break
t[k] = True
s = sorted(list(set([i for i in range(n)]) - s))
print(len(s))
[print(i+1) for i in s]
``` | output | 1 | 3,035 | 23 | 6,071 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given set of n points in 5-dimensional space. The points are labeled from 1 to n. No two points coincide.
We will call point a bad if there are different points b and c, not equal to a, from the given set such that angle between vectors <image> and <image> is acute (i.e. strictly less than <image>). Otherwise, the point is called good.
The angle between vectors <image> and <image> in 5-dimensional space is defined as <image>, where <image> is the scalar product and <image> is length of <image>.
Given the list of points, print the indices of the good points in ascending order.
Input
The first line of input contains a single integer n (1 ≤ n ≤ 103) — the number of points.
The next n lines of input contain five integers ai, bi, ci, di, ei (|ai|, |bi|, |ci|, |di|, |ei| ≤ 103) — the coordinates of the i-th point. All points are distinct.
Output
First, print a single integer k — the number of good points.
Then, print k integers, each on their own line — the indices of the good points in ascending order.
Examples
Input
6
0 0 0 0 0
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
Output
1
1
Input
3
0 0 1 2 0
0 0 9 2 0
0 0 5 9 0
Output
0
Note
In the first sample, the first point forms exactly a <image> angle with all other pairs of points, so it is good.
In the second sample, along the cd plane, we can see the points look as follows:
<image>
We can see that all angles here are acute, so no points are good. | instruction | 0 | 3,036 | 23 | 6,072 |
Tags: brute force, geometry, math
Correct Solution:
```
import math
def check(i, j, k):
a = []
b = []
for ii in range(len(i)):
a.append(j[ii] - i[ii])
b.append(k[ii] - i[ii])
la = 0
lb = 0
scal = 0
for ii in range(len(i)):
la += a[ii] * a[ii]
lb += b[ii] * b[ii]
scal += a[ii] * b[ii]
return (scal / (la * lb)) <= 0
def count(w):
ans = []
for i in range(len(w)):
f = 1
for j in w:
if (w[i] == j):
continue
for k in w:
if (w[i] == k or j == k):
continue
f &= check(w[i], j, k)
ans += [i + 1] if f else []
return [len(ans)] + ans
n = int(input())
w = []
for i in range(n):
w.append(list(map(int, input().split())))
if (n < 20):
print(*count(w), end='\n')
else:
print(0)
``` | output | 1 | 3,036 | 23 | 6,073 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given set of n points in 5-dimensional space. The points are labeled from 1 to n. No two points coincide.
We will call point a bad if there are different points b and c, not equal to a, from the given set such that angle between vectors <image> and <image> is acute (i.e. strictly less than <image>). Otherwise, the point is called good.
The angle between vectors <image> and <image> in 5-dimensional space is defined as <image>, where <image> is the scalar product and <image> is length of <image>.
Given the list of points, print the indices of the good points in ascending order.
Input
The first line of input contains a single integer n (1 ≤ n ≤ 103) — the number of points.
The next n lines of input contain five integers ai, bi, ci, di, ei (|ai|, |bi|, |ci|, |di|, |ei| ≤ 103) — the coordinates of the i-th point. All points are distinct.
Output
First, print a single integer k — the number of good points.
Then, print k integers, each on their own line — the indices of the good points in ascending order.
Examples
Input
6
0 0 0 0 0
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
Output
1
1
Input
3
0 0 1 2 0
0 0 9 2 0
0 0 5 9 0
Output
0
Note
In the first sample, the first point forms exactly a <image> angle with all other pairs of points, so it is good.
In the second sample, along the cd plane, we can see the points look as follows:
<image>
We can see that all angles here are acute, so no points are good. | instruction | 0 | 3,037 | 23 | 6,074 |
Tags: brute force, geometry, math
Correct Solution:
```
d = lambda i, j, k: sum((a - c) * (b - c) for a, b, c in zip(p[i], p[j], p[k])) * (i != j)
n = int(input())
r = range(n)
p = [list(map(int, input().split())) for i in r]
t = [k + 1 for k in r if all(d(i, j, k) <= 0 for i in r for j in r)] if n < 12 else []
for q in [len(t)] + t: print(q)
``` | output | 1 | 3,037 | 23 | 6,075 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You are given set of n points in 5-dimensional space. The points are labeled from 1 to n. No two points coincide.
We will call point a bad if there are different points b and c, not equal to a, from the given set such that angle between vectors <image> and <image> is acute (i.e. strictly less than <image>). Otherwise, the point is called good.
The angle between vectors <image> and <image> in 5-dimensional space is defined as <image>, where <image> is the scalar product and <image> is length of <image>.
Given the list of points, print the indices of the good points in ascending order.
Input
The first line of input contains a single integer n (1 ≤ n ≤ 103) — the number of points.
The next n lines of input contain five integers ai, bi, ci, di, ei (|ai|, |bi|, |ci|, |di|, |ei| ≤ 103) — the coordinates of the i-th point. All points are distinct.
Output
First, print a single integer k — the number of good points.
Then, print k integers, each on their own line — the indices of the good points in ascending order.
Examples
Input
6
0 0 0 0 0
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
Output
1
1
Input
3
0 0 1 2 0
0 0 9 2 0
0 0 5 9 0
Output
0
Note
In the first sample, the first point forms exactly a <image> angle with all other pairs of points, so it is good.
In the second sample, along the cd plane, we can see the points look as follows:
<image>
We can see that all angles here are acute, so no points are good. | instruction | 0 | 3,038 | 23 | 6,076 |
Tags: brute force, geometry, math
Correct Solution:
```
def sc(x, y):
return x[0] * y[0] + x[1] * y[1] + x[2] * y[2] + x[3] * y[3] + x[4] * y[4]
def check(a, b, c):
if sc([b[0] - a[0], b[1] - a[1], b[2] - a[2], b[3] - a[3], b[4] - a[4]], [c[0] - a[0], c[1] - a[1], c[2] - a[2], c[3] - a[3], c[4] - a[4]]) > 0:
return True
return False
n = int(input())
data = []
for i in range(n):
a = list(map(int, input().split()))
data.append(a)
an = 0
t = False
ann = []
for i in range(n):
for j in range(n):
if i == j:
continue
for k in range(n):
if k == i or k == j:
continue
if check(data[i], data[j], data[k]):
an += 1
t = True
break
if t:
break
if not t:
ann.append(i + 1)
t = False
print(n - an)
print(*ann)
``` | output | 1 | 3,038 | 23 | 6,077 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given set of n points in 5-dimensional space. The points are labeled from 1 to n. No two points coincide.
We will call point a bad if there are different points b and c, not equal to a, from the given set such that angle between vectors <image> and <image> is acute (i.e. strictly less than <image>). Otherwise, the point is called good.
The angle between vectors <image> and <image> in 5-dimensional space is defined as <image>, where <image> is the scalar product and <image> is length of <image>.
Given the list of points, print the indices of the good points in ascending order.
Input
The first line of input contains a single integer n (1 ≤ n ≤ 103) — the number of points.
The next n lines of input contain five integers ai, bi, ci, di, ei (|ai|, |bi|, |ci|, |di|, |ei| ≤ 103) — the coordinates of the i-th point. All points are distinct.
Output
First, print a single integer k — the number of good points.
Then, print k integers, each on their own line — the indices of the good points in ascending order.
Examples
Input
6
0 0 0 0 0
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
Output
1
1
Input
3
0 0 1 2 0
0 0 9 2 0
0 0 5 9 0
Output
0
Note
In the first sample, the first point forms exactly a <image> angle with all other pairs of points, so it is good.
In the second sample, along the cd plane, we can see the points look as follows:
<image>
We can see that all angles here are acute, so no points are good.
Submitted Solution:
```
def sc(i,j,k):
xx=0
for t in range(5):
xx+=(m[i][t]-m[j][t])*(m[i][t]-m[k][t])
return xx
n= int(input())
m=[]
mm=n
ans=[]
if (n > 36):
print("0")
else:
for i in range(n):
ans.append(1)
a,b,c,d,e=map(int,input().split())
m.append([a,b,c,d,e])
for i in range(n):
for j in range(n):
if (i != j):
for k in range(n):
if (i != k) and ( j != k):
if sc(i,j,k) >0:
ans[i]=-1
for i in range(n):
if ans[i]==-1:
mm+=-1
print(mm)
for i in range(n):
if ans[i]==1:
print(i+1)
``` | instruction | 0 | 3,039 | 23 | 6,078 |
Yes | output | 1 | 3,039 | 23 | 6,079 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given set of n points in 5-dimensional space. The points are labeled from 1 to n. No two points coincide.
We will call point a bad if there are different points b and c, not equal to a, from the given set such that angle between vectors <image> and <image> is acute (i.e. strictly less than <image>). Otherwise, the point is called good.
The angle between vectors <image> and <image> in 5-dimensional space is defined as <image>, where <image> is the scalar product and <image> is length of <image>.
Given the list of points, print the indices of the good points in ascending order.
Input
The first line of input contains a single integer n (1 ≤ n ≤ 103) — the number of points.
The next n lines of input contain five integers ai, bi, ci, di, ei (|ai|, |bi|, |ci|, |di|, |ei| ≤ 103) — the coordinates of the i-th point. All points are distinct.
Output
First, print a single integer k — the number of good points.
Then, print k integers, each on their own line — the indices of the good points in ascending order.
Examples
Input
6
0 0 0 0 0
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
Output
1
1
Input
3
0 0 1 2 0
0 0 9 2 0
0 0 5 9 0
Output
0
Note
In the first sample, the first point forms exactly a <image> angle with all other pairs of points, so it is good.
In the second sample, along the cd plane, we can see the points look as follows:
<image>
We can see that all angles here are acute, so no points are good.
Submitted Solution:
```
n = int(input())
points = []
for p in range(n):
points.append(list(map(int, input().split())))
def dot(x, y):
res = 0
for a, b in zip(x, y):
res += a*b
return res
def minus(x, y):
res = []
for a, b in zip(x, y):
res.append(a-b)
return res
indices = set(range(n))
if n <= 50:
for x in range(n):
for y in range(n):
if x != y:
for z in range(n):
if z != y and z != x:
if dot(minus(points[y], points[x]), minus(points[z], points[x])) > 0:
indices.discard(x)
# indices.discard(z)
print(len(indices))
for i in sorted(indices):
print(i+1)
else:
print(0)
``` | instruction | 0 | 3,040 | 23 | 6,080 |
Yes | output | 1 | 3,040 | 23 | 6,081 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given set of n points in 5-dimensional space. The points are labeled from 1 to n. No two points coincide.
We will call point a bad if there are different points b and c, not equal to a, from the given set such that angle between vectors <image> and <image> is acute (i.e. strictly less than <image>). Otherwise, the point is called good.
The angle between vectors <image> and <image> in 5-dimensional space is defined as <image>, where <image> is the scalar product and <image> is length of <image>.
Given the list of points, print the indices of the good points in ascending order.
Input
The first line of input contains a single integer n (1 ≤ n ≤ 103) — the number of points.
The next n lines of input contain five integers ai, bi, ci, di, ei (|ai|, |bi|, |ci|, |di|, |ei| ≤ 103) — the coordinates of the i-th point. All points are distinct.
Output
First, print a single integer k — the number of good points.
Then, print k integers, each on their own line — the indices of the good points in ascending order.
Examples
Input
6
0 0 0 0 0
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
Output
1
1
Input
3
0 0 1 2 0
0 0 9 2 0
0 0 5 9 0
Output
0
Note
In the first sample, the first point forms exactly a <image> angle with all other pairs of points, so it is good.
In the second sample, along the cd plane, we can see the points look as follows:
<image>
We can see that all angles here are acute, so no points are good.
Submitted Solution:
```
# ------------------- fast io --------------------
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# ------------------- fast io --------------------
from math import gcd, ceil
def prod(a, mod=10**9+7):
ans = 1
for each in a:
ans = (ans * each) % mod
return ans
def lcm(a, b): return a * b // gcd(a, b)
def binary(x, length=16):
y = bin(x)[2:]
return y if len(y) >= length else "0" * (length - len(y)) + y
for _ in range(int(input()) if not True else 1):
n = int(input())
#a, b = map(int, input().split())
#c, d = map(int, input().split())
#a = list(map(int, input().split()))
#b = list(map(int, input().split()))
#s = input()
from math import acos,pi,sqrt
points = []
for i in range(n):
points += [list(map(int, input().split()))]
if n <= 69:
ans = []
for i in range(n):
pos=True
for j in range(n):
for k in range(n):
if j==i or k==i or j==k:continue
ab = [points[i][x] - points[j][x] for x in range(5)]
ac = [points[i][x] - points[k][x] for x in range(5)]
xy = sum([ab[x]*ac[x] for x in range(5)])
m = sqrt(sum(x**2 for x in ab)) * sqrt(sum(x**2 for x in ac))
angle = acos(xy/m)*180/pi
if angle < 90:
pos=False
break
if not pos:break
if pos:ans+=[i+1]
print(len(ans))
print(*ans)
else:
print(0)
``` | instruction | 0 | 3,041 | 23 | 6,082 |
Yes | output | 1 | 3,041 | 23 | 6,083 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given set of n points in 5-dimensional space. The points are labeled from 1 to n. No two points coincide.
We will call point a bad if there are different points b and c, not equal to a, from the given set such that angle between vectors <image> and <image> is acute (i.e. strictly less than <image>). Otherwise, the point is called good.
The angle between vectors <image> and <image> in 5-dimensional space is defined as <image>, where <image> is the scalar product and <image> is length of <image>.
Given the list of points, print the indices of the good points in ascending order.
Input
The first line of input contains a single integer n (1 ≤ n ≤ 103) — the number of points.
The next n lines of input contain five integers ai, bi, ci, di, ei (|ai|, |bi|, |ci|, |di|, |ei| ≤ 103) — the coordinates of the i-th point. All points are distinct.
Output
First, print a single integer k — the number of good points.
Then, print k integers, each on their own line — the indices of the good points in ascending order.
Examples
Input
6
0 0 0 0 0
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
Output
1
1
Input
3
0 0 1 2 0
0 0 9 2 0
0 0 5 9 0
Output
0
Note
In the first sample, the first point forms exactly a <image> angle with all other pairs of points, so it is good.
In the second sample, along the cd plane, we can see the points look as follows:
<image>
We can see that all angles here are acute, so no points are good.
Submitted Solution:
```
d = lambda i, j, k: not j - i < 0 < sum((a - c) * (b - c) for a, b, c in zip(p[i], p[j], p[k]))
n = int(input())
r = range(n)
p = [list(map(int, input().split())) for i in r]
t = [k + 1 for k in r if all(d(i, j, k) for i in r for j in r)] if n < 12 else []
for q in [len(t)] + t: print(q)
``` | instruction | 0 | 3,042 | 23 | 6,084 |
Yes | output | 1 | 3,042 | 23 | 6,085 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given set of n points in 5-dimensional space. The points are labeled from 1 to n. No two points coincide.
We will call point a bad if there are different points b and c, not equal to a, from the given set such that angle between vectors <image> and <image> is acute (i.e. strictly less than <image>). Otherwise, the point is called good.
The angle between vectors <image> and <image> in 5-dimensional space is defined as <image>, where <image> is the scalar product and <image> is length of <image>.
Given the list of points, print the indices of the good points in ascending order.
Input
The first line of input contains a single integer n (1 ≤ n ≤ 103) — the number of points.
The next n lines of input contain five integers ai, bi, ci, di, ei (|ai|, |bi|, |ci|, |di|, |ei| ≤ 103) — the coordinates of the i-th point. All points are distinct.
Output
First, print a single integer k — the number of good points.
Then, print k integers, each on their own line — the indices of the good points in ascending order.
Examples
Input
6
0 0 0 0 0
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
Output
1
1
Input
3
0 0 1 2 0
0 0 9 2 0
0 0 5 9 0
Output
0
Note
In the first sample, the first point forms exactly a <image> angle with all other pairs of points, so it is good.
In the second sample, along the cd plane, we can see the points look as follows:
<image>
We can see that all angles here are acute, so no points are good.
Submitted Solution:
```
from math import *
n = int(input())
arr = [list(map(int, input().split())) for i in range(n)]
ans = []
a = 0
for a in range(n):
for b in range(n):
bad = False
for c in range(n):
if arr[b] != arr[a] and arr[c] != arr[a]:
ab = [arr[b][i] - arr[a][i] for i in range(5)]
ac = [arr[c][i] - arr[a][i] for i in range(5)]
sc = sum([ab[i] * ac[i] for i in range(5)])
lab = (sum([ab[i] ** 2 for i in range(5)])) ** 0.5
lac = (sum([ac[i] ** 2 for i in range(5)])) ** 0.5
try:
if acos(sc / (lab * lac)) * 180 / pi >= 90:
ans.append(a + 1)
bad = True
break
except ValueError:
pass
if bad:
break
print(len(ans))
if ans:
print('\n'.join(map(str, ans)))
``` | instruction | 0 | 3,043 | 23 | 6,086 |
No | output | 1 | 3,043 | 23 | 6,087 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given set of n points in 5-dimensional space. The points are labeled from 1 to n. No two points coincide.
We will call point a bad if there are different points b and c, not equal to a, from the given set such that angle between vectors <image> and <image> is acute (i.e. strictly less than <image>). Otherwise, the point is called good.
The angle between vectors <image> and <image> in 5-dimensional space is defined as <image>, where <image> is the scalar product and <image> is length of <image>.
Given the list of points, print the indices of the good points in ascending order.
Input
The first line of input contains a single integer n (1 ≤ n ≤ 103) — the number of points.
The next n lines of input contain five integers ai, bi, ci, di, ei (|ai|, |bi|, |ci|, |di|, |ei| ≤ 103) — the coordinates of the i-th point. All points are distinct.
Output
First, print a single integer k — the number of good points.
Then, print k integers, each on their own line — the indices of the good points in ascending order.
Examples
Input
6
0 0 0 0 0
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
Output
1
1
Input
3
0 0 1 2 0
0 0 9 2 0
0 0 5 9 0
Output
0
Note
In the first sample, the first point forms exactly a <image> angle with all other pairs of points, so it is good.
In the second sample, along the cd plane, we can see the points look as follows:
<image>
We can see that all angles here are acute, so no points are good.
Submitted Solution:
```
from math import *
n = int(input())
arr = [list(map(int, input().split())) for i in range(n)]
ans = []
a = 0
for a in range(n):
for b in range(n):
bad = False
for c in range(n):
if arr[b] != arr[a] and arr[c] != arr[a]:
ab = [arr[b][i] - arr[a][i] for i in range(5)]
ac = [arr[c][i] - arr[a][i] for i in range(5)]
sc = sum([ab[i] * ac[i] for i in range(5)])
lab = (sum([ab[i] ** 2 for i in range(5)])) ** 0.5
lac = (sum([ac[i] ** 2 for i in range(5)])) ** 0.5
try:
if degrees(acos(sc / (lab * lac))) >= 90:
ans.append(a + 1)
bad = True
break
except ValueError:
pass
if bad:
break
print(len(ans))
if ans:
print('\n'.join(map(str, sorted(ans))))
``` | instruction | 0 | 3,044 | 23 | 6,088 |
No | output | 1 | 3,044 | 23 | 6,089 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given set of n points in 5-dimensional space. The points are labeled from 1 to n. No two points coincide.
We will call point a bad if there are different points b and c, not equal to a, from the given set such that angle between vectors <image> and <image> is acute (i.e. strictly less than <image>). Otherwise, the point is called good.
The angle between vectors <image> and <image> in 5-dimensional space is defined as <image>, where <image> is the scalar product and <image> is length of <image>.
Given the list of points, print the indices of the good points in ascending order.
Input
The first line of input contains a single integer n (1 ≤ n ≤ 103) — the number of points.
The next n lines of input contain five integers ai, bi, ci, di, ei (|ai|, |bi|, |ci|, |di|, |ei| ≤ 103) — the coordinates of the i-th point. All points are distinct.
Output
First, print a single integer k — the number of good points.
Then, print k integers, each on their own line — the indices of the good points in ascending order.
Examples
Input
6
0 0 0 0 0
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
Output
1
1
Input
3
0 0 1 2 0
0 0 9 2 0
0 0 5 9 0
Output
0
Note
In the first sample, the first point forms exactly a <image> angle with all other pairs of points, so it is good.
In the second sample, along the cd plane, we can see the points look as follows:
<image>
We can see that all angles here are acute, so no points are good.
Submitted Solution:
```
def check(coor1, coor2, coor3):
v1 = [coor2[i] - coor1[i] for i in range(5)]
v2 = [coor3[i] - coor1[i] for i in range(5)]
# print(v1, v2)
return scalar_product(v1, v2)
def scalar_product(coor1, coor2):
a1, a2, a3, a4, a5 = coor1
b1, b2, b3, b4, b5 = coor2
return (a1 * b1 + a2 * b2 + a3 * b3 + a4 * b4 + a5 * b5)
n = int(input())
idx___coor = []
for idx in range(n):
coor = [int(x) for x in input().split()]
idx___coor.append(coor)
if n > 256:
print(0)
else:
good_idxes = []
for idx1, coor1 in enumerate(idx___coor):
is_ok_flag = True
pairs_checked = 0
for idx2, coor2 in enumerate(idx___coor):
for idx3, coor3 in enumerate(idx___coor):
pairs_checked += 1
# print(pairs_checked)
if idx2 == idx3 or idx1 == idx3 or idx1 == idx2 or not is_ok_flag:
continue
if check(coor1, coor2, coor3) > 0:
is_ok_flag = False
# print(pairs_checked, is_ok_flag, idx1, idx2, idx3)
if pairs_checked >= (n * n - 1) and is_ok_flag:
good_idxes.append(idx1)
good_idxes = sorted(good_idxes)
good_idxes = [(x + 1) for x in good_idxes]
print(len(good_idxes))
print(*good_idxes)
``` | instruction | 0 | 3,045 | 23 | 6,090 |
No | output | 1 | 3,045 | 23 | 6,091 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are given set of n points in 5-dimensional space. The points are labeled from 1 to n. No two points coincide.
We will call point a bad if there are different points b and c, not equal to a, from the given set such that angle between vectors <image> and <image> is acute (i.e. strictly less than <image>). Otherwise, the point is called good.
The angle between vectors <image> and <image> in 5-dimensional space is defined as <image>, where <image> is the scalar product and <image> is length of <image>.
Given the list of points, print the indices of the good points in ascending order.
Input
The first line of input contains a single integer n (1 ≤ n ≤ 103) — the number of points.
The next n lines of input contain five integers ai, bi, ci, di, ei (|ai|, |bi|, |ci|, |di|, |ei| ≤ 103) — the coordinates of the i-th point. All points are distinct.
Output
First, print a single integer k — the number of good points.
Then, print k integers, each on their own line — the indices of the good points in ascending order.
Examples
Input
6
0 0 0 0 0
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
Output
1
1
Input
3
0 0 1 2 0
0 0 9 2 0
0 0 5 9 0
Output
0
Note
In the first sample, the first point forms exactly a <image> angle with all other pairs of points, so it is good.
In the second sample, along the cd plane, we can see the points look as follows:
<image>
We can see that all angles here are acute, so no points are good.
Submitted Solution:
```
d = lambda i, j, k: sum((a - c) * (b - c) for a, b, c in zip(p[i], p[j], p[k])) * (i != j)
n = int(input())
r = range(n)
p = [list(map(int, input().split())) for i in r]
t = [k + 1 for k in r if all(d(i, j, k) <= 0 for i in r for j in r)] if n < 11 else []
for q in [len(t)] + t: print(q)
``` | instruction | 0 | 3,046 | 23 | 6,092 |
No | output | 1 | 3,046 | 23 | 6,093 |
Provide a correct Python 3 solution for this coding contest problem.
Relative B came to Mr. A's house. He is 3 years old and loves blocks. The block he has is shaped like Figure 1.
<image>
Figure 1
Mr. B is laying blocks on the board. When I asked him, "What are you making?", He replied cheerfully, "Maze !!". The maze he says is the arrangement of blocks that are in contact with each other from the start to the goal and can be traced only by blocks of the same color. Figure 2 shows that the yellow block creates a maze from the upper left (start) to the lower right (goal).
<image>
Figure 2
With Mr. B playing innocently, you, the programmer, decided to see if the blocks were arranged in a maze.
Create a program that inputs the block information, start, and goal coordinates, and outputs OK if the block is a maze, and NG if it is not. The board has the size of w in the horizontal direction and h in the vertical direction, and the upper left coordinate is (1, 1) and the lower right coordinate is (w, h). The blocks are 2x4 rectangles, all the same size. The block color c can be 1 (white), 2 (yellow), 3 (green), 4 (blue), or 5 (red). The orientation d of the block on the board is 0 if it is long horizontally and 1 if it is long vertically. The position of the block is represented by the coordinates (x, y) at the top left of the block. The position of the block does not overlap with other blocks and does not protrude from the board.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
w h
xs ys ys
xg yg
n
c1 d1 x1 y1
c2 d2 x2 y2
::
cn dn xn yn
The board size w, h (4 ≤ w, h ≤ 100) is given on the first line. The second line gives the start coordinates xs, ys, and the third line gives the goal coordinates xg, yg.
The number of blocks n is given on the 4th line. The next n lines are given the color ci, orientation di, position xi, yi of the i-th block.
The number of datasets does not exceed 30.
Output
The discrimination result is output to one line for each input data set.
Example
Input
20 20
1 1
9 9
7
2 0 1 1
5 1 1 3
2 1 3 3
1 1 5 2
5 1 7 3
2 0 2 7
2 0 6 8
20 20
9 9
1 1
6
2 0 1 1
1 0 5 1
2 1 1 3
5 0 1 7
3 1 5 5
4 1 8 5
0 0
Output
OK
NG | instruction | 0 | 3,239 | 23 | 6,478 |
"Correct Solution:
```
# Aizu Problem 0207: Block
import sys, math, os
# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
sys.stdin = open("sample-input.txt", "rt")
def build_grid(W, H, N, blocks):
grid = [[0 for _ in range(W)] for __ in range(H)]
for c, d, x, y in blocks:
x, y = x - 1, y - 1
if d == 0:
# horizontal long:
dx, dy = 4, 2
else:
# vertical long:
dx, dy = 2, 4
for xi in range(x, x + dx):
for yi in range(y, y + dy):
grid[yi][xi] = c
return grid
def bfs(W, H, grid, xs, ys, xg, yg):
delta = [[0, 1], [0, -1], [1, 0], [-1, 0]]
color = grid[ys][xs]
if color == 0:
return "NG"
visited = set()
queue = [(xs, ys)]
while len(queue) > 0:
current = queue.pop(0)
visited.add(current)
col, row = current
for drow, dcol in delta:
row2, col2 = row + drow, col + dcol
if 0 <= row2 < H and 0 <= col2 < W and grid[row2][col2] == color:
# neighboring piece has same color:
neighbor = (col2, row2)
if neighbor == (xg, yg):
return "OK"
if neighbor not in queue and neighbor not in visited:
queue.append(neighbor)
return "NG"
while True:
W, H = [int(_) for _ in input().split()]
if W == H == 0:
break
xs, ys = [int(_) - 1 for _ in input().split()]
xg, yg = [int(_) - 1 for _ in input().split()]
N = int(input())
blocks = [[int(_) for _ in input().split()] for __ in range(N)]
grid = build_grid(W, H, N, blocks)
print(bfs(W, H, grid, xs, ys, xg, yg))
#for row in grid:
# print(''.join([str(r) for r in row]))
#print(color)
``` | output | 1 | 3,239 | 23 | 6,479 |
Provide a correct Python 3 solution for this coding contest problem.
Relative B came to Mr. A's house. He is 3 years old and loves blocks. The block he has is shaped like Figure 1.
<image>
Figure 1
Mr. B is laying blocks on the board. When I asked him, "What are you making?", He replied cheerfully, "Maze !!". The maze he says is the arrangement of blocks that are in contact with each other from the start to the goal and can be traced only by blocks of the same color. Figure 2 shows that the yellow block creates a maze from the upper left (start) to the lower right (goal).
<image>
Figure 2
With Mr. B playing innocently, you, the programmer, decided to see if the blocks were arranged in a maze.
Create a program that inputs the block information, start, and goal coordinates, and outputs OK if the block is a maze, and NG if it is not. The board has the size of w in the horizontal direction and h in the vertical direction, and the upper left coordinate is (1, 1) and the lower right coordinate is (w, h). The blocks are 2x4 rectangles, all the same size. The block color c can be 1 (white), 2 (yellow), 3 (green), 4 (blue), or 5 (red). The orientation d of the block on the board is 0 if it is long horizontally and 1 if it is long vertically. The position of the block is represented by the coordinates (x, y) at the top left of the block. The position of the block does not overlap with other blocks and does not protrude from the board.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
w h
xs ys ys
xg yg
n
c1 d1 x1 y1
c2 d2 x2 y2
::
cn dn xn yn
The board size w, h (4 ≤ w, h ≤ 100) is given on the first line. The second line gives the start coordinates xs, ys, and the third line gives the goal coordinates xg, yg.
The number of blocks n is given on the 4th line. The next n lines are given the color ci, orientation di, position xi, yi of the i-th block.
The number of datasets does not exceed 30.
Output
The discrimination result is output to one line for each input data set.
Example
Input
20 20
1 1
9 9
7
2 0 1 1
5 1 1 3
2 1 3 3
1 1 5 2
5 1 7 3
2 0 2 7
2 0 6 8
20 20
9 9
1 1
6
2 0 1 1
1 0 5 1
2 1 1 3
5 0 1 7
3 1 5 5
4 1 8 5
0 0
Output
OK
NG | instruction | 0 | 3,240 | 23 | 6,480 |
"Correct Solution:
```
# ref: https://qiita.com/masashi127/items/0c794e28f4b295ad82c6
import heapq
import itertools
def astar(init_pos, goal,dg):
passed_list = [init_pos]
init_score = distance(passed_list) + heuristic(init_pos,goal)
checked = {init_pos: init_score}
searching_heap = []
heapq.heappush(searching_heap, (init_score, passed_list))
while len(searching_heap) > 0:
score, passed_list = heapq.heappop(searching_heap)
last_passed_pos = passed_list[-1]
if last_passed_pos == goal:
return passed_list
for pos in nexts(dg,last_passed_pos):
new_passed_list = passed_list + [pos]
pos_score = distance(new_passed_list) + heuristic(pos,goal)
if pos in checked and checked[pos] <= pos_score:
continue
checked[pos] = pos_score
heapq.heappush(searching_heap, (pos_score, new_passed_list))
return []
def solve_dungeon(dungeon):
init = find_ch(dungeon,"S")
goal = find_ch(dungeon,"G")
path = astar(init, goal,dungeon)
if len(path) > 0:
return True
else:
return False
def find_ch(dg,ch):
for i, l in enumerate(dg):
for j, c in enumerate(l):
if c == ch:
return (i, j)
def nexts(dg,pos):
wall = "x"
for a, b in [[' + 1',''], [' - 1',''], ['',' + 1'], ['',' - 1']]:
if a or b:
if dg[eval('pos[0]' + a)][eval('pos[1]' + b)] != wall:
yield (eval('pos[0]' + a), eval('pos[1]' + b))
def heuristic(pos,goal):
return ((pos[0] - goal[0]) ** 2 + (pos[1] - goal[1]) ** 2) ** 0.5
def distance(path):
return len(path)
def render_path(dg,path):
buf = [[ch for ch in l] for l in dg]
for pos in path[1:-1]:
buf[pos[0]][pos[1]] = "*"
buf[path[0][0]][path[0][1]] = "s"
buf[path[-1][0]][path[-1][1]] = "g"
return ["".join(l) for l in buf]
if __name__ == "__main__":
while(True):
w,h = map(int,input().split())
if w == 0 and h == 0:
break
xs,ys = map(int,input().split())
xg,yg = map(int,input().split())
n = int(input())
d = [ list(map(int,input().split())) for _ in range(n)]
m = [[0]*w for _ in range(h)]
for e in d:
for r in m[e[3]-1:e[3]+2*(e[1]+1)-1]:
r[e[2]-1:e[2]+2*(2-e[1])-1] = [e[0]]*2*(2-e[1])
ans = False
for col in range(1,6):
txt = [''.join(str(' ' if e==col else 'x') for e in f) for f in m]
if txt[ys-1][xs-1] == "x" or txt[yg-1][xg-1] == "x":
continue
txt[ys-1] = txt[ys-1][:xs-1] + 'S' + txt[ys-1][xs:]
txt[yg-1] = txt[yg-1][:xg-1] + 'G' + txt[yg-1][xg:]
for i in range(len(txt)):
txt[i] = 'x' + txt[i] + 'x'
txt.insert(0,'x'*(w+2))
txt.append('x'*(w+2))
ans |= solve_dungeon(txt)
if ans:
print("OK")
break
if not ans: print("NG")
``` | output | 1 | 3,240 | 23 | 6,481 |
Provide a correct Python 3 solution for this coding contest problem.
Relative B came to Mr. A's house. He is 3 years old and loves blocks. The block he has is shaped like Figure 1.
<image>
Figure 1
Mr. B is laying blocks on the board. When I asked him, "What are you making?", He replied cheerfully, "Maze !!". The maze he says is the arrangement of blocks that are in contact with each other from the start to the goal and can be traced only by blocks of the same color. Figure 2 shows that the yellow block creates a maze from the upper left (start) to the lower right (goal).
<image>
Figure 2
With Mr. B playing innocently, you, the programmer, decided to see if the blocks were arranged in a maze.
Create a program that inputs the block information, start, and goal coordinates, and outputs OK if the block is a maze, and NG if it is not. The board has the size of w in the horizontal direction and h in the vertical direction, and the upper left coordinate is (1, 1) and the lower right coordinate is (w, h). The blocks are 2x4 rectangles, all the same size. The block color c can be 1 (white), 2 (yellow), 3 (green), 4 (blue), or 5 (red). The orientation d of the block on the board is 0 if it is long horizontally and 1 if it is long vertically. The position of the block is represented by the coordinates (x, y) at the top left of the block. The position of the block does not overlap with other blocks and does not protrude from the board.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
w h
xs ys ys
xg yg
n
c1 d1 x1 y1
c2 d2 x2 y2
::
cn dn xn yn
The board size w, h (4 ≤ w, h ≤ 100) is given on the first line. The second line gives the start coordinates xs, ys, and the third line gives the goal coordinates xg, yg.
The number of blocks n is given on the 4th line. The next n lines are given the color ci, orientation di, position xi, yi of the i-th block.
The number of datasets does not exceed 30.
Output
The discrimination result is output to one line for each input data set.
Example
Input
20 20
1 1
9 9
7
2 0 1 1
5 1 1 3
2 1 3 3
1 1 5 2
5 1 7 3
2 0 2 7
2 0 6 8
20 20
9 9
1 1
6
2 0 1 1
1 0 5 1
2 1 1 3
5 0 1 7
3 1 5 5
4 1 8 5
0 0
Output
OK
NG | instruction | 0 | 3,241 | 23 | 6,482 |
"Correct Solution:
```
while True:
w,h=map(int, input().split())
if w==h==0:
break
xs,ys=map(int, input().split())
xg,yg=map(int, input().split())
xs-=1
ys-=1
xg-=1
yg-=1
n=int(input())
dataset=[]
for _ in range(n):
dataset.append(list(map(int,input().split())))
maze=[ [0 for i in range(w) ] for j in range(h)]
for data in dataset:
color=data[0]
drc=data[1]
x=data[2]-1
y=data[3]-1
x_list=[x,x+1] if drc==1 else [x,x+1,x+2,x+3]
y_list=[y,y+1,y+2,y+3] if drc==1 else [y,y+1]
for dy in y_list:
for dx in x_list:
maze[dy][dx]=color
history=[[-1 for i in range(w)] for j in range(h)]
ws=[]
dx_list=[0,0,1,-1]
dy_list=[1,-1,0,0]
flag=False
if maze[ys][xs]!=0:
ws.append([xs,ys])
while len(ws)!=0:
current=ws.pop(0)
current_color=maze[current[1]][current[0]]
if current[0]==xg and current[1]==yg:
flag=True
break
for dx,dy in zip(dx_list,dy_list):
next_x=current[0]+dx
next_y=current[1]+dy
if next_x>=0 and next_x<w and next_y>=0 and next_y<h and maze[next_y][next_x]==current_color and history[next_y][next_x]==-1:
history[next_y][next_x]=0
ws.append([next_x,next_y])
if flag:
print('OK')
else:
print('NG')
``` | output | 1 | 3,241 | 23 | 6,483 |
Provide a correct Python 3 solution for this coding contest problem.
Relative B came to Mr. A's house. He is 3 years old and loves blocks. The block he has is shaped like Figure 1.
<image>
Figure 1
Mr. B is laying blocks on the board. When I asked him, "What are you making?", He replied cheerfully, "Maze !!". The maze he says is the arrangement of blocks that are in contact with each other from the start to the goal and can be traced only by blocks of the same color. Figure 2 shows that the yellow block creates a maze from the upper left (start) to the lower right (goal).
<image>
Figure 2
With Mr. B playing innocently, you, the programmer, decided to see if the blocks were arranged in a maze.
Create a program that inputs the block information, start, and goal coordinates, and outputs OK if the block is a maze, and NG if it is not. The board has the size of w in the horizontal direction and h in the vertical direction, and the upper left coordinate is (1, 1) and the lower right coordinate is (w, h). The blocks are 2x4 rectangles, all the same size. The block color c can be 1 (white), 2 (yellow), 3 (green), 4 (blue), or 5 (red). The orientation d of the block on the board is 0 if it is long horizontally and 1 if it is long vertically. The position of the block is represented by the coordinates (x, y) at the top left of the block. The position of the block does not overlap with other blocks and does not protrude from the board.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
w h
xs ys ys
xg yg
n
c1 d1 x1 y1
c2 d2 x2 y2
::
cn dn xn yn
The board size w, h (4 ≤ w, h ≤ 100) is given on the first line. The second line gives the start coordinates xs, ys, and the third line gives the goal coordinates xg, yg.
The number of blocks n is given on the 4th line. The next n lines are given the color ci, orientation di, position xi, yi of the i-th block.
The number of datasets does not exceed 30.
Output
The discrimination result is output to one line for each input data set.
Example
Input
20 20
1 1
9 9
7
2 0 1 1
5 1 1 3
2 1 3 3
1 1 5 2
5 1 7 3
2 0 2 7
2 0 6 8
20 20
9 9
1 1
6
2 0 1 1
1 0 5 1
2 1 1 3
5 0 1 7
3 1 5 5
4 1 8 5
0 0
Output
OK
NG | instruction | 0 | 3,242 | 23 | 6,484 |
"Correct Solution:
```
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0207
"""
import sys
from sys import stdin
from collections import deque
input = stdin.readline
def solve(w, h, xs, ys, xg, yg, blocks):
# ????????????????????±????????°???????????????l
field = [[0] * (w+1) for _ in range(h+1)]
for c, d, x, y in blocks:
field[y][x] = c
field[y][x+1] = c
field[y+1][x] = c
field[y+1][x+1] = c
if d == 0: # ?¨????
field[y][x+2] = c
field[y][x+3] = c
field[y+1][x+2] = c
field[y+1][x+3] = c
else: # ??????
field[y+2][x] = c
field[y+3][x] = c
field[y+2][x+1] = c
field[y+3][x+1] = c
# ????????????????§????????????´????????§?¨??????????
dy = [-1, 1, 0, 0]
dx = [0, 0, -1, 1]
d = [[-1] * (w+1) for _ in range(h+1)] # ?????¢?´¢?????°??¨???????????¨??????-1???
Q = deque()
Q.append((xs, ys))
d[ys][xs] = 0
color = field[ys][xs] # ?????????????????¨?????§?????????????????????????????°?????¨???????????????
while Q:
xc, yc = Q.popleft() # ?????¨??°?????§?¨?
if xc == xg and yc == yg:
return 'OK'
for i in range(len(dx)):
xn = xc + dx[i]
yn = yc + dy[i]
# ?§????????????§???????????? ?????? ??¢?????¢?´¢?????§???????????°??????????????§?????¢?´¢????¶???????
if 0 < xn <= w and 0 < yn <= h and d[yn][xn] == -1 and field[yn][xn] == color:
Q.append((xn, yn))
d[yn][xn] = d[yc][xc] + 1
return 'NG'
def main(args):
while True:
w, h = map(int, input().split())
if w == 0 and h == 0:
break
xs, ys = map(int, input().split())
xg, yg = map(int, input().split())
n = int(input())
blocks = [list(map(int, input().split())) for _ in range(n)]
result = solve(w, h, xs, ys, xg, yg, blocks)
print(result)
if __name__ == '__main__':
main(sys.argv[1:])
``` | output | 1 | 3,242 | 23 | 6,485 |
Provide a correct Python 3 solution for this coding contest problem.
Relative B came to Mr. A's house. He is 3 years old and loves blocks. The block he has is shaped like Figure 1.
<image>
Figure 1
Mr. B is laying blocks on the board. When I asked him, "What are you making?", He replied cheerfully, "Maze !!". The maze he says is the arrangement of blocks that are in contact with each other from the start to the goal and can be traced only by blocks of the same color. Figure 2 shows that the yellow block creates a maze from the upper left (start) to the lower right (goal).
<image>
Figure 2
With Mr. B playing innocently, you, the programmer, decided to see if the blocks were arranged in a maze.
Create a program that inputs the block information, start, and goal coordinates, and outputs OK if the block is a maze, and NG if it is not. The board has the size of w in the horizontal direction and h in the vertical direction, and the upper left coordinate is (1, 1) and the lower right coordinate is (w, h). The blocks are 2x4 rectangles, all the same size. The block color c can be 1 (white), 2 (yellow), 3 (green), 4 (blue), or 5 (red). The orientation d of the block on the board is 0 if it is long horizontally and 1 if it is long vertically. The position of the block is represented by the coordinates (x, y) at the top left of the block. The position of the block does not overlap with other blocks and does not protrude from the board.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
w h
xs ys ys
xg yg
n
c1 d1 x1 y1
c2 d2 x2 y2
::
cn dn xn yn
The board size w, h (4 ≤ w, h ≤ 100) is given on the first line. The second line gives the start coordinates xs, ys, and the third line gives the goal coordinates xg, yg.
The number of blocks n is given on the 4th line. The next n lines are given the color ci, orientation di, position xi, yi of the i-th block.
The number of datasets does not exceed 30.
Output
The discrimination result is output to one line for each input data set.
Example
Input
20 20
1 1
9 9
7
2 0 1 1
5 1 1 3
2 1 3 3
1 1 5 2
5 1 7 3
2 0 2 7
2 0 6 8
20 20
9 9
1 1
6
2 0 1 1
1 0 5 1
2 1 1 3
5 0 1 7
3 1 5 5
4 1 8 5
0 0
Output
OK
NG | instruction | 0 | 3,243 | 23 | 6,486 |
"Correct Solution:
```
from collections import deque
def search(xs, ys, xg, yg, board):
direct = ((-1, 0), (1, 0), (0, -1), (0, 1))
visited = [[False] * (w + 2) for _ in range(h + 2)]
visited[ys][xs] = True
que = deque()
que.append((xs, ys))
while que:
x, y = que.popleft()
for dx, dy in direct:
nx, ny = x + dx, y + dy
if (nx, ny) == (xg, yg):
print("OK")
return
if not visited[ny][nx]:
visited[ny][nx] = True
if board[ny][nx] == color:
que.append((nx, ny))
else:
print("NG")
while True:
w, h = map(int, input().split())
if w == 0:
break
xs, ys = map(int, input().split())
xg, yg = map(int, input().split())
board = [[0] * (w + 2) for _ in range(h + 2)]
n = int(input())
for _ in range(n):
c, d, x, y = map(int, input().split())
if d == 0:
for xi in range(x, x + 4):
for yi in range(y, y + 2):
board[yi][xi] = c
else:
for xi in range(x, x + 2):
for yi in range(y, y + 4):
board[yi][xi] = c
color = board[ys][xs]
if color == 0 or board[yg][xg] != color:
print("NG")
continue
search(xs, ys, xg, yg, board)
``` | output | 1 | 3,243 | 23 | 6,487 |
Provide a correct Python 3 solution for this coding contest problem.
Relative B came to Mr. A's house. He is 3 years old and loves blocks. The block he has is shaped like Figure 1.
<image>
Figure 1
Mr. B is laying blocks on the board. When I asked him, "What are you making?", He replied cheerfully, "Maze !!". The maze he says is the arrangement of blocks that are in contact with each other from the start to the goal and can be traced only by blocks of the same color. Figure 2 shows that the yellow block creates a maze from the upper left (start) to the lower right (goal).
<image>
Figure 2
With Mr. B playing innocently, you, the programmer, decided to see if the blocks were arranged in a maze.
Create a program that inputs the block information, start, and goal coordinates, and outputs OK if the block is a maze, and NG if it is not. The board has the size of w in the horizontal direction and h in the vertical direction, and the upper left coordinate is (1, 1) and the lower right coordinate is (w, h). The blocks are 2x4 rectangles, all the same size. The block color c can be 1 (white), 2 (yellow), 3 (green), 4 (blue), or 5 (red). The orientation d of the block on the board is 0 if it is long horizontally and 1 if it is long vertically. The position of the block is represented by the coordinates (x, y) at the top left of the block. The position of the block does not overlap with other blocks and does not protrude from the board.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
w h
xs ys ys
xg yg
n
c1 d1 x1 y1
c2 d2 x2 y2
::
cn dn xn yn
The board size w, h (4 ≤ w, h ≤ 100) is given on the first line. The second line gives the start coordinates xs, ys, and the third line gives the goal coordinates xg, yg.
The number of blocks n is given on the 4th line. The next n lines are given the color ci, orientation di, position xi, yi of the i-th block.
The number of datasets does not exceed 30.
Output
The discrimination result is output to one line for each input data set.
Example
Input
20 20
1 1
9 9
7
2 0 1 1
5 1 1 3
2 1 3 3
1 1 5 2
5 1 7 3
2 0 2 7
2 0 6 8
20 20
9 9
1 1
6
2 0 1 1
1 0 5 1
2 1 1 3
5 0 1 7
3 1 5 5
4 1 8 5
0 0
Output
OK
NG | instruction | 0 | 3,244 | 23 | 6,488 |
"Correct Solution:
```
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0207
"""
import sys
from sys import stdin
from collections import deque
input = stdin.readline
def solve(w, h, xs, ys, xg, yg, blocks):
# ????????????????????±????????°???????????????l
field = [[0] * (w+1) for _ in range(h+1)]
for c, d, x, y in blocks:
field[y][x] = c
field[y][x+1] = c
field[y+1][x] = c
field[y+1][x+1] = c
if d == 0: # ?¨????
field[y][x+2] = c
field[y][x+3] = c
field[y+1][x+2] = c
field[y+1][x+3] = c
else: # ??????
field[y+2][x] = c
field[y+3][x] = c
field[y+2][x+1] = c
field[y+3][x+1] = c
# ????????????????§????????????´????????§?¨??????????
dy = [-1, 1, 0, 0]
dx = [0, 0, -1, 1]
explored = set() # ??¢?´¢????????§?¨?(x, y)????¨????
explored.add((xs, ys))
Q = deque()
Q.append((xs, ys))
color = field[ys][xs] # ?????????????????¨?????§?????????????????????????????°?????¨???????????????
while Q:
xc, yc = Q.popleft() # ?????¨??°?????§?¨?
if xc == xg and yc == yg:
return 'OK'
for i in range(len(dx)):
xn = xc + dx[i]
yn = yc + dy[i]
# ?§????????????§???????????? ?????? ??¢?????¢?´¢?????§???????????°??????????????§?????¢?´¢????¶???????
if 0 < xn <= w and 0 < yn <= h and field[yn][xn] == color and (xn, yn) not in explored:
Q.append((xn, yn))
explored.add((xn, yn))
return 'NG'
def main(args):
while True:
w, h = map(int, input().split())
if w == 0 and h == 0:
break
xs, ys = map(int, input().split())
xg, yg = map(int, input().split())
n = int(input())
blocks = [list(map(int, input().split())) for _ in range(n)]
result = solve(w, h, xs, ys, xg, yg, blocks)
print(result)
if __name__ == '__main__':
main(sys.argv[1:])
``` | output | 1 | 3,244 | 23 | 6,489 |
Provide a correct Python 3 solution for this coding contest problem.
Relative B came to Mr. A's house. He is 3 years old and loves blocks. The block he has is shaped like Figure 1.
<image>
Figure 1
Mr. B is laying blocks on the board. When I asked him, "What are you making?", He replied cheerfully, "Maze !!". The maze he says is the arrangement of blocks that are in contact with each other from the start to the goal and can be traced only by blocks of the same color. Figure 2 shows that the yellow block creates a maze from the upper left (start) to the lower right (goal).
<image>
Figure 2
With Mr. B playing innocently, you, the programmer, decided to see if the blocks were arranged in a maze.
Create a program that inputs the block information, start, and goal coordinates, and outputs OK if the block is a maze, and NG if it is not. The board has the size of w in the horizontal direction and h in the vertical direction, and the upper left coordinate is (1, 1) and the lower right coordinate is (w, h). The blocks are 2x4 rectangles, all the same size. The block color c can be 1 (white), 2 (yellow), 3 (green), 4 (blue), or 5 (red). The orientation d of the block on the board is 0 if it is long horizontally and 1 if it is long vertically. The position of the block is represented by the coordinates (x, y) at the top left of the block. The position of the block does not overlap with other blocks and does not protrude from the board.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
w h
xs ys ys
xg yg
n
c1 d1 x1 y1
c2 d2 x2 y2
::
cn dn xn yn
The board size w, h (4 ≤ w, h ≤ 100) is given on the first line. The second line gives the start coordinates xs, ys, and the third line gives the goal coordinates xg, yg.
The number of blocks n is given on the 4th line. The next n lines are given the color ci, orientation di, position xi, yi of the i-th block.
The number of datasets does not exceed 30.
Output
The discrimination result is output to one line for each input data set.
Example
Input
20 20
1 1
9 9
7
2 0 1 1
5 1 1 3
2 1 3 3
1 1 5 2
5 1 7 3
2 0 2 7
2 0 6 8
20 20
9 9
1 1
6
2 0 1 1
1 0 5 1
2 1 1 3
5 0 1 7
3 1 5 5
4 1 8 5
0 0
Output
OK
NG | instruction | 0 | 3,245 | 23 | 6,490 |
"Correct Solution:
```
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0207
"""
import sys
from sys import stdin
from collections import deque
input = stdin.readline
def solve(w, h, xs, ys, xg, yg, blocks):
# ????????????????????±????????°???????????????l
field = [[0] * (w+1) for _ in range(h+1)]
for c, d, x, y in blocks:
field[y][x] = c
field[y][x+1] = c
field[y+1][x] = c
field[y+1][x+1] = c
if d == 0: # ?¨????
field[y][x+2] = c
field[y][x+3] = c
field[y+1][x+2] = c
field[y+1][x+3] = c
else: # ??????
field[y+2][x] = c
field[y+3][x] = c
field[y+2][x+1] = c
field[y+3][x+1] = c
# ????????????????§????????????´????????§?¨??????????
dy = [-1, 1, 0, 0]
dx = [0, 0, -1, 1]
# ??????????????°???(S)??¨??´????????°???(G)?????????
d = [[float('inf')] * (w+1) for _ in range(h+1)] # ??????????????°?????????????????¢???INF??§?????????
Q = deque()
Q.append((xs, ys))
d[ys][xs] = 0
color = field[ys][xs]
while Q:
xc, yc = Q.popleft() # ?????¨??°?????§?¨?
if xc == xg and yc == yg:
return 'OK'
for i in range(len(dx)):
xn = xc + dx[i]
yn = yc + dy[i]
# ?§????????????§???????????? ?????? ??¢?????¢?´¢?????§???????????°??????????????§?????¢?´¢????¶???????
if 0 < xn <= w and 0 < yn <= h and d[yn][xn] == float('inf') and field[yn][xn] == color:
Q.append((xn, yn))
d[yn][xn] = d[yc][xc] + 1
return 'NG'
def main(args):
while True:
w, h = map(int, input().split())
if w == 0 and h == 0:
break
xs, ys = map(int, input().split())
xg, yg = map(int, input().split())
n = int(input())
blocks = [list(map(int, input().split())) for _ in range(n)]
result = solve(w, h, xs, ys, xg, yg, blocks)
print(result)
if __name__ == '__main__':
main(sys.argv[1:])
``` | output | 1 | 3,245 | 23 | 6,491 |
Provide a correct Python 3 solution for this coding contest problem.
Relative B came to Mr. A's house. He is 3 years old and loves blocks. The block he has is shaped like Figure 1.
<image>
Figure 1
Mr. B is laying blocks on the board. When I asked him, "What are you making?", He replied cheerfully, "Maze !!". The maze he says is the arrangement of blocks that are in contact with each other from the start to the goal and can be traced only by blocks of the same color. Figure 2 shows that the yellow block creates a maze from the upper left (start) to the lower right (goal).
<image>
Figure 2
With Mr. B playing innocently, you, the programmer, decided to see if the blocks were arranged in a maze.
Create a program that inputs the block information, start, and goal coordinates, and outputs OK if the block is a maze, and NG if it is not. The board has the size of w in the horizontal direction and h in the vertical direction, and the upper left coordinate is (1, 1) and the lower right coordinate is (w, h). The blocks are 2x4 rectangles, all the same size. The block color c can be 1 (white), 2 (yellow), 3 (green), 4 (blue), or 5 (red). The orientation d of the block on the board is 0 if it is long horizontally and 1 if it is long vertically. The position of the block is represented by the coordinates (x, y) at the top left of the block. The position of the block does not overlap with other blocks and does not protrude from the board.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
w h
xs ys ys
xg yg
n
c1 d1 x1 y1
c2 d2 x2 y2
::
cn dn xn yn
The board size w, h (4 ≤ w, h ≤ 100) is given on the first line. The second line gives the start coordinates xs, ys, and the third line gives the goal coordinates xg, yg.
The number of blocks n is given on the 4th line. The next n lines are given the color ci, orientation di, position xi, yi of the i-th block.
The number of datasets does not exceed 30.
Output
The discrimination result is output to one line for each input data set.
Example
Input
20 20
1 1
9 9
7
2 0 1 1
5 1 1 3
2 1 3 3
1 1 5 2
5 1 7 3
2 0 2 7
2 0 6 8
20 20
9 9
1 1
6
2 0 1 1
1 0 5 1
2 1 1 3
5 0 1 7
3 1 5 5
4 1 8 5
0 0
Output
OK
NG | instruction | 0 | 3,246 | 23 | 6,492 |
"Correct Solution:
```
""" Created by Jieyi on 9/20/16. """
def block(board, w, h, direction, color):
# horizontal.
if direction == 0:
for i in range(2):
for j in range(4):
board[h + i][w + j] = color
# vertical.
elif direction == 1:
for i in range(2):
for j in range(4):
board[h + j][w + i] = color
def print_board(board):
for i in range(len(board)):
print(board[i])
def go_maze(board, s_w, s_h, g_w, g_h):
place = board[s_h][s_w]
if place == 0:
return 'NG'
w, h = s_w, s_h
stack = []
while True:
board[h][w] = place + 1
if h + 1 < len(board) and board[h + 1][w] == place:
h += 1
stack.append((w, h))
elif w + 1 < len(board[h]) and board[h][w + 1] == place:
w += 1
stack.append((w, h))
elif h - 1 > 0 and board[h - 1][w] == place:
h -= 1
stack.append((w, h))
elif w - 1 > 0 and board[h][w - 1] == place:
w -= 1
stack.append((w, h))
else:
if len(stack) == 0 and board[h + 1][w] != place and board[h][w + 1] != place and board[h - 1][w] != place and board[h][w - 1] != place:
return 'NG'
(w, h) = stack.pop()
if w == g_w and h == g_h:
return 'OK'
def main():
while True:
w, h = map(int, input().split())
if w == 0 and h == 0:
break
board = [[0 for _ in range(110)] for _ in range(110)]
s_w, s_h = map(int, input().split())
g_w, g_h = map(int, input().split())
for _ in range(int(input())):
color, direction, w, h = map(int, input().split())
block(board, w, h, direction, color)
# print_board(board)
print(go_maze(board, s_w, s_h, g_w, g_h))
if __name__ == '__main__':
main()
``` | output | 1 | 3,246 | 23 | 6,493 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Relative B came to Mr. A's house. He is 3 years old and loves blocks. The block he has is shaped like Figure 1.
<image>
Figure 1
Mr. B is laying blocks on the board. When I asked him, "What are you making?", He replied cheerfully, "Maze !!". The maze he says is the arrangement of blocks that are in contact with each other from the start to the goal and can be traced only by blocks of the same color. Figure 2 shows that the yellow block creates a maze from the upper left (start) to the lower right (goal).
<image>
Figure 2
With Mr. B playing innocently, you, the programmer, decided to see if the blocks were arranged in a maze.
Create a program that inputs the block information, start, and goal coordinates, and outputs OK if the block is a maze, and NG if it is not. The board has the size of w in the horizontal direction and h in the vertical direction, and the upper left coordinate is (1, 1) and the lower right coordinate is (w, h). The blocks are 2x4 rectangles, all the same size. The block color c can be 1 (white), 2 (yellow), 3 (green), 4 (blue), or 5 (red). The orientation d of the block on the board is 0 if it is long horizontally and 1 if it is long vertically. The position of the block is represented by the coordinates (x, y) at the top left of the block. The position of the block does not overlap with other blocks and does not protrude from the board.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
w h
xs ys ys
xg yg
n
c1 d1 x1 y1
c2 d2 x2 y2
::
cn dn xn yn
The board size w, h (4 ≤ w, h ≤ 100) is given on the first line. The second line gives the start coordinates xs, ys, and the third line gives the goal coordinates xg, yg.
The number of blocks n is given on the 4th line. The next n lines are given the color ci, orientation di, position xi, yi of the i-th block.
The number of datasets does not exceed 30.
Output
The discrimination result is output to one line for each input data set.
Example
Input
20 20
1 1
9 9
7
2 0 1 1
5 1 1 3
2 1 3 3
1 1 5 2
5 1 7 3
2 0 2 7
2 0 6 8
20 20
9 9
1 1
6
2 0 1 1
1 0 5 1
2 1 1 3
5 0 1 7
3 1 5 5
4 1 8 5
0 0
Output
OK
NG
Submitted Solution:
```
import sys
BLOCK_WIDTH = 4
BLOCK_HEIGHT = 2
field = []
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]
w = 0
h = 0
xg = 0
yg = 0
start_color = 0
def main():
global field, w, h, c, xg, yg, start_color
while True:
w, h = map(int, input().split())
if w == 0 and h == 0:
break
xs, ys = map(int, input().split())
xg, yg = map(int, input().split())
xs -= 1
ys -= 1
xg -= 1
yg -= 1
n = int(input())
field = [[0] * w for _ in range(h)]
for i in range(n):
c, d, x, y = map(int, input().split())
arrangement(c, d, x - 1, y - 1)
start_color = field[ys][xs]
if dfs(xs, ys):
print("OK")
else:
print("NG")
def arrangement(c, d, x, y):
if d == 0:
[[draw(x + j, y + i, c) for j in range(BLOCK_WIDTH)] for i in range(BLOCK_HEIGHT)]
else:
[[draw(x + j, y + i, c) for j in range(BLOCK_HEIGHT)] for i in range(BLOCK_WIDTH)]
def draw(x, y, c):
global field
field[y][x] = c
def dfs(x, y):
global field
if x == xg and y == yg:
return True
if start_color == 0:
return False
field[y][x] = 0
for i in range(4):
next_x = x + dx[i]
next_y = y + dy[i]
if next_x < 0 or next_x >= w or next_y < 0 or next_y >= h:
continue
if field[next_y][next_x] != start_color:
continue
if dfs(next_x, next_y):
return True
return False
if __name__ == '__main__':
main()
``` | instruction | 0 | 3,247 | 23 | 6,494 |
Yes | output | 1 | 3,247 | 23 | 6,495 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Relative B came to Mr. A's house. He is 3 years old and loves blocks. The block he has is shaped like Figure 1.
<image>
Figure 1
Mr. B is laying blocks on the board. When I asked him, "What are you making?", He replied cheerfully, "Maze !!". The maze he says is the arrangement of blocks that are in contact with each other from the start to the goal and can be traced only by blocks of the same color. Figure 2 shows that the yellow block creates a maze from the upper left (start) to the lower right (goal).
<image>
Figure 2
With Mr. B playing innocently, you, the programmer, decided to see if the blocks were arranged in a maze.
Create a program that inputs the block information, start, and goal coordinates, and outputs OK if the block is a maze, and NG if it is not. The board has the size of w in the horizontal direction and h in the vertical direction, and the upper left coordinate is (1, 1) and the lower right coordinate is (w, h). The blocks are 2x4 rectangles, all the same size. The block color c can be 1 (white), 2 (yellow), 3 (green), 4 (blue), or 5 (red). The orientation d of the block on the board is 0 if it is long horizontally and 1 if it is long vertically. The position of the block is represented by the coordinates (x, y) at the top left of the block. The position of the block does not overlap with other blocks and does not protrude from the board.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
w h
xs ys ys
xg yg
n
c1 d1 x1 y1
c2 d2 x2 y2
::
cn dn xn yn
The board size w, h (4 ≤ w, h ≤ 100) is given on the first line. The second line gives the start coordinates xs, ys, and the third line gives the goal coordinates xg, yg.
The number of blocks n is given on the 4th line. The next n lines are given the color ci, orientation di, position xi, yi of the i-th block.
The number of datasets does not exceed 30.
Output
The discrimination result is output to one line for each input data set.
Example
Input
20 20
1 1
9 9
7
2 0 1 1
5 1 1 3
2 1 3 3
1 1 5 2
5 1 7 3
2 0 2 7
2 0 6 8
20 20
9 9
1 1
6
2 0 1 1
1 0 5 1
2 1 1 3
5 0 1 7
3 1 5 5
4 1 8 5
0 0
Output
OK
NG
Submitted Solution:
```
from sys import stdin
readline = stdin.readline
from collections import namedtuple
Point = namedtuple('Point', ['x', 'y'])
Block = namedtuple('Block', ['color', 'direction', 'x', 'y'])
def occupation_point(block):
x, y = block.x, block.y
d = [(0, 0), (1, 0), (0, 1), (1, 1)]
for dx, dy in d:
yield x + dx, y + dy
if block.direction:
y += 2
else:
x += 2
for dx, dy in d:
yield x + dx, y + dy
def paintout(board, start, value):
color = board[start.y][start.x]
if color == 0:
return
que =[(start.x, start.y)]
while que:
x,y = que.pop()
if board[y][x] == color:
board[y][x] = value
que.extend([(x + dx, y + dy) for dx, dy in [(-1, 0), (0, -1), (1, 0), (0, 1)]])
def is_reachable(size, start, goal, blocks):
board = [[0] * (size.x + 2) for _ in range(size.y + 2)]
for bi in blocks:
for x, y in occupation_point(bi):
board[y][x] = bi.color
paintout(board, start, -1)
return board[goal.y][goal.x] == -1
while True:
size = Point(*map(int, readline().split()))
if size.x == 0:
break
start = Point(*map(int, readline().split()))
goal = Point(*map(int, readline().split()))
blocks = []
for _ in range(int(readline())):
blocks.append(Block(*map(int, readline().split())))
print('OK' if is_reachable(size, start, goal, blocks) else 'NG')
``` | instruction | 0 | 3,248 | 23 | 6,496 |
Yes | output | 1 | 3,248 | 23 | 6,497 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Relative B came to Mr. A's house. He is 3 years old and loves blocks. The block he has is shaped like Figure 1.
<image>
Figure 1
Mr. B is laying blocks on the board. When I asked him, "What are you making?", He replied cheerfully, "Maze !!". The maze he says is the arrangement of blocks that are in contact with each other from the start to the goal and can be traced only by blocks of the same color. Figure 2 shows that the yellow block creates a maze from the upper left (start) to the lower right (goal).
<image>
Figure 2
With Mr. B playing innocently, you, the programmer, decided to see if the blocks were arranged in a maze.
Create a program that inputs the block information, start, and goal coordinates, and outputs OK if the block is a maze, and NG if it is not. The board has the size of w in the horizontal direction and h in the vertical direction, and the upper left coordinate is (1, 1) and the lower right coordinate is (w, h). The blocks are 2x4 rectangles, all the same size. The block color c can be 1 (white), 2 (yellow), 3 (green), 4 (blue), or 5 (red). The orientation d of the block on the board is 0 if it is long horizontally and 1 if it is long vertically. The position of the block is represented by the coordinates (x, y) at the top left of the block. The position of the block does not overlap with other blocks and does not protrude from the board.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
w h
xs ys ys
xg yg
n
c1 d1 x1 y1
c2 d2 x2 y2
::
cn dn xn yn
The board size w, h (4 ≤ w, h ≤ 100) is given on the first line. The second line gives the start coordinates xs, ys, and the third line gives the goal coordinates xg, yg.
The number of blocks n is given on the 4th line. The next n lines are given the color ci, orientation di, position xi, yi of the i-th block.
The number of datasets does not exceed 30.
Output
The discrimination result is output to one line for each input data set.
Example
Input
20 20
1 1
9 9
7
2 0 1 1
5 1 1 3
2 1 3 3
1 1 5 2
5 1 7 3
2 0 2 7
2 0 6 8
20 20
9 9
1 1
6
2 0 1 1
1 0 5 1
2 1 1 3
5 0 1 7
3 1 5 5
4 1 8 5
0 0
Output
OK
NG
Submitted Solution:
```
""" Created by Jieyi on 9/20/16. """
def block(board, w, h, direction, color):
# horizontal.
if direction == 0:
for i in range(2):
for j in range(4):
board[h + i][w + j] = color
# vertical.
elif direction == 1:
for i in range(2):
for j in range(4):
board[h + j][w + i] = color
def print_board(board):
for i in range(len(board)):
print(board[i])
direct = [(1, 0), (0, 1), (-1, 0), (0, -1)]
def is_goable(board, w, h, color):
for x, y in direct:
if board[h + x][w + y] == color:
return True
return False
def go_maze(board, s_w, s_h, g_w, g_h):
place = board[s_h][s_w]
if place == 0:
return 'NG'
w, h = s_w, s_h
stack = []
while True:
board[h][w] = place + 1
is_no_way = True
for x, y in direct:
if board[h + x][w + y] == place:
w, h = w + y, h + x
stack.append((w, h))
is_no_way = False
break
if is_no_way:
if len(stack) == 0 and not is_goable(board, w, h, place):
return 'NG'
(w, h) = stack.pop()
if w == g_w and h == g_h:
return 'OK'
def main():
while True:
w, h = map(int, input().split())
if w == 0 and h == 0:
break
board = [[0 for _ in range(110)] for _ in range(110)]
s_w, s_h = map(int, input().split())
g_w, g_h = map(int, input().split())
for _ in range(int(input())):
color, direction, w, h = map(int, input().split())
block(board, w, h, direction, color)
# print_board(board)
print(go_maze(board, s_w, s_h, g_w, g_h))
if __name__ == '__main__':
main()
``` | instruction | 0 | 3,249 | 23 | 6,498 |
Yes | output | 1 | 3,249 | 23 | 6,499 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Relative B came to Mr. A's house. He is 3 years old and loves blocks. The block he has is shaped like Figure 1.
<image>
Figure 1
Mr. B is laying blocks on the board. When I asked him, "What are you making?", He replied cheerfully, "Maze !!". The maze he says is the arrangement of blocks that are in contact with each other from the start to the goal and can be traced only by blocks of the same color. Figure 2 shows that the yellow block creates a maze from the upper left (start) to the lower right (goal).
<image>
Figure 2
With Mr. B playing innocently, you, the programmer, decided to see if the blocks were arranged in a maze.
Create a program that inputs the block information, start, and goal coordinates, and outputs OK if the block is a maze, and NG if it is not. The board has the size of w in the horizontal direction and h in the vertical direction, and the upper left coordinate is (1, 1) and the lower right coordinate is (w, h). The blocks are 2x4 rectangles, all the same size. The block color c can be 1 (white), 2 (yellow), 3 (green), 4 (blue), or 5 (red). The orientation d of the block on the board is 0 if it is long horizontally and 1 if it is long vertically. The position of the block is represented by the coordinates (x, y) at the top left of the block. The position of the block does not overlap with other blocks and does not protrude from the board.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
w h
xs ys ys
xg yg
n
c1 d1 x1 y1
c2 d2 x2 y2
::
cn dn xn yn
The board size w, h (4 ≤ w, h ≤ 100) is given on the first line. The second line gives the start coordinates xs, ys, and the third line gives the goal coordinates xg, yg.
The number of blocks n is given on the 4th line. The next n lines are given the color ci, orientation di, position xi, yi of the i-th block.
The number of datasets does not exceed 30.
Output
The discrimination result is output to one line for each input data set.
Example
Input
20 20
1 1
9 9
7
2 0 1 1
5 1 1 3
2 1 3 3
1 1 5 2
5 1 7 3
2 0 2 7
2 0 6 8
20 20
9 9
1 1
6
2 0 1 1
1 0 5 1
2 1 1 3
5 0 1 7
3 1 5 5
4 1 8 5
0 0
Output
OK
NG
Submitted Solution:
```
import sys
from collections import deque
while True:
W, H = map(int, input().split())
if W == 0:
break
(xs, ys), (xg, yg) = map(int, input().split()), map(int, input().split())
square = [[0]*(W+2) for _ in [0]*(H+2)]
visited = [[0]*(W+2) for _ in [0]*(H+2)]
visited[ys][xs] = 1
N = int(input())
for c, d, from_x, from_y in (map(int, sys.stdin.readline().split()) for _ in [0]*N):
to_x, to_y = (from_x+4, from_y+2) if d == 0 else (from_x+2, from_y+4)
for y in range(from_y, to_y):
square[y][from_x:to_x] = [c]*(to_x-from_x)
color = square[ys][xs] or -1
dq = deque([(ys, xs)])
popleft, append = dq.popleft, dq.append
while dq:
y, x = popleft()
if y == yg and x == xg:
print("OK")
break
for ny, nx in ((y+1, x), (y-1, x), (y, x+1), (y, x-1)):
if not visited[ny][nx] and square[ny][nx] == color:
visited[ny][nx] = 1
append((ny, nx))
else:
print("NG")
``` | instruction | 0 | 3,250 | 23 | 6,500 |
Yes | output | 1 | 3,250 | 23 | 6,501 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Relative B came to Mr. A's house. He is 3 years old and loves blocks. The block he has is shaped like Figure 1.
<image>
Figure 1
Mr. B is laying blocks on the board. When I asked him, "What are you making?", He replied cheerfully, "Maze !!". The maze he says is the arrangement of blocks that are in contact with each other from the start to the goal and can be traced only by blocks of the same color. Figure 2 shows that the yellow block creates a maze from the upper left (start) to the lower right (goal).
<image>
Figure 2
With Mr. B playing innocently, you, the programmer, decided to see if the blocks were arranged in a maze.
Create a program that inputs the block information, start, and goal coordinates, and outputs OK if the block is a maze, and NG if it is not. The board has the size of w in the horizontal direction and h in the vertical direction, and the upper left coordinate is (1, 1) and the lower right coordinate is (w, h). The blocks are 2x4 rectangles, all the same size. The block color c can be 1 (white), 2 (yellow), 3 (green), 4 (blue), or 5 (red). The orientation d of the block on the board is 0 if it is long horizontally and 1 if it is long vertically. The position of the block is represented by the coordinates (x, y) at the top left of the block. The position of the block does not overlap with other blocks and does not protrude from the board.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
w h
xs ys ys
xg yg
n
c1 d1 x1 y1
c2 d2 x2 y2
::
cn dn xn yn
The board size w, h (4 ≤ w, h ≤ 100) is given on the first line. The second line gives the start coordinates xs, ys, and the third line gives the goal coordinates xg, yg.
The number of blocks n is given on the 4th line. The next n lines are given the color ci, orientation di, position xi, yi of the i-th block.
The number of datasets does not exceed 30.
Output
The discrimination result is output to one line for each input data set.
Example
Input
20 20
1 1
9 9
7
2 0 1 1
5 1 1 3
2 1 3 3
1 1 5 2
5 1 7 3
2 0 2 7
2 0 6 8
20 20
9 9
1 1
6
2 0 1 1
1 0 5 1
2 1 1 3
5 0 1 7
3 1 5 5
4 1 8 5
0 0
Output
OK
NG
Submitted Solution:
```
def block(board, y, x, direction, color):
# ???
if direction == 0:
for i in range(2):
for j in range(4):
print(x + i, y + j)
board[x + i][y + j] = color
# ??±
elif direction == 1:
for i in range(2):
for j in range(4):
board[x + j][y + i] = color
def print_board(board):
for i in range(len(board)):
print(board[i])
def go_maze(board, s_x, s_y, g_x, g_y):
place = board[s_x][s_y]
if place == 0:
return 'NG'
x, y = s_x, s_y
stack = []
while True:
# print(x, y)
board[x][y] = place + 1
if x + 1 < len(board) and board[x + 1][y] == place:
x += 1
stack.append((x, y))
elif y + 1 < len(board[x]) and board[x][y + 1] == place:
y += 1
stack.append((x, y))
elif x - 1 > 0 and board[x - 1][y] == place:
x -= 1
stack.append((x, y))
elif y - 1 > 0 and board[x][y - 1] == place:
y -= 1
stack.append((x, y))
else:
if len(stack) == 0 and board[x + 1][y] != place and board[x][y + 1] != place and board[x - 1][y] != place and board[x][y - 1] != place:
return 'NG'
(x, y) = stack.pop()
if x == g_x and y == g_y:
return 'OK'
def main():
while True:
w, h = map(int, input().split())
if w == 0 and h == 0:
break
board = [[0 for _ in range(w + 1)] for _ in range(h + 1)]
s_x, s_y = map(int, input().split())
g_x, g_y = map(int, input().split())
for _ in range(int(input())):
color, direction, x, y = map(int, input().split())
block(board, x, y, direction, color)
print_board(board)
# print(go_maze(board, s_y, s_x, g_y, g_x))
if __name__ == '__main__':
main()
``` | instruction | 0 | 3,251 | 23 | 6,502 |
No | output | 1 | 3,251 | 23 | 6,503 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Relative B came to Mr. A's house. He is 3 years old and loves blocks. The block he has is shaped like Figure 1.
<image>
Figure 1
Mr. B is laying blocks on the board. When I asked him, "What are you making?", He replied cheerfully, "Maze !!". The maze he says is the arrangement of blocks that are in contact with each other from the start to the goal and can be traced only by blocks of the same color. Figure 2 shows that the yellow block creates a maze from the upper left (start) to the lower right (goal).
<image>
Figure 2
With Mr. B playing innocently, you, the programmer, decided to see if the blocks were arranged in a maze.
Create a program that inputs the block information, start, and goal coordinates, and outputs OK if the block is a maze, and NG if it is not. The board has the size of w in the horizontal direction and h in the vertical direction, and the upper left coordinate is (1, 1) and the lower right coordinate is (w, h). The blocks are 2x4 rectangles, all the same size. The block color c can be 1 (white), 2 (yellow), 3 (green), 4 (blue), or 5 (red). The orientation d of the block on the board is 0 if it is long horizontally and 1 if it is long vertically. The position of the block is represented by the coordinates (x, y) at the top left of the block. The position of the block does not overlap with other blocks and does not protrude from the board.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
w h
xs ys ys
xg yg
n
c1 d1 x1 y1
c2 d2 x2 y2
::
cn dn xn yn
The board size w, h (4 ≤ w, h ≤ 100) is given on the first line. The second line gives the start coordinates xs, ys, and the third line gives the goal coordinates xg, yg.
The number of blocks n is given on the 4th line. The next n lines are given the color ci, orientation di, position xi, yi of the i-th block.
The number of datasets does not exceed 30.
Output
The discrimination result is output to one line for each input data set.
Example
Input
20 20
1 1
9 9
7
2 0 1 1
5 1 1 3
2 1 3 3
1 1 5 2
5 1 7 3
2 0 2 7
2 0 6 8
20 20
9 9
1 1
6
2 0 1 1
1 0 5 1
2 1 1 3
5 0 1 7
3 1 5 5
4 1 8 5
0 0
Output
OK
NG
Submitted Solution:
```
from sys import stdin
readline = stdin.readline
from collections import namedtuple
Point = namedtuple('Point', ['x', 'y'])
Block = namedtuple('Block', ['color', 'direction', 'x', 'y'])
def occupation_point(block):
x, y = block.x, block.y
d = [(0, 0), (1, 0), (0, 1), (1, 1)]
for dx, dy in d:
yield x + dx, y + dy
if block.direction:
y += 2
else:
x += 2
for dx, dy in d:
yield x + dx, y + dy
def paintout(board, start, value):
color = board[start.y][start.x]
if color == 0:
return
que =[(start.x, start.y)]
print('cv',color, value)
while que:
x,y = que.pop()
if board[y][x] == color:
board[y][x] = value
que.extend([(x + dx, y + dy) for dx, dy in [(-1, 0), (0, -1), (1, 0), (0, 1)]])
def is_reachable(size, start, goal, blocks):
board = [[0] * (size.x + 2) for _ in range(size.y + 2)]
for bi in blocks:
for x, y in occupation_point(bi):
board[y][x] = bi.color
paintout(board, start, -1)
for bi in board:
print(bi)
return board[goal.y][goal.x] == -1
while True:
size = Point(*map(int, readline().split()))
if size.x == 0:
break
start = Point(*map(int, readline().split()))
goal = Point(*map(int, readline().split()))
blocks = []
for _ in range(int(readline())):
blocks.append(Block(*map(int, readline().split())))
print('OK' if is_reachable(size, start, goal, blocks) else 'NG')
``` | instruction | 0 | 3,252 | 23 | 6,504 |
No | output | 1 | 3,252 | 23 | 6,505 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Relative B came to Mr. A's house. He is 3 years old and loves blocks. The block he has is shaped like Figure 1.
<image>
Figure 1
Mr. B is laying blocks on the board. When I asked him, "What are you making?", He replied cheerfully, "Maze !!". The maze he says is the arrangement of blocks that are in contact with each other from the start to the goal and can be traced only by blocks of the same color. Figure 2 shows that the yellow block creates a maze from the upper left (start) to the lower right (goal).
<image>
Figure 2
With Mr. B playing innocently, you, the programmer, decided to see if the blocks were arranged in a maze.
Create a program that inputs the block information, start, and goal coordinates, and outputs OK if the block is a maze, and NG if it is not. The board has the size of w in the horizontal direction and h in the vertical direction, and the upper left coordinate is (1, 1) and the lower right coordinate is (w, h). The blocks are 2x4 rectangles, all the same size. The block color c can be 1 (white), 2 (yellow), 3 (green), 4 (blue), or 5 (red). The orientation d of the block on the board is 0 if it is long horizontally and 1 if it is long vertically. The position of the block is represented by the coordinates (x, y) at the top left of the block. The position of the block does not overlap with other blocks and does not protrude from the board.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
w h
xs ys ys
xg yg
n
c1 d1 x1 y1
c2 d2 x2 y2
::
cn dn xn yn
The board size w, h (4 ≤ w, h ≤ 100) is given on the first line. The second line gives the start coordinates xs, ys, and the third line gives the goal coordinates xg, yg.
The number of blocks n is given on the 4th line. The next n lines are given the color ci, orientation di, position xi, yi of the i-th block.
The number of datasets does not exceed 30.
Output
The discrimination result is output to one line for each input data set.
Example
Input
20 20
1 1
9 9
7
2 0 1 1
5 1 1 3
2 1 3 3
1 1 5 2
5 1 7 3
2 0 2 7
2 0 6 8
20 20
9 9
1 1
6
2 0 1 1
1 0 5 1
2 1 1 3
5 0 1 7
3 1 5 5
4 1 8 5
0 0
Output
OK
NG
Submitted Solution:
```
BLOCK_WIDTH = 4
BLOCK_HEIGHT = 2
field = []
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]
w = 0
h = 0
xg = 0
yg = 0
def main():
global field, w, h, xg, yg
while True:
w, h = map(int, input().split())
if w == 0 and h == 0:
break
xs, ys = map(int, input().split())
xg, yg = map(int, input().split())
n = int(input())
field = [[0] * w for _ in range(h)]
for i in range(n):
c, d, x, y = map(int, input().split())
arrangement(c, d, x, y)
if dfs(field[ys][xs], xs, ys):
print("OK")
else:
print("NG")
def arrangement(c, d, x, y):
global field
if d == 0:
[[draw(x + j, y + i, c) for j in range(BLOCK_WIDTH)] for i in range(BLOCK_HEIGHT)]
else:
[[draw(x + j, y + i, c) for j in range(BLOCK_HEIGHT)] for i in range(BLOCK_WIDTH)]
def draw(x, y, c):
field[y][x] = c
def dfs(c, x, y):
global field
if x == xg and y == yg:
return True
field[y][x] = -1
for i in range(4):
next_x = x + dx[i]
next_y = y + dy[i]
if next_x < 0 or next_x >= w or next_y < 0 or next_y >= h:
continue
if field[next_y][next_x] == c:
if dfs(c, next_x, next_y):
return True
return False
if __name__ == '__main__':
main()
``` | instruction | 0 | 3,253 | 23 | 6,506 |
No | output | 1 | 3,253 | 23 | 6,507 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Relative B came to Mr. A's house. He is 3 years old and loves blocks. The block he has is shaped like Figure 1.
<image>
Figure 1
Mr. B is laying blocks on the board. When I asked him, "What are you making?", He replied cheerfully, "Maze !!". The maze he says is the arrangement of blocks that are in contact with each other from the start to the goal and can be traced only by blocks of the same color. Figure 2 shows that the yellow block creates a maze from the upper left (start) to the lower right (goal).
<image>
Figure 2
With Mr. B playing innocently, you, the programmer, decided to see if the blocks were arranged in a maze.
Create a program that inputs the block information, start, and goal coordinates, and outputs OK if the block is a maze, and NG if it is not. The board has the size of w in the horizontal direction and h in the vertical direction, and the upper left coordinate is (1, 1) and the lower right coordinate is (w, h). The blocks are 2x4 rectangles, all the same size. The block color c can be 1 (white), 2 (yellow), 3 (green), 4 (blue), or 5 (red). The orientation d of the block on the board is 0 if it is long horizontally and 1 if it is long vertically. The position of the block is represented by the coordinates (x, y) at the top left of the block. The position of the block does not overlap with other blocks and does not protrude from the board.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
w h
xs ys ys
xg yg
n
c1 d1 x1 y1
c2 d2 x2 y2
::
cn dn xn yn
The board size w, h (4 ≤ w, h ≤ 100) is given on the first line. The second line gives the start coordinates xs, ys, and the third line gives the goal coordinates xg, yg.
The number of blocks n is given on the 4th line. The next n lines are given the color ci, orientation di, position xi, yi of the i-th block.
The number of datasets does not exceed 30.
Output
The discrimination result is output to one line for each input data set.
Example
Input
20 20
1 1
9 9
7
2 0 1 1
5 1 1 3
2 1 3 3
1 1 5 2
5 1 7 3
2 0 2 7
2 0 6 8
20 20
9 9
1 1
6
2 0 1 1
1 0 5 1
2 1 1 3
5 0 1 7
3 1 5 5
4 1 8 5
0 0
Output
OK
NG
Submitted Solution:
```
def dfs(x, y, m, v, W, H, xg, yg):
'''
'''
v[y][x] = True
for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
nx = x + dx
ny = y + dy
if nx in range(W) and ny in range(H) and m[ny][nx] == m[y][x] and v[ny][nx] == False:
dfs(nx, ny, m, v, W, H, xg, yg)
if __name__ == '__main__':
while True:
H, W = list(map(int, input().split()))
if H == 0 and W == 0:
break
xs, ys = list(map(lambda x: int(x)-1, input().split()))
xg, yg = list(map(lambda x: int(x)-1, input().split()))
n = int(input())
m = [[0 for _ in range(W)] for __ in range(H)]
v = [[False for _ in range(W)] for __ in range(H)]
for _ in range(n):
c, d, x, y = list(map(int, input().split()))
x -= 1
y -= 1
if d == 0:
for y in range(2):
for x in range(4):
m[y][x] = c
else:
for y in range(4):
for x in range(2):
m[y][x] = c
dfs(xs, ys, m, v, W, H, xg, yg)
if v[yg][xg]:
print('OK')
else:
print('NG')
``` | instruction | 0 | 3,254 | 23 | 6,508 |
No | output | 1 | 3,254 | 23 | 6,509 |
Provide a correct Python 3 solution for this coding contest problem.
For a given polygon g and target points t, print "2" if g contains t, "1" if t is on a segment of g, "0" otherwise.
g is represented by a sequence of points p1, p2,..., pn where line segments connecting pi and pi+1 (1 ≤ i ≤ n-1) are sides of the polygon. The line segment connecting pn and p1 is also a side of the polygon.
Note that the polygon is not necessarily convex.
Constraints
* 3 ≤ n ≤ 100
* 1 ≤ q ≤ 1000
* -10000 ≤ xi, yi ≤ 10000
* No point of the polygon will occur more than once.
* Two sides of the polygon can intersect only at a common endpoint.
Input
The entire input looks like:
g (the sequence of the points of the polygon)
q (the number of queris = the number of target points)
1st query
2nd query
:
qth query
g is given by coordinates of the points p1,..., pn in the following format:
n
x1 y1
x2 y2
:
xn yn
The first integer n is the number of points. The coordinate of a point pi is given by two integers xi and yi. The coordinates of points are given in the order of counter-clockwise visit of them.
Each query consists of the coordinate of a target point t. The coordinate is given by two intgers x and y.
Output
For each query, print "2", "1" or "0".
Example
Input
4
0 0
3 1
2 3
0 3
3
2 1
0 2
3 2
Output
2
1
0 | instruction | 0 | 3,297 | 23 | 6,594 |
"Correct Solution:
```
import math
class Vector:
def __init__(self,x,y):
self.x = x
self.y = y
def __add__(self,other):
return Vector(self.x+other.x,self.y+other.y)
def __sub__(self,other):
return Vector(self.x-other.x,self.y-other.y)
def __mul__(self,scalar):
return Vector(self.x*scalar,self.y*scalar)
def __rmul__(self, scalar):
return Vector(self.x*scalar,self.y*scalar)
def __repr__(self):
return str([self.x,self.y])
def norm_2(self):
return dot(self,self)
def norm(self):
return math.sqrt(self.norm_2())
def v_sum(v1,v2):
return Vector(v1.x+v2.x,v1.y+v2.y)
def scalar_multi(k,v):
return Vector(k*v.x,k*v.y)
def v_diff(v1,v2):
return v_sum(v1,scalar_multi(-1,v2))
def dot(vector1,vector2):
return vector1.x*vector2.x+vector1.y*vector2.y
def cross(vector1,vector2):
return vector1.x*vector2.y-vector1.y*vector2.x
def is_crossed(p,far_point,s,t):
a = far_point-p
b = t-s
v = s-p
w = t-p
x = p-s
y = far_point-s
if cross(a,v)*cross(a,w)<0 and cross(b,x)*cross(b,y)<0:
return 0#'crossed on the edge'
if cross(b,x) == 0 and 0<=dot(b,x)<=b.norm_2():
return 1#'p on segment'
if (cross(a,v)== 0 and 0<=dot(a,v)<=a.norm_2()) or (cross(a,w)==0 and 0<=dot(a,w)<=a.norm_2()):
return 2#'crossed on the corner'
return 3#not crossed
def polygon_content(point):
insec_count = 0
far_point = Vector(21001,21011) + point
for i in range(n):
judge = is_crossed(point,far_point,p[i-1],p[i])
if judge==0:
insec_count+=1
elif judge==1:
return 1
elif judge==2:
insec_count+=0.5
if insec_count==0:
return 0
elif insec_count%2==0:
return 0
else:
return 2
n = int(input())
p = []
for i in range(n):
x,y = map(int,input().split())
p.append(Vector(x,y))
q = int(input())
for i in range(q):
x,y = map(int,input().split())
print(polygon_content(Vector(x,y)))
``` | output | 1 | 3,297 | 23 | 6,595 |
Provide a correct Python 3 solution for this coding contest problem.
For a given polygon g and target points t, print "2" if g contains t, "1" if t is on a segment of g, "0" otherwise.
g is represented by a sequence of points p1, p2,..., pn where line segments connecting pi and pi+1 (1 ≤ i ≤ n-1) are sides of the polygon. The line segment connecting pn and p1 is also a side of the polygon.
Note that the polygon is not necessarily convex.
Constraints
* 3 ≤ n ≤ 100
* 1 ≤ q ≤ 1000
* -10000 ≤ xi, yi ≤ 10000
* No point of the polygon will occur more than once.
* Two sides of the polygon can intersect only at a common endpoint.
Input
The entire input looks like:
g (the sequence of the points of the polygon)
q (the number of queris = the number of target points)
1st query
2nd query
:
qth query
g is given by coordinates of the points p1,..., pn in the following format:
n
x1 y1
x2 y2
:
xn yn
The first integer n is the number of points. The coordinate of a point pi is given by two integers xi and yi. The coordinates of points are given in the order of counter-clockwise visit of them.
Each query consists of the coordinate of a target point t. The coordinate is given by two intgers x and y.
Output
For each query, print "2", "1" or "0".
Example
Input
4
0 0
3 1
2 3
0 3
3
2 1
0 2
3 2
Output
2
1
0 | instruction | 0 | 3,298 | 23 | 6,596 |
"Correct Solution:
```
from sys import stdin
import math
EPS = 1e-10
class Vector:
def __init__(self, x=None, y=None):
self.x = x
self.y = y
def __add__(self, other):
return Vector(self.x + other.x, self.y + other.y)
def __sub__(self, other):
return Vector(self.x - other.x, self.y - other.y)
# can't apply "number * Vector" but "Vector * number"
def __mul__(self, k):
return Vector(self.x * k, self.y * k)
def __truediv__(self, k):
return Vector(self.x / k, self.y / k)
def __gt__(self, other):
return self.x > other.x and self.y > other.yb
def __lt__(self, other):
return self.x < other.x and self.y < other.yb
def __eq__(self, other):
return self.x == other.x and self.y == other.y
def dot(self, other):
return self.x * other.x + self.y * other.y
# usually cross operation return Vector but it returns scalor
def cross(self, other):
return self.x * other.y - self.y * other.x
def norm(self):
return self.x * self.x + self.y * self.y
def abs(self):
return math.sqrt(self.norm())
def rotate(self, theta):
return Vector(self.x * math.cos(theta)
- self.y * math.sin(theta),
self.x * math.sin(theta)
+ self.y * math.cos(theta))
class Point(Vector):
def __init__(self, *args, **kargs):
return super().__init__(*args, **kargs)
class Segment:
def __init__(self, p1=Point(0, 0), p2=Point(1, 1)):
self.p1 = p1
self.p2 = p2
class Line(Segment):
def __init__(self, *args, **kargs):
return super().__init__(*args, **kargs)
def ccw(p0, p1, p2):
a = p1 - p0
b = p2 - p0
if a.cross(b) > EPS:
return 1
elif a.cross(b) < -EPS:
return -1
elif a.dot(b) < -EPS:
return 2
elif a.norm() < b.norm():
return -2
else:
return 0
def contains(P, p):
n = len(P)
count = 0
for i in range(n):
a = P[i]
b = P[ (i + 1) % n ]
if a.y > b.y:
a, b = b, a
if ccw(p, a, b) == 2 or p == a or p == b:
return 1
if ccw(p, a, b) == 1 and b.y > p.y and a.y <= p.y:
count = count + 1
if count % 2 == 0:
return 0
else:
return 2
def read_polygon(n):
P = []
for _ in range(n):
line = stdin.readline().strip().split()
p = Vector(int(line[0]), int(line[1]))
P.append(p)
return P
def read_and_print_results(k, P):
for _ in range(k):
line = stdin.readline().strip().split()
p = Vector(int(line[0]), int(line[1]))
result = contains(P, p)
print(result)
def __main():
n = int(input())
P = read_polygon(n)
k = int(input())
read_and_print_results(k, P)
if __name__ == '__main__':
__main()
``` | output | 1 | 3,298 | 23 | 6,597 |
Provide a correct Python 3 solution for this coding contest problem.
For a given polygon g and target points t, print "2" if g contains t, "1" if t is on a segment of g, "0" otherwise.
g is represented by a sequence of points p1, p2,..., pn where line segments connecting pi and pi+1 (1 ≤ i ≤ n-1) are sides of the polygon. The line segment connecting pn and p1 is also a side of the polygon.
Note that the polygon is not necessarily convex.
Constraints
* 3 ≤ n ≤ 100
* 1 ≤ q ≤ 1000
* -10000 ≤ xi, yi ≤ 10000
* No point of the polygon will occur more than once.
* Two sides of the polygon can intersect only at a common endpoint.
Input
The entire input looks like:
g (the sequence of the points of the polygon)
q (the number of queris = the number of target points)
1st query
2nd query
:
qth query
g is given by coordinates of the points p1,..., pn in the following format:
n
x1 y1
x2 y2
:
xn yn
The first integer n is the number of points. The coordinate of a point pi is given by two integers xi and yi. The coordinates of points are given in the order of counter-clockwise visit of them.
Each query consists of the coordinate of a target point t. The coordinate is given by two intgers x and y.
Output
For each query, print "2", "1" or "0".
Example
Input
4
0 0
3 1
2 3
0 3
3
2 1
0 2
3 2
Output
2
1
0 | instruction | 0 | 3,299 | 23 | 6,598 |
"Correct Solution:
```
import math
from typing import Union, Tuple
class Point(object):
__slots__ = ['x', 'y']
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
return Point(self.x + other.x, self.y + other.y)
def __sub__(self, other):
return Point(self.x - other.x, self.y - other.y)
def __mul__(self, other: Union[int, float]):
return Point(self.x * other, self.y * other)
def norm(self):
return pow(self.x, 2) + pow(self.y, 2)
def abs(self):
return math.sqrt(self.norm())
def __repr__(self):
return f"({self.x},{self.y})"
class Vector(Point):
__slots__ = ['x', 'y', 'pt1', 'pt2']
def __init__(self, pt1: Point, pt2: Point):
super().__init__(pt2.x - pt1.x, pt2.y - pt1.y)
self.pt1 = pt1
self.pt2 = pt2
def dot(self, other):
return self.x * other.x + self.y * other.y
def cross(self, other):
return self.x * other.y - self.y * other.x
def arg(self) -> float:
return math.atan2(self.y, self.x)
@staticmethod
def polar(r, theta) -> Point:
return Point(r * math.cos(theta), r * math.sin(theta))
def __repr__(self):
return f"({self.x},{self.y})"
class Segment(Vector):
__slots__ = ['x', 'y', 'pt1', 'pt2']
def __init__(self, pt1: Point, pt2: Point):
super().__init__(pt1, pt2)
def projection(self, pt: Point)-> Point:
t = self.dot(Vector(self.pt1, pt)) / self.norm()
return self.pt1 + self * t
def reflection(self, pt: Point) -> Point:
return self.projection(pt) * 2 - pt
def is_intersected_with(self, other) -> bool:
if (self.point_geometry(other.pt1) * self.point_geometry(other.pt2)) <= 0\
and other.point_geometry(self.pt1) * other.point_geometry(self.pt2) <= 0:
return True
else:
return False
def point_geometry(self, pt: Point) -> int:
"""
[-2:"Online Back", -1:"Counter Clockwise", 0:"On Segment", 1:"Clockwise", 2:"Online Front"]
"""
vec_pt1_to_pt = Vector(self.pt1, pt)
cross = self.cross(vec_pt1_to_pt)
if cross > 0:
return -1 # counter clockwise
elif cross < 0:
return 1 # clockwise
else: # cross == 0
dot = self.dot(vec_pt1_to_pt)
if dot < 0:
return -2 # online back
else: # dot > 0
if self.abs() < vec_pt1_to_pt.abs():
return 2 # online front
else:
return 0 # on segment
def cross_point(self, other) -> Point:
d1 = abs(self.cross(Vector(self.pt1, other.pt1))) # / self.abs()
d2 = abs(self.cross(Vector(self.pt1, other.pt2))) # / self.abs()
t = d1 / (d1 + d2)
return other.pt1 + other * t
def distance_to_point(self, pt: Point) -> Union[int, float]:
vec_pt1_to_pt = Vector(self.pt1, pt)
if self.dot(vec_pt1_to_pt) <= 0:
return vec_pt1_to_pt.abs()
vec_pt2_to_pt = Vector(self.pt2, pt)
if Vector.dot(self * -1, vec_pt2_to_pt) <= 0:
return vec_pt2_to_pt.abs()
return (self.projection(pt) - pt).abs()
def distance_to_segment(self, other) -> Union[int, float]:
if self.is_intersected_with(other):
return 0.0
else:
return min(
self.distance_to_point(other.pt1),
self.distance_to_point(other.pt2),
other.distance_to_point(self.pt1),
other.distance_to_point(self.pt2)
)
def __repr__(self):
return f"{self.pt1},{self.pt2}"
class Circle(Point):
__slots__ = ['x', 'y', 'r']
def __init__(self, x, y, r):
super().__init__(x, y)
self.r = r
def cross_point_with_circle(self, other) -> Tuple[Point, Point]:
vec_self_to_other = Vector(self, other)
vec_abs = vec_self_to_other.abs()
# if vec_abs > (self.r + other.r):
# raise AssertionError
t = ((pow(self.r, 2) - pow(other.r, 2)) / pow(vec_abs, 2) + 1) / 2
pt = (other - self) * t
abs_from_pt = math.sqrt(pow(self.r, 2) - pt.norm())
inv = Point(vec_self_to_other.y / vec_abs, - vec_self_to_other.x / vec_abs) * abs_from_pt
pt_ = self + pt
return (pt_ + inv), (pt_ - inv)
def cross_point_with_circle2(self, other) -> Tuple[Point, Point]:
vec_self_to_other = Vector(self, other)
vec_abs = vec_self_to_other.abs()
# if vec_abs > (self.r + other.r):
# raise AssertionError
theta_base_to_other = vec_self_to_other.arg()
theta_other_to_pt = math.acos((pow(self.r, 2) + pow(vec_abs, 2) - pow(other.r, 2)) / (2 * self.r * vec_abs))
return self + Vector.polar(self.r, theta_base_to_other + theta_other_to_pt),\
self + Vector.polar(self.r, theta_base_to_other - theta_other_to_pt)
def __repr__(self):
return f"({self.x},{self.y}), {self.r}"
class Polygon(object):
def __init__(self, vertices):
self.vertices = vertices
self.num_vertices = len(vertices)
def contains_point(self, pt: Point) -> int:
"""
{0:"not contained", 1: "on a edge", 2:"contained"}
"""
cross_count = 0
for i in range(self.num_vertices):
vec_a = Vector(pt, self.vertices[i])
vec_b = Vector(pt, self.vertices[(i+1) % self.num_vertices])
if vec_a.y > vec_b.y:
vec_a, vec_b = vec_b, vec_a
dot = vec_a.dot( vec_b)
cross = vec_a.cross(vec_b)
#print("pt", pt, "vtx", self.vertices[i], self.vertices[(i+1) % self.num_vertices], "vec", vec_a, vec_b, "dot", dot, "cross", cross)
if math.isclose(cross, 0.0) and dot <= 0:
return 1 # on a edge
elif vec_a.y <= 0.0 < vec_b.y and cross > 0:
cross_count += 1
return [0, 2][cross_count % 2]
def __repr__(self):
return f"{self.vertices}"
def main():
num_vertices = int(input())
polygon_vertices = []
for i in range(num_vertices):
pt_x, pt_y = map(int, input().split())
polygon_vertices.append(Point(pt_x, pt_y))
polygon = Polygon(polygon_vertices)
num_queries = int(input())
for i in range(num_queries):
pt_x, pt_y = map(int, input().split())
ret = polygon.contains_point(Point(pt_x, pt_y))
print(ret)
return
main()
``` | output | 1 | 3,299 | 23 | 6,599 |
Provide a correct Python 3 solution for this coding contest problem.
For a given polygon g and target points t, print "2" if g contains t, "1" if t is on a segment of g, "0" otherwise.
g is represented by a sequence of points p1, p2,..., pn where line segments connecting pi and pi+1 (1 ≤ i ≤ n-1) are sides of the polygon. The line segment connecting pn and p1 is also a side of the polygon.
Note that the polygon is not necessarily convex.
Constraints
* 3 ≤ n ≤ 100
* 1 ≤ q ≤ 1000
* -10000 ≤ xi, yi ≤ 10000
* No point of the polygon will occur more than once.
* Two sides of the polygon can intersect only at a common endpoint.
Input
The entire input looks like:
g (the sequence of the points of the polygon)
q (the number of queris = the number of target points)
1st query
2nd query
:
qth query
g is given by coordinates of the points p1,..., pn in the following format:
n
x1 y1
x2 y2
:
xn yn
The first integer n is the number of points. The coordinate of a point pi is given by two integers xi and yi. The coordinates of points are given in the order of counter-clockwise visit of them.
Each query consists of the coordinate of a target point t. The coordinate is given by two intgers x and y.
Output
For each query, print "2", "1" or "0".
Example
Input
4
0 0
3 1
2 3
0 3
3
2 1
0 2
3 2
Output
2
1
0 | instruction | 0 | 3,300 | 23 | 6,600 |
"Correct Solution:
```
import sys
from operator import itemgetter, attrgetter
from itertools import starmap
import cmath
from math import isinf, sqrt, acos, atan2
readline = sys.stdin.readline
EPS = 1e-9
ONLINE_FRONT = -2
CLOCKWISE = -1
ON_SEGMENT = 0
COUNTER_CLOCKWISE = 1
ONLINE_BACK = 2
class Circle(object):
__slots__ = ('c', 'r')
def __init__(self, c, r):
self.c = c
self.r = r
class Segment(object):
__slots__ = ('fi', 'se')
def __init__(self, fi, se):
self.fi = fi
self.se = se
Line = Segment
def cross(a, b):
return a.real * b.imag - a.imag * b.real
def dot(a, b):
return a.real * b.real + a.imag * b.imag
def norm(base):
return abs(base) ** 2
def project(s, p2):
base = s.fi - s.se
r = dot(p2 - s.fi, base) / norm(base)
return s.fi + base * r
def reflect(s, p):
return p + (project(s, p) - p) * 2.0
def ccw(p1, p2, p3):
a = p2 - p1
b = p3 - p1
if cross(a, b) > EPS: return 1
if cross(a, b) < -EPS: return -1
if dot(a, b) < -EPS: return 2
if norm(a) < norm(b): return -2
return 0
def intersect4(p1, p2, p3, p4):
return (ccw(p1, p2, p3) * ccw(p1, p2, p4) <= 0 and
ccw(p3, p4, p1) * ccw(p3, p4, p2) <= 0)
def intersect2(s1, s2):
return intersect4(s1.fi, s1.se, s2.fi, s2.se)
def getDistance(a, b): return abs(a - b)
def getDistanceLP(l, p):
return abs(cross(l.se - l.fi, p - l.fi) / abs(l.se - l.fi))
def getDistanceSP(s, p):
if dot(s.se - s.fi, p - s.fi) < 0.0: return abs(p - s.fi)
if dot(s.fi - s.se, p - s.se) < 0.0: return abs(p - s.se)
return getDistanceLP(s, p)
def getDistances(s1, s2):
if intersect2(s1, s2): return 0.0
return min(getDistanceSP(s1, s2.fi), getDistanceSP(s1, s2.se),
getDistanceSP(s2, s1.fi), getDistanceSP(s2, s1.se))
def getCrossPoint(s1, s2):
base = s2.se - s2.fi
d1 = abs(cross(base, s1.fi - s2.fi))
d2 = abs(cross(base, s1.se - s2.fi))
t = d1 / (d1 + d2)
return s1.fi + (s1.se - s1.fi) * t
def getCrossPointsCL(c, l):
pr = project(l, c.c)
e = (l.se - l.fi) / abs(l.se - l.fi)
base = sqrt(c.r * c.r - norm(pr - c.c))
return Segment(*sorted((pr + e * base, pr - e * base)), key=attrgetter('real', 'imag'))
def getCrossPointsCC(c1, c2):
d = abs(c1.c - c2.c)
a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2.0 * c1.r * d))
t = cmath.phase(c2.c - c1.c)
return Segment(*sorted((c1.c + cmath.rect(c1.r, t + a), c1.c + cmath.rect(c1.r, t - a)), key=attrgetter('real', 'imag')))
def contains(g, p):
n = len(g)
x = False
for i in range(n):
a = g[i] - p
b = g[(i + 1) % n] - p
if abs(cross(a, b)) < EPS and dot(a, b) < EPS: return 1
if a.imag > b.imag: a, b = b, a
if a.imag < EPS and EPS < b.imag and cross(a, b) > EPS: x = not x
return 2 if x else 0
n = int(readline())
pg = tuple(complex(*map(int, readline().split())) for _ in [0] * n)
q = int(readline())
for _ in [0] * q:
print(contains(pg, complex(*map(int, readline().split()))))
``` | output | 1 | 3,300 | 23 | 6,601 |
Provide a correct Python 3 solution for this coding contest problem.
For a given polygon g and target points t, print "2" if g contains t, "1" if t is on a segment of g, "0" otherwise.
g is represented by a sequence of points p1, p2,..., pn where line segments connecting pi and pi+1 (1 ≤ i ≤ n-1) are sides of the polygon. The line segment connecting pn and p1 is also a side of the polygon.
Note that the polygon is not necessarily convex.
Constraints
* 3 ≤ n ≤ 100
* 1 ≤ q ≤ 1000
* -10000 ≤ xi, yi ≤ 10000
* No point of the polygon will occur more than once.
* Two sides of the polygon can intersect only at a common endpoint.
Input
The entire input looks like:
g (the sequence of the points of the polygon)
q (the number of queris = the number of target points)
1st query
2nd query
:
qth query
g is given by coordinates of the points p1,..., pn in the following format:
n
x1 y1
x2 y2
:
xn yn
The first integer n is the number of points. The coordinate of a point pi is given by two integers xi and yi. The coordinates of points are given in the order of counter-clockwise visit of them.
Each query consists of the coordinate of a target point t. The coordinate is given by two intgers x and y.
Output
For each query, print "2", "1" or "0".
Example
Input
4
0 0
3 1
2 3
0 3
3
2 1
0 2
3 2
Output
2
1
0 | instruction | 0 | 3,301 | 23 | 6,602 |
"Correct Solution:
```
# Aizu Problem CGL_3_C: Polygon-Point-Containment
#
import sys, math, os
# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
sys.stdin = open("sample-input.txt", "rt")
def cross_product_test(A, B, C):
if A[1] == B[1] == C[1]:
if B[0] <= A[0] <= C[0] or C[0] <= A[0] <= B[0]:
return 0
else:
return 1
if B[1] > C[1]:
B, C = C[:], B[:]
if A[1] == B[1] and A[0] == B[0]:
return 0
if A[1] <= B[1] or A[1] > C[1]:
return 1
delta = (B[0] - A[0]) * (C[1] - A[1]) - (B[1] - A[1]) * (C[0] - A[0])
if delta > 0:
return -1
elif delta < 0:
return 1
else:
return 0
def point_in_polygon(polygon, point):
t = -1
polygon.append(polygon[0])
for i in range(len(polygon) - 1):
t *= cross_product_test(point, polygon[i], polygon[i+1])
return t
N = int(input())
P = [[int(_) for _ in input().split()] for __ in range(N)]
Q = int(input())
for q in range(Q):
x, y = [int(_) for _ in input().split()]
print(point_in_polygon(P, [x, y]) + 1)
``` | output | 1 | 3,301 | 23 | 6,603 |
Provide a correct Python 3 solution for this coding contest problem.
For a given polygon g and target points t, print "2" if g contains t, "1" if t is on a segment of g, "0" otherwise.
g is represented by a sequence of points p1, p2,..., pn where line segments connecting pi and pi+1 (1 ≤ i ≤ n-1) are sides of the polygon. The line segment connecting pn and p1 is also a side of the polygon.
Note that the polygon is not necessarily convex.
Constraints
* 3 ≤ n ≤ 100
* 1 ≤ q ≤ 1000
* -10000 ≤ xi, yi ≤ 10000
* No point of the polygon will occur more than once.
* Two sides of the polygon can intersect only at a common endpoint.
Input
The entire input looks like:
g (the sequence of the points of the polygon)
q (the number of queris = the number of target points)
1st query
2nd query
:
qth query
g is given by coordinates of the points p1,..., pn in the following format:
n
x1 y1
x2 y2
:
xn yn
The first integer n is the number of points. The coordinate of a point pi is given by two integers xi and yi. The coordinates of points are given in the order of counter-clockwise visit of them.
Each query consists of the coordinate of a target point t. The coordinate is given by two intgers x and y.
Output
For each query, print "2", "1" or "0".
Example
Input
4
0 0
3 1
2 3
0 3
3
2 1
0 2
3 2
Output
2
1
0 | instruction | 0 | 3,302 | 23 | 6,604 |
"Correct Solution:
```
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
input:
4
0 0
3 1
2 3
0 3
3
2 1
0 2
3 2
output:
2
1
0
"""
import sys
EPS = 1e-9
def cross(a, b):
return a.real * b.imag - a.imag * b.real
def dot(a, b):
return a.real * b.real + a.imag * b.imag
def check_contains(g, p):
flag = False
for j in range(edges):
a, b = g[j] - p, g[(j + 1) % edges] - p
if abs(cross(a, b)) < EPS and dot(a, b) < EPS:
return 1
elif a.imag > b.imag:
a, b = b, a
if a.imag < EPS < b.imag and cross(a, b) > EPS:
flag = not flag
return 2 if flag else 0
def solve(_p_info):
for point in _p_info:
px, py = map(float, point)
p = px + py * 1j
print(check_contains(polygon, p))
return None
if __name__ == '__main__':
_input = sys.stdin.readlines()
edges = int(_input[0])
e_info = map(lambda x: x.split(), _input[1:edges + 1])
points = int(_input[edges + 1])
p_info = map(lambda x: x.split(), _input[edges + 2:])
polygon = [float(x) + float(y) * 1j for x, y in e_info]
solve(p_info)
``` | output | 1 | 3,302 | 23 | 6,605 |
Provide a correct Python 3 solution for this coding contest problem.
For a given polygon g and target points t, print "2" if g contains t, "1" if t is on a segment of g, "0" otherwise.
g is represented by a sequence of points p1, p2,..., pn where line segments connecting pi and pi+1 (1 ≤ i ≤ n-1) are sides of the polygon. The line segment connecting pn and p1 is also a side of the polygon.
Note that the polygon is not necessarily convex.
Constraints
* 3 ≤ n ≤ 100
* 1 ≤ q ≤ 1000
* -10000 ≤ xi, yi ≤ 10000
* No point of the polygon will occur more than once.
* Two sides of the polygon can intersect only at a common endpoint.
Input
The entire input looks like:
g (the sequence of the points of the polygon)
q (the number of queris = the number of target points)
1st query
2nd query
:
qth query
g is given by coordinates of the points p1,..., pn in the following format:
n
x1 y1
x2 y2
:
xn yn
The first integer n is the number of points. The coordinate of a point pi is given by two integers xi and yi. The coordinates of points are given in the order of counter-clockwise visit of them.
Each query consists of the coordinate of a target point t. The coordinate is given by two intgers x and y.
Output
For each query, print "2", "1" or "0".
Example
Input
4
0 0
3 1
2 3
0 3
3
2 1
0 2
3 2
Output
2
1
0 | instruction | 0 | 3,303 | 23 | 6,606 |
"Correct Solution:
```
#!/usr/bin/env python3
# CGL_3_C: Polygon - Polygon-Point Containment
from enum import Enum
class Position(Enum):
OUTSIDE = 0
BORDER = 1
INSIDE = 2
class Polygon:
def __init__(self, ps):
self.ps = ps
self.convex_poligons = divide(ps)
def position(self, p):
if p in self.ps:
return Position.BORDER
pos = [position(*c, p) for c in self.convex_poligons]
if all([x == Position.OUTSIDE for x in pos]):
return Position.OUTSIDE
elif any([x == Position.INSIDE for x in pos]):
return Position.INSIDE
elif len([x for x in pos if x == Position.BORDER]) > 1:
return Position.INSIDE
else:
return Position.BORDER
def divide(ps):
if len(ps) < 3:
return []
p0, p1, p2, *ps = ps
if not ccw(p0, p1, p2):
return divide([p1, p2] + ps + [p0])
for p in ps:
if position(p0, p1, p2, p) != Position.OUTSIDE:
return divide([p1, p2] + ps + [p0])
return [(p0, p1, p2)] + divide([p0, p2] + ps)
def ccw(p0, p1, p2):
x0, y0 = p0
x1, y1 = p1
x2, y2 = p2
v1 = (x1-x0, y1-y0)
v2 = (x2-x1, y2-y1)
return dot(orthogonal(v1), v2) > 0
def dot(v1, v2):
x1, y1 = v1
x2, y2 = v2
return x1 * x2 + y1 * y2
def cross(v1, v2):
x1, y1 = v1
x2, y2 = v2
return x1 * y2 - y1 * x2
def orthogonal(v):
x, y = v
return -y, x
def position(p0, p1, p2, p):
x, y = p
ps = [p0, p1, p2]
online = False
for i in range(3):
pa = ps[i]
pb = ps[(i+1) % 3]
pc = ps[(i+2) % 3]
xa, ya = pa
xb, yb = pb
xc, yc = pc
vab = (xb-xa, yb-ya)
vac = (xc-xa, yc-ya)
vp = (x-xa, y-ya)
c = cross(vab, vp) * cross(vp, vac)
if c == 0:
online = True
elif c < 0:
return Position.OUTSIDE
if online:
return Position.BORDER
else:
return Position.INSIDE
def run():
g = int(input())
ps = []
for _ in range(g):
x, y = [int(i) for i in input().split()]
ps.append((x, y))
poly = Polygon(ps)
n = int(input())
for _ in range(n):
qx, qy = [int(i) for i in input().split()]
pos = poly.position((qx, qy))
print(pos.value)
if __name__ == '__main__':
run()
``` | output | 1 | 3,303 | 23 | 6,607 |
Provide a correct Python 3 solution for this coding contest problem.
For a given polygon g and target points t, print "2" if g contains t, "1" if t is on a segment of g, "0" otherwise.
g is represented by a sequence of points p1, p2,..., pn where line segments connecting pi and pi+1 (1 ≤ i ≤ n-1) are sides of the polygon. The line segment connecting pn and p1 is also a side of the polygon.
Note that the polygon is not necessarily convex.
Constraints
* 3 ≤ n ≤ 100
* 1 ≤ q ≤ 1000
* -10000 ≤ xi, yi ≤ 10000
* No point of the polygon will occur more than once.
* Two sides of the polygon can intersect only at a common endpoint.
Input
The entire input looks like:
g (the sequence of the points of the polygon)
q (the number of queris = the number of target points)
1st query
2nd query
:
qth query
g is given by coordinates of the points p1,..., pn in the following format:
n
x1 y1
x2 y2
:
xn yn
The first integer n is the number of points. The coordinate of a point pi is given by two integers xi and yi. The coordinates of points are given in the order of counter-clockwise visit of them.
Each query consists of the coordinate of a target point t. The coordinate is given by two intgers x and y.
Output
For each query, print "2", "1" or "0".
Example
Input
4
0 0
3 1
2 3
0 3
3
2 1
0 2
3 2
Output
2
1
0 | instruction | 0 | 3,304 | 23 | 6,608 |
"Correct Solution:
```
n = int(input())
vec = []
for i in range(n):
vec += [list(map(int, input().split()))]
vec += [vec[0]]
def cross(a, b):
return a[0]*b[1]-a[1]*b[0]
def ab(a, b):
c = (b[0]-a[0],b[1]-a[1])
return c
def dot(a, b):
n = len(a)
if n != len(b):
return None
ans = 0
for i, j in zip(a, b):
ans += i*j
return ans
def check(a, b):
if abs(cross(a, b)) < pow(10,-8) and dot(a,b)<pow(10,-8):
return 1
else:
return 0
def contain():
x = 0
p = list(map(int, input().split()))
for a,b in zip(vec[:-1],vec[1:]):
a = ab(p,a)
b = ab(p,b)
if check(a, b) == 1:
print(1)
return
if a[1] > b[1]:
a, b = b, a
if a[1] < pow(10,-8) and b[1] > pow(10,-8) and cross(a, b) > 0:
x += 1
if x%2==1:print(2)
else:print(0)
k = int(input())
for i in range(k):
contain()
``` | output | 1 | 3,304 | 23 | 6,609 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a given polygon g and target points t, print "2" if g contains t, "1" if t is on a segment of g, "0" otherwise.
g is represented by a sequence of points p1, p2,..., pn where line segments connecting pi and pi+1 (1 ≤ i ≤ n-1) are sides of the polygon. The line segment connecting pn and p1 is also a side of the polygon.
Note that the polygon is not necessarily convex.
Constraints
* 3 ≤ n ≤ 100
* 1 ≤ q ≤ 1000
* -10000 ≤ xi, yi ≤ 10000
* No point of the polygon will occur more than once.
* Two sides of the polygon can intersect only at a common endpoint.
Input
The entire input looks like:
g (the sequence of the points of the polygon)
q (the number of queris = the number of target points)
1st query
2nd query
:
qth query
g is given by coordinates of the points p1,..., pn in the following format:
n
x1 y1
x2 y2
:
xn yn
The first integer n is the number of points. The coordinate of a point pi is given by two integers xi and yi. The coordinates of points are given in the order of counter-clockwise visit of them.
Each query consists of the coordinate of a target point t. The coordinate is given by two intgers x and y.
Output
For each query, print "2", "1" or "0".
Example
Input
4
0 0
3 1
2 3
0 3
3
2 1
0 2
3 2
Output
2
1
0
Submitted Solution:
```
def dot(a, b):
return a.real * b.real + a.imag * b.imag
def cross(a, b):
return a.real * b.imag - a.imag * b.real
n = int(input())
vertices = [complex(*map(int, input().split())) for _ in range(n)]
edges = [(p0, p1, p1 - p0) for p0, p1 in zip(vertices, vertices[1:] + [vertices[0]])]
q = int(input())
while q:
q -= 1
p = complex(*map(int, input().split()))
counter = 0
for p0, p1, edge in edges:
a, b = p0 - p, p1 - p
if a.imag > b.imag:
a, b = b, a
crs = cross(a, b)
if a.imag <= 0 and 0 < b.imag and crs < 0:
counter += 1
if crs == 0 and dot(a, b) <= 0:
print(1)
break
else:
print(2 if counter % 2 else 0)
``` | instruction | 0 | 3,305 | 23 | 6,610 |
Yes | output | 1 | 3,305 | 23 | 6,611 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.