Created
July 9, 2021 15:07
-
-
Save tinwritescode/931f26bdd97cc4dbfaae14f8af71365d to your computer and use it in GitHub Desktop.
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 PIL import Image | |
| import numpy as np | |
| import re | |
| IMG_PATH = "map.bmp" | |
| INPUT_PATH = "input.txt" | |
| OUTPUT_PATH = "output.txt" | |
| def bitmapToArray(path): | |
| image = Image.open("map.bmp") | |
| return np.array(image) | |
| def arrayToBitmap(arr, path): | |
| im = Image.fromarray(arr) | |
| im.save(path) | |
| return True | |
| def solve(arr, start, end, m): | |
| # X and Y are array size | |
| x = len(arr) | |
| y = len(arr[0]) | |
| # Queue for storing vertex, and score for storing a* score | |
| queue = [] | |
| scoreArr = np.zeros([x, y], dtype=int) | |
| # append the start vertex | |
| queue.append(start) | |
| # Pop vertex and calculate F(n) and H(n) | |
| # where F(n): Distance function | |
| # H(n): Heuristic function | |
| # Mask array base on clock dimension, relative index: https://imgur.com/a/WYSrEZ4 | |
| mask = [[-1,0], [-1,1], [0,1], [1,1], [1,0], [1,-1], [0,-1], [-1,-1]] | |
| for i in arr: | |
| k = queue.pop(0) | |
| for j in mask: | |
| # From/to point | |
| fromP = k | |
| toP = [k[0] + j[0], k[1] + j[1]] | |
| distance = calculateDistance(arr, fromP, toP) | |
| if distance != inf: | |
| # Can go | |
| h = calculateHeuristic(arr, fromP, toP) | |
| # TODO: check if current point is higher than f(n) + h(n), if it is, change to the new value | |
| def outputToFile(path): | |
| print("outputFile", path) | |
| return True | |
| def main(): | |
| # Initialize | |
| arr = bitmapToArray(IMG_PATH) | |
| start = [] | |
| end = [] | |
| m = 0 | |
| # Read file | |
| with open(INPUT_PATH) as f: | |
| lines = f.readlines() | |
| # Start point | |
| tmp = re.match("\((\d+);(\d+)\)", lines[0]) | |
| if tmp: | |
| start.append(int(tmp.groups()[0])) | |
| start.append(int(tmp.groups()[1])) | |
| # Destination point | |
| tmp = re.match("\((\d+);(\d+)\)", lines[1]) | |
| if tmp: | |
| end.append(int(tmp.groups()[0])) | |
| end.append(int(tmp.groups()[1])) | |
| m = int(lines[2]) | |
| solve(arr, start, end, m) | |
| # Output to file | |
| arrayToBitmap(arr, "test.bmp") | |
| outputToFile(OUTPUT_PATH) | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment