Chittopadhyay 1
Chittopadhyay 1
if(b+c<=z): ans.reverse()
ans.append(state) print(i)
return True
else:
if( get_all_states((a,b-(z-c),z)) ):
ans.append(state)
return True
#empty jug c
if(c>0):
#empty b into a
if(a+c<=x):
if( get_all_states((a+c,b,0)) ):
ans.append(state)
return True
else:
if( get_all_states((x,b,c-(x-a))) ):
#PRACTICAL-4 astar 'A': {'B': 1, 'C': 4},
import heapq 'B': {'A': 1, 'D': 2, 'E': 5},
def astar(graph, start, goal, heuristic): 'C': {'A': 4, 'F': 3},
open_set = [] 'D': {'B': 2},
heapq.heappush(open_set, (heuristic(start, 'E': {'B': 5, 'F': 1},
goal), start))
'F': {'C': 3, 'E': 1} }
came_from = {}
def manhattan_distance(a, b):
cost_so_far = {start: 0}
# Replace with your actual heuristic
while open_set: calculation
_, current = heapq.heappop(open_set) return abs(ord(a) - ord(b))
if current == goal: start_node = 'A'
return reconstruct_path(came_from, goal_node = 'F'
current)
path = astar(graph, start_node, goal_node,
manhattan_distance)
for neighbor in graph[current]: if path:
new_cost = cost_so_far[current] + print("Path found:", path)
graph[current][neighbor]
else:
if neighbor not in cost_so_far or
new_cost < cost_so_far[neighbor]: print("No path found.")
cost_so_far[neighbor] = new_cost
priority = new_cost +
heuristic(neighbor, goal)
heapq.heappush(open_set, (priority,
neighbor))
came_from[neighbor] = current
return None
def reconstruct_path(came_from, current):
total_path = [current]
while current in came_from:
current = came_from[current]
total_path.insert(0, current)
return total_path
# Example usage:
graph = {
#practical-5 tic tac toe elif(board[4] == board[5] and board[5] ==
board[6] and board[4] != ' '):
import os , import time
Game = Win
board = [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ']
elif(board[7] == board[8] and board[8] ==
player = 1 board[9] and board[7] != ' '):
####Win Flags######### Game = Win
Win = 1 ,Draw = -1 ,Running = 0 #Vertical Winning Condition
Stop = 1 elif(board[1] == board[4] and board[4] ==
##################################### board[7] and board[1] != ' '):
return d1 + d2 + d3 + d4 d3 = newDistance(starting_Point[0],
starting_Point[1]+increment, point1, point2,
point3, point4)
def newDistance(x1, y1, point1, point2, d4 = newDistance(starting_Point[0],
point3, point4): starting_Point[1]-increment, point1, point2,
d1 = [x1, y1] point3, point4)