AI Lab Manual1.1
AI Lab Manual1.1
graph = {
'A' : ['B','C'],
'B' : ['D', 'E'],
'C' : ['F'],
'D' : [],
'E' : ['F'],
'F' : []
}
visited = [] # List to keep track of visited nodes.
queue = [] #Initialize a queue
while queue:
s = queue.pop(0)
print (s, end = " ")
Output:-
A B C D E F
EXPERIMENT 2
#Write a Program to Implement Depth First Search using Python.
A
B
D
E
F
C
EXPERIMENT 3
#Write a Program to Implement Tic-Tac-Toe game using Python.
for i in range(len(board)):
for j in range(len(board)):
if board[i][j] == 0:
l.append((i, j))
return(l)
for y in range(len(board)):
if board[x, y] != player:
win = False
continue
if win == True:
return(win)
return(win)
for y in range(len(board)):
if board[y][x] != player:
win = False
continue
if win == True:
return(win)
return(win)
while winner == 0:
for player in [1, 2]:
board = random_place(board, player)
print("Board after " + str(counter) + "
move") print(board) sleep(2) counter += 1
winner = evaluate(board) if winner != 0:
break
return(winner)
Output:-
[[0 0 0]
[0 0 0]
[0 0 0]]
Board after 1 move
[[0 0 0]
[0 0 0]
[1 0 0]]
Board after 2 move
[[0 0 0]
[0 2 0]
[1 0 0]]
Board after 3 move
[[0 1 0]
[0 2 0]
[1 0 0]]
Board after 4 move
[[0 1 0]
[2 2 0]
[1 0 0]]
Board after 5 move
[[1 1 0]
[2 2 0]
[1 0 0]]
Board after 6 move
[[1 1 0]
[2 2 0]
[1 2 0]]
Board after 7 move
[[1 1 0]
[2 2 0]
[1 2 1]]
Board after 8 move
[[1 1 0]
[2 2 2]
[1 2 1]]
Winner is: 2
EXPERIMENT 4
class Solution:
def solve(self, board):
dict = {}
flatten = []
for i in range(len(board)):
flatten += board[i]
flatten = tuple(flatten)
dict[flatten] = 0
return self.get_paths(dict)
def get_paths(self, dict):
cnt = 0
while True:
current_nodes = [x for x in dict if dict[x] == cnt]
if len(current_nodes) == 0:
return -1
results = []
pos_0 = node.index(0)
for move in moves[pos_0]:
new_node = list(node)
new_node[move], new_node[pos_0] = new_node[pos_0],
new_node[move]
results.append(tuple(new_node))
return results
ob = Solution()
matrix = [
[3, 1, 2],
[4, 7, 5],
[6, 8, 0]
]
print(ob.solve(matrix))
Output:-
4
EXPERIMENT 5
waterJugSolver(amt1, amt2):
else:
return False
print("Steps: ")
waterJugSolver(0, 0)
Output:-
Steps:
0 0
4 0
4 3
0 3
3 0
3 3
4 2
0 2
EXPERIMENT 6
# Driver code
Output:-
80
EXPERIMENT 7
Output:-
Move disk 1 from rod A to rod B
Move disk 2 from rod A to rod C
Move disk 1 from rod B to rod C
Move disk 3 from rod A to rod B
Move disk 1 from rod C to rod A
Move disk 2 from rod C to rod B
Move disk 1 from rod A to rod B
Move disk 4 from rod A to rod C
Move disk 1 from rod B to rod C
Move disk 2 from rod B to rod A
Move disk 1 from rod C to rod A
Move disk 3 from rod B to rod C
Move disk 1 from rod A to rod B
Move disk 2 from rod A to rod C
Move disk 1 from rod B to rod C
EXPERIMENT 8
# Write a Program to Implement Monkey Banana Problem using
Python.
'''
Python programming implementation of monkey picking banana problem
'''
#Global Variable i i=0 def
Monkey_go_box(x,y):
global i i=i+1
print('step:',i,'monkey slave',x,'Go to'+y)
def Monkey_move_box(x,y):
global i i = i + 1
print('step:', i, 'monkey take the box from', x, 'deliver to' + y)
import sys
else:
best = MAX
# Recur for left and
# right children for
i in range(0, 2):
return best
global N
N=4
def printSolution(board):
for i in range(N):
for j in range(N):
print board[i][j],
print
return True
def solveNQUtil(board, col):
# base case: If all queens are placed
# then return true if
col >= N:
return True
for i in range(N):
if isSafe(board, i, col):
# Place this queen in board[i][col]
board[i][col] = 1
# recur to place rest of the queens if
solveNQUtil(board, col + 1) == True:
return True
board[i][col] = 0
return False
def solveNQ():
board = [ [0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
]
if solveNQUtil(board, 0) == False: print
"Solution does not exist" return
False
printSolution(board)
return True
# driver program to test above function
solveNQ()
Output:-
0 0 1 0
1 0 0 0
0 0 0 1
0 1 0 0