Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 37 additions & 29 deletions dp.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,54 @@
SEARCH_DEPTH = 4 # Anything above 4 may cause lag

def get_flooded_size_sim(graph, current_colors):
"""Helper: Counts size of flooded region for a simulation state."""
target_c = current_colors[0]
count = 0
visited = {0}
q = [0]
while q:
u = q.pop(0)
count += 1
for v in graph[u]:
if v not in visited and current_colors[v] == target_c:
visited.add(v)
q.append(v)
return count
start_node = 0
target_color = current_colors[start_node]

queue = [start_node]
visited = {start_node}

while queue:
node = queue.pop(0)

for neighbour in graph[node]:
if neighbour not in visited and current_colors[neighbour] == target_color:
visited.add(neighbour)
queue.append(neighbour)

return len(visited)


def simulate_move(graph, current_colors, move_color):
def simulate_move(graph, current_color, move_color):
"""
Returns a NEW list of colors after applying a hypothetical move.
Does NOT change the global 'color' list.
State Transition Function (S → S′)
Creates a new hypothetical board after applying a move.
Does NOT modify the original board.
"""
new_colors = list(current_colors)

new_colors = list(current_color)

start_node = 0
old_c = new_colors[start_node]

if old_c == move_color:
return new_colors
old_color = new_colors[start_node]

# BFS to change color
q = [start_node]
if old_color == move_color:
return new_colors

# BFS
queue = [start_node]
visited = {start_node}
new_colors[start_node] = move_color
while q:
u = q.pop(0)

while queue:
u = queue.pop(0)
for v in graph[u]:
if v not in visited and new_colors[v] == old_c:
if v not in visited and new_colors[v] == old_color:
visited.add(v)
new_colors[v] = move_color
q.append(v)
queue.append(v)

return new_colors


def dp_solve(current_colors, depth, graph):
"""
Recursive DP Function with Memoization.
Expand Down Expand Up @@ -106,4 +114,4 @@ def dp_color_selector(graph, color) -> int:
if move is None:
return 1

return move
return move
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,4 +399,4 @@ def draw_grid(color):
iconlabel2 = tk.Label(root, image=icon2, bg="#282A36")
iconlabel2.place(x=700, y=70, anchor="sw")

root.mainloop()
root.mainloop()