Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6dd871e

Browse files
init
0 parents commit 6dd871e

38 files changed

+865
-0
lines changed

‎.vscode/launch.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Python: Current File",
9+
"type": "python",
10+
"request": "launch",
11+
"program": "${file}",
12+
"console": "integratedTerminal"
13+
}
14+
]
15+
}
1.6 KB
Binary file not shown.
Binary file not shown.
955 Bytes
Binary file not shown.
Binary file not shown.
837 Bytes
Binary file not shown.
1.38 KB
Binary file not shown.

‎Algorithms/a_star.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import pygame
2+
from queue import PriorityQueue
3+
4+
def h(p1, p2):
5+
x1, y1 = p1
6+
x2, y2 = p2
7+
return abs(x1 - x2) + abs(y1 - y2)
8+
9+
def reconstruct_path(came_from, current, draw):
10+
while current in came_from:
11+
current = came_from[current]
12+
current.make_path()
13+
draw()
14+
15+
def astar(draw, grid, start, end):
16+
count = 0
17+
open_set = PriorityQueue()
18+
open_set.put((0, count, start))
19+
came_from = {}
20+
g_score = {spot: float("inf") for row in grid for spot in row}
21+
g_score[start] = 0
22+
f_score = {spot: float("inf") for row in grid for spot in row}
23+
f_score[start] = h(start.get_pos(), end.get_pos())
24+
25+
open_set_hash = {start}
26+
27+
while not open_set.empty():
28+
for event in pygame.event.get():
29+
if event.type == pygame.QUIT:
30+
pygame.quit()
31+
32+
current = open_set.get()[2]
33+
open_set_hash.remove(current)
34+
35+
if current == end:
36+
reconstruct_path(came_from, end, draw)
37+
end.make_end()
38+
return True
39+
40+
for neighbor in current.neighbors:
41+
temp_g_score = g_score[current] + 1
42+
43+
if temp_g_score < g_score[neighbor]:
44+
came_from[neighbor] = current
45+
g_score[neighbor] = temp_g_score
46+
f_score[neighbor] = temp_g_score + h(neighbor.get_pos(), end.get_pos())
47+
if neighbor not in open_set_hash:
48+
count += 1
49+
open_set.put((f_score[neighbor], count, neighbor))
50+
open_set_hash.add(neighbor)
51+
neighbor.make_open()
52+
53+
draw()
54+
55+
if current != start:
56+
current.make_closed()
57+
58+
return False

‎Algorithms/best_first_search.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import pygame
2+
from queue import PriorityQueue
3+
4+
def color_best(draw, grid, path):
5+
for node in path:
6+
grid[node[0]][node[1]].make_path()
7+
draw()
8+
9+
def h_best(node, end):
10+
11+
x1 = node.row
12+
y1 = node.col
13+
x2 = end.row
14+
y2 = end.col
15+
return abs(x1 - x2) + abs(y1 - y2)
16+
17+
def best_first_search(draw, grid, start, end):
18+
19+
path = []
20+
pqueue = PriorityQueue()
21+
t = (start.row, start.col)
22+
q = (h_best(start,end), t)
23+
pqueue.put(q)
24+
25+
while pqueue:
26+
#print(pqueue)
27+
u = pqueue.get()
28+
#print(u)
29+
t = u[1]
30+
grid[t[0]][t[1]].make_closed()
31+
path.append(t)
32+
33+
if grid[t[0]][t[1]] == end:
34+
color_best(draw, grid, path)
35+
return
36+
else:
37+
for neighbor in grid[t[0]][t[1]].neighbors:
38+
if neighbor.isVisited[0] == 0:
39+
neighbor.isVisited[0] = 1
40+
t = (neighbor.row,neighbor.col)
41+
q = (h_best(neighbor, end), t)
42+
pqueue.put(q)
43+
grid[t[0]][t[1]].make_open()
44+
grid[t[0]][t[1]].isVisited[0] = 1

‎Algorithms/bfs.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import pygame
2+
3+
def color_bfs_path(path, grid, draw):
4+
5+
for node in path:
6+
grid[node[0]][node[1]].make_path()
7+
draw()
8+
9+
def bfs(draw, grid, start, end):
10+
11+
queue = []
12+
t = (start.row, start.col)
13+
queue.append([t])
14+
15+
while queue:
16+
17+
path = []
18+
path = queue.pop(0)
19+
node = path[-1]
20+
21+
grid[node[0]][node[1]].isVisited[0] = 1;
22+
23+
grid[node[0]][node[1]].make_closed()
24+
25+
if grid[node[0]][node[1]] == end:
26+
color_bfs_path(path, grid, draw)
27+
return
28+
29+
for adjacent in grid[node[0]][node[1]].neighbors:
30+
31+
if (grid[adjacent.row][adjacent.col]).isVisited[0] == 0:
32+
33+
adjacent.make_open()
34+
new_path = list(path)
35+
t = (adjacent.row, adjacent.col)
36+
new_path.append(t)
37+
queue.append(new_path)
38+
grid[adjacent.row][adjacent.col].isVisited[0] = 1
39+
draw()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /