mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-25 10:28:39 +00:00
Compare commits
4 Commits
9972afa80a
...
eee84fcd04
Author | SHA1 | Date | |
---|---|---|---|
|
eee84fcd04 | ||
|
129176a4f3 | ||
|
85d33bea6c | ||
|
60a8d009eb |
@ -1,13 +1,4 @@
|
|||||||
import heapq
|
import heapq
|
||||||
import doctest
|
|
||||||
|
|
||||||
|
|
||||||
class UniformCostSearch:
|
|
||||||
def __init__(
|
|
||||||
self, current: list[int], final: list[int], grid: list[list[int]]
|
|
||||||
) -> None:
|
|
||||||
self.m = len(grid[0])
|
|
||||||
self.n = len(grid)
|
|
||||||
|
|
||||||
# diagonal clockwise
|
# diagonal clockwise
|
||||||
dxy1 = [
|
dxy1 = [
|
||||||
@ -77,6 +68,12 @@ class UniformCostSearch:
|
|||||||
(-1, 1),
|
(-1, 1),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
class UniformCostSearch:
|
||||||
|
def __init__(self, grid: list[list[int]]) -> None:
|
||||||
|
self.m = len(grid[0])
|
||||||
|
self.n = len(grid)
|
||||||
|
|
||||||
def get_shortest_path(
|
def get_shortest_path(
|
||||||
self,
|
self,
|
||||||
start: list[int],
|
start: list[int],
|
||||||
@ -86,8 +83,12 @@ class UniformCostSearch:
|
|||||||
) -> list[list[int]]:
|
) -> list[list[int]]:
|
||||||
"""
|
"""
|
||||||
Return 2D list where optimal path is stored.
|
Return 2D list where optimal path is stored.
|
||||||
>>> get_shortest_path([0, 2],[2, 2], [['inf','inf',1],['inf,2,2],['inf',0,3]], [(1, 1),(1, 0),(1, -1),(0, -1),(-1, -1),(-1, 0),(-1, 1),(0, 1)])
|
>>> start = [0,0]
|
||||||
[[0,2],[1,2],[2,2]]
|
>>> end = [1, 2]
|
||||||
|
>>> dist = [[1,0],[0,2]]
|
||||||
|
>>> dxy = [(1, 1),(1, 0),(1, -1),(0, -1),(-1, -1),(-1, 0),(-1, 1),(0, 1)]
|
||||||
|
>>> get_shortest_path(start,end,dist,dxy)
|
||||||
|
[[0,0],[1,1]]
|
||||||
"""
|
"""
|
||||||
shortest_path = []
|
shortest_path = []
|
||||||
curr_node = end
|
curr_node = end
|
||||||
@ -120,8 +121,14 @@ class UniformCostSearch:
|
|||||||
) -> list[list[int]]:
|
) -> list[list[int]]:
|
||||||
"""
|
"""
|
||||||
Return 2D list where optimal path is stored.
|
Return 2D list where optimal path is stored.
|
||||||
>>> ucs([0, 2],[[1,2],[2, 2]], [[0,0,0],[0,0,0],[0,0,0]], [[None, None, None], [None, None, None],[None, None, None]], [(1, 1),(1, 0),(1, -1),(0, -1),(-1, -1),(-1, 0),(-1, 1),(0, 1)], [1000000, 100000])
|
>>> current = [0, 0]
|
||||||
[[0,2],[1,2],[2,2]]
|
>>> final = [[1, 1]]
|
||||||
|
>>> grid = [[0,0],[0,0]]
|
||||||
|
>>> prev = [[None, None],[None, None]]
|
||||||
|
>>> dxy = [(1, 1),(1, 0),(1, -1),(0, -1),(-1, -1),(-1, 0),(-1, 1),(0, 1)]
|
||||||
|
>>> goal_answer = [1000000, 100000]
|
||||||
|
>>> ucs(current,final,grid,dxy,prev,goal_answer)
|
||||||
|
[[0,2],[1,1]]
|
||||||
"""
|
"""
|
||||||
|
|
||||||
dist = [[float("inf") for _ in range(self.m)] for _ in range(self.n)]
|
dist = [[float("inf") for _ in range(self.m)] for _ in range(self.n)]
|
||||||
@ -175,25 +182,28 @@ class UniformCostSearch:
|
|||||||
) -> list[list[int]]:
|
) -> list[list[int]]:
|
||||||
"""
|
"""
|
||||||
Return 2D list where optimal path is stored.
|
Return 2D list where optimal path is stored.
|
||||||
>>> your_algorithm([0, 2],[2, 2], [[0,0,0],[0,0,0],[0,0,0]])
|
>>> start_point = [0, 0]
|
||||||
[[0,2],[1,2],[2,2]]
|
>>> end_point = [1, 1]
|
||||||
|
>>> grid = [[0,0],[0,0]]
|
||||||
|
>>> your_algorithm(start_point, end_point, grid)
|
||||||
|
[[0,0],[1,1]]
|
||||||
"""
|
"""
|
||||||
prev = [[None for _ in range(self.m)] for _ in range(self.n)]
|
prev = [[None for _ in range(self.m)] for _ in range(self.n)]
|
||||||
dxy = []
|
dxy = []
|
||||||
if start_point[1] - end_point[1] == 0 and start_point[0] - end_point[0] < 0:
|
if start_point[1] - end_point[1] == 0 and start_point[0] - end_point[0] < 0:
|
||||||
dxy = self.dxy5
|
dxy = dxy5
|
||||||
elif start_point[1] - end_point[1] == 0 and start_point[0] - end_point[0] > 0:
|
elif start_point[1] - end_point[1] == 0 and start_point[0] - end_point[0] > 0:
|
||||||
dxy = self.dxy6
|
dxy = dxy6
|
||||||
elif start_point[0] - end_point[0] == 0 and start_point[1] - end_point[1] < 0:
|
elif start_point[0] - end_point[0] == 0 and start_point[1] - end_point[1] < 0:
|
||||||
dxy = self.dxy4
|
dxy = dxy4
|
||||||
elif start_point[0] - end_point[0] == 0 and start_point[1] - end_point[1] > 0:
|
elif start_point[0] - end_point[0] == 0 and start_point[1] - end_point[1] > 0:
|
||||||
dxy = self.dxy3
|
dxy = dxy3
|
||||||
elif start_point[0] - end_point[0] > 0:
|
elif start_point[0] - end_point[0] > 0:
|
||||||
dxy = self.dxy2
|
dxy = dxy2
|
||||||
elif start_point[0] - end_point[0] < 0:
|
elif start_point[0] - end_point[0] < 0:
|
||||||
dxy = self.dxy1
|
dxy = dxy1
|
||||||
goal_answer = []
|
goal_answer = []
|
||||||
for cell in end_point:
|
for _ in range(0, len(end_point)):
|
||||||
goal_answer.append(10**8)
|
goal_answer.append(10**8)
|
||||||
path = self.ucs(start_point, [end_point], grid, prev, dxy, goal_answer)
|
path = self.ucs(start_point, [end_point], grid, prev, dxy, goal_answer)
|
||||||
return path
|
return path
|
||||||
@ -202,12 +212,10 @@ class UniformCostSearch:
|
|||||||
def run() -> None:
|
def run() -> None:
|
||||||
"""
|
"""
|
||||||
Return None. Its just running the UCS algorithm class.
|
Return None. Its just running the UCS algorithm class.
|
||||||
>>> run(2, 2)
|
>>> run()
|
||||||
None
|
None
|
||||||
"""
|
"""
|
||||||
executed_object = UniformCostSearch(
|
executed_object = UniformCostSearch(
|
||||||
[0, 7],
|
|
||||||
[19, 17],
|
|
||||||
[
|
[
|
||||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
||||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
|
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
|
||||||
|
Loading…
x
Reference in New Issue
Block a user