Skip to content

Instantly share code, notes, and snippets.

@YeyoM
Created July 28, 2023 00:53
Show Gist options
  • Save YeyoM/36912d4dcd5a34b90f3b8290825d144b to your computer and use it in GitHub Desktop.
Save YeyoM/36912d4dcd5a34b90f3b8290825d144b to your computer and use it in GitHub Desktop.
My solution to the castle on grid problem from hackerrank! Using bfs, queues and stacks!
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'minimumMoves' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. STRING_ARRAY grid
# 2. INTEGER startX
# 3. INTEGER startY
# 4. INTEGER goalX
# 5. INTEGER goalY
#
def get_grid(list_grid):
grid = []
for i in range(len(list_grid)):
grid.append(list(list_grid[i]))
return grid
def get_neighbours(current, visited, matrix, prev, queue):
coord_x = current[0]
coord_y = current[1]
neighbours = []
# Get neighbours to the right
for x in range(coord_x, len(matrix)):
if not visited[x][coord_y]:
if matrix[x][coord_y] == ".":
queue.append([x, coord_y])
visited[x][coord_y] = True
prev[x][coord_y] = [coord_x, coord_y]
else: break
# Get neighbours to down
for y in range(coord_y, len(matrix)):
if not visited[coord_x][y]:
if matrix[coord_x][y] == ".":
queue.append([coord_x, y])
visited[coord_x][y] = True
prev[coord_x][y] = [coord_x, coord_y]
else: break
# Get neighbours to left
for x in range(coord_x, -1, -1):
if not visited[x][coord_y]:
if matrix[x][coord_y] == ".":
queue.append([x, coord_y])
visited[x][coord_y] = True
prev[x][coord_y] = [coord_x, coord_y]
else: break
# Get neighbours to up
for y in range(coord_y, -1, -1):
if not visited[coord_x][y]:
if matrix[coord_x][y] == ".":
queue.append([coord_x, y])
visited[coord_x][y] = True
prev[coord_x][y] = [coord_x, coord_y]
else: break
def minimumMoves(grid, startX, startY, goalX, goalY):
# Write your code here
matrix = get_grid(grid)
visited = [[False for _ in range(len(matrix))] for _ in range(len(matrix))]
prev = [[None for _ in range(len(matrix))] for _ in range(len(matrix))]
queue = []
queue.append([startX, startY])
visited[startX][startY] = True
current = queue.pop(0)
get_neighbours(current, visited, matrix, prev, queue)
print(visited)
print(prev)
print(queue)
while len(queue) != 0:
current = queue.pop(0)
get_neighbours(current, visited, matrix, prev, queue)
if [goalX, goalY] in queue: break
print(visited)
print(prev)
print(queue)
current = [goalX, goalY]
stack = []
stack.append(current)
while current != [startX, startY]:
current = prev[current[0]][current[1]]
stack.append(current)
print(stack)
return len(stack) - 1
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
n = int(input().strip())
grid = []
for _ in range(n):
grid_item = input()
grid.append(grid_item)
first_multiple_input = input().rstrip().split()
startX = int(first_multiple_input[0])
startY = int(first_multiple_input[1])
goalX = int(first_multiple_input[2])
goalY = int(first_multiple_input[3])
result = minimumMoves(grid, startX, startY, goalX, goalY)
fptr.write(str(result) + '\n')
fptr.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment