message
stringlengths
2
19.9k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
322
108k
cluster
float64
15
15
__index_level_0__
int64
644
217k
Provide tags and a correct Python 3 solution for this coding contest problem. You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (xi, yi). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: 1. Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k β‰₯ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. 2. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. 3. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane. Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (xi, yi) ( - 109 ≀ xi, yi ≀ 109) β€” the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0). Output In a single line print a single integer k β€” the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≀ 106. Examples Input 2 1 1 -1 -1 Output 12 1 1 R 1 1 U 2 1 1 L 1 1 D 3 1 1 L 1 1 D 2 1 1 R 1 1 U 3 Input 3 5 0 0 5 1 0 Output 12 1 1 R 2 1 1 L 3 1 5 R 2 1 5 L 3 1 5 U 2 1 5 D 3
instruction
0
61,761
15
123,522
Tags: greedy, implementation, sortings Correct Solution: ``` ################################################################### # ."-,.__ # `. `. , # .--' .._,'"-' `. # . .' `' # `. / ,' # ` '--. ,-"' # `"` | \ # -. \, | # `--Y.' ___. # \ L._, \ # _., `. < <\ _ # ,' ' `, `. | \ ( ` # ../, `. ` | .\`. \ \_ # ,' ,.. . _.,' ||\l ) '". # , ,' \ ,'.-.`-._,' | . _._`. # ,' / \ \ `' ' `--/ | \ / / ..\ # .' / \ . |\__ - _ ,'` ` / / `.`. # | ' .. `-...-" | `-' / / . `. # | / |L__ | | / / `. `. # , / . . | | / / ` ` # / / ,. ,`._ `-_ | | _ ,-' / ` \ # / . \"`_/. `-_ \_,. ,' +-' `-' _, ..,-. \`. # . ' .-f ,' ` '. \__.---' _ .' ' \ \ # ' / `.' l .' / \.. ,_|/ `. ,'` L` # |' _.-""` `. \ _,' ` \ `.___`.'"`-. , | | | \ # || ,' `. `. ' _,...._ ` | `/ ' | ' .| # || ,' `. ;.,.---' ,' `. `.. `-' .-' /_ .' ;_ || # || ' V / / ` | ` ,' ,' '. ! `. || # ||/ _,-------7 ' . | `-' l / `|| # . | ,' .- ,' || | .-. `. .' || # `' ,' `".' | | `. '. -.' `' # / ,' | |,' \-.._,.'/' # . / . . \ .'' # .`. | `. / :_,'.' # \ `...\ _ ,'-. .' /_.-' # `-.__ `, `' . _.>----''. _ __ / # .' /"' | "' '_ # /_|.-'\ ,". '.'`__'-( \ # / ,"'"\,' `/ `-.|" ################################################################### from sys import stdin, stdout from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log from collections import defaultdict as dd, deque from heapq import merge, heapify, heappop, heappush, nsmallest from bisect import bisect_left as bl, bisect_right as br, bisect mod = pow(10, 9) + 7 mod2 = 998244353 def inp(): return stdin.readline().strip() def iinp(): return int(inp()) def out(var, end="\n"): stdout.write(str(var)+"\n") def outa(*var, end="\n"): stdout.write(' '.join(map(str, var)) + end) def lmp(): return list(mp()) def mp(): return map(int, inp().split()) def smp(): return map(str, inp().split()) def l1d(n, val=0): return [val for i in range(n)] def l2d(n, m, val=0): return [l1d(m, val) for j in range(n)] def remadd(x, y): return 1 if x%y else 0 def ceil(a,b): return (a+b-1)//b def isprime(x): if x<=1: return False if x in (2, 3): return True if x%2 == 0: return False for i in range(3, int(sqrt(x))+1, 2): if x%i == 0: return False return True n = iinp() ml = [] for i in range(n): x, y = mp() ml.append((x, y)) ml.sort(key=lambda x: abs(x[0])+abs(x[1])) ansl = [] for x, y in ml: if x>0: ansl.append('1 '+str(x)+' R') if x<0: ansl.append('1 '+str(-x)+ ' L') if y>0: ansl.append('1 '+str(y)+ ' U') if y<0: ansl.append('1 '+str(-y)+ ' D') ansl.append('2') if x>0: ansl.append('1 '+str(x)+ ' L') if x<0: ansl.append('1 '+str(-x)+ ' R') if y>0: ansl.append('1 '+str(y)+ ' D') if y<0: ansl.append('1 '+str(-y)+ ' U') ansl.append('3') out(len(ansl)) out('\n'.join(ansl)) ```
output
1
61,761
15
123,523
Provide tags and a correct Python 3 solution for this coding contest problem. You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (xi, yi). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: 1. Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k β‰₯ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. 2. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. 3. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane. Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (xi, yi) ( - 109 ≀ xi, yi ≀ 109) β€” the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0). Output In a single line print a single integer k β€” the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≀ 106. Examples Input 2 1 1 -1 -1 Output 12 1 1 R 1 1 U 2 1 1 L 1 1 D 3 1 1 L 1 1 D 2 1 1 R 1 1 U 3 Input 3 5 0 0 5 1 0 Output 12 1 1 R 2 1 1 L 3 1 5 R 2 1 5 L 3 1 5 U 2 1 5 D 3
instruction
0
61,762
15
123,524
Tags: greedy, implementation, sortings Correct Solution: ``` #!/usr/bin/env python3 import io import os import sys input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline def printd(*args, **kwargs): #print(*args, **kwargs, file=sys.stderr) print(*args, **kwargs) pass def get_str(): return input().decode().strip() def rint(): return map(int, input().split()) def oint(): return int(input()) n = oint() p = [] for i in range(n): x, y = rint() p.append([x, y]) p.sort(key=lambda a: abs(a[0]) + abs(a[1])) ans = [] for i in range(n): x, y = p[i] if x > 0: ans.append(str(1) + ' ' + str(x) + ' R') elif x < 0: ans.append(str(1) + ' ' + str(-x) + ' L') if y > 0: ans.append(str(1) + ' ' + str(y) + ' U') elif y < 0: ans.append(str(1) + ' ' + str(-y) + ' D') ans.append('2') if x > 0: ans.append(str(1) + ' ' + str(x) + ' L') elif x < 0: ans.append(str(1) + ' ' + str(-x) + ' R') if y > 0: ans.append(str(1) + ' ' + str(y) + ' D') elif y < 0: ans.append(str(1) + ' ' + str(-y) + ' U') ans.append('3') print(len(ans)) for a in ans: print(a) ```
output
1
61,762
15
123,525
Provide tags and a correct Python 3 solution for this coding contest problem. You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains n bombs, the i-th bomb is at point with coordinates (xi, yi). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0, 0). Initially, the robot is at point with coordinates (0, 0). Also, let's mark the robot's current position as (x, y). In order to destroy all the bombs, the robot can perform three types of operations: 1. Operation has format "1 k dir". To perform the operation robot have to move in direction dir k (k β‰₯ 1) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) (corresponding to directions). It is forbidden to move from point (x, y), if at least one point on the path (besides the destination point) contains a bomb. 2. Operation has format "2". To perform the operation robot have to pick a bomb at point (x, y) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (x, y) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. 3. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0, 0). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane. Input The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of bombs on the coordinate plane. Next n lines contain two integers each. The i-th line contains numbers (xi, yi) ( - 109 ≀ xi, yi ≀ 109) β€” the coordinates of the i-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0, 0). Output In a single line print a single integer k β€” the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these k operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where k ≀ 106. Examples Input 2 1 1 -1 -1 Output 12 1 1 R 1 1 U 2 1 1 L 1 1 D 3 1 1 L 1 1 D 2 1 1 R 1 1 U 3 Input 3 5 0 0 5 1 0 Output 12 1 1 R 2 1 1 L 3 1 5 R 2 1 5 L 3 1 5 U 2 1 5 D 3
instruction
0
61,763
15
123,526
Tags: greedy, implementation, sortings Correct Solution: ``` def dist(p): return abs(p[0]) + abs(p[1]) n = int(input()) points = [] for i in range(n): x,y = map(int,input().split()) points.append((x,y)) points.sort(key = dist) ans = [] for x,y in points: if x > 0: ans.append(f'1 {x} R') if x < 0: ans.append(f'1 {-x} L') if y > 0: ans.append(f'1 {y} U') if y < 0: ans.append(f'1 {-y} D') ans.append('2') if x > 0: ans.append(f'1 {x} L') if x < 0: ans.append(f'1 {-x} R') if y > 0: ans.append(f'1 {y} D') if y < 0: ans.append(f'1 {-y} U') ans.append('3') print(len(ans)) print('\n'.join(ans)) ```
output
1
61,763
15
123,527
Provide tags and a correct Python 3 solution for this coding contest problem. They say "years are like dominoes, tumbling one after the other". But would a year fit into a grid? I don't think so. Limak is a little polar bear who loves to play. He has recently got a rectangular grid with h rows and w columns. Each cell is a square, either empty (denoted by '.') or forbidden (denoted by '#'). Rows are numbered 1 through h from top to bottom. Columns are numbered 1 through w from left to right. Also, Limak has a single domino. He wants to put it somewhere in a grid. A domino will occupy exactly two adjacent cells, located either in one row or in one column. Both adjacent cells must be empty and must be inside a grid. Limak needs more fun and thus he is going to consider some queries. In each query he chooses some rectangle and wonders, how many way are there to put a single domino inside of the chosen rectangle? Input The first line of the input contains two integers h and w (1 ≀ h, w ≀ 500) – the number of rows and the number of columns, respectively. The next h lines describe a grid. Each line contains a string of the length w. Each character is either '.' or '#' β€” denoting an empty or forbidden cell, respectively. The next line contains a single integer q (1 ≀ q ≀ 100 000) β€” the number of queries. Each of the next q lines contains four integers r1i, c1i, r2i, c2i (1 ≀ r1i ≀ r2i ≀ h, 1 ≀ c1i ≀ c2i ≀ w) β€” the i-th query. Numbers r1i and c1i denote the row and the column (respectively) of the upper left cell of the rectangle. Numbers r2i and c2i denote the row and the column (respectively) of the bottom right cell of the rectangle. Output Print q integers, i-th should be equal to the number of ways to put a single domino inside the i-th rectangle. Examples Input 5 8 ....#..# .#...... ##.#.... ##..#.## ........ 4 1 1 2 3 4 1 4 1 1 2 4 5 2 5 5 8 Output 4 0 10 15 Input 7 39 ....................................... .###..###..#..###.....###..###..#..###. ...#..#.#..#..#.........#..#.#..#..#... .###..#.#..#..###.....###..#.#..#..###. .#....#.#..#....#.....#....#.#..#..#.#. .###..###..#..###.....###..###..#..###. ....................................... 6 1 1 3 20 2 10 6 30 2 10 7 30 2 2 7 7 1 7 7 7 1 8 7 8 Output 53 89 120 23 0 2 Note A red frame below corresponds to the first query of the first sample. A domino can be placed in 4 possible ways. <image>
instruction
0
61,888
15
123,776
Tags: dp, implementation Correct Solution: ``` n,m=map(int,input().split()) a=[[] for i in range(n)] for i in range(n): a[i]=list(input()) hor,ver=[[0]*2005 for i in range(2005)],[[0]*2005 for i in range(2005)] for i in range(0,n+1): for j in range(0,m+1): if i==0 or j==0: ver[i][j]=hor[i][j]=0 else: ver[i][j]=ver[i-1][j]+ver[i][j-1]-ver[i-1][j-1] hor[i][j]=hor[i-1][j]+hor[i][j-1]-hor[i-1][j-1] if i<n and a[i-1][j-1]=='.' and a[i][j-1]=='.':ver[i][j]+=1 if j<m and a[i-1][j-1] == '.' and a[i - 1][j] == '.':hor[i][j]+=1 for _ in range(int(input())): xb,yb,xa,ya=map(int,input().split()) ans=0 ans += hor[xa][ya - 1] - hor[xb - 1][ya - 1] - hor[xa][yb - 1] + hor[xb - 1][yb - 1] ans += ver[xa - 1][ya] - ver[xa - 1][yb - 1] - ver[xb - 1][ya] + ver[xb - 1][yb - 1] print(ans) ```
output
1
61,888
15
123,777
Provide tags and a correct Python 3 solution for this coding contest problem. They say "years are like dominoes, tumbling one after the other". But would a year fit into a grid? I don't think so. Limak is a little polar bear who loves to play. He has recently got a rectangular grid with h rows and w columns. Each cell is a square, either empty (denoted by '.') or forbidden (denoted by '#'). Rows are numbered 1 through h from top to bottom. Columns are numbered 1 through w from left to right. Also, Limak has a single domino. He wants to put it somewhere in a grid. A domino will occupy exactly two adjacent cells, located either in one row or in one column. Both adjacent cells must be empty and must be inside a grid. Limak needs more fun and thus he is going to consider some queries. In each query he chooses some rectangle and wonders, how many way are there to put a single domino inside of the chosen rectangle? Input The first line of the input contains two integers h and w (1 ≀ h, w ≀ 500) – the number of rows and the number of columns, respectively. The next h lines describe a grid. Each line contains a string of the length w. Each character is either '.' or '#' β€” denoting an empty or forbidden cell, respectively. The next line contains a single integer q (1 ≀ q ≀ 100 000) β€” the number of queries. Each of the next q lines contains four integers r1i, c1i, r2i, c2i (1 ≀ r1i ≀ r2i ≀ h, 1 ≀ c1i ≀ c2i ≀ w) β€” the i-th query. Numbers r1i and c1i denote the row and the column (respectively) of the upper left cell of the rectangle. Numbers r2i and c2i denote the row and the column (respectively) of the bottom right cell of the rectangle. Output Print q integers, i-th should be equal to the number of ways to put a single domino inside the i-th rectangle. Examples Input 5 8 ....#..# .#...... ##.#.... ##..#.## ........ 4 1 1 2 3 4 1 4 1 1 2 4 5 2 5 5 8 Output 4 0 10 15 Input 7 39 ....................................... .###..###..#..###.....###..###..#..###. ...#..#.#..#..#.........#..#.#..#..#... .###..#.#..#..###.....###..#.#..#..###. .#....#.#..#....#.....#....#.#..#..#.#. .###..###..#..###.....###..###..#..###. ....................................... 6 1 1 3 20 2 10 6 30 2 10 7 30 2 2 7 7 1 7 7 7 1 8 7 8 Output 53 89 120 23 0 2 Note A red frame below corresponds to the first query of the first sample. A domino can be placed in 4 possible ways. <image>
instruction
0
61,889
15
123,778
Tags: dp, implementation Correct Solution: ``` n, m = map(int, input().split()) li = [' '*(m+1)]+[' '+input() for _ in range(n)] r,c = [[0 for i in range(m+1)] for i in range(n+1)],[[0 for i in range(m+1)] for i in range(n+1)] q = int(input()) for i in range(1,n+1): for j in range(1,m+1): r[i][j]=r[i-1][j]+r[i][j-1]-r[i-1][j-1]+(li[i][j]=='.' and li[i][j-1]=='.') c[i][j]=c[i-1][j]+c[i][j-1]-c[i-1][j-1]+(li[i][j]=='.' and li[i-1][j]=='.') for _ in range(q): r1,c1,r2,c2 = map(int,input().split()) print (c[r2][c2]-c[r1][c2]-c[r2][c1-1]+c[r1][c1-1]+r[r2][c2]-r[r2][c1]-r[r1-1][c2]+r[r1-1][c1]) ```
output
1
61,889
15
123,779
Provide tags and a correct Python 3 solution for this coding contest problem. They say "years are like dominoes, tumbling one after the other". But would a year fit into a grid? I don't think so. Limak is a little polar bear who loves to play. He has recently got a rectangular grid with h rows and w columns. Each cell is a square, either empty (denoted by '.') or forbidden (denoted by '#'). Rows are numbered 1 through h from top to bottom. Columns are numbered 1 through w from left to right. Also, Limak has a single domino. He wants to put it somewhere in a grid. A domino will occupy exactly two adjacent cells, located either in one row or in one column. Both adjacent cells must be empty and must be inside a grid. Limak needs more fun and thus he is going to consider some queries. In each query he chooses some rectangle and wonders, how many way are there to put a single domino inside of the chosen rectangle? Input The first line of the input contains two integers h and w (1 ≀ h, w ≀ 500) – the number of rows and the number of columns, respectively. The next h lines describe a grid. Each line contains a string of the length w. Each character is either '.' or '#' β€” denoting an empty or forbidden cell, respectively. The next line contains a single integer q (1 ≀ q ≀ 100 000) β€” the number of queries. Each of the next q lines contains four integers r1i, c1i, r2i, c2i (1 ≀ r1i ≀ r2i ≀ h, 1 ≀ c1i ≀ c2i ≀ w) β€” the i-th query. Numbers r1i and c1i denote the row and the column (respectively) of the upper left cell of the rectangle. Numbers r2i and c2i denote the row and the column (respectively) of the bottom right cell of the rectangle. Output Print q integers, i-th should be equal to the number of ways to put a single domino inside the i-th rectangle. Examples Input 5 8 ....#..# .#...... ##.#.... ##..#.## ........ 4 1 1 2 3 4 1 4 1 1 2 4 5 2 5 5 8 Output 4 0 10 15 Input 7 39 ....................................... .###..###..#..###.....###..###..#..###. ...#..#.#..#..#.........#..#.#..#..#... .###..#.#..#..###.....###..#.#..#..###. .#....#.#..#....#.....#....#.#..#..#.#. .###..###..#..###.....###..###..#..###. ....................................... 6 1 1 3 20 2 10 6 30 2 10 7 30 2 2 7 7 1 7 7 7 1 8 7 8 Output 53 89 120 23 0 2 Note A red frame below corresponds to the first query of the first sample. A domino can be placed in 4 possible ways. <image>
instruction
0
61,890
15
123,780
Tags: dp, implementation Correct Solution: ``` #! /usr/bin/env python # -*- coding: utf-8 -*- # vim:fenc=utf-8 # # Copyright Β© 2016 missingdays <missingdays@missingdays> # # Distributed under terms of the MIT license. """ """ def read_list(): return [int(i) for i in input().split()] def new_list(n): return [0 for i in range(n)] def new_matrix(n, m=0): return [[0 for i in range(m)] for i in range(n)] m = 2005 h, w = read_list() d = [] for i in range(h): d.append(input()) ver = new_matrix(m, m) hor = new_matrix(m, m) for j in range(h): for i in range(w): hor[i+1][j+1] = hor[i+1][j] + hor[i][j+1] - hor[i][j] ver[i+1][j+1] = ver[i+1][j] + ver[i][j+1] - ver[i][j] if d[j][i] == ".": if i < w-1 and d[j][i+1] == ".": hor[i+1][j+1] += 1 if j < h-1 and d[j+1][i] == ".": ver[i+1][j+1] += 1 n = int(input()) for i in range(n): y1, x1, y2, x2 = read_list() x1 -= 1 y1 -= 1 answ = 0 answ += (hor[x2-1][y2] - hor[x1][y2] - hor[x2-1][y1] + hor[x1][y1]) answ += (ver[x2][y2-1] - ver[x1][y2-1] - ver[x2][y1] + ver[x1][y1]) print(answ) ```
output
1
61,890
15
123,781
Provide tags and a correct Python 3 solution for this coding contest problem. They say "years are like dominoes, tumbling one after the other". But would a year fit into a grid? I don't think so. Limak is a little polar bear who loves to play. He has recently got a rectangular grid with h rows and w columns. Each cell is a square, either empty (denoted by '.') or forbidden (denoted by '#'). Rows are numbered 1 through h from top to bottom. Columns are numbered 1 through w from left to right. Also, Limak has a single domino. He wants to put it somewhere in a grid. A domino will occupy exactly two adjacent cells, located either in one row or in one column. Both adjacent cells must be empty and must be inside a grid. Limak needs more fun and thus he is going to consider some queries. In each query he chooses some rectangle and wonders, how many way are there to put a single domino inside of the chosen rectangle? Input The first line of the input contains two integers h and w (1 ≀ h, w ≀ 500) – the number of rows and the number of columns, respectively. The next h lines describe a grid. Each line contains a string of the length w. Each character is either '.' or '#' β€” denoting an empty or forbidden cell, respectively. The next line contains a single integer q (1 ≀ q ≀ 100 000) β€” the number of queries. Each of the next q lines contains four integers r1i, c1i, r2i, c2i (1 ≀ r1i ≀ r2i ≀ h, 1 ≀ c1i ≀ c2i ≀ w) β€” the i-th query. Numbers r1i and c1i denote the row and the column (respectively) of the upper left cell of the rectangle. Numbers r2i and c2i denote the row and the column (respectively) of the bottom right cell of the rectangle. Output Print q integers, i-th should be equal to the number of ways to put a single domino inside the i-th rectangle. Examples Input 5 8 ....#..# .#...... ##.#.... ##..#.## ........ 4 1 1 2 3 4 1 4 1 1 2 4 5 2 5 5 8 Output 4 0 10 15 Input 7 39 ....................................... .###..###..#..###.....###..###..#..###. ...#..#.#..#..#.........#..#.#..#..#... .###..#.#..#..###.....###..#.#..#..###. .#....#.#..#....#.....#....#.#..#..#.#. .###..###..#..###.....###..###..#..###. ....................................... 6 1 1 3 20 2 10 6 30 2 10 7 30 2 2 7 7 1 7 7 7 1 8 7 8 Output 53 89 120 23 0 2 Note A red frame below corresponds to the first query of the first sample. A domino can be placed in 4 possible ways. <image>
instruction
0
61,891
15
123,782
Tags: dp, implementation Correct Solution: ``` n, m = map(int, input().split()) a = [' ' + input() for i in range(n)] a = [' '*(m+1)] + a q = int(input()) col = [[0 for i in range(m+1)] for i in range(n+1)] row = [[0 for i in range(m+1)] for i in range(n+1)] for i in range(1, n+1): for j in range(1, m+1): col[i][j] = col[i-1][j] + col[i][j-1] - col[i-1][j-1] + (a[i][j] == '.' and a[i-1][j] == '.') row[i][j] = row[i-1][j] + row[i][j-1] - row[i-1][j-1] + (a[i][j] == '.' and a[i][j-1] == '.') for i in range(q): r1, c1, r2, c2 = map(int, input().split()) print (col[r2][c2] - col[r1][c2] - col[r2][c1-1] + col[r1][c1-1] \ + row[r2][c2] - row[r2][c1] - row[r1-1][c2] + row[r1-1][c1]) ```
output
1
61,891
15
123,783
Provide tags and a correct Python 3 solution for this coding contest problem. They say "years are like dominoes, tumbling one after the other". But would a year fit into a grid? I don't think so. Limak is a little polar bear who loves to play. He has recently got a rectangular grid with h rows and w columns. Each cell is a square, either empty (denoted by '.') or forbidden (denoted by '#'). Rows are numbered 1 through h from top to bottom. Columns are numbered 1 through w from left to right. Also, Limak has a single domino. He wants to put it somewhere in a grid. A domino will occupy exactly two adjacent cells, located either in one row or in one column. Both adjacent cells must be empty and must be inside a grid. Limak needs more fun and thus he is going to consider some queries. In each query he chooses some rectangle and wonders, how many way are there to put a single domino inside of the chosen rectangle? Input The first line of the input contains two integers h and w (1 ≀ h, w ≀ 500) – the number of rows and the number of columns, respectively. The next h lines describe a grid. Each line contains a string of the length w. Each character is either '.' or '#' β€” denoting an empty or forbidden cell, respectively. The next line contains a single integer q (1 ≀ q ≀ 100 000) β€” the number of queries. Each of the next q lines contains four integers r1i, c1i, r2i, c2i (1 ≀ r1i ≀ r2i ≀ h, 1 ≀ c1i ≀ c2i ≀ w) β€” the i-th query. Numbers r1i and c1i denote the row and the column (respectively) of the upper left cell of the rectangle. Numbers r2i and c2i denote the row and the column (respectively) of the bottom right cell of the rectangle. Output Print q integers, i-th should be equal to the number of ways to put a single domino inside the i-th rectangle. Examples Input 5 8 ....#..# .#...... ##.#.... ##..#.## ........ 4 1 1 2 3 4 1 4 1 1 2 4 5 2 5 5 8 Output 4 0 10 15 Input 7 39 ....................................... .###..###..#..###.....###..###..#..###. ...#..#.#..#..#.........#..#.#..#..#... .###..#.#..#..###.....###..#.#..#..###. .#....#.#..#....#.....#....#.#..#..#.#. .###..###..#..###.....###..###..#..###. ....................................... 6 1 1 3 20 2 10 6 30 2 10 7 30 2 2 7 7 1 7 7 7 1 8 7 8 Output 53 89 120 23 0 2 Note A red frame below corresponds to the first query of the first sample. A domino can be placed in 4 possible ways. <image>
instruction
0
61,892
15
123,784
Tags: dp, implementation Correct Solution: ``` #!/usr/bin/env python3 import itertools, functools, math def II(r1, c1, r2, c2, table): if r1 > r2: return 0 if c1 > c2: return 0 res = table[r2][c2] if r1: res -= table[r1-1][c2] if c1: res -= table[r2][c1-1] if r1 and c1: res += table[r1-1][c1-1] return res def solve(): h, w = map(int, input().split()) grid = [input() for _ in range(h)] row = [[0]*w for _ in range(h)] col = [[0]*w for _ in range(h)] for i in range(h): col_prev = 0 row_prev = 0 for j in range(w): row[i][j] = row_prev col[i][j] = col_prev if i: row[i][j] += row[i-1][j] col[i][j] += col[i-1][j] if grid[i][j] == '#': continue if i+1 < h and grid[i+1][j] != '#': row[i][j] += 1 row_prev += 1 if j+1 < w and grid[i][j+1] != '#': col[i][j] += 1 col_prev += 1 q = int(input()) for _ in range(q): r1, c1, r2, c2 = map(int, input().split()) r1 -= 1 c1 -= 1 r2 -= 1 c2 -= 1 print(II(r1, c1, r2-1, c2, row) + II(r1, c1, r2, c2-1, col)) if __name__ == '__main__': solve() ```
output
1
61,892
15
123,785
Provide tags and a correct Python 3 solution for this coding contest problem. They say "years are like dominoes, tumbling one after the other". But would a year fit into a grid? I don't think so. Limak is a little polar bear who loves to play. He has recently got a rectangular grid with h rows and w columns. Each cell is a square, either empty (denoted by '.') or forbidden (denoted by '#'). Rows are numbered 1 through h from top to bottom. Columns are numbered 1 through w from left to right. Also, Limak has a single domino. He wants to put it somewhere in a grid. A domino will occupy exactly two adjacent cells, located either in one row or in one column. Both adjacent cells must be empty and must be inside a grid. Limak needs more fun and thus he is going to consider some queries. In each query he chooses some rectangle and wonders, how many way are there to put a single domino inside of the chosen rectangle? Input The first line of the input contains two integers h and w (1 ≀ h, w ≀ 500) – the number of rows and the number of columns, respectively. The next h lines describe a grid. Each line contains a string of the length w. Each character is either '.' or '#' β€” denoting an empty or forbidden cell, respectively. The next line contains a single integer q (1 ≀ q ≀ 100 000) β€” the number of queries. Each of the next q lines contains four integers r1i, c1i, r2i, c2i (1 ≀ r1i ≀ r2i ≀ h, 1 ≀ c1i ≀ c2i ≀ w) β€” the i-th query. Numbers r1i and c1i denote the row and the column (respectively) of the upper left cell of the rectangle. Numbers r2i and c2i denote the row and the column (respectively) of the bottom right cell of the rectangle. Output Print q integers, i-th should be equal to the number of ways to put a single domino inside the i-th rectangle. Examples Input 5 8 ....#..# .#...... ##.#.... ##..#.## ........ 4 1 1 2 3 4 1 4 1 1 2 4 5 2 5 5 8 Output 4 0 10 15 Input 7 39 ....................................... .###..###..#..###.....###..###..#..###. ...#..#.#..#..#.........#..#.#..#..#... .###..#.#..#..###.....###..#.#..#..###. .#....#.#..#....#.....#....#.#..#..#.#. .###..###..#..###.....###..###..#..###. ....................................... 6 1 1 3 20 2 10 6 30 2 10 7 30 2 2 7 7 1 7 7 7 1 8 7 8 Output 53 89 120 23 0 2 Note A red frame below corresponds to the first query of the first sample. A domino can be placed in 4 possible ways. <image>
instruction
0
61,893
15
123,786
Tags: dp, implementation Correct Solution: ``` read = lambda: map(int, input().split()) h, w = read() a = [input() for i in range(h)] N = 501 vr = [[0] * N for i in range(N)] hr = [[0] * N for i in range(N)] for i in range(h): for j in range(w): vr[j + 1][i + 1] = vr[j][i + 1] + vr[j + 1][i] - vr[j][i] hr[j + 1][i + 1] = hr[j][i + 1] + hr[j + 1][i] - hr[j][i] if a[i][j] == '#': continue if i != h - 1 and a[i + 1][j] == '.': vr[j + 1][i + 1] += 1 if j != w - 1 and a[i][j + 1] == '.': hr[j + 1][i + 1] += 1 q = int(input()) for i in range(q): r1, c1, r2, c2 = read() p1 = hr[c2 - 1][r2] - hr[c1 - 1][r2] - hr[c2 - 1][r1 - 1] + hr[c1 - 1][r1 - 1] p2 = vr[c2][r2 - 1] - vr[c1 - 1][r2 - 1] - vr[c2][r1 - 1] + vr[c1 - 1][r1 - 1] ans = p1 + p2 print(ans) ```
output
1
61,893
15
123,787
Provide tags and a correct Python 3 solution for this coding contest problem. They say "years are like dominoes, tumbling one after the other". But would a year fit into a grid? I don't think so. Limak is a little polar bear who loves to play. He has recently got a rectangular grid with h rows and w columns. Each cell is a square, either empty (denoted by '.') or forbidden (denoted by '#'). Rows are numbered 1 through h from top to bottom. Columns are numbered 1 through w from left to right. Also, Limak has a single domino. He wants to put it somewhere in a grid. A domino will occupy exactly two adjacent cells, located either in one row or in one column. Both adjacent cells must be empty and must be inside a grid. Limak needs more fun and thus he is going to consider some queries. In each query he chooses some rectangle and wonders, how many way are there to put a single domino inside of the chosen rectangle? Input The first line of the input contains two integers h and w (1 ≀ h, w ≀ 500) – the number of rows and the number of columns, respectively. The next h lines describe a grid. Each line contains a string of the length w. Each character is either '.' or '#' β€” denoting an empty or forbidden cell, respectively. The next line contains a single integer q (1 ≀ q ≀ 100 000) β€” the number of queries. Each of the next q lines contains four integers r1i, c1i, r2i, c2i (1 ≀ r1i ≀ r2i ≀ h, 1 ≀ c1i ≀ c2i ≀ w) β€” the i-th query. Numbers r1i and c1i denote the row and the column (respectively) of the upper left cell of the rectangle. Numbers r2i and c2i denote the row and the column (respectively) of the bottom right cell of the rectangle. Output Print q integers, i-th should be equal to the number of ways to put a single domino inside the i-th rectangle. Examples Input 5 8 ....#..# .#...... ##.#.... ##..#.## ........ 4 1 1 2 3 4 1 4 1 1 2 4 5 2 5 5 8 Output 4 0 10 15 Input 7 39 ....................................... .###..###..#..###.....###..###..#..###. ...#..#.#..#..#.........#..#.#..#..#... .###..#.#..#..###.....###..#.#..#..###. .#....#.#..#....#.....#....#.#..#..#.#. .###..###..#..###.....###..###..#..###. ....................................... 6 1 1 3 20 2 10 6 30 2 10 7 30 2 2 7 7 1 7 7 7 1 8 7 8 Output 53 89 120 23 0 2 Note A red frame below corresponds to the first query of the first sample. A domino can be placed in 4 possible ways. <image>
instruction
0
61,894
15
123,788
Tags: dp, implementation Correct Solution: ``` # Description of the problem can be found at http://codeforces.com/problemset/problem/611/C read = lambda: map(int, input().split()) h, w = read() a = [input() for i in range(h)] N = 501 vr = [[0] * N for i in range(N)] hr = [[0] * N for i in range(N)] for i in range(h): for j in range(w): vr[j + 1][i + 1] = vr[j][i + 1] + vr[j + 1][i] - vr[j][i] hr[j + 1][i + 1] = hr[j][i + 1] + hr[j + 1][i] - hr[j][i] if a[i][j] == '#': continue if i != h - 1 and a[i + 1][j] == '.': vr[j + 1][i + 1] += 1 if j != w - 1 and a[i][j + 1] == '.': hr[j + 1][i + 1] += 1 q = int(input()) for i in range(q): r1, c1, r2, c2 = read() p1 = hr[c2 - 1][r2] - hr[c1 - 1][r2] - hr[c2 - 1][r1 - 1] + hr[c1 - 1][r1 - 1] p2 = vr[c2][r2 - 1] - vr[c1 - 1][r2 - 1] - vr[c2][r1 - 1] + vr[c1 - 1][r1 - 1] ans = p1 + p2 print(ans) ```
output
1
61,894
15
123,789
Provide tags and a correct Python 3 solution for this coding contest problem. They say "years are like dominoes, tumbling one after the other". But would a year fit into a grid? I don't think so. Limak is a little polar bear who loves to play. He has recently got a rectangular grid with h rows and w columns. Each cell is a square, either empty (denoted by '.') or forbidden (denoted by '#'). Rows are numbered 1 through h from top to bottom. Columns are numbered 1 through w from left to right. Also, Limak has a single domino. He wants to put it somewhere in a grid. A domino will occupy exactly two adjacent cells, located either in one row or in one column. Both adjacent cells must be empty and must be inside a grid. Limak needs more fun and thus he is going to consider some queries. In each query he chooses some rectangle and wonders, how many way are there to put a single domino inside of the chosen rectangle? Input The first line of the input contains two integers h and w (1 ≀ h, w ≀ 500) – the number of rows and the number of columns, respectively. The next h lines describe a grid. Each line contains a string of the length w. Each character is either '.' or '#' β€” denoting an empty or forbidden cell, respectively. The next line contains a single integer q (1 ≀ q ≀ 100 000) β€” the number of queries. Each of the next q lines contains four integers r1i, c1i, r2i, c2i (1 ≀ r1i ≀ r2i ≀ h, 1 ≀ c1i ≀ c2i ≀ w) β€” the i-th query. Numbers r1i and c1i denote the row and the column (respectively) of the upper left cell of the rectangle. Numbers r2i and c2i denote the row and the column (respectively) of the bottom right cell of the rectangle. Output Print q integers, i-th should be equal to the number of ways to put a single domino inside the i-th rectangle. Examples Input 5 8 ....#..# .#...... ##.#.... ##..#.## ........ 4 1 1 2 3 4 1 4 1 1 2 4 5 2 5 5 8 Output 4 0 10 15 Input 7 39 ....................................... .###..###..#..###.....###..###..#..###. ...#..#.#..#..#.........#..#.#..#..#... .###..#.#..#..###.....###..#.#..#..###. .#....#.#..#....#.....#....#.#..#..#.#. .###..###..#..###.....###..###..#..###. ....................................... 6 1 1 3 20 2 10 6 30 2 10 7 30 2 2 7 7 1 7 7 7 1 8 7 8 Output 53 89 120 23 0 2 Note A red frame below corresponds to the first query of the first sample. A domino can be placed in 4 possible ways. <image>
instruction
0
61,895
15
123,790
Tags: dp, implementation Correct Solution: ``` def main(): h, w = map(int, input().split()) l = [[c == '.' for c in input()] for _ in range(h)] ver = [[0] * (w + 1) for _ in range(h + 1)] for y, aa, bb, l0, l1 in zip(range(h - 1), ver, ver[1:], l, l[1:]): for x in range(w): bb[x + 1] = (l0[x] and l1[x]) + bb[x] + aa[x + 1] - aa[x] hor = [[0] * (w + 1) for _ in range(h + 1)] for y, aa, bb, l0 in zip(range(h), hor, hor[1:], l): for x in range(w - 1): bb[x + 1] = (l0[x] and l0[x + 1]) + bb[x] + aa[x + 1] - aa[x] res = [] for _ in range(int(input())): r1, c1, r2, c2 = map(int, input().split()) r1 -= 1 c1 -= 1 res.append(str(ver[r2 - 1][c2] - ver[r1][c2] - ver[r2 - 1][c1] + ver[r1][c1] + hor[r2][c2 - 1] - hor[r1][c2 - 1] - hor[r2][c1] + hor[r1][c1])) print('\n'.join(res)) if __name__ == '__main__': main() ```
output
1
61,895
15
123,791
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. They say "years are like dominoes, tumbling one after the other". But would a year fit into a grid? I don't think so. Limak is a little polar bear who loves to play. He has recently got a rectangular grid with h rows and w columns. Each cell is a square, either empty (denoted by '.') or forbidden (denoted by '#'). Rows are numbered 1 through h from top to bottom. Columns are numbered 1 through w from left to right. Also, Limak has a single domino. He wants to put it somewhere in a grid. A domino will occupy exactly two adjacent cells, located either in one row or in one column. Both adjacent cells must be empty and must be inside a grid. Limak needs more fun and thus he is going to consider some queries. In each query he chooses some rectangle and wonders, how many way are there to put a single domino inside of the chosen rectangle? Input The first line of the input contains two integers h and w (1 ≀ h, w ≀ 500) – the number of rows and the number of columns, respectively. The next h lines describe a grid. Each line contains a string of the length w. Each character is either '.' or '#' β€” denoting an empty or forbidden cell, respectively. The next line contains a single integer q (1 ≀ q ≀ 100 000) β€” the number of queries. Each of the next q lines contains four integers r1i, c1i, r2i, c2i (1 ≀ r1i ≀ r2i ≀ h, 1 ≀ c1i ≀ c2i ≀ w) β€” the i-th query. Numbers r1i and c1i denote the row and the column (respectively) of the upper left cell of the rectangle. Numbers r2i and c2i denote the row and the column (respectively) of the bottom right cell of the rectangle. Output Print q integers, i-th should be equal to the number of ways to put a single domino inside the i-th rectangle. Examples Input 5 8 ....#..# .#...... ##.#.... ##..#.## ........ 4 1 1 2 3 4 1 4 1 1 2 4 5 2 5 5 8 Output 4 0 10 15 Input 7 39 ....................................... .###..###..#..###.....###..###..#..###. ...#..#.#..#..#.........#..#.#..#..#... .###..#.#..#..###.....###..#.#..#..###. .#....#.#..#....#.....#....#.#..#..#.#. .###..###..#..###.....###..###..#..###. ....................................... 6 1 1 3 20 2 10 6 30 2 10 7 30 2 2 7 7 1 7 7 7 1 8 7 8 Output 53 89 120 23 0 2 Note A red frame below corresponds to the first query of the first sample. A domino can be placed in 4 possible ways. <image> Submitted Solution: ``` def pre(matrix,n,m): dp1 = [[0 for i in range(m)] for j in range(n)] for i in range(n): for j in range(m): if j > 0: if matrix[i][j] == '.' and matrix[i][j-1] == '.': dp1[i][j] += 1 dp1[i][j] += dp1[i][j-1] dp2 = [[0 for i in range(m)] for j in range(n)] for i in range(1,n): for j in range(m): if matrix[i][j] == '.' and matrix[i-1][j] == '.': dp2[i][j] += 1 dp2[i][j] += dp2[i-1][j] return dp1,dp2 def solve(matrix,dp1,dp2,r1,c1,r2,c2,ans): total = 0 for i in range(r1-1,r2): total += dp1[i][c2-1] if c1-2 >= 0: total -= dp1[i][c1-2] if matrix[i][c1-1] == '.' and matrix[i][c1-2] == '.': total -= 1 for j in range(c1-1,c2): total += dp2[r2-1][j] if r1-2 >= 0: total -= dp2[r1-2][j] if matrix[r1-1][j] == '.' and matrix[r1-2][j] == '.': total -= 1 ans.append(total) def main(): h,w = map(int,input().split()) matrix = [] for i in range(h): matrix.append(input()) dp1,dp2 = pre(matrix,h,w) q = int(input()) ans = [] for i in range(q): r1,c1,r2,c2 = map(int,input().split()) solve(matrix,dp1,dp2,r1,c1,r2,c2,ans) for i in ans: print(i) main() ```
instruction
0
61,896
15
123,792
Yes
output
1
61,896
15
123,793
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. They say "years are like dominoes, tumbling one after the other". But would a year fit into a grid? I don't think so. Limak is a little polar bear who loves to play. He has recently got a rectangular grid with h rows and w columns. Each cell is a square, either empty (denoted by '.') or forbidden (denoted by '#'). Rows are numbered 1 through h from top to bottom. Columns are numbered 1 through w from left to right. Also, Limak has a single domino. He wants to put it somewhere in a grid. A domino will occupy exactly two adjacent cells, located either in one row or in one column. Both adjacent cells must be empty and must be inside a grid. Limak needs more fun and thus he is going to consider some queries. In each query he chooses some rectangle and wonders, how many way are there to put a single domino inside of the chosen rectangle? Input The first line of the input contains two integers h and w (1 ≀ h, w ≀ 500) – the number of rows and the number of columns, respectively. The next h lines describe a grid. Each line contains a string of the length w. Each character is either '.' or '#' β€” denoting an empty or forbidden cell, respectively. The next line contains a single integer q (1 ≀ q ≀ 100 000) β€” the number of queries. Each of the next q lines contains four integers r1i, c1i, r2i, c2i (1 ≀ r1i ≀ r2i ≀ h, 1 ≀ c1i ≀ c2i ≀ w) β€” the i-th query. Numbers r1i and c1i denote the row and the column (respectively) of the upper left cell of the rectangle. Numbers r2i and c2i denote the row and the column (respectively) of the bottom right cell of the rectangle. Output Print q integers, i-th should be equal to the number of ways to put a single domino inside the i-th rectangle. Examples Input 5 8 ....#..# .#...... ##.#.... ##..#.## ........ 4 1 1 2 3 4 1 4 1 1 2 4 5 2 5 5 8 Output 4 0 10 15 Input 7 39 ....................................... .###..###..#..###.....###..###..#..###. ...#..#.#..#..#.........#..#.#..#..#... .###..#.#..#..###.....###..#.#..#..###. .#....#.#..#....#.....#....#.#..#..#.#. .###..###..#..###.....###..###..#..###. ....................................... 6 1 1 3 20 2 10 6 30 2 10 7 30 2 2 7 7 1 7 7 7 1 8 7 8 Output 53 89 120 23 0 2 Note A red frame below corresponds to the first query of the first sample. A domino can be placed in 4 possible ways. <image> Submitted Solution: ``` __author__ = 'Utena' h,w=map(int,input().split()) q=[[0]*w for i in range(h)] for i1 in range(h): n0=input() for i in range(w): if n0[i]=='#': q[i1][i]=1 a=[[0]*w for i2 in range(h+1)] b=[[0]*(w+1) for i3 in range(h)] for i in range(h): for j in range(w-1): if q[i][j]==q[i][j+1]==0: a[i+1][j+1]=a[i+1][j]+1 else: a[i+1][j+1]=a[i+1][j] for i in range(1,h+1): for j in range(w): a[i][j]+=a[i-1][j] for i in range(h-1): for j in range(w): if q[i][j]==q[i+1][j]==0: b[i+1][j+1]=b[i][j+1]+1 else: b[i+1][j+1]=b[i][j+1] for i in range(h): for j in range(1,w+1): b[i][j]+=b[i][j-1] n=int(input()) for k in range(n): r1,c1,r2,c2=list(map(int,input().split())) t=a[r2][c2-1]-a[r2][c1-1]-a[r1-1][c2-1]+a[r1-1][c1-1]+b[r2-1][c2]-b[r1-1][c2]-b[r2-1][c1-1]+b[r1-1][c1-1] print(t) ```
instruction
0
61,897
15
123,794
Yes
output
1
61,897
15
123,795
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. They say "years are like dominoes, tumbling one after the other". But would a year fit into a grid? I don't think so. Limak is a little polar bear who loves to play. He has recently got a rectangular grid with h rows and w columns. Each cell is a square, either empty (denoted by '.') or forbidden (denoted by '#'). Rows are numbered 1 through h from top to bottom. Columns are numbered 1 through w from left to right. Also, Limak has a single domino. He wants to put it somewhere in a grid. A domino will occupy exactly two adjacent cells, located either in one row or in one column. Both adjacent cells must be empty and must be inside a grid. Limak needs more fun and thus he is going to consider some queries. In each query he chooses some rectangle and wonders, how many way are there to put a single domino inside of the chosen rectangle? Input The first line of the input contains two integers h and w (1 ≀ h, w ≀ 500) – the number of rows and the number of columns, respectively. The next h lines describe a grid. Each line contains a string of the length w. Each character is either '.' or '#' β€” denoting an empty or forbidden cell, respectively. The next line contains a single integer q (1 ≀ q ≀ 100 000) β€” the number of queries. Each of the next q lines contains four integers r1i, c1i, r2i, c2i (1 ≀ r1i ≀ r2i ≀ h, 1 ≀ c1i ≀ c2i ≀ w) β€” the i-th query. Numbers r1i and c1i denote the row and the column (respectively) of the upper left cell of the rectangle. Numbers r2i and c2i denote the row and the column (respectively) of the bottom right cell of the rectangle. Output Print q integers, i-th should be equal to the number of ways to put a single domino inside the i-th rectangle. Examples Input 5 8 ....#..# .#...... ##.#.... ##..#.## ........ 4 1 1 2 3 4 1 4 1 1 2 4 5 2 5 5 8 Output 4 0 10 15 Input 7 39 ....................................... .###..###..#..###.....###..###..#..###. ...#..#.#..#..#.........#..#.#..#..#... .###..#.#..#..###.....###..#.#..#..###. .#....#.#..#....#.....#....#.#..#..#.#. .###..###..#..###.....###..###..#..###. ....................................... 6 1 1 3 20 2 10 6 30 2 10 7 30 2 2 7 7 1 7 7 7 1 8 7 8 Output 53 89 120 23 0 2 Note A red frame below corresponds to the first query of the first sample. A domino can be placed in 4 possible ways. <image> Submitted Solution: ``` # You lost the game. from math import * def afft(A): for i in range(len(A)): ch = "" for j in range(len(A[i])): if A[i][j]>=2: ch = ch + str(A[i][j]) + " "*(3-int(log(A[i][j],10))) else: ch = ch + str(A[i][j]) + " " print(ch) h,w = map(int, input().split()) G = [str(input()) for _ in range(h)] q = int(input()) L = [list(map(int, input().split())) for _ in range(q)] H = [[0 for _ in range(w)] for _ in range(h)] V = [[0 for _ in range(w)] for _ in range(h)] for i in range(h): for j in range(w): H[i][j] = H[i][j-1] if j and G[i][j] == "." and G[i][j-1] == ".": H[i][j] += 1 for j in range(w): for i in range(h): V[i][j] = V[i-1][j] if i and G[i][j] == "." and G[i-1][j] == ".": V[i][j] += 1 for i in range(1,h): H[i] = [H[i-1][j] + H[i][j] for j in range(w)] for j in range(1,w): for i in range(h): V[i][j] = V[i][j] + V[i][j-1] for Q in L: r1 = Q[0]-1 c1 = Q[1]-1 r2 = Q[2]-1 c2 = Q[3]-1 if r1 > r2 or c1 > c2: print(0) else: r = H[r2][c2] + V[r2][c2] - H[r2][c1] - V[r1][c2] if r1: r = r - H[r1-1][c2] if c1: r = r - V[r2][c1-1] if r1 and c1: r = r + H[r1-1][c1] + V[r1][c1-1] print(r) ```
instruction
0
61,898
15
123,796
Yes
output
1
61,898
15
123,797
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. They say "years are like dominoes, tumbling one after the other". But would a year fit into a grid? I don't think so. Limak is a little polar bear who loves to play. He has recently got a rectangular grid with h rows and w columns. Each cell is a square, either empty (denoted by '.') or forbidden (denoted by '#'). Rows are numbered 1 through h from top to bottom. Columns are numbered 1 through w from left to right. Also, Limak has a single domino. He wants to put it somewhere in a grid. A domino will occupy exactly two adjacent cells, located either in one row or in one column. Both adjacent cells must be empty and must be inside a grid. Limak needs more fun and thus he is going to consider some queries. In each query he chooses some rectangle and wonders, how many way are there to put a single domino inside of the chosen rectangle? Input The first line of the input contains two integers h and w (1 ≀ h, w ≀ 500) – the number of rows and the number of columns, respectively. The next h lines describe a grid. Each line contains a string of the length w. Each character is either '.' or '#' β€” denoting an empty or forbidden cell, respectively. The next line contains a single integer q (1 ≀ q ≀ 100 000) β€” the number of queries. Each of the next q lines contains four integers r1i, c1i, r2i, c2i (1 ≀ r1i ≀ r2i ≀ h, 1 ≀ c1i ≀ c2i ≀ w) β€” the i-th query. Numbers r1i and c1i denote the row and the column (respectively) of the upper left cell of the rectangle. Numbers r2i and c2i denote the row and the column (respectively) of the bottom right cell of the rectangle. Output Print q integers, i-th should be equal to the number of ways to put a single domino inside the i-th rectangle. Examples Input 5 8 ....#..# .#...... ##.#.... ##..#.## ........ 4 1 1 2 3 4 1 4 1 1 2 4 5 2 5 5 8 Output 4 0 10 15 Input 7 39 ....................................... .###..###..#..###.....###..###..#..###. ...#..#.#..#..#.........#..#.#..#..#... .###..#.#..#..###.....###..#.#..#..###. .#....#.#..#....#.....#....#.#..#..#.#. .###..###..#..###.....###..###..#..###. ....................................... 6 1 1 3 20 2 10 6 30 2 10 7 30 2 2 7 7 1 7 7 7 1 8 7 8 Output 53 89 120 23 0 2 Note A red frame below corresponds to the first query of the first sample. A domino can be placed in 4 possible ways. <image> Submitted Solution: ``` h,w = map(int, input().split()) lt = [[0 for _ in range(600)] for _ in range(600)] for i in range(1,h+1): s = '0' + input() for j in range(1, w+1): lt[i][j] = s[j] dp1, dp2 = [[0 for _ in range(600)] for _ in range(600)], [[0 for _ in range(600)] for _ in range(600)] for i in range(1, h+1): for j in range(1, w+1): dp1[i][j] = dp1[i-1][j] + dp1[i][j-1] - dp1[i-1][j-1] + (lt[i][j]==lt[i][j-1]=='.') dp2[i][j] = dp2[i-1][j] + dp2[i][j-1] - dp2[i-1][j-1] + (lt[i][j]==lt[i-1][j]=='.') q = int(input()) for _ in range(q): r1, c1, r2, c2 = map(int, input().split()) ans=0 ans+=dp1[r2][c2] - dp1[r1-1][c2] - dp1[r2][c1] + dp1[r1-1][c1] ans+=dp2[r2][c2] - dp2[r1][c2] - dp2[r2][c1-1] + dp2[r1][c1-1] print(ans) ```
instruction
0
61,899
15
123,798
Yes
output
1
61,899
15
123,799
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. They say "years are like dominoes, tumbling one after the other". But would a year fit into a grid? I don't think so. Limak is a little polar bear who loves to play. He has recently got a rectangular grid with h rows and w columns. Each cell is a square, either empty (denoted by '.') or forbidden (denoted by '#'). Rows are numbered 1 through h from top to bottom. Columns are numbered 1 through w from left to right. Also, Limak has a single domino. He wants to put it somewhere in a grid. A domino will occupy exactly two adjacent cells, located either in one row or in one column. Both adjacent cells must be empty and must be inside a grid. Limak needs more fun and thus he is going to consider some queries. In each query he chooses some rectangle and wonders, how many way are there to put a single domino inside of the chosen rectangle? Input The first line of the input contains two integers h and w (1 ≀ h, w ≀ 500) – the number of rows and the number of columns, respectively. The next h lines describe a grid. Each line contains a string of the length w. Each character is either '.' or '#' β€” denoting an empty or forbidden cell, respectively. The next line contains a single integer q (1 ≀ q ≀ 100 000) β€” the number of queries. Each of the next q lines contains four integers r1i, c1i, r2i, c2i (1 ≀ r1i ≀ r2i ≀ h, 1 ≀ c1i ≀ c2i ≀ w) β€” the i-th query. Numbers r1i and c1i denote the row and the column (respectively) of the upper left cell of the rectangle. Numbers r2i and c2i denote the row and the column (respectively) of the bottom right cell of the rectangle. Output Print q integers, i-th should be equal to the number of ways to put a single domino inside the i-th rectangle. Examples Input 5 8 ....#..# .#...... ##.#.... ##..#.## ........ 4 1 1 2 3 4 1 4 1 1 2 4 5 2 5 5 8 Output 4 0 10 15 Input 7 39 ....................................... .###..###..#..###.....###..###..#..###. ...#..#.#..#..#.........#..#.#..#..#... .###..#.#..#..###.....###..#.#..#..###. .#....#.#..#....#.....#....#.#..#..#.#. .###..###..#..###.....###..###..#..###. ....................................... 6 1 1 3 20 2 10 6 30 2 10 7 30 2 2 7 7 1 7 7 7 1 8 7 8 Output 53 89 120 23 0 2 Note A red frame below corresponds to the first query of the first sample. A domino can be placed in 4 possible ways. <image> Submitted Solution: ``` n , m = map(int, input().split()) s = ['#' * (m+2)] for i in range(n): s += ['#'+input()+'#'] s+= ['#' * (m+2)] u = [[0 for i in range(m+2)] for j in range(n+2)] r = [[0 for i in range(m+2)] for j in range(n+2)] for i in range(1, n+1): for j in range(1, m+1): if s[i][j] == '.': if s[i+1][j] == '.': u[i][j] = 1 if s[i][j+1] == '.': r[i][j] = 1 u[i][j]+= u[i-1][j]+u[i][j-1]-u[i-1][j-1] r[i][j]+= r[i-1][j]+r[i][j-1]-r[i-1][j-1] for l in s: print(l) for l in u: print(*l) q = int(input()) for i in range(q): A = 0 x, y, a, b = map(int, input().split()) if x == a and y == b: A = 0 else: A += r[a][b-1]-r[x-1][b-1]-r[a][y-1]+r[x-1][y-1] A += u[a-1][b]-u[x-1][b]-u[a-1][y-1]+u[x-1][y-1] print(A) ```
instruction
0
61,900
15
123,800
No
output
1
61,900
15
123,801
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. They say "years are like dominoes, tumbling one after the other". But would a year fit into a grid? I don't think so. Limak is a little polar bear who loves to play. He has recently got a rectangular grid with h rows and w columns. Each cell is a square, either empty (denoted by '.') or forbidden (denoted by '#'). Rows are numbered 1 through h from top to bottom. Columns are numbered 1 through w from left to right. Also, Limak has a single domino. He wants to put it somewhere in a grid. A domino will occupy exactly two adjacent cells, located either in one row or in one column. Both adjacent cells must be empty and must be inside a grid. Limak needs more fun and thus he is going to consider some queries. In each query he chooses some rectangle and wonders, how many way are there to put a single domino inside of the chosen rectangle? Input The first line of the input contains two integers h and w (1 ≀ h, w ≀ 500) – the number of rows and the number of columns, respectively. The next h lines describe a grid. Each line contains a string of the length w. Each character is either '.' or '#' β€” denoting an empty or forbidden cell, respectively. The next line contains a single integer q (1 ≀ q ≀ 100 000) β€” the number of queries. Each of the next q lines contains four integers r1i, c1i, r2i, c2i (1 ≀ r1i ≀ r2i ≀ h, 1 ≀ c1i ≀ c2i ≀ w) β€” the i-th query. Numbers r1i and c1i denote the row and the column (respectively) of the upper left cell of the rectangle. Numbers r2i and c2i denote the row and the column (respectively) of the bottom right cell of the rectangle. Output Print q integers, i-th should be equal to the number of ways to put a single domino inside the i-th rectangle. Examples Input 5 8 ....#..# .#...... ##.#.... ##..#.## ........ 4 1 1 2 3 4 1 4 1 1 2 4 5 2 5 5 8 Output 4 0 10 15 Input 7 39 ....................................... .###..###..#..###.....###..###..#..###. ...#..#.#..#..#.........#..#.#..#..#... .###..#.#..#..###.....###..#.#..#..###. .#....#.#..#....#.....#....#.#..#..#.#. .###..###..#..###.....###..###..#..###. ....................................... 6 1 1 3 20 2 10 6 30 2 10 7 30 2 2 7 7 1 7 7 7 1 8 7 8 Output 53 89 120 23 0 2 Note A red frame below corresponds to the first query of the first sample. A domino can be placed in 4 possible ways. <image> Submitted Solution: ``` # You lost the game. h,w = map(int, input().split()) G = [str(input()) for _ in range(h)] q = int(input()) L = [list(map(int, input().split())) for _ in range(q)] H = [[0 for _ in range(w)] for _ in range(h)] V = [[0 for _ in range(w)] for _ in range(h)] for i in range(h): for j in range(w): H[i][j] = H[i][j-1] if j and G[i][j] == "." and G[i][j-1] == ".": H[i][j] += 1 for j in range(w): for i in range(h): V[i][j] = V[i-1][j] if i and G[i][j] == "." and G[i-1][j] == ".": V[i][j] += 1 for i in range(1,h): H[i] = [H[i-1][j] + H[i][j] for j in range(w)] for j in range(1,w): for i in range(h): V[i][j] = V[i][j] + V[i][j-1] for Q in L: r1 = Q[0]-1 c1 = Q[1]-1 r2 = Q[2]-1 c2 = Q[3]-1 r = H[r2][c2] + V[r2][c2] if r1: r = r - V[r1][c2] - H[r1-1][c2] if c1: r = r - H[r2][c1] - V[r2][c1-1] if r1 and c1: r = r + H[r1-1][c1-1] + V[r1-1][c1-1] print(max(0,r)) ```
instruction
0
61,901
15
123,802
No
output
1
61,901
15
123,803
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. They say "years are like dominoes, tumbling one after the other". But would a year fit into a grid? I don't think so. Limak is a little polar bear who loves to play. He has recently got a rectangular grid with h rows and w columns. Each cell is a square, either empty (denoted by '.') or forbidden (denoted by '#'). Rows are numbered 1 through h from top to bottom. Columns are numbered 1 through w from left to right. Also, Limak has a single domino. He wants to put it somewhere in a grid. A domino will occupy exactly two adjacent cells, located either in one row or in one column. Both adjacent cells must be empty and must be inside a grid. Limak needs more fun and thus he is going to consider some queries. In each query he chooses some rectangle and wonders, how many way are there to put a single domino inside of the chosen rectangle? Input The first line of the input contains two integers h and w (1 ≀ h, w ≀ 500) – the number of rows and the number of columns, respectively. The next h lines describe a grid. Each line contains a string of the length w. Each character is either '.' or '#' β€” denoting an empty or forbidden cell, respectively. The next line contains a single integer q (1 ≀ q ≀ 100 000) β€” the number of queries. Each of the next q lines contains four integers r1i, c1i, r2i, c2i (1 ≀ r1i ≀ r2i ≀ h, 1 ≀ c1i ≀ c2i ≀ w) β€” the i-th query. Numbers r1i and c1i denote the row and the column (respectively) of the upper left cell of the rectangle. Numbers r2i and c2i denote the row and the column (respectively) of the bottom right cell of the rectangle. Output Print q integers, i-th should be equal to the number of ways to put a single domino inside the i-th rectangle. Examples Input 5 8 ....#..# .#...... ##.#.... ##..#.## ........ 4 1 1 2 3 4 1 4 1 1 2 4 5 2 5 5 8 Output 4 0 10 15 Input 7 39 ....................................... .###..###..#..###.....###..###..#..###. ...#..#.#..#..#.........#..#.#..#..#... .###..#.#..#..###.....###..#.#..#..###. .#....#.#..#....#.....#....#.#..#..#.#. .###..###..#..###.....###..###..#..###. ....................................... 6 1 1 3 20 2 10 6 30 2 10 7 30 2 2 7 7 1 7 7 7 1 8 7 8 Output 53 89 120 23 0 2 Note A red frame below corresponds to the first query of the first sample. A domino can be placed in 4 possible ways. <image> Submitted Solution: ``` # You lost the game. h,w = map(int, input().split()) G = [str(input()) for _ in range(h)] q = int(input()) L = [list(map(int, input().split())) for _ in range(q)] H = [[0 for _ in range(w)] for _ in range(h)] V = [[0 for _ in range(w)] for _ in range(h)] for i in range(h): for j in range(w): H[i][j] = H[i][j-1] if j and G[i][j] == "." and G[i][j-1] == ".": H[i][j] += 1 for j in range(w): for i in range(h): V[i][j] = V[i-1][j] if i and G[i][j] == "." and G[i-1][j] == ".": V[i][j] += 1 for i in range(1,h): H[i] = [H[i-1][j] + H[i][j] for j in range(w)] for j in range(1,w): for i in range(h): V[i][j] = V[i][j] + V[i][j-1] for Q in L: r1 = Q[0]-1 c1 = Q[1]-1 r2 = Q[2]-1 c2 = Q[3]-1 r = H[r2][c2] + V[r2][c2] - H[r2][c1] - V[r1][c2] if r1: r = r - H[r1-1][c2] if c1: r = r - V[r2][c1-1] if r1 and c1: r = r + H[r1-1][c1-1] + V[r1][c1-1] print(max(0,r)) ```
instruction
0
61,902
15
123,804
No
output
1
61,902
15
123,805
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. They say "years are like dominoes, tumbling one after the other". But would a year fit into a grid? I don't think so. Limak is a little polar bear who loves to play. He has recently got a rectangular grid with h rows and w columns. Each cell is a square, either empty (denoted by '.') or forbidden (denoted by '#'). Rows are numbered 1 through h from top to bottom. Columns are numbered 1 through w from left to right. Also, Limak has a single domino. He wants to put it somewhere in a grid. A domino will occupy exactly two adjacent cells, located either in one row or in one column. Both adjacent cells must be empty and must be inside a grid. Limak needs more fun and thus he is going to consider some queries. In each query he chooses some rectangle and wonders, how many way are there to put a single domino inside of the chosen rectangle? Input The first line of the input contains two integers h and w (1 ≀ h, w ≀ 500) – the number of rows and the number of columns, respectively. The next h lines describe a grid. Each line contains a string of the length w. Each character is either '.' or '#' β€” denoting an empty or forbidden cell, respectively. The next line contains a single integer q (1 ≀ q ≀ 100 000) β€” the number of queries. Each of the next q lines contains four integers r1i, c1i, r2i, c2i (1 ≀ r1i ≀ r2i ≀ h, 1 ≀ c1i ≀ c2i ≀ w) β€” the i-th query. Numbers r1i and c1i denote the row and the column (respectively) of the upper left cell of the rectangle. Numbers r2i and c2i denote the row and the column (respectively) of the bottom right cell of the rectangle. Output Print q integers, i-th should be equal to the number of ways to put a single domino inside the i-th rectangle. Examples Input 5 8 ....#..# .#...... ##.#.... ##..#.## ........ 4 1 1 2 3 4 1 4 1 1 2 4 5 2 5 5 8 Output 4 0 10 15 Input 7 39 ....................................... .###..###..#..###.....###..###..#..###. ...#..#.#..#..#.........#..#.#..#..#... .###..#.#..#..###.....###..#.#..#..###. .#....#.#..#....#.....#....#.#..#..#.#. .###..###..#..###.....###..###..#..###. ....................................... 6 1 1 3 20 2 10 6 30 2 10 7 30 2 2 7 7 1 7 7 7 1 8 7 8 Output 53 89 120 23 0 2 Note A red frame below corresponds to the first query of the first sample. A domino can be placed in 4 possible ways. <image> Submitted Solution: ``` n,m=map(int,input().split()) a=[] for i in range(n): s=input() tmp=list(s) a.append(tmp) right=[] up=[] for i in range(n): tmp1=[] tmp2=[] for j in range(m): tmp1.append(0) tmp2.append(0) right.append(tmp1) up.append(tmp2) for i in range(1,m): if a[0][i]=="#": right[0][i]=right[0][i-1] else: if a[0][i-1]==".": right[0][i]=1+right[0][i-1] else: right[0][i] = right[0][i - 1] for i in range(1,n): if a[i][0]=="#": up[i][0]=up[i-1][0] else: if a[i-1][0]==".": up[i][0]=1+up[i-1][0] else: up[i][0] = up[i - 1][0] for i in range(1,n): for j in range(1,m): if a[i][j]=="#": right[i][j]=right[i-1][j]+right[i][j-1]-right[i-1][j-1] up[i][j]=up[i-1][j]+up[i][j-1]-up[i-1][j-1] else: if a[i][j-1]==".": right[i][j]=1+right[i-1][j]+right[i][j-1]-right[i-1][j-1] else: right[i][j] = right[i - 1][j] + right[i][j - 1] - right[i - 1][j - 1] if a[i-1][j]==".": up[i][j]=up[i-1][j]+up[i][j-1]-up[i-1][j-1]+1 else: up[i][j] = up[i - 1][j] + up[i][j - 1] - up[i - 1][j - 1] q=int(input()) for _ in range(q): i1,j1,i2,j2=map(int,input().split()) i1-=1 j1-=1 i2-=1 j2-=1 if i1>0: ans=right[i2][j2]-right[i2][j1]-right[i1-1][j2]+right[i1-1][j1] else: ans = right[i2][j2] - right[i2][j1] if j1>0: ans+=up[i2][j2]-up[i2][j1-1]-up[i1][j2]+up[i1][j1] else: ans += up[i2][j2] - up[i1][j2] print(ans) ```
instruction
0
61,903
15
123,806
No
output
1
61,903
15
123,807
Provide a correct Python 3 solution for this coding contest problem. Description F, who likes to dance, decided to practice a hardcore dance called JUMP STYLE at a certain dance hall. The floor is tiled in an N Γ— N grid. To support the dance practice, each tile has the coordinates of the tile to jump to the next step. F has strong motor nerves through daily practice and can jump to any tile on the floor. F realized that if he continued this dance for a long enough time, he would eventually enter a steady state and just loop on the same route. F wondered how many such loops existed on this floor and decided to talk to you, the programmer. Input The input consists of multiple test cases. Each test case begins with one line containing the length N of one side of the floor. (1 ≀ N ≀ 100) The following N lines represent the coordinates of the jump destination written on each tile on the floor as follows. \\ begin {array} {ccccccc} x_ {0,0} & y_ {0,0} & x_ {1,0} & y_ {1,0} & \\ cdots & x_ {N-1,0} & y_ {N-1,0} \ \\\ x_ {0,1} & y_ {0,1} & x_ {1,1} & y_ {1,1} & \\ cdots & x_ {N-1,1} & y_ {N-1,1} \ \\\ \\ vdots & \\ vdots & \\ vdots & \\ vdots & \\ ddots & \\ vdots & \\ vdots \\\\ x_ {0, N-1} & y_ {0, N-1} & x_ {1, N-1} & y_ {1, N-1} & \\ cdots & x_ {N-1, N-1} & y_ {N-1, N-1} \\ end {array} x_ {i, j} and \\ y_ {i, j} represent the x-coordinate and y-coordinate of the jump destination written on the tile at the coordinate (i, j), respectively. The coordinates are 0-origin, and the values ​​of all coordinates are integers greater than or equal to 0 and less than N. The input ends with a line consisting of only 0s. Output For each test case, the number of different loops is output in one line. Example Input 1 0 0 2 1 1 0 1 1 0 0 0 2 1 1 0 1 1 1 1 0 3 0 1 2 2 2 1 0 2 1 2 2 1 0 0 0 1 1 1 4 3 2 2 0 3 2 2 1 1 1 0 3 1 1 3 1 0 3 2 3 3 0 2 3 1 1 1 1 3 2 1 3 0 Output 1 2 1 2 3
instruction
0
62,222
15
124,444
"Correct Solution: ``` while True: n = int(input()) if n == 0:break to = [] fr = [[] for _ in range(n * n)] for i in range(n): line = list(map(int, input().split())) for j in range(n): x, y = line[2 * j:2 * j + 2] to.append(y * n + x) fr[y * n + x].append(i * n + j) order = [] used = [False] * (n * n) def dfs(x): if used[x]:return used[x] = True dfs(to[x]) order.append(x) for i in range(n * n): dfs(i) order.reverse() def dfs2(x, used, group): if used[x]:return False if x in group:return True group.append(x) ret = False ret = ret or dfs2(to[x], used, group) return ret used = [False] * (n * n) ans = 0 for i in order: group = [] if not used[i]: if dfs2(i, used, group):ans += 1 for g in group:used[g] = True print(ans) ```
output
1
62,222
15
124,445
Provide a correct Python 3 solution for this coding contest problem. Description F, who likes to dance, decided to practice a hardcore dance called JUMP STYLE at a certain dance hall. The floor is tiled in an N Γ— N grid. To support the dance practice, each tile has the coordinates of the tile to jump to the next step. F has strong motor nerves through daily practice and can jump to any tile on the floor. F realized that if he continued this dance for a long enough time, he would eventually enter a steady state and just loop on the same route. F wondered how many such loops existed on this floor and decided to talk to you, the programmer. Input The input consists of multiple test cases. Each test case begins with one line containing the length N of one side of the floor. (1 ≀ N ≀ 100) The following N lines represent the coordinates of the jump destination written on each tile on the floor as follows. \\ begin {array} {ccccccc} x_ {0,0} & y_ {0,0} & x_ {1,0} & y_ {1,0} & \\ cdots & x_ {N-1,0} & y_ {N-1,0} \ \\\ x_ {0,1} & y_ {0,1} & x_ {1,1} & y_ {1,1} & \\ cdots & x_ {N-1,1} & y_ {N-1,1} \ \\\ \\ vdots & \\ vdots & \\ vdots & \\ vdots & \\ ddots & \\ vdots & \\ vdots \\\\ x_ {0, N-1} & y_ {0, N-1} & x_ {1, N-1} & y_ {1, N-1} & \\ cdots & x_ {N-1, N-1} & y_ {N-1, N-1} \\ end {array} x_ {i, j} and \\ y_ {i, j} represent the x-coordinate and y-coordinate of the jump destination written on the tile at the coordinate (i, j), respectively. The coordinates are 0-origin, and the values ​​of all coordinates are integers greater than or equal to 0 and less than N. The input ends with a line consisting of only 0s. Output For each test case, the number of different loops is output in one line. Example Input 1 0 0 2 1 1 0 1 1 0 0 0 2 1 1 0 1 1 1 1 0 3 0 1 2 2 2 1 0 2 1 2 2 1 0 0 0 1 1 1 4 3 2 2 0 3 2 2 1 1 1 0 3 1 1 3 1 0 3 2 3 3 0 2 3 1 1 1 1 3 2 1 3 0 Output 1 2 1 2 3
instruction
0
62,223
15
124,446
"Correct Solution: ``` while True: n = int(input()) if n == 0:break to = [] for i in range(n): line = list(map(int, input().split())) for j in range(n): x, y = line[2 * j:2 * j + 2] to.append(y * n + x) order = [] used = [False] * (n * n) def dfs(x): if used[x]:return used[x] = True dfs(to[x]) order.append(x) for i in range(n * n): dfs(i) order.reverse() def dfs2(x, used, group): if used[x]:return False if x in group:return True group.add(x) return dfs2(to[x], used, group) used = [False] * (n * n) ans = 0 for i in order: group = set() if not used[i]: if dfs2(i, used, group):ans += 1 for g in group:used[g] = True print(ans) ```
output
1
62,223
15
124,447
Provide a correct Python 3 solution for this coding contest problem. Description F, who likes to dance, decided to practice a hardcore dance called JUMP STYLE at a certain dance hall. The floor is tiled in an N Γ— N grid. To support the dance practice, each tile has the coordinates of the tile to jump to the next step. F has strong motor nerves through daily practice and can jump to any tile on the floor. F realized that if he continued this dance for a long enough time, he would eventually enter a steady state and just loop on the same route. F wondered how many such loops existed on this floor and decided to talk to you, the programmer. Input The input consists of multiple test cases. Each test case begins with one line containing the length N of one side of the floor. (1 ≀ N ≀ 100) The following N lines represent the coordinates of the jump destination written on each tile on the floor as follows. \\ begin {array} {ccccccc} x_ {0,0} & y_ {0,0} & x_ {1,0} & y_ {1,0} & \\ cdots & x_ {N-1,0} & y_ {N-1,0} \ \\\ x_ {0,1} & y_ {0,1} & x_ {1,1} & y_ {1,1} & \\ cdots & x_ {N-1,1} & y_ {N-1,1} \ \\\ \\ vdots & \\ vdots & \\ vdots & \\ vdots & \\ ddots & \\ vdots & \\ vdots \\\\ x_ {0, N-1} & y_ {0, N-1} & x_ {1, N-1} & y_ {1, N-1} & \\ cdots & x_ {N-1, N-1} & y_ {N-1, N-1} \\ end {array} x_ {i, j} and \\ y_ {i, j} represent the x-coordinate and y-coordinate of the jump destination written on the tile at the coordinate (i, j), respectively. The coordinates are 0-origin, and the values ​​of all coordinates are integers greater than or equal to 0 and less than N. The input ends with a line consisting of only 0s. Output For each test case, the number of different loops is output in one line. Example Input 1 0 0 2 1 1 0 1 1 0 0 0 2 1 1 0 1 1 1 1 0 3 0 1 2 2 2 1 0 2 1 2 2 1 0 0 0 1 1 1 4 3 2 2 0 3 2 2 1 1 1 0 3 1 1 3 1 0 3 2 3 3 0 2 3 1 1 1 1 3 2 1 3 0 Output 1 2 1 2 3
instruction
0
62,224
15
124,448
"Correct Solution: ``` while True: n = int(input()) if n == 0:break to = [] for i in range(n): line = list(map(int, input().split())) for j in range(n): x, y = line[2 * j:2 * j + 2] to.append(y * n + x) order = [] used = [False] * (n * n) def dfs(x): if used[x]:return used[x] = True dfs(to[x]) order.append(x) for i in range(n * n): dfs(i) order.reverse() def dfs2(x, used, group): if x in group:return True if used[x]:return False group.add(x) used[x] = True return dfs2(to[x], used, group) used = [False] * (n * n) ans = 0 for i in order: group = set() if not used[i]: if dfs2(i, used, group):ans += 1 print(ans) ```
output
1
62,224
15
124,449
Provide a correct Python 3 solution for this coding contest problem. Description F, who likes to dance, decided to practice a hardcore dance called JUMP STYLE at a certain dance hall. The floor is tiled in an N Γ— N grid. To support the dance practice, each tile has the coordinates of the tile to jump to the next step. F has strong motor nerves through daily practice and can jump to any tile on the floor. F realized that if he continued this dance for a long enough time, he would eventually enter a steady state and just loop on the same route. F wondered how many such loops existed on this floor and decided to talk to you, the programmer. Input The input consists of multiple test cases. Each test case begins with one line containing the length N of one side of the floor. (1 ≀ N ≀ 100) The following N lines represent the coordinates of the jump destination written on each tile on the floor as follows. \\ begin {array} {ccccccc} x_ {0,0} & y_ {0,0} & x_ {1,0} & y_ {1,0} & \\ cdots & x_ {N-1,0} & y_ {N-1,0} \ \\\ x_ {0,1} & y_ {0,1} & x_ {1,1} & y_ {1,1} & \\ cdots & x_ {N-1,1} & y_ {N-1,1} \ \\\ \\ vdots & \\ vdots & \\ vdots & \\ vdots & \\ ddots & \\ vdots & \\ vdots \\\\ x_ {0, N-1} & y_ {0, N-1} & x_ {1, N-1} & y_ {1, N-1} & \\ cdots & x_ {N-1, N-1} & y_ {N-1, N-1} \\ end {array} x_ {i, j} and \\ y_ {i, j} represent the x-coordinate and y-coordinate of the jump destination written on the tile at the coordinate (i, j), respectively. The coordinates are 0-origin, and the values ​​of all coordinates are integers greater than or equal to 0 and less than N. The input ends with a line consisting of only 0s. Output For each test case, the number of different loops is output in one line. Example Input 1 0 0 2 1 1 0 1 1 0 0 0 2 1 1 0 1 1 1 1 0 3 0 1 2 2 2 1 0 2 1 2 2 1 0 0 0 1 1 1 4 3 2 2 0 3 2 2 1 1 1 0 3 1 1 3 1 0 3 2 3 3 0 2 3 1 1 1 1 3 2 1 3 0 Output 1 2 1 2 3
instruction
0
62,225
15
124,450
"Correct Solution: ``` while True: n = int(input()) if n == 0:break to = [] fr = [[] for _ in range(n * n)] for i in range(n): line = list(map(int, input().split())) for j in range(n): x, y = line[2 * j:2 * j + 2] to.append(y * n + x) fr[y * n + x].append(i * n + j) order = [] used = [False] * (n * n) def dfs(x): if used[x]:return used[x] = True dfs(to[x]) order.append(x) for i in range(n * n): dfs(i) def dfs2(x, used, group): if used[x]:return used[x] = True for f in fr[x]: dfs2(f, used, group) group.append(x) used = [False] * (n * n) ans = 0 for i in order: group = [] if not used[i]: dfs2(i, used, group) if len(group) >= 1: ans += 1 print(ans) ```
output
1
62,225
15
124,451
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Note that the difference between easy and hard versions is that in hard version unavailable cells can become available again and in easy version can't. You can make hacks only if all versions are solved. Ildar and Ivan are tired of chess, but they really like the chessboard, so they invented a new game. The field is a chessboard 2n Γ— 2m: it has 2n rows, 2m columns, and the cell in row i and column j is colored white if i+j is even, and is colored black otherwise. The game proceeds as follows: Ildar marks some of the white cells of the chessboard as unavailable, and asks Ivan to place n Γ— m kings on the remaining white cells in such way, so that there are no kings attacking each other. A king can attack another king if they are located in the adjacent cells, sharing an edge or a corner. Ildar would like to explore different combinations of cells. Initially all cells are marked as available, and then he has q queries. In each query he marks a cell as unavailable. After each query he would like to know whether it is possible to place the kings on the available cells in a desired way. Please help him! Input The first line of input contains three integers n, m, q (1 ≀ n, m, q ≀ 200 000) β€” the size of the board and the number of queries. q lines follow, each of them contains a description of a query: two integers i and j, denoting a white cell (i, j) on the board (1 ≀ i ≀ 2n, 1 ≀ j ≀ 2m, i + j is even) that becomes unavailable. It's guaranteed, that each cell (i, j) appears in input at most once. Output Output q lines, i-th line should contain answer for a board after i queries of Ildar. This line should contain "YES" if it is possible to place the kings on the available cells in the desired way, or "NO" otherwise. Examples Input 1 3 3 1 1 1 5 2 4 Output YES YES NO Input 3 2 7 4 2 6 4 1 3 2 2 2 4 4 4 3 1 Output YES YES NO NO NO NO NO Note In the first example case after the second query only cells (1, 1) and (1, 5) are unavailable. Then Ivan can place three kings on cells (2, 2), (2, 4) and (2, 6). After the third query three cells (1, 1), (1, 5) and (2, 4) are unavailable, so there remain only 3 available cells: (2, 2), (1, 3) and (2, 6). Ivan can not put 3 kings on those cells, because kings on cells (2, 2) and (1, 3) attack each other, since these cells share a corner. Submitted Solution: ``` from bisect import bisect_left as bl from bisect import bisect_right as br from heapq import heappush,heappop,heapify import math from collections import * from functools import reduce,cmp_to_key import sys input = sys.stdin.readline M = mod = 998244353 def factors(n):return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))) def inv_mod(n):return pow(n, mod - 2, mod) def li():return [int(i) for i in input().rstrip('\n').split()] def st():return input().rstrip('\n') def val():return int(input().rstrip('\n')) def li2():return [i for i in input().rstrip('\n')] def li3():return [int(i) for i in input().rstrip('\n')] n, m, q = li() L = [] R = [] visited = {} ans = 1 for i in range(q): a, b = li() if (a + b)&1: print('YES' if ans else 'NO') continue if (a, b) in visited: visited.pop((a, b)) while L and L[0] not in visited:heappop(L) while R and (-R[0][0],-R[1][1]) not in visited:heappop(R) else: visited[(a, b)] = 1 if a & 1: heappush(L,(a,b)) else: heappush(R,(-a,-b)) ans = 1 if len(L) and len(R) and L[0][0] <= -R[0][0] and L[0][1] <= -R[0][1]: ans = 0 print('YES' if ans else 'NO') ```
instruction
0
62,473
15
124,946
No
output
1
62,473
15
124,947
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Note that the difference between easy and hard versions is that in hard version unavailable cells can become available again and in easy version can't. You can make hacks only if all versions are solved. Ildar and Ivan are tired of chess, but they really like the chessboard, so they invented a new game. The field is a chessboard 2n Γ— 2m: it has 2n rows, 2m columns, and the cell in row i and column j is colored white if i+j is even, and is colored black otherwise. The game proceeds as follows: Ildar marks some of the white cells of the chessboard as unavailable, and asks Ivan to place n Γ— m kings on the remaining white cells in such way, so that there are no kings attacking each other. A king can attack another king if they are located in the adjacent cells, sharing an edge or a corner. Ildar would like to explore different combinations of cells. Initially all cells are marked as available, and then he has q queries. In each query he marks a cell as unavailable. After each query he would like to know whether it is possible to place the kings on the available cells in a desired way. Please help him! Input The first line of input contains three integers n, m, q (1 ≀ n, m, q ≀ 200 000) β€” the size of the board and the number of queries. q lines follow, each of them contains a description of a query: two integers i and j, denoting a white cell (i, j) on the board (1 ≀ i ≀ 2n, 1 ≀ j ≀ 2m, i + j is even) that becomes unavailable. It's guaranteed, that each cell (i, j) appears in input at most once. Output Output q lines, i-th line should contain answer for a board after i queries of Ildar. This line should contain "YES" if it is possible to place the kings on the available cells in the desired way, or "NO" otherwise. Examples Input 1 3 3 1 1 1 5 2 4 Output YES YES NO Input 3 2 7 4 2 6 4 1 3 2 2 2 4 4 4 3 1 Output YES YES NO NO NO NO NO Note In the first example case after the second query only cells (1, 1) and (1, 5) are unavailable. Then Ivan can place three kings on cells (2, 2), (2, 4) and (2, 6). After the third query three cells (1, 1), (1, 5) and (2, 4) are unavailable, so there remain only 3 available cells: (2, 2), (1, 3) and (2, 6). Ivan can not put 3 kings on those cells, because kings on cells (2, 2) and (1, 3) attack each other, since these cells share a corner. Submitted Solution: ``` n,m,q=map(int,input().split()) c=True mp=0 while q: x,y=map(int,input().split()) if mp==0: mp=x elif abs(x-mp)%2: c=False if c: print("YES") else: print("NO") q=q-1 ```
instruction
0
62,474
15
124,948
No
output
1
62,474
15
124,949
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Note that the difference between easy and hard versions is that in hard version unavailable cells can become available again and in easy version can't. You can make hacks only if all versions are solved. Ildar and Ivan are tired of chess, but they really like the chessboard, so they invented a new game. The field is a chessboard 2n Γ— 2m: it has 2n rows, 2m columns, and the cell in row i and column j is colored white if i+j is even, and is colored black otherwise. The game proceeds as follows: Ildar marks some of the white cells of the chessboard as unavailable, and asks Ivan to place n Γ— m kings on the remaining white cells in such way, so that there are no kings attacking each other. A king can attack another king if they are located in the adjacent cells, sharing an edge or a corner. Ildar would like to explore different combinations of cells. Initially all cells are marked as available, and then he has q queries. In each query he marks a cell as unavailable. After each query he would like to know whether it is possible to place the kings on the available cells in a desired way. Please help him! Input The first line of input contains three integers n, m, q (1 ≀ n, m, q ≀ 200 000) β€” the size of the board and the number of queries. q lines follow, each of them contains a description of a query: two integers i and j, denoting a white cell (i, j) on the board (1 ≀ i ≀ 2n, 1 ≀ j ≀ 2m, i + j is even) that becomes unavailable. It's guaranteed, that each cell (i, j) appears in input at most once. Output Output q lines, i-th line should contain answer for a board after i queries of Ildar. This line should contain "YES" if it is possible to place the kings on the available cells in the desired way, or "NO" otherwise. Examples Input 1 3 3 1 1 1 5 2 4 Output YES YES NO Input 3 2 7 4 2 6 4 1 3 2 2 2 4 4 4 3 1 Output YES YES NO NO NO NO NO Note In the first example case after the second query only cells (1, 1) and (1, 5) are unavailable. Then Ivan can place three kings on cells (2, 2), (2, 4) and (2, 6). After the third query three cells (1, 1), (1, 5) and (2, 4) are unavailable, so there remain only 3 available cells: (2, 2), (1, 3) and (2, 6). Ivan can not put 3 kings on those cells, because kings on cells (2, 2) and (1, 3) attack each other, since these cells share a corner. Submitted Solution: ``` def valid_move(blocked, move): if move[0] % 2 \ and blocked[0] > move[0] \ and blocked[1] > move[1] \ and not blocked[0] % 2: return False if not (move[0] % 2) \ and blocked[0] < move[0] \ and blocked[1] < move[1] \ and blocked[0] % 2: return False return True def needs_no_adding(blocked, move): if move[0] % 2: return blocked[0] < move[0] and blocked[1] < move[1] return blocked[0] > move[0] and blocked[1] > move[1] n, m, q = input().split(' ') n, m, q = int(n), int(m), int(q) moves = set() dead = False for _ in range(q): i, j = input().split(' ') i, j = int(i), int(j) if not dead: for p in moves: vm = valid_move(p, (i, j)) if not vm: dead = True if not any([needs_no_adding(b, (i, j)) for b in moves]): moves.add((i, j)) print('NO' if dead else 'YES') ```
instruction
0
62,475
15
124,950
No
output
1
62,475
15
124,951
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Note that the difference between easy and hard versions is that in hard version unavailable cells can become available again and in easy version can't. You can make hacks only if all versions are solved. Ildar and Ivan are tired of chess, but they really like the chessboard, so they invented a new game. The field is a chessboard 2n Γ— 2m: it has 2n rows, 2m columns, and the cell in row i and column j is colored white if i+j is even, and is colored black otherwise. The game proceeds as follows: Ildar marks some of the white cells of the chessboard as unavailable, and asks Ivan to place n Γ— m kings on the remaining white cells in such way, so that there are no kings attacking each other. A king can attack another king if they are located in the adjacent cells, sharing an edge or a corner. Ildar would like to explore different combinations of cells. Initially all cells are marked as available, and then he has q queries. In each query he marks a cell as unavailable. After each query he would like to know whether it is possible to place the kings on the available cells in a desired way. Please help him! Input The first line of input contains three integers n, m, q (1 ≀ n, m, q ≀ 200 000) β€” the size of the board and the number of queries. q lines follow, each of them contains a description of a query: two integers i and j, denoting a white cell (i, j) on the board (1 ≀ i ≀ 2n, 1 ≀ j ≀ 2m, i + j is even) that becomes unavailable. It's guaranteed, that each cell (i, j) appears in input at most once. Output Output q lines, i-th line should contain answer for a board after i queries of Ildar. This line should contain "YES" if it is possible to place the kings on the available cells in the desired way, or "NO" otherwise. Examples Input 1 3 3 1 1 1 5 2 4 Output YES YES NO Input 3 2 7 4 2 6 4 1 3 2 2 2 4 4 4 3 1 Output YES YES NO NO NO NO NO Note In the first example case after the second query only cells (1, 1) and (1, 5) are unavailable. Then Ivan can place three kings on cells (2, 2), (2, 4) and (2, 6). After the third query three cells (1, 1), (1, 5) and (2, 4) are unavailable, so there remain only 3 available cells: (2, 2), (1, 3) and (2, 6). Ivan can not put 3 kings on those cells, because kings on cells (2, 2) and (1, 3) attack each other, since these cells share a corner. Submitted Solution: ``` from bisect import bisect_left as bl from bisect import bisect_right as br from heapq import heappush,heappop,heapify import math from collections import * from functools import reduce,cmp_to_key import sys input = sys.stdin.readline M = mod = 998244353 def factors(n):return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))) def inv_mod(n):return pow(n, mod - 2, mod) def li():return [int(i) for i in input().rstrip('\n').split()] def st():return input().rstrip('\n') def val():return int(input().rstrip('\n')) def li2():return [i for i in input().rstrip('\n')] def li3():return [int(i) for i in input().rstrip('\n')] ```
instruction
0
62,476
15
124,952
No
output
1
62,476
15
124,953
Provide tags and a correct Python 3 solution for this coding contest problem. Captain Marmot wants to prepare a huge and important battle against his enemy, Captain Snake. For this battle he has n regiments, each consisting of 4 moles. Initially, each mole i (1 ≀ i ≀ 4n) is placed at some position (xi, yi) in the Cartesian plane. Captain Marmot wants to move some moles to make the regiments compact, if it's possible. Each mole i has a home placed at the position (ai, bi). Moving this mole one time means rotating his position point (xi, yi) 90 degrees counter-clockwise around it's home point (ai, bi). A regiment is compact only if the position points of the 4 moles form a square with non-zero area. Help Captain Marmot to find out for each regiment the minimal number of moves required to make that regiment compact, if it's possible. Input The first line contains one integer n (1 ≀ n ≀ 100), the number of regiments. The next 4n lines contain 4 integers xi, yi, ai, bi ( - 104 ≀ xi, yi, ai, bi ≀ 104). Output Print n lines to the standard output. If the regiment i can be made compact, the i-th line should contain one integer, the minimal number of required moves. Otherwise, on the i-th line print "-1" (without quotes). Examples Input 4 1 1 0 0 -1 1 0 0 -1 1 0 0 1 -1 0 0 1 1 0 0 -2 1 0 0 -1 1 0 0 1 -1 0 0 1 1 0 0 -1 1 0 0 -1 1 0 0 -1 1 0 0 2 2 0 1 -1 0 0 -2 3 0 0 -2 -1 1 -2 0 Output 1 -1 3 3 Note In the first regiment we can move once the second or the third mole. We can't make the second regiment compact. In the third regiment, from the last 3 moles we can move once one and twice another one. In the fourth regiment, we can move twice the first mole and once the third mole.
instruction
0
62,704
15
125,408
Tags: brute force, geometry Correct Solution: ``` import copy import sys def solve(): n = int(input()) for reg in range(n): temp = help() print(-1 if temp == 1000 else temp) def help(): res = 1000 read = [list(map(int, input().split())) for _ in range(4)] for _1 in range(4): for _2 in range(4): for _3 in range(4): for _4 in range(4): l = copy.deepcopy(read) for i in range(_1): rot(l[0]) for i in range(_2): rot(l[1]) for i in range(_3): rot(l[2]) for i in range(_4): rot(l[3]) if square(l): res = min(res, _1 + _2 + _3 + _4) return res def rot(l): x = l[0] y = l[1] homex = l[2] homey = l[3] yabove = y - homey xright = x - homex newx = homex - yabove newy = homey + xright l[0] = newx l[1] = newy return l def square(l): distances = list() for i in range(4): for j in range(i + 1, 4): distances.append(dist(l[i], l[j])) distances.sort() if distances[0] < 0.000001: return False #same point different = 0 for i in range(len(distances) - 1): if abs(distances[i] - distances[i+1]) > 0.000001: different += 1 return different == 1 def dist(a, b): return (a[0] - b[0]) * (a[0] - b[0]) + (a[1] - b[1]) * (a[1] - b[1]) if sys.hexversion == 50594544 : sys.stdin = open("test.txt") # print(rot(rot(rot(rot([-11, -22, 2, 3]))))) solve() ```
output
1
62,704
15
125,409
Provide tags and a correct Python 3 solution for this coding contest problem. Captain Marmot wants to prepare a huge and important battle against his enemy, Captain Snake. For this battle he has n regiments, each consisting of 4 moles. Initially, each mole i (1 ≀ i ≀ 4n) is placed at some position (xi, yi) in the Cartesian plane. Captain Marmot wants to move some moles to make the regiments compact, if it's possible. Each mole i has a home placed at the position (ai, bi). Moving this mole one time means rotating his position point (xi, yi) 90 degrees counter-clockwise around it's home point (ai, bi). A regiment is compact only if the position points of the 4 moles form a square with non-zero area. Help Captain Marmot to find out for each regiment the minimal number of moves required to make that regiment compact, if it's possible. Input The first line contains one integer n (1 ≀ n ≀ 100), the number of regiments. The next 4n lines contain 4 integers xi, yi, ai, bi ( - 104 ≀ xi, yi, ai, bi ≀ 104). Output Print n lines to the standard output. If the regiment i can be made compact, the i-th line should contain one integer, the minimal number of required moves. Otherwise, on the i-th line print "-1" (without quotes). Examples Input 4 1 1 0 0 -1 1 0 0 -1 1 0 0 1 -1 0 0 1 1 0 0 -2 1 0 0 -1 1 0 0 1 -1 0 0 1 1 0 0 -1 1 0 0 -1 1 0 0 -1 1 0 0 2 2 0 1 -1 0 0 -2 3 0 0 -2 -1 1 -2 0 Output 1 -1 3 3 Note In the first regiment we can move once the second or the third mole. We can't make the second regiment compact. In the third regiment, from the last 3 moles we can move once one and twice another one. In the fourth regiment, we can move twice the first mole and once the third mole.
instruction
0
62,705
15
125,410
Tags: brute force, geometry Correct Solution: ``` #!/usr/bin/python3 from itertools import product, combinations def process(case): regiment = map(rotate, homes, positions, case) return sum(case) if is_square(regiment) else float('inf') def is_square(regiment): dists = list(map(lambda arg: dist_sq(*arg), combinations(regiment, 2))) dists.sort() return 0 < dists[0] == dists[3] < dists[4] == dists[5] def dist_sq(p1, p2): return (p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2 def rotate(home, position, step): (x, y), (a, b) = position, home if step == 0: return x, y if step == 1: return a-(y-b), b+(x-a) if step == 2: return a-(x-a), b-(y-b) if step == 3: return a+(y-b), b-(x-a) n = int(input()) for r in range(n): positions = [] homes = [] for i in range(4): x, y, a, b = map(int, input().split()) positions.append((x, y)) homes.append((a, b)) all_cases = product(range(4), repeat=4) result = min(map(process, all_cases)) print(result if result != float('inf') else -1) ```
output
1
62,705
15
125,411
Provide tags and a correct Python 3 solution for this coding contest problem. Captain Marmot wants to prepare a huge and important battle against his enemy, Captain Snake. For this battle he has n regiments, each consisting of 4 moles. Initially, each mole i (1 ≀ i ≀ 4n) is placed at some position (xi, yi) in the Cartesian plane. Captain Marmot wants to move some moles to make the regiments compact, if it's possible. Each mole i has a home placed at the position (ai, bi). Moving this mole one time means rotating his position point (xi, yi) 90 degrees counter-clockwise around it's home point (ai, bi). A regiment is compact only if the position points of the 4 moles form a square with non-zero area. Help Captain Marmot to find out for each regiment the minimal number of moves required to make that regiment compact, if it's possible. Input The first line contains one integer n (1 ≀ n ≀ 100), the number of regiments. The next 4n lines contain 4 integers xi, yi, ai, bi ( - 104 ≀ xi, yi, ai, bi ≀ 104). Output Print n lines to the standard output. If the regiment i can be made compact, the i-th line should contain one integer, the minimal number of required moves. Otherwise, on the i-th line print "-1" (without quotes). Examples Input 4 1 1 0 0 -1 1 0 0 -1 1 0 0 1 -1 0 0 1 1 0 0 -2 1 0 0 -1 1 0 0 1 -1 0 0 1 1 0 0 -1 1 0 0 -1 1 0 0 -1 1 0 0 2 2 0 1 -1 0 0 -2 3 0 0 -2 -1 1 -2 0 Output 1 -1 3 3 Note In the first regiment we can move once the second or the third mole. We can't make the second regiment compact. In the third regiment, from the last 3 moles we can move once one and twice another one. In the fourth regiment, we can move twice the first mole and once the third mole.
instruction
0
62,706
15
125,412
Tags: brute force, geometry 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") ALPHA='abcdefghijklmnopqrstuvwxyz' M=10**9+7 EPS=1e-6 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() def RotateAntiClockBy90(P,H): x,y=P a,b=H x-=a y-=b X=-y+a Y=x+b return (X,Y) def Dist(p1,p2): return (p2[1]-p1[1])**2 + (p2[0]-p1[0])**2 def isSquare(P): p1,p2,p3,p4=P # print(p1,p2,p3,p4) d2 = Dist(p1,p2) d3 = Dist(p1,p3) d4 = Dist(p1,p4) if(d2==0 or d3==0 or d4==0): return False if(d2==d3 and 2*d2==d4 and 2*Dist(p2,p4)==Dist(p2,p3)): return True if(d3==d4 and 2*d3==d2 and 2*Dist(p3,p2)==Dist(p3,p4)): return True if(d2==d4 and 2*d2==d3 and 2*Dist(p2,p3)==Dist(p2,p4)): return True return False for _ in range(Int()): home=[] point=[] for i in range(4): V=value() point.append((V[0],V[1])) home.append((V[2],V[3])) ans=[inf] heapify(ans) # print(point) # print(home) # print() for i in range(4): point[0]=RotateAntiClockBy90(point[0],home[0]) cost1=(i+1)%4 for j in range(4): cost2=(j+1)%4 point[1]=RotateAntiClockBy90(point[1],home[1]) for k in range(4): cost3=(k+1)%4 point[2]=RotateAntiClockBy90(point[2],home[2]) for l in range(4): cost4=(l+1)%4 point[3]=RotateAntiClockBy90(point[3],home[3]) if(isSquare(point)): # print(point,cost1+cost2+cost3+cost4) heappush(ans,cost1+cost2+cost3+cost4) ans=heappop(ans) if(ans==inf):ans=-1 print(ans) ```
output
1
62,706
15
125,413
Provide tags and a correct Python 3 solution for this coding contest problem. Captain Marmot wants to prepare a huge and important battle against his enemy, Captain Snake. For this battle he has n regiments, each consisting of 4 moles. Initially, each mole i (1 ≀ i ≀ 4n) is placed at some position (xi, yi) in the Cartesian plane. Captain Marmot wants to move some moles to make the regiments compact, if it's possible. Each mole i has a home placed at the position (ai, bi). Moving this mole one time means rotating his position point (xi, yi) 90 degrees counter-clockwise around it's home point (ai, bi). A regiment is compact only if the position points of the 4 moles form a square with non-zero area. Help Captain Marmot to find out for each regiment the minimal number of moves required to make that regiment compact, if it's possible. Input The first line contains one integer n (1 ≀ n ≀ 100), the number of regiments. The next 4n lines contain 4 integers xi, yi, ai, bi ( - 104 ≀ xi, yi, ai, bi ≀ 104). Output Print n lines to the standard output. If the regiment i can be made compact, the i-th line should contain one integer, the minimal number of required moves. Otherwise, on the i-th line print "-1" (without quotes). Examples Input 4 1 1 0 0 -1 1 0 0 -1 1 0 0 1 -1 0 0 1 1 0 0 -2 1 0 0 -1 1 0 0 1 -1 0 0 1 1 0 0 -1 1 0 0 -1 1 0 0 -1 1 0 0 2 2 0 1 -1 0 0 -2 3 0 0 -2 -1 1 -2 0 Output 1 -1 3 3 Note In the first regiment we can move once the second or the third mole. We can't make the second regiment compact. In the third regiment, from the last 3 moles we can move once one and twice another one. In the fourth regiment, we can move twice the first mole and once the third mole.
instruction
0
62,707
15
125,414
Tags: brute force, geometry Correct Solution: ``` import math import copy def getRot(mPos): x = mPos[0] y = mPos[1] aX = mPos[2] aY = mPos[3] oldX = x - aX oldY = y - aY newX = oldY newY = oldX newX *= -1 newX += aX newY += aY return([newX, newY]) def getRotArr(mPosArr): rotmPosArr = [] for mPos in mPosArr: rotmPos = [] x = mPos[0] y = mPos[1] aX = mPos[2] aY = mPos[3] oldX = x - aX oldY = y - aY for _ in range(4): rotmPos.append(oldX+aX) rotmPos.append(oldY+aY) newX = oldY newY = oldX newX *= -1 oldX = newX oldY = newY rotmPosArr.append(rotmPos) return(rotmPosArr) def isCompact(rotmPos): INF = 9876543 nCompact = INF for i in range(4): for j in range(4): for k in range(4): for l in range(4): posArr = [] posArr.append([rotmPos[0][2*i], rotmPos[0][2*i+1]]) posArr.append([rotmPos[1][2*j], rotmPos[1][2*j+1]]) posArr.append([rotmPos[2][2*k], rotmPos[2][2*k+1]]) posArr.append([rotmPos[3][2*l], rotmPos[3][2*l+1]]) #print(posArr) maxX = max(posArr[m][0] for m in range(4)) minX = min(posArr[m][0] for m in range(4)) maxY = max(posArr[m][1] for m in range(4)) minY = min(posArr[m][1] for m in range(4)) #print('maxX: %d, minX: %d, maxY: %d, minY: %d' % (maxX, minX, maxY, minY)) if abs(maxX-minX) == abs(maxY-minY) and isSquare(posArr): #print('abs(maxX-minX), abs(maxY-minY): %d, %d' % (abs(maxX-minX), abs(maxY-minY)) ) #print(posArr) #print('nCompact, i+j+k+l: %d, %d' % (nCompact, i+j+k+l)) nCompact = min(nCompact, i+j+k+l) if nCompact == INF: nCompact = -1 print(nCompact) def isSquare(posArr): for i in range(4): for j in range(i+1,4): if posArr[i] == posArr[j]: return(False) avgX = sum(posArr[i][0] for i in range(4)) / 4 avgY = sum(posArr[i][1] for i in range(4)) / 4 tempPosArr = copy.deepcopy(posArr) rotPosA = getRot([tempPosArr[0][0], tempPosArr[0][1], avgX, avgY]) tempPosArr.pop(0) for _ in range(3): for tempPos in tempPosArr: #print('rotPosA', rotPosA) #print('tempPos', tempPos) #print('tempPosArr', tempPosArr) if rotPosA == tempPos: #print('before popup', tempPosArr) tempPosArr.remove(tempPos) #print('after popup', tempPosArr) break rotPosA = getRot([tempPos[0], tempPos[1], avgX, avgY]) if len(tempPosArr) > 0: return(False) else: return(True) #distFromAvgPt = getDist([avgX, avgY], posArr[0]) #for i in range(1, 4): #if distFromAvgPt != getDist([avgX, avgY], posArr[i]): #return (False) #return(True) def getDist(posA, posB): return(math.sqrt((posA[0] - posB[0]) ** 2 + (posA[1] - posB[1]) ** 2)) mPosArr = [] nRegiment = int(input()) for i in range(nRegiment*4): mPos = list(map(int, input().split(' '))) mPosArr.append(mPos) #with open("inputCptMar.txt", "r") as f: # nRegiment = int(f.readline()) # for _ in range(nRegiment*4): # mPos = list(map(int, f.readline().split(' '))) # mPosArr.append(mPos) rotmPosArr = getRotArr(mPosArr) #print(rotmPosArr) for i in range(nRegiment): #for i in range(1): isCompact(rotmPosArr[i*4:i*4+4]) ```
output
1
62,707
15
125,415
Provide tags and a correct Python 3 solution for this coding contest problem. Captain Marmot wants to prepare a huge and important battle against his enemy, Captain Snake. For this battle he has n regiments, each consisting of 4 moles. Initially, each mole i (1 ≀ i ≀ 4n) is placed at some position (xi, yi) in the Cartesian plane. Captain Marmot wants to move some moles to make the regiments compact, if it's possible. Each mole i has a home placed at the position (ai, bi). Moving this mole one time means rotating his position point (xi, yi) 90 degrees counter-clockwise around it's home point (ai, bi). A regiment is compact only if the position points of the 4 moles form a square with non-zero area. Help Captain Marmot to find out for each regiment the minimal number of moves required to make that regiment compact, if it's possible. Input The first line contains one integer n (1 ≀ n ≀ 100), the number of regiments. The next 4n lines contain 4 integers xi, yi, ai, bi ( - 104 ≀ xi, yi, ai, bi ≀ 104). Output Print n lines to the standard output. If the regiment i can be made compact, the i-th line should contain one integer, the minimal number of required moves. Otherwise, on the i-th line print "-1" (without quotes). Examples Input 4 1 1 0 0 -1 1 0 0 -1 1 0 0 1 -1 0 0 1 1 0 0 -2 1 0 0 -1 1 0 0 1 -1 0 0 1 1 0 0 -1 1 0 0 -1 1 0 0 -1 1 0 0 2 2 0 1 -1 0 0 -2 3 0 0 -2 -1 1 -2 0 Output 1 -1 3 3 Note In the first regiment we can move once the second or the third mole. We can't make the second regiment compact. In the third regiment, from the last 3 moles we can move once one and twice another one. In the fourth regiment, we can move twice the first mole and once the third mole.
instruction
0
62,708
15
125,416
Tags: brute force, geometry Correct Solution: ``` #ROTATE IS PROBABLY BROKEN def rotate(x,y,a,b): x=x-a y=y-b new_y = x new_x = -y new_x=new_x+a new_y=new_y+b return[new_x,new_y] def marmot_distance(x1,y1,x2,y2): #distance between 2 pts answer = pow(pow(x2 - x1, 2) + pow(y2 - y1, 2), 0.5) return answer def beethovens_command(formation): #check if there's a square #these count how many rotations there have been for first in range(4): for second in range(4): for third in range(4): for fourth in range(4): #check if its a square here #any three points have to make right angles distance_1 = 0 distance_2 = 0 uncompact = False for d in range(4): #in here all the distances are calculated and you come away with d1 and d2 for e in range(d+1,4): distance_0 = marmot_distance(formation[e][0],formation[e][1],formation[d][0],formation[d][1]) if distance_0 ==0: #check if the points are on top of each other uncompact = True #print("Tripped the points-on-top-of-each-other wire") if distance_1 == 0: distance_1 = distance_0 elif distance_2 == 0 and distance_0 != distance_1: distance_2 = distance_0 if distance_0 != distance_1 and distance_0 != distance_2: #if you find a third distance, this isnt the answer, you should... uncompact = True #print("The third distance is ", distance_0) #print("Tripped the 3rd distance wire") if distance_1 > distance_2: #distance 1 should always be the smaller one so swap d1 and d2 big_marmot = distance_1 distance_1 = distance_2 distance_2 = big_marmot #distance_1, distance_2 = distance_2, distance_1 #but no marmot here so.... if pow(2*pow(distance_1,2),0.5) == distance_2 and not uncompact: #print("its a square") print(first+second+third+fourth) #print(formation) return else: #this is not the answer, keep going pass formation[3][0], formation[3][1] = rotate( formation[3][0], formation[3][1], formation[3][2], formation[3][3] ) formation[2][0], formation[2][1] = rotate( formation[2][0], formation[2][1], formation[2][2], formation[2][3] ) formation[1][0], formation[1][1] = rotate( formation[1][0], formation[1][1], formation[1][2], formation[1][3] ) formation[0][0], formation[0][1] = rotate( formation[0][0], formation[0][1], formation[0][2], formation[0][3] ) #if you reach here then youve done all the iterations and you couldnt find a square print("-1") #print(first+second+third+fourth) return regiments = int(input()) for i in range(regiments): formation = [] #this is always going to be a set of 4 points in a regiment. # will refresh for each new regiment. therefore temporary #formation[[x,y,a,b],[x,y,a,b]] and so on for t in range(4): temp = [int(num) for num in input().split()] formation.append(temp) #print(formation) beethovens_command(formation) #print("idk it failed") ```
output
1
62,708
15
125,417
Provide tags and a correct Python 3 solution for this coding contest problem. Captain Marmot wants to prepare a huge and important battle against his enemy, Captain Snake. For this battle he has n regiments, each consisting of 4 moles. Initially, each mole i (1 ≀ i ≀ 4n) is placed at some position (xi, yi) in the Cartesian plane. Captain Marmot wants to move some moles to make the regiments compact, if it's possible. Each mole i has a home placed at the position (ai, bi). Moving this mole one time means rotating his position point (xi, yi) 90 degrees counter-clockwise around it's home point (ai, bi). A regiment is compact only if the position points of the 4 moles form a square with non-zero area. Help Captain Marmot to find out for each regiment the minimal number of moves required to make that regiment compact, if it's possible. Input The first line contains one integer n (1 ≀ n ≀ 100), the number of regiments. The next 4n lines contain 4 integers xi, yi, ai, bi ( - 104 ≀ xi, yi, ai, bi ≀ 104). Output Print n lines to the standard output. If the regiment i can be made compact, the i-th line should contain one integer, the minimal number of required moves. Otherwise, on the i-th line print "-1" (without quotes). Examples Input 4 1 1 0 0 -1 1 0 0 -1 1 0 0 1 -1 0 0 1 1 0 0 -2 1 0 0 -1 1 0 0 1 -1 0 0 1 1 0 0 -1 1 0 0 -1 1 0 0 -1 1 0 0 2 2 0 1 -1 0 0 -2 3 0 0 -2 -1 1 -2 0 Output 1 -1 3 3 Note In the first regiment we can move once the second or the third mole. We can't make the second regiment compact. In the third regiment, from the last 3 moles we can move once one and twice another one. In the fourth regiment, we can move twice the first mole and once the third mole.
instruction
0
62,709
15
125,418
Tags: brute force, geometry Correct Solution: ``` def siblings(initial, root): th = (initial[0] - root[0], initial[1] - root[1]) return [(root[0] + th[0], root[1] + th[1]), (root[0] -th[1], root[1] + th[0]), (root[0] - th[0], root[1] - th[1]), (root[0] + th[1], root[1] - th[0])] def dist(x, y): return (x[0]-y[0])**2 + (x[1] - y[1])**2 def isSquare(p1,p2,p3,p4): d2 = dist(p1, p2) d3 = dist(p1, p3) d4 = dist(p1, p4) u = {p1, p2, p3, p4} if (len(u) != 4): return 0 if (d2==d3 and 2*d2 == d4): d = dist(p2, p4) return (d == dist(p3,p4) and d == d2) if (d3==d4 and 2*d3 == d2): d = dist(p2, p3) return (d == dist(p2,p4) and d == d3) if (d2==d4 and 2*d2 == d3): d = dist(p2, p3) return (d == dist(p3,p4) and d == d2) return False def distOri(x, y, root): th1 = (y[0] - root[0], y[1] - root[1]) th2 = (x[0] - root[0], x[1] - root[1]) if th1[0]==th2[0] and th1[1] == th2[1]: return 0 if (th2[0] == -th1[1] and th2[1] == th1[0]): return 1 if (th2[0] == - th1[0] and th2[1] == -th1[1]): return 2 if (th2[0] == th1[1] and th2[1] == -th1[0]): return 3 n = int(input()) for i in range(n): initial = [] root = [] _min = 1000 for j in range(4): x,y,a,b = [int(k) for k in input().split()] initial.append((x,y)) root.append((a,b)) for x1 in siblings(initial[0], root[0]): for x2 in siblings(initial[1], root[1]): for x3 in siblings(initial[2], root[2]): for x4 in siblings(initial[3], root[3]): if isSquare(x1, x2, x3, x4): #print(x1, x2, x3, x4) _min = min(_min, distOri(x1, initial[0], root[0]) + distOri(x2, initial[1], root[1]) + \ distOri(x3, initial[2], root[2]) + distOri(x4, initial[3], root[3])) if (_min == 1000): print(-1) else: print(_min) ```
output
1
62,709
15
125,419
Provide tags and a correct Python 3 solution for this coding contest problem. Captain Marmot wants to prepare a huge and important battle against his enemy, Captain Snake. For this battle he has n regiments, each consisting of 4 moles. Initially, each mole i (1 ≀ i ≀ 4n) is placed at some position (xi, yi) in the Cartesian plane. Captain Marmot wants to move some moles to make the regiments compact, if it's possible. Each mole i has a home placed at the position (ai, bi). Moving this mole one time means rotating his position point (xi, yi) 90 degrees counter-clockwise around it's home point (ai, bi). A regiment is compact only if the position points of the 4 moles form a square with non-zero area. Help Captain Marmot to find out for each regiment the minimal number of moves required to make that regiment compact, if it's possible. Input The first line contains one integer n (1 ≀ n ≀ 100), the number of regiments. The next 4n lines contain 4 integers xi, yi, ai, bi ( - 104 ≀ xi, yi, ai, bi ≀ 104). Output Print n lines to the standard output. If the regiment i can be made compact, the i-th line should contain one integer, the minimal number of required moves. Otherwise, on the i-th line print "-1" (without quotes). Examples Input 4 1 1 0 0 -1 1 0 0 -1 1 0 0 1 -1 0 0 1 1 0 0 -2 1 0 0 -1 1 0 0 1 -1 0 0 1 1 0 0 -1 1 0 0 -1 1 0 0 -1 1 0 0 2 2 0 1 -1 0 0 -2 3 0 0 -2 -1 1 -2 0 Output 1 -1 3 3 Note In the first regiment we can move once the second or the third mole. We can't make the second regiment compact. In the third regiment, from the last 3 moles we can move once one and twice another one. In the fourth regiment, we can move twice the first mole and once the third mole.
instruction
0
62,710
15
125,420
Tags: brute force, geometry Correct Solution: ``` def rotate(i, j): x, y, p, q = a[i] for k in range(1, j+1): x, y = p - (y - q), q + (x - p) c[i] = x, y def dist(i, j): x, y = c[i] p, q = c[j] return (x - p) ** 2 + (y - q) ** 2 def check(move): for i in range(4): rotate(i, move[i]) p = [1,2,3] p.sort(key = lambda x: dist(0, x)) d = [dist(0, x) for x in p] if d[0] == d[1] == d[2] / 2 and d[2] > 0: return True if dist(p[0], p[1]) == d[2] else False else: return False n = int(input()) a = [0] * 4 c = [0] * 4 for i in range(n): a[0] = list(map(int, input().split())) a[1] = list(map(int, input().split())) a[2] = list(map(int, input().split())) a[3] = list(map(int, input().split())) ans = 17 for k in range(4): for l in range(4): for p in range(4): for q in range(4): if k + l + p + q < ans and check([k, l, p, q]): ans = k + l + p + q print(ans if ans != 17 else -1) ```
output
1
62,710
15
125,421
Provide tags and a correct Python 3 solution for this coding contest problem. Captain Marmot wants to prepare a huge and important battle against his enemy, Captain Snake. For this battle he has n regiments, each consisting of 4 moles. Initially, each mole i (1 ≀ i ≀ 4n) is placed at some position (xi, yi) in the Cartesian plane. Captain Marmot wants to move some moles to make the regiments compact, if it's possible. Each mole i has a home placed at the position (ai, bi). Moving this mole one time means rotating his position point (xi, yi) 90 degrees counter-clockwise around it's home point (ai, bi). A regiment is compact only if the position points of the 4 moles form a square with non-zero area. Help Captain Marmot to find out for each regiment the minimal number of moves required to make that regiment compact, if it's possible. Input The first line contains one integer n (1 ≀ n ≀ 100), the number of regiments. The next 4n lines contain 4 integers xi, yi, ai, bi ( - 104 ≀ xi, yi, ai, bi ≀ 104). Output Print n lines to the standard output. If the regiment i can be made compact, the i-th line should contain one integer, the minimal number of required moves. Otherwise, on the i-th line print "-1" (without quotes). Examples Input 4 1 1 0 0 -1 1 0 0 -1 1 0 0 1 -1 0 0 1 1 0 0 -2 1 0 0 -1 1 0 0 1 -1 0 0 1 1 0 0 -1 1 0 0 -1 1 0 0 -1 1 0 0 2 2 0 1 -1 0 0 -2 3 0 0 -2 -1 1 -2 0 Output 1 -1 3 3 Note In the first regiment we can move once the second or the third mole. We can't make the second regiment compact. In the third regiment, from the last 3 moles we can move once one and twice another one. In the fourth regiment, we can move twice the first mole and once the third mole.
instruction
0
62,711
15
125,422
Tags: brute force, geometry Correct Solution: ``` def r(t): t[0], t[1] = t[2] + t[3] - t[1], t[3] + t[0] - t[2] f = [(0, 0), (1, 3), (2, 15), (3, 63)] h = [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)] g = lambda u, v: (u[0] - v[0]) ** 2 + (u[1] - v[1]) ** 2 for i in range(int(input())): p = [list(map(int, input().split())) for j in range(4)] s = 13 for q in range(256): t = sum((q >> k) & 3 for k in (0, 2, 4, 6)) d = sorted(g(p[u], p[v]) for u, v in h) if 0 != d[4] == d[5] == 2 * d[0] == 2 * d[3]: s = min(s, t) for i, k in f: if q & k == k: r(p[i]) print(s if s < 13 else -1) ```
output
1
62,711
15
125,423
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Captain Marmot wants to prepare a huge and important battle against his enemy, Captain Snake. For this battle he has n regiments, each consisting of 4 moles. Initially, each mole i (1 ≀ i ≀ 4n) is placed at some position (xi, yi) in the Cartesian plane. Captain Marmot wants to move some moles to make the regiments compact, if it's possible. Each mole i has a home placed at the position (ai, bi). Moving this mole one time means rotating his position point (xi, yi) 90 degrees counter-clockwise around it's home point (ai, bi). A regiment is compact only if the position points of the 4 moles form a square with non-zero area. Help Captain Marmot to find out for each regiment the minimal number of moves required to make that regiment compact, if it's possible. Input The first line contains one integer n (1 ≀ n ≀ 100), the number of regiments. The next 4n lines contain 4 integers xi, yi, ai, bi ( - 104 ≀ xi, yi, ai, bi ≀ 104). Output Print n lines to the standard output. If the regiment i can be made compact, the i-th line should contain one integer, the minimal number of required moves. Otherwise, on the i-th line print "-1" (without quotes). Examples Input 4 1 1 0 0 -1 1 0 0 -1 1 0 0 1 -1 0 0 1 1 0 0 -2 1 0 0 -1 1 0 0 1 -1 0 0 1 1 0 0 -1 1 0 0 -1 1 0 0 -1 1 0 0 2 2 0 1 -1 0 0 -2 3 0 0 -2 -1 1 -2 0 Output 1 -1 3 3 Note In the first regiment we can move once the second or the third mole. We can't make the second regiment compact. In the third regiment, from the last 3 moles we can move once one and twice another one. In the fourth regiment, we can move twice the first mole and once the third mole. Submitted Solution: ``` class Point: # Structure of a point in 2D space def __init__(self, x, y): self.x = x self.y = y # A utility function to find square of # distance from point 'p' to point 'q' def distSq(p, q): return (p.x - q.x) * (p.x - q.x) +(p.y - q.y) * (p.y - q.y) # This function returns true if (p1, p2, p3, p4) # form a square, otherwise false def isSquare(p1, p2, p3, p4): d2 = distSq(p1, p2) # from p1 to p2 d3 = distSq(p1, p3) # from p1 to p3 d4 = distSq(p1, p4) # from p1 to p4 if d2 == 0 or d3 == 0 or d4 == 0: return False # If lengths if (p1, p2) and (p1, p3) are same, then # following conditions must be met to form a square. # 1) Square of length of (p1, p4) is same as twice # the square of (p1, p2) # 2) Square of length of (p2, p3) is same # as twice the square of (p2, p4) if d2 == d3 and 2 * d2 == d4 and 2 * distSq(p2, p4) == distSq(p2, p3): return True # The below two cases are similar to above case if d3 == d4 and 2 * d3 == d2 and 2 * distSq(p3, p2) == distSq(p3, p4): return True if d2 == d4 and 2 * d2 == d3 and 2 * distSq(p2, p3) == distSq(p2, p4): return True return False n=int(input()) for _ in range(n): r=[] for i in range(4): x,y,a,b=list(map(int,input().split())) r.append([[x,y],[a+b-y,x+b-a],[2*a-x,2*b-y],[a-b+y,a+b-x]]) ans=64 f=0 r1,r2,r3,r4=r for i in range(4): for j in range(4): for k in range(4): for l in range(4): p1 = Point(r1[i][0],r1[i][1]) p2 = Point(r2[j][0],r2[j][1]) p3 = Point(r3[k][0],r3[k][1]) p4 = Point(r4[l][0],r4[l][1]) if isSquare(p1, p2, p3, p4): f=1 ans=min(ans,i+j+k+l) if f: print(ans) else: print(-1) ```
instruction
0
62,712
15
125,424
Yes
output
1
62,712
15
125,425
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Captain Marmot wants to prepare a huge and important battle against his enemy, Captain Snake. For this battle he has n regiments, each consisting of 4 moles. Initially, each mole i (1 ≀ i ≀ 4n) is placed at some position (xi, yi) in the Cartesian plane. Captain Marmot wants to move some moles to make the regiments compact, if it's possible. Each mole i has a home placed at the position (ai, bi). Moving this mole one time means rotating his position point (xi, yi) 90 degrees counter-clockwise around it's home point (ai, bi). A regiment is compact only if the position points of the 4 moles form a square with non-zero area. Help Captain Marmot to find out for each regiment the minimal number of moves required to make that regiment compact, if it's possible. Input The first line contains one integer n (1 ≀ n ≀ 100), the number of regiments. The next 4n lines contain 4 integers xi, yi, ai, bi ( - 104 ≀ xi, yi, ai, bi ≀ 104). Output Print n lines to the standard output. If the regiment i can be made compact, the i-th line should contain one integer, the minimal number of required moves. Otherwise, on the i-th line print "-1" (without quotes). Examples Input 4 1 1 0 0 -1 1 0 0 -1 1 0 0 1 -1 0 0 1 1 0 0 -2 1 0 0 -1 1 0 0 1 -1 0 0 1 1 0 0 -1 1 0 0 -1 1 0 0 -1 1 0 0 2 2 0 1 -1 0 0 -2 3 0 0 -2 -1 1 -2 0 Output 1 -1 3 3 Note In the first regiment we can move once the second or the third mole. We can't make the second regiment compact. In the third regiment, from the last 3 moles we can move once one and twice another one. In the fourth regiment, we can move twice the first mole and once the third mole. Submitted Solution: ``` def siblings(initial, root): th = (initial[0] - root[0], initial[1] - root[1]) return [(root[0] + th[0], root[1] + th[1]), (root[0] -th[1], root[1] + th[0]), (root[0] - th[0], root[1] - th[1]), (root[0] + th[1], root[1] - th[0])] def dist(x, y): return (x[0]-y[0])**2 + (x[1] - y[1])**2 def isSquare(p1,p2,p3,p4): d2 = dist(p1, p2) d3 = dist(p1, p3) d4 = dist(p1, p4) u = {p1, p2, p3, p4} if (len(u) != 4): return 0 if (d2==d3 and 2*d2 == d4): d = dist(p2, p4) return (d == dist(p3,p4) and d == d2) if (d3==d4 and 2*d3 == d2): d = dist(p2, p3) return (d == dist(p2,p4) and d == d3) if (d2==d4 and 2*d2 == d3): d = dist(p2, p3) return (d == dist(p3,p4) and d == d2) return False def distOri(x, y, root): th1 = (y[0] - root[0], y[1] - root[1]) th2 = (x[0] - root[0], x[1] - root[1]) if th1[0]==th2[0] and th1[1] == th2[1]: return 0 if (th2[0] == -th1[1] and th2[1] == th1[0]): return 1 if (th2[0] == - th1[0] and th2[1] == -th1[1]): return 2 if (th2[0] == th1[1] and th2[1] == -th1[0]): return 3 n = int(input()) for i in range(n): initial = [] root = [] _max = 1000 for j in range(4): x,y,a,b = [int(k) for k in input().split()] initial.append((x,y)) root.append((a,b)) for x1 in siblings(initial[0], root[0]): for x2 in siblings(initial[1], root[1]): for x3 in siblings(initial[2], root[2]): for x4 in siblings(initial[3], root[3]): if isSquare(x1, x2, x3, x4): #print(x1, x2, x3, x4) _max = min(_max, distOri(x1, initial[0], root[0]) + distOri(x2, initial[1], root[1]) + \ distOri(x3, initial[2], root[2]) + distOri(x4, initial[3], root[3])) if (_max == 1000): print(-1) else: print(_max) ```
instruction
0
62,713
15
125,426
Yes
output
1
62,713
15
125,427
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Captain Marmot wants to prepare a huge and important battle against his enemy, Captain Snake. For this battle he has n regiments, each consisting of 4 moles. Initially, each mole i (1 ≀ i ≀ 4n) is placed at some position (xi, yi) in the Cartesian plane. Captain Marmot wants to move some moles to make the regiments compact, if it's possible. Each mole i has a home placed at the position (ai, bi). Moving this mole one time means rotating his position point (xi, yi) 90 degrees counter-clockwise around it's home point (ai, bi). A regiment is compact only if the position points of the 4 moles form a square with non-zero area. Help Captain Marmot to find out for each regiment the minimal number of moves required to make that regiment compact, if it's possible. Input The first line contains one integer n (1 ≀ n ≀ 100), the number of regiments. The next 4n lines contain 4 integers xi, yi, ai, bi ( - 104 ≀ xi, yi, ai, bi ≀ 104). Output Print n lines to the standard output. If the regiment i can be made compact, the i-th line should contain one integer, the minimal number of required moves. Otherwise, on the i-th line print "-1" (without quotes). Examples Input 4 1 1 0 0 -1 1 0 0 -1 1 0 0 1 -1 0 0 1 1 0 0 -2 1 0 0 -1 1 0 0 1 -1 0 0 1 1 0 0 -1 1 0 0 -1 1 0 0 -1 1 0 0 2 2 0 1 -1 0 0 -2 3 0 0 -2 -1 1 -2 0 Output 1 -1 3 3 Note In the first regiment we can move once the second or the third mole. We can't make the second regiment compact. In the third regiment, from the last 3 moles we can move once one and twice another one. In the fourth regiment, we can move twice the first mole and once the third mole. Submitted Solution: ``` def f(x, y, a, b, n): return a + (x - a) * cos[n] - (y - b) * sin[n], b + (x - a) * sin[n] + (y - b) * cos[n] def check(p): d = {} for i in range(len(p) - 1): for j in range(i + 1, len(p)): dist = (p[i][0] - p[j][0]) ** 2 + (p[i][1] - p[j][1]) ** 2 d[dist] = d.get(dist, 0) + 1 if len(d) != 2: return 0 a, b = sorted(d) return 2 * a == b and d[a] == 4 and d[b] == 2 cos, sin, variants = [1, 0, -1, 0], [0, 1, 0, -1], [[x, y, z, a] for x in range(4) for y in range(4) for z in range(4) for a in range(4)] for t in range(int(input())): moles, ans = [list(map(int, input().split())) for x in range(4)], 13 for a in variants: if check([f(moles[i][0], moles[i][1], moles[i][2], moles[i][3], a[i]) for i in range(4)]): ans = min(ans, sum(a)) print(ans if ans != 13 else -1) ```
instruction
0
62,714
15
125,428
Yes
output
1
62,714
15
125,429
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Captain Marmot wants to prepare a huge and important battle against his enemy, Captain Snake. For this battle he has n regiments, each consisting of 4 moles. Initially, each mole i (1 ≀ i ≀ 4n) is placed at some position (xi, yi) in the Cartesian plane. Captain Marmot wants to move some moles to make the regiments compact, if it's possible. Each mole i has a home placed at the position (ai, bi). Moving this mole one time means rotating his position point (xi, yi) 90 degrees counter-clockwise around it's home point (ai, bi). A regiment is compact only if the position points of the 4 moles form a square with non-zero area. Help Captain Marmot to find out for each regiment the minimal number of moves required to make that regiment compact, if it's possible. Input The first line contains one integer n (1 ≀ n ≀ 100), the number of regiments. The next 4n lines contain 4 integers xi, yi, ai, bi ( - 104 ≀ xi, yi, ai, bi ≀ 104). Output Print n lines to the standard output. If the regiment i can be made compact, the i-th line should contain one integer, the minimal number of required moves. Otherwise, on the i-th line print "-1" (without quotes). Examples Input 4 1 1 0 0 -1 1 0 0 -1 1 0 0 1 -1 0 0 1 1 0 0 -2 1 0 0 -1 1 0 0 1 -1 0 0 1 1 0 0 -1 1 0 0 -1 1 0 0 -1 1 0 0 2 2 0 1 -1 0 0 -2 3 0 0 -2 -1 1 -2 0 Output 1 -1 3 3 Note In the first regiment we can move once the second or the third mole. We can't make the second regiment compact. In the third regiment, from the last 3 moles we can move once one and twice another one. In the fourth regiment, we can move twice the first mole and once the third mole. Submitted Solution: ``` #a+b-y #x+b-a #1 - import sys input=sys.stdin.readline def distSq(p1,p2): return (p1[0]-p2[0])*(p1[0]-p2[0])+(p1[1]-p2[1])*(p1[1]-p2[1]) def isSquare(p1, p2, p3, p4): d2 = distSq(p1, p2) # from p1 to p2 d3 = distSq(p1, p3) # from p1 to p3 d4 = distSq(p1, p4) # from p1 to p4 # If lengths if (p1, p2) and (p1, p3) are same, then # following conditions must be met to form a square. # 1) Square of length of (p1, p4) is same as twice # the square of (p1, p2) # 2) Square of length of (p2, p3) is same # as twice the square of (p2, p4) if d2 == d3 and 2 * d2 == d4 and 2 * distSq(p2, p4) == distSq(p2, p3): return True # The below two cases are similar to above case if d3 == d4 and 2 * d3 == d2 and 2 * distSq(p3, p2) == distSq(p3, p4): return True if d2 == d4 and 2 * d2 == d3 and 2 * distSq(p2, p3) == distSq(p2, p4): return True return False for _ in range(int(input())): l=[] for i in range(4): x,y,a,b=map(int,input().split()) for j in range(4): l.append([x,y,j]) x,y=a+b-y,x+b-a mini=10**9 for i in range(4): for j in range(4,8): for k in range(8,12): for z in range(12,16): if l[i]==l[j] or l[j]==l[k] or l[i]==l[k] or l[i]==l[z] or l[j]==l[z] or l[k]==l[z]: continue if isSquare(l[i],l[j],l[k],l[z]): # print(l[i],l[j],l[k],l[z]) curr=l[i][2]+l[j][2]+l[k][2]+l[z][2] #print(curr) if curr<mini: mini=curr print(mini if mini<10**9 else -1) ```
instruction
0
62,715
15
125,430
Yes
output
1
62,715
15
125,431
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Captain Marmot wants to prepare a huge and important battle against his enemy, Captain Snake. For this battle he has n regiments, each consisting of 4 moles. Initially, each mole i (1 ≀ i ≀ 4n) is placed at some position (xi, yi) in the Cartesian plane. Captain Marmot wants to move some moles to make the regiments compact, if it's possible. Each mole i has a home placed at the position (ai, bi). Moving this mole one time means rotating his position point (xi, yi) 90 degrees counter-clockwise around it's home point (ai, bi). A regiment is compact only if the position points of the 4 moles form a square with non-zero area. Help Captain Marmot to find out for each regiment the minimal number of moves required to make that regiment compact, if it's possible. Input The first line contains one integer n (1 ≀ n ≀ 100), the number of regiments. The next 4n lines contain 4 integers xi, yi, ai, bi ( - 104 ≀ xi, yi, ai, bi ≀ 104). Output Print n lines to the standard output. If the regiment i can be made compact, the i-th line should contain one integer, the minimal number of required moves. Otherwise, on the i-th line print "-1" (without quotes). Examples Input 4 1 1 0 0 -1 1 0 0 -1 1 0 0 1 -1 0 0 1 1 0 0 -2 1 0 0 -1 1 0 0 1 -1 0 0 1 1 0 0 -1 1 0 0 -1 1 0 0 -1 1 0 0 2 2 0 1 -1 0 0 -2 3 0 0 -2 -1 1 -2 0 Output 1 -1 3 3 Note In the first regiment we can move once the second or the third mole. We can't make the second regiment compact. In the third regiment, from the last 3 moles we can move once one and twice another one. In the fourth regiment, we can move twice the first mole and once the third mole. Submitted Solution: ``` #a+b-y #x+b-a #1 - import sys input=sys.stdin.readline def distSq(p1,p2): return (p1[0]-p2[0])*(p1[0]-p2[0])+(p1[1]-p2[1])*(p1[1]-p2[1]) def isSquare(p1, p2, p3, p4): d2 = distSq(p1, p2) # from p1 to p2 d3 = distSq(p1, p3) # from p1 to p3 d4 = distSq(p1, p4) # from p1 to p4 # If lengths if (p1, p2) and (p1, p3) are same, then # following conditions must be met to form a square. # 1) Square of length of (p1, p4) is same as twice # the square of (p1, p2) # 2) Square of length of (p2, p3) is same # as twice the square of (p2, p4) if d2 == d3 and 2 * d2 == d4 and 2 * distSq(p2, p4) == distSq(p2, p3): return True # The below two cases are similar to above case if d3 == d4 and 2 * d3 == d2 and 2 * distSq(p3, p2) == distSq(p3, p4): return True if d2 == d4 and 2 * d2 == d3 and 2 * distSq(p2, p3) == distSq(p2, p4): return True return False for _ in range(int(input())): l=[] for i in range(4): x,y,a,b=map(int,input().split()) for j in range(4): l.append([x,y,j]) x,y=a+b-y,x+b-a mini=10**9 for i in range(4): for j in range(4,8): for k in range(8,12): for z in range(12,16): if i==j or j==k or i==k or i==z or j==z or k==z: continue if isSquare(l[i],l[j],l[k],l[z]): # print(l[i],l[j],l[k],l[z]) curr=l[i][2]+l[j][2]+l[k][2]+l[z][2] if curr<mini: mini=curr print(mini if mini<10**9 else -1) ```
instruction
0
62,716
15
125,432
No
output
1
62,716
15
125,433
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Captain Marmot wants to prepare a huge and important battle against his enemy, Captain Snake. For this battle he has n regiments, each consisting of 4 moles. Initially, each mole i (1 ≀ i ≀ 4n) is placed at some position (xi, yi) in the Cartesian plane. Captain Marmot wants to move some moles to make the regiments compact, if it's possible. Each mole i has a home placed at the position (ai, bi). Moving this mole one time means rotating his position point (xi, yi) 90 degrees counter-clockwise around it's home point (ai, bi). A regiment is compact only if the position points of the 4 moles form a square with non-zero area. Help Captain Marmot to find out for each regiment the minimal number of moves required to make that regiment compact, if it's possible. Input The first line contains one integer n (1 ≀ n ≀ 100), the number of regiments. The next 4n lines contain 4 integers xi, yi, ai, bi ( - 104 ≀ xi, yi, ai, bi ≀ 104). Output Print n lines to the standard output. If the regiment i can be made compact, the i-th line should contain one integer, the minimal number of required moves. Otherwise, on the i-th line print "-1" (without quotes). Examples Input 4 1 1 0 0 -1 1 0 0 -1 1 0 0 1 -1 0 0 1 1 0 0 -2 1 0 0 -1 1 0 0 1 -1 0 0 1 1 0 0 -1 1 0 0 -1 1 0 0 -1 1 0 0 2 2 0 1 -1 0 0 -2 3 0 0 -2 -1 1 -2 0 Output 1 -1 3 3 Note In the first regiment we can move once the second or the third mole. We can't make the second regiment compact. In the third regiment, from the last 3 moles we can move once one and twice another one. In the fourth regiment, we can move twice the first mole and once the third mole. Submitted Solution: ``` n = int(input()) for reg in range(n): moles = [] for i in range(4): moles.append([int(x) for x in input().split()]) pos = [] for x,y,a,b in moles: x -= a y -= b p = [(x,y)] #print(x,y,a,b) #print(x,y) for bruh in range(3): x,y = -y,x #print(x,y) p.append((x,y)) pos.append([(c[0]+a, c[1]+b) for c in p]) low = float('inf') for a in range(4): for b in range(4): for c in range(4): for d in range(4): points = [pos[0][a], pos[1][b], pos[2][c], pos[3][d]] points.sort() if (points[0][0] == points[1][0] and points[2][0] == points[3][0] and points[0][1] == points[2][1] and points [1][1] == points[3][1] and points[3][0] - points[0][0] == points[3][1]-points[0][1] and points[3] != points[0]): low = min(low, a+b+c+d) if low == float('inf'): print(-1) else: print(low) ```
instruction
0
62,717
15
125,434
No
output
1
62,717
15
125,435
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Captain Marmot wants to prepare a huge and important battle against his enemy, Captain Snake. For this battle he has n regiments, each consisting of 4 moles. Initially, each mole i (1 ≀ i ≀ 4n) is placed at some position (xi, yi) in the Cartesian plane. Captain Marmot wants to move some moles to make the regiments compact, if it's possible. Each mole i has a home placed at the position (ai, bi). Moving this mole one time means rotating his position point (xi, yi) 90 degrees counter-clockwise around it's home point (ai, bi). A regiment is compact only if the position points of the 4 moles form a square with non-zero area. Help Captain Marmot to find out for each regiment the minimal number of moves required to make that regiment compact, if it's possible. Input The first line contains one integer n (1 ≀ n ≀ 100), the number of regiments. The next 4n lines contain 4 integers xi, yi, ai, bi ( - 104 ≀ xi, yi, ai, bi ≀ 104). Output Print n lines to the standard output. If the regiment i can be made compact, the i-th line should contain one integer, the minimal number of required moves. Otherwise, on the i-th line print "-1" (without quotes). Examples Input 4 1 1 0 0 -1 1 0 0 -1 1 0 0 1 -1 0 0 1 1 0 0 -2 1 0 0 -1 1 0 0 1 -1 0 0 1 1 0 0 -1 1 0 0 -1 1 0 0 -1 1 0 0 2 2 0 1 -1 0 0 -2 3 0 0 -2 -1 1 -2 0 Output 1 -1 3 3 Note In the first regiment we can move once the second or the third mole. We can't make the second regiment compact. In the third regiment, from the last 3 moles we can move once one and twice another one. In the fourth regiment, we can move twice the first mole and once the third mole. Submitted Solution: ``` def siblings(initial, root): th = (initial[0] - root[0], initial[1] - root[1]) return [(root[0] + th[0], root[1] + th[1]), (root[0] -th[1], root[1] + th[0]), (root[0] - th[0], root[1] - th[1]), (root[0] + th[1], root[1] - th[0])] def dist(x, y): return (x[0]-y[0])**2 + (x[1] - y[1])**2 def isSquare(p1,p2,p3,p4): d2 = dist(p1, p2) d3 = dist(p1, p3) d4 = dist(p1, p4) if (d2==0 or d3==0 or d4==0): return False if (d2==d3 and 2*d2 == d4): d = dist(p2, p4) return (d == dist(p3,p4) and d == d2) if (d3==d4 and 2*d3 == d2): d = dist(p2, p3) return (d == dist(p2,p4) and d == d3) if (d2==d4 and 2*d2 == d3): d = dist(p2, p3) return (d == dist(p3,p4) and d == d2) return False def distOri(x, y, root): th1 = (y[0] - root[0], y[1] - root[1]) th2 = (x[0] - root[0], x[1] - root[1]) if th1==th2: return 0 if (th2[0] == -th1[1]): return 1 if (th2[0] == - th1[0]): return 2 if (th2[0] == th1[1]): return 3 n = int(input()) for i in range(n): initial = [] root = [] _max = 1000 for j in range(4): x,y,a,b = [int(k) for k in input().split()] initial.append((x,y)) root.append((a,b)) for x1 in siblings(initial[0], root[0]): for x2 in siblings(initial[1], root[1]): for x3 in siblings(initial[2], root[2]): for x4 in siblings(initial[3], root[3]): if isSquare(x1, x2, x3, x4): _max = min(_max, distOri(x1, initial[0], root[0]) + distOri(x2, initial[1], root[1]) + \ distOri(x3, initial[2], root[2]) + distOri(x4, initial[3], root[3])) if (_max == 1000): print(-1) else: print(_max) ```
instruction
0
62,718
15
125,436
No
output
1
62,718
15
125,437
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Captain Marmot wants to prepare a huge and important battle against his enemy, Captain Snake. For this battle he has n regiments, each consisting of 4 moles. Initially, each mole i (1 ≀ i ≀ 4n) is placed at some position (xi, yi) in the Cartesian plane. Captain Marmot wants to move some moles to make the regiments compact, if it's possible. Each mole i has a home placed at the position (ai, bi). Moving this mole one time means rotating his position point (xi, yi) 90 degrees counter-clockwise around it's home point (ai, bi). A regiment is compact only if the position points of the 4 moles form a square with non-zero area. Help Captain Marmot to find out for each regiment the minimal number of moves required to make that regiment compact, if it's possible. Input The first line contains one integer n (1 ≀ n ≀ 100), the number of regiments. The next 4n lines contain 4 integers xi, yi, ai, bi ( - 104 ≀ xi, yi, ai, bi ≀ 104). Output Print n lines to the standard output. If the regiment i can be made compact, the i-th line should contain one integer, the minimal number of required moves. Otherwise, on the i-th line print "-1" (without quotes). Examples Input 4 1 1 0 0 -1 1 0 0 -1 1 0 0 1 -1 0 0 1 1 0 0 -2 1 0 0 -1 1 0 0 1 -1 0 0 1 1 0 0 -1 1 0 0 -1 1 0 0 -1 1 0 0 2 2 0 1 -1 0 0 -2 3 0 0 -2 -1 1 -2 0 Output 1 -1 3 3 Note In the first regiment we can move once the second or the third mole. We can't make the second regiment compact. In the third regiment, from the last 3 moles we can move once one and twice another one. In the fourth regiment, we can move twice the first mole and once the third mole. Submitted Solution: ``` ''' Created on Oct 6, 2014 @author: Ismael ''' import bisect from math import atan,pi import sys from math import * def rotate2d(rads,point,origin): """ A rotation function that rotates a point around a point to rotate around the origin use [0,0] """ x = point[0] - origin[0] yorz = point[1] - origin[1] newx = (x*cos(rads)) - (yorz*sin(rads)) newyorz = (x*sin(rads)) + (yorz*cos(rads)) newx += origin[0] newyorz += origin[1] return newx,newyorz rots = [(0,0),(pi/2,1),(-pi/2,1),(pi,2)] def computeSqDist(pt1,pt2): dx = pt1[0]-pt2[0] dy = pt1[1]-pt2[1] return dx*dx+dy*dy def computeCenter(pt1,pt2): x = (pt1[0]+pt2[0])/2 y = (pt1[1]+pt2[1])/2 return (x,y) def computeAngle(pt1,pt2): dx = pt1[0]-pt2[0] dy = pt1[1]-pt2[1] if(dx == 0): return pi/2 angle = atan(dy/dx) return angle def computeAnglePerp(angle): anglePerp = angle + pi/2 while(anglePerp >= pi): anglePerp -= pi return anglePerp def computePoint(mole,angle): return rotate2d(angle,mole[0],mole[1]) DISP = False def formAsquare(seg1,seg2): d1 = computeSqDist(seg1[0], seg1[1]) d2 = computeSqDist(seg2[0], seg2[1]) c1 = computeCenter(seg1[0], seg1[1]) c2 = computeCenter(seg2[0], seg2[1]) a1 = computeAngle(seg1[0], seg1[1]) a1perp = computeAnglePerp(a1) a2 = computeAngle(seg2[0], seg2[1]) sameLen = (d1 == d2) sameCenter = (abs(c1[0]-c2[0])<1e-9 and abs(c1[1]-c2[1])<1e-9) arePerp = (abs(a2-a1perp)<1e-9 or abs(abs(a2-a1perp)-pi)<1e-9) '''if(DISP): if(sameLen): print("sameLen") if(sameCenter): print("sameCenter") if(arePerp): print("arePerp") print("")''' return sameLen and sameCenter and arePerp def isSquare(reg,ang1,ang2,ang3,ang4): p1 = computePoint(reg[0],ang1) p2 = computePoint(reg[1],ang2) p3 = computePoint(reg[2],ang3) p4 = computePoint(reg[3],ang4) if(formAsquare((p1,p2),(p3,p4))): return True if(formAsquare((p1,p3),(p2,p4))): return True if(formAsquare((p1,p4),(p2,p3))): return True return False def makeCompact(reg): global DISP minCost = sys.maxsize for rot1 in rots: for rot2 in rots: for rot3 in rots: for rot4 in rots: cost = rot1[1]+rot2[1]+rot3[1]+rot4[1] '''if(cost == 3): print(rot1,rot2,rot3,rot4) DISP = True print("cost=",cost)''' if(isSquare(reg,rot1[0],rot2[0],rot3[0],rot4[0])): minCost = min(minCost,cost) #DISP = False if(minCost == sys.maxsize): return -1 else: return minCost def solve(regs): for reg in regs: print(makeCompact(reg)) n = int(input()) regs = [] for _ in range(n): reg = [] for _ in range(4): x,y,a,b = map(int,input().split()) reg.append(((x,y),(a,b))) regs.append(reg) solve(regs) ```
instruction
0
62,719
15
125,438
No
output
1
62,719
15
125,439
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Wilbur the pig now wants to play with strings. He has found an n by m table consisting only of the digits from 0 to 9 where the rows are numbered 1 to n and the columns are numbered 1 to m. Wilbur starts at some square and makes certain moves. If he is at square (x, y) and the digit d (0 ≀ d ≀ 9) is written at position (x, y), then he must move to the square (x + ad, y + bd), if that square lies within the table, and he stays in the square (x, y) otherwise. Before Wilbur makes a move, he can choose whether or not to write the digit written in this square on the white board. All digits written on the whiteboard form some string. Every time a new digit is written, it goes to the end of the current string. Wilbur has q strings that he is worried about. For each string si, Wilbur wants to know whether there exists a starting position (x, y) so that by making finitely many moves, Wilbur can end up with the string si written on the white board. Input The first line of the input consists of three integers n, m, and q (1 ≀ n, m, q ≀ 200) β€” the dimensions of the table and the number of strings to process, respectively. Each of the next n lines contains m digits from 0 and 9 giving the table itself. Then follow 10 lines. The i-th of them contains the values ai - 1 and bi - 1 ( - 200 ≀ ai, bi ≀ 200), i.e. the vector that Wilbur uses to make a move from the square with a digit i - 1 in it. There are q lines that follow. The i-th of them will contain a string si consisting only of digits from 0 to 9. It is guaranteed that the total length of these q strings won't exceed 1 000 000. Output For each of the q strings, print "YES" if Wilbur can choose x and y in order to finish with this string after some finite number of moves. If it's impossible, than print "NO" for the corresponding string. Examples Input 1 1 2 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0000000000000 2413423432432 Output YES NO Input 4 2 5 01 23 45 67 0 1 0 -1 0 1 0 -1 0 1 0 -1 0 1 0 -1 0 1 0 -1 0000000000 010101011101 32232232322 44343222342444324 6767 Output YES YES YES NO YES Note In the first sample, there is a 1 by 1 table consisting of the only digit 0. The only move that can be made is staying on the square. The first string can be written on the white board by writing 0 repeatedly. The second string cannot be written as there is no 2 on the table. Submitted Solution: ``` from itertools import chain from sys import setrecursionlimit setrecursionlimit(100000) read = lambda: map(int, input().split()) read_digit_string = lambda: [ord(c) - ord('0') for c in input()] n, m, q = read() field = list(chain.from_iterable(read_digit_string() for r in range(n))) delta = [tuple(read()) for _ in range(10)] def get_to(x, y): x1, y1 = delta[field[x * m + y]] x1 += x y1 += y return x1 * m + y1 if 0 <= x1 < n and 0 <= y1 < m else x * m + y def test(mask, bit): return ((mask >> bit) & 1) == 1 def dfs(v, s): if dp[v] != None: return dp[v] dp[v] = -2 pos = dfs(to[v], s) if pos == -1: dp[v] = -1 return -1 if pos == -2: mask = 1 << field[v] u = to[v] while u != v: mask |= 1 << field[u] u = to[u] pos = max(last[dig] for dig in range(11) if not test(mask, dig)) dp[v] = pos return pos if s[pos] == field[v]: pos -= 1 dp[v] = pos return pos def f(s): global dp dp = [None] * (n * m) global last last = [-1] * 11 for i, dig in enumerate(s): last[dig] = i return any(dfs(v, s) == -1 for v in range(n * m)) to = [get_to(i, j) for i in range(n) for j in range(m)] for _ in range(q): print("YES" if f(read_digit_string()) else "NO") ```
instruction
0
62,733
15
125,466
No
output
1
62,733
15
125,467
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. John Doe has a field, which is a rectangular table of size n Γ— m. We assume that the field rows are numbered from 1 to n from top to bottom, and the field columns are numbered from 1 to m from left to right. Then the cell of the field at the intersection of the x-th row and the y-th column has coordinates (x; y). We know that some cells of John's field are painted white, and some are painted black. Also, John has a tortoise, which can move along the white cells of the field. The tortoise can get from a white cell with coordinates (x; y) into cell (x + 1; y) or (x; y + 1), if the corresponding cell is painted white. In other words, the turtle can move only along the white cells of the field to the right or down. The turtle can not go out of the bounds of the field. In addition, John has q queries, each of them is characterized by four numbers x1, y1, x2, y2 (x1 ≀ x2, y1 ≀ y2). For each query John wants to know whether the tortoise can start from the point with coordinates (x1; y1), and reach the point with coordinates (x2; y2), moving only along the white squares of the field. Input The first line contains two space-separated integers n and m (1 ≀ n, m ≀ 500) β€” the field sizes. Each of the next n lines contains m characters "#" and ".": the j-th character of the i-th line equals "#", if the cell (i; j) is painted black and ".", if it is painted white. The next line contains integer q (1 ≀ q ≀ 6Β·105) β€” the number of queries. Next q lines contain four space-separated integers x1, y1, x2 and y2 (1 ≀ x1 ≀ x2 ≀ n, 1 ≀ y1 ≀ y2 ≀ m) β€” the coordinates of the starting and the finishing cells. It is guaranteed that cells (x1; y1) and (x2; y2) are white. Output For each of q queries print on a single line "Yes", if there is a way from cell (x1; y1) to cell (x2; y2), that meets the requirements, and "No" otherwise. Print the answers to the queries in the order, in which the queries are given in the input. Examples Input 3 3 ... .## .#. 5 1 1 3 3 1 1 1 3 1 1 3 1 1 1 1 2 1 1 2 1 Output No Yes Yes Yes Yes Input 5 5 ..... .###. ..... .###. ..... 5 1 1 5 5 1 1 1 5 1 1 3 4 2 1 2 5 1 1 2 5 Output Yes Yes Yes No Yes Submitted Solution: ``` def isok(a,x1,y1,x2,y2): b = [ [0] * (m+1) for i in range(n+1) ] b[x1][y1] = 1 for i in range(y1,len(a)): for j in range(1,len(a[i])): if (j+1 <= m): if (a[i][j+1] == '.'): b[i][j+1] = b[i][j+1] + b[i][j] if (i+1 <= n): if (a[i+1][j] == '.'): b[i+1][j] = b[i+1][j] + b[i][j] if (b[x2][y2] != 0): return True else: return False n,m = map(int,input().split()) a = [ [0] * (m+1) for i in range(n+1) ] for i in range(n): tmp = input() tmp.rstrip() for j in range(len(tmp)): a[i+1][j+1] = tmp[j] q = int(input()) for i in range(q): x1,y1,x2,y2 = map(int,input().split()) if (isok(a,x1,y1,x2,y2)): print('Yes') else: print('No') ```
instruction
0
63,421
15
126,842
No
output
1
63,421
15
126,843
Provide a correct Python 3 solution for this coding contest problem. There is a grid that consists of W \times H cells. The upper-left-most cell is (1, 1). You are standing on the cell of (1,1) and you are going to move to cell of (W, H). You can only move to adjacent lower-left, lower or lower-right cells. There are obstructions on several cells. You can not move to it. You cannot move out the grid, either. Write a program that outputs the number of ways to reach (W,H) modulo 1,000,000,009. You can assume that there is no obstruction at (1,1). Input The first line contains three integers, the width W, the height H, and the number of obstructions N. (1 \leq W \leq 75, 2 \leq H \leq 10^{18}, 0 \leq N \leq 30) Each of following N lines contains 2 integers, denoting the position of an obstruction (x_i, y_i). The last test case is followed by a line containing three zeros. Output For each test case, print its case number and the number of ways to reach (W,H) modulo 1,000,000,009. Example Input 2 4 1 2 1 2 2 1 2 2 0 0 0 Output Case 1: 4 Case 2: 0
instruction
0
63,946
15
127,892
"Correct Solution: ``` from collections import defaultdict import sys readline = sys.stdin.readline write = sys.stdout.write MOD = 10**9 + 9 def matmul(N, A, B): C = [[0]*N for i in range(N)] for i in range(N): for j in range(N): C[i][j] = sum(A[i][k] * B[k][j] for k in range(N)) % MOD return C def prepare(N, H): mat = [[0]*N for i in range(N)] res = [] for i in range(N): mat[i][i] = 1 res.append([e[:] for e in mat]) for i in range(N-1): mat[i][i+1] = mat[i+1][i] = 1 res.append([e[:] for e in mat]) while H: mat = matmul(N, mat, mat) res.append([e[:] for e in mat]) H >>= 1 return res def matpow(X, N, h, MS): k = 1 while h: if h & 1: mat = MS[k] X = [sum(ai*xi for ai, xi in zip(mat_i, X)) % MOD for mat_i in mat] h >>= 1 k += 1 return X cnt = 1 def solve(): W, H, N = map(int, readline().split()) if W == H == N == 0: return False P = defaultdict(list) for i in range(N): x, y = map(int, readline().split()) if y > 1: P[y-1].append(x-1) MS = prepare(W, H) *S, = P.items() S.sort() X = [0]*W X[0] = 1 prv = 0 for y, vs in S: X = matpow(X, W, y-prv, MS) for v in vs: X[v] = 0 prv = y if prv < H-1: X = matpow(X, W, H-1-prv, MS) write("Case %d: %d\n" % (cnt, X[W-1])) return True while solve(): cnt += 1 ```
output
1
63,946
15
127,893
Provide a correct Python 3 solution for this coding contest problem. There is a grid that consists of W \times H cells. The upper-left-most cell is (1, 1). You are standing on the cell of (1,1) and you are going to move to cell of (W, H). You can only move to adjacent lower-left, lower or lower-right cells. There are obstructions on several cells. You can not move to it. You cannot move out the grid, either. Write a program that outputs the number of ways to reach (W,H) modulo 1,000,000,009. You can assume that there is no obstruction at (1,1). Input The first line contains three integers, the width W, the height H, and the number of obstructions N. (1 \leq W \leq 75, 2 \leq H \leq 10^{18}, 0 \leq N \leq 30) Each of following N lines contains 2 integers, denoting the position of an obstruction (x_i, y_i). The last test case is followed by a line containing three zeros. Output For each test case, print its case number and the number of ways to reach (W,H) modulo 1,000,000,009. Example Input 2 4 1 2 1 2 2 1 2 2 0 0 0 Output Case 1: 4 Case 2: 0
instruction
0
63,947
15
127,894
"Correct Solution: ``` import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**13 mod = 10**9+9 dd = [(-1,0),(0,1),(1,0),(0,-1)] ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def pf(s): return print(s, flush=True) class Matrix(): def __init__(self, A): self.A = A self.row = len(A) self.col = len(A[0]) def __iter__(self): return self.A.__iter__() def __getitem__(self, i): return self.A.__getitem__(i) def __add__(self, B): aa = self.A bb = B.A return Matrix([[aa[i][j] + bb[i][j] for j in range(self.col)] for i in range(self.row)]) def __sub__(self, B): aa = self.A bb = B.A return Matrix([[aa[i][j] - bb[i][j] for j in range(self.col)] for i in range(self.row)]) def __mul__(self, B): bb = [[B.A[j][i] for j in range(B.row)] for i in range(B.col)] return Matrix([[sum([ak * bk for ak,bk in zip(ai,bj)]) % mod for bj in bb] for ai in self.A]) def __truediv__(self, x): pass def pow_init(self, a): self.PA = pa = {} t = self.pow(a[0]) c = a[0] pa[c] = t for d in a[1:]: t = t * self.pow(d-c) pa[d] = t c = d def pow(self, n): if n in self.PA: return self.PA[n] A = self r = Matrix([[0 if j != i else 1 for j in range(self.row)] for i in range(self.row)]) while n > 0: if n % 2 == 1: r = r * A A = A * A n //= 2 return r def __str__(self): return self.A.__str__() def main(): rr = [] def f(w,h,n): a = sorted([LI()[::-1] for _ in range(n)]) aa = [[0]*w for _ in range(w)] for i in range(w): ai = aa[i] ai[i] = 1 if i > 0: ai[i-1] = 1 if i < w-1: ai[i+1] = 1 A = Matrix(aa) A.pow_init(list(map(lambda x: x[0], a)) + [h]) t = [[0] for _ in range(w)] t[0][0] = 1 T = Matrix(t) c = 1 for hi,wi in a: T = A.pow(hi-c) * T T.A[wi-1][0] = 0 c = hi T = A.pow(h-c) * T return T.A[-1][0] ci = 0 while 1: ci += 1 n,m,l = LI() if n == 0: break rr.append('Case {}: {}'.format(ci, f(n,m,l))) return '\n'.join(map(str, rr)) print(main()) ```
output
1
63,947
15
127,895
Provide tags and a correct Python 3 solution for this coding contest problem. You've got a 5 Γ— 5 matrix, consisting of 24 zeroes and a single number one. Let's index the matrix rows by numbers from 1 to 5 from top to bottom, let's index the matrix columns by numbers from 1 to 5 from left to right. In one move, you are allowed to apply one of the two following transformations to the matrix: 1. Swap two neighboring matrix rows, that is, rows with indexes i and i + 1 for some integer i (1 ≀ i < 5). 2. Swap two neighboring matrix columns, that is, columns with indexes j and j + 1 for some integer j (1 ≀ j < 5). You think that a matrix looks beautiful, if the single number one of the matrix is located in its middle (in the cell that is on the intersection of the third row and the third column). Count the minimum number of moves needed to make the matrix beautiful. Input The input consists of five lines, each line contains five integers: the j-th integer in the i-th line of the input represents the element of the matrix that is located on the intersection of the i-th row and the j-th column. It is guaranteed that the matrix consists of 24 zeroes and a single number one. Output Print a single integer β€” the minimum number of moves needed to make the matrix beautiful. Examples Input 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Output 3 Input 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 Output 1
instruction
0
64,274
15
128,548
Tags: implementation Correct Solution: ``` #263A in codeforces matrix = [] for _ in range(5): matrix.append(list(map(int,input().split()))) for i in range(5): flag = 0 for j in range(5): if matrix[i][j] == 1: flag=1 break if flag == 1: break moves = abs(2-i)+abs(2-j) print(moves) ```
output
1
64,274
15
128,549
Provide tags and a correct Python 3 solution for this coding contest problem. You've got a 5 Γ— 5 matrix, consisting of 24 zeroes and a single number one. Let's index the matrix rows by numbers from 1 to 5 from top to bottom, let's index the matrix columns by numbers from 1 to 5 from left to right. In one move, you are allowed to apply one of the two following transformations to the matrix: 1. Swap two neighboring matrix rows, that is, rows with indexes i and i + 1 for some integer i (1 ≀ i < 5). 2. Swap two neighboring matrix columns, that is, columns with indexes j and j + 1 for some integer j (1 ≀ j < 5). You think that a matrix looks beautiful, if the single number one of the matrix is located in its middle (in the cell that is on the intersection of the third row and the third column). Count the minimum number of moves needed to make the matrix beautiful. Input The input consists of five lines, each line contains five integers: the j-th integer in the i-th line of the input represents the element of the matrix that is located on the intersection of the i-th row and the j-th column. It is guaranteed that the matrix consists of 24 zeroes and a single number one. Output Print a single integer β€” the minimum number of moves needed to make the matrix beautiful. Examples Input 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Output 3 Input 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 Output 1
instruction
0
64,279
15
128,558
Tags: implementation Correct Solution: ``` x = input().split() y = input().split() z = input().split() w = input().split() s = input().split() count = 0 col = 0 def col_distance(r): for index, xs in enumerate(r): if xs == '1': if index < 2: return 2 - index else: return index - 2 if '1' in x: count = 2 col = col_distance(x) if '1' in y: count = 1 col = col_distance(y) if '1' in z: count = 0 col = col_distance(z) if '1' in w: count = 1 col = col_distance(w) if '1' in s: count = 2 col = col_distance(s) print(col + count) ```
output
1
64,279
15
128,559
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You've got a 5 Γ— 5 matrix, consisting of 24 zeroes and a single number one. Let's index the matrix rows by numbers from 1 to 5 from top to bottom, let's index the matrix columns by numbers from 1 to 5 from left to right. In one move, you are allowed to apply one of the two following transformations to the matrix: 1. Swap two neighboring matrix rows, that is, rows with indexes i and i + 1 for some integer i (1 ≀ i < 5). 2. Swap two neighboring matrix columns, that is, columns with indexes j and j + 1 for some integer j (1 ≀ j < 5). You think that a matrix looks beautiful, if the single number one of the matrix is located in its middle (in the cell that is on the intersection of the third row and the third column). Count the minimum number of moves needed to make the matrix beautiful. Input The input consists of five lines, each line contains five integers: the j-th integer in the i-th line of the input represents the element of the matrix that is located on the intersection of the i-th row and the j-th column. It is guaranteed that the matrix consists of 24 zeroes and a single number one. Output Print a single integer β€” the minimum number of moves needed to make the matrix beautiful. Examples Input 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Output 3 Input 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 Output 1 Submitted Solution: ``` diffX = 0 diffY = 0 matrix = [] x,y = 0,0 for _ in range(5): #matrix.append(list(map(int,"0 0 0 0 0".split(' ')))) matrix.append(list(map(int,input().split(' ')))) #matrix.append(list(map(int,"0 0 1 0 0".split(' ')))) #print(matrix) for i in range(5): for j in range(5): if matrix[i][j] == 1: x = i y = j #print("x: " + str(x) + " y: " + str(y)) if y > 2: diffY = 2 - y else: diffY = y - 2 if x > 2: diffX = 2 - x else: diffX = x - 2 print(abs(diffX+diffY)) ```
instruction
0
64,280
15
128,560
Yes
output
1
64,280
15
128,561