Last active
July 23, 2017 00:15
-
-
Save KarthikMAM/b7df863c259b5d3b761cb57778df848c to your computer and use it in GitHub Desktop.
Solutions to http://practice.geeksforgeeks.org/problems/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from functools import reduce | |
| for _ in range(int(input())): | |
| input() | |
| print(reduce(lambda x, y: x ^ y, list(map(int, input().split())))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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