Skip to content

Instantly share code, notes, and snippets.

@KarthikMAM
Last active July 23, 2017 00:15
Show Gist options
  • Select an option

  • Save KarthikMAM/b7df863c259b5d3b761cb57778df848c to your computer and use it in GitHub Desktop.

Select an option

Save KarthikMAM/b7df863c259b5d3b761cb57778df848c to your computer and use it in GitHub Desktop.
from functools import reduce
for _ in range(int(input())):
input()
print(reduce(lambda x, y: x ^ y, list(map(int, input().split()))))
valid_pos = [(1, 2),(1, -2),(-1, 2),(-1, -2),(2, 1),(2, -1),(-2, 1),(-2, -1)]
from pprint import pprint
for _ in range(int(input())):
n = int(input())
x1, y1 = list(map(lambda i: int(i) - 1, input().split()))
x2, y2 = list(map(lambda i: int(i) - 1, input().split()))
d = [ [ n * n ] * n for i in range(n) ]
d[x1][y1] = 0
s = [(x1, y1)]
while len(s) > 0:
x, y = s.pop()
if d[x][y] != n * n:
for i, j in valid_pos:
if 0 <= x + i < n and 0 <= y + j < n and d[x+i][y+j] > d[x][y] + 1:
d[x + i][y + j] = d[x][y] + 1
s.insert(0, tuple([x + i, y + j]))
print(1 if d[x2][y2] == n * n else d[x2][y2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment