Last active
October 3, 2023 13:55
-
-
Save navin-bhaskar/588f2d808e7dbd880bcaa42a28a26ce2 to your computer and use it in GitHub Desktop.
Assasin in maze
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
| import pprint | |
| from collections import deque | |
| import os | |
| import time | |
| X_MARK = "X" | |
| def solve(inp_array): | |
| rows = len(inp_array) | |
| cols = len(inp_array[0]) | |
| pp = pprint.PrettyPrinter(width=41, compact=True) | |
| print("Input: ") | |
| pp.pprint(inp_array) | |
| board = [["_" for _ in range(cols)] for _ in range(rows)] | |
| for i in range(rows): | |
| for j in range(cols): | |
| if inp_array[i][j] == "v": | |
| board[i][j] = X_MARK | |
| k = i + 1 | |
| while k < rows and inp_array[k][j] == ".": | |
| board[k][j] = X_MARK | |
| k += 1 | |
| elif inp_array[i][j] == "^": | |
| board[i][j] = X_MARK | |
| k = i - 1 | |
| while k >= 0 and inp_array[k][j] == ".": | |
| board[k][j] = X_MARK | |
| k -= 1 | |
| elif inp_array[i][j] == ">": | |
| board[i][j] = X_MARK | |
| k = j + 1 | |
| while k < cols and inp_array[i][k] == ".": | |
| board[i][k] = X_MARK | |
| k += 1 | |
| elif inp_array[i][j] == "<": | |
| board[i][j] = X_MARK | |
| k = j - 1 | |
| while k >= 0 and inp_array[i][k] == ".": | |
| board[i][k] = X_MARK | |
| k -= 1 | |
| elif board[i][j] == "_": | |
| board[i][j] = inp_array[i][j] | |
| pp.pprint(board) | |
| nodes_to_visit = deque() | |
| nbrs_dx = [[-1, 0], [0, 1], [1, 0], [0, -1]] | |
| for i in range(rows): | |
| for j in range(cols): | |
| if inp_array[i][j] == "A": | |
| nodes_to_visit.append([i, j]) | |
| break | |
| while nodes_to_visit: | |
| os.system("cls") | |
| cur_i, cur_j = nodes_to_visit.popleft() | |
| board[cur_i][cur_j] = "A" # Mark current node as visiting | |
| pp.pprint(board) | |
| if cur_i == rows - 1 and cur_j == cols - 1: | |
| board[cur_i][cur_j] = "T" # Target reached | |
| os.system("cls") | |
| pp.pprint(board) | |
| return True | |
| # Find next location | |
| for nbr_dx in nbrs_dx: | |
| temp_i, temp_j = cur_i + nbr_dx[0], cur_j + nbr_dx[1] | |
| if temp_i >= 0 and temp_i < rows and temp_j >= 0 and temp_j < cols: | |
| if board[temp_i][temp_j] == ".": | |
| nodes_to_visit.append([temp_i, temp_j]) | |
| # board[cur_i][cur_j] = "0" | |
| # os.system("cls") | |
| # pp.pprint(board) | |
| board[cur_i][cur_j] = "-" | |
| inp = input() | |
| return False | |
| # inp = [".v.", "...", "A.."] | |
| # print("Can reach? " + str(solve(inp))) | |
| # time.sleep(2) | |
| # inp = ["...<", "A..^", "X..."] | |
| # print("Can reach? " + str(solve(inp))) | |
| # time.sleep(2) | |
| # inp = ["x.....>", "..v..X.", ".>..X..", "A......"] | |
| # print("Can reach? " + str(solve(inp))) | |
| # time.sleep(2) | |
| inp = ["...Xv", "AX..^", ".XX.."] | |
| print("Can reach? " + str(solve(inp))) | |
| time.sleep(2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment