I wrote a Monte Carlo tree search algorithm (based on https://en.wikipedia.org/wiki/Monte_Carlo_tree_search), and connected it with the "python-chess" library.
Basically, the algorithm gets stuck somewhere, because it keeps printing as output "1/2-1/2" (draw).
There's probably an issue with the expansion function, but I don't really know where.
Here's the code:
class MonteCarloTreeSearch():
def __init__(self):
self.board = chess.Board()
self.results = []
self.total_matches = 0
def expansion(self, leaf):
done = False
if not (self.board.is_game_over()):
if len(leaf.children != 0):
possible = np.array([i for i in self.board.legal_moves])
actual = []
for i in range(len(possible)):
test = possible[i]
cnt = 0
for j in range(len(leaf.children)):
if (not test == leaf.children[j].value) and cnt == 0:
actual.append(test)
cnt += 1
move = random.choice(actual)
else:
move = random.choice([i for i in self.board.legal_moves])
self.board.push(move)
if self.board.is_game_over():
done = True
child = Node(move, player="white" if leaf.player=="black" else "black", parent=[leaf], score=0)
leaf.children = np.append(leaf.children, child)
if not done:
return self.expansion(child)
return leaf
def playout(self, starting_node):
childr = self.expansion(starting_node)
result = self.board.result()
if result == "1-0":
result = 1
elif result == "0-1":
result = -1
elif result == "1/2-1/2":
result = .5
elif result == "*":
raise Exception("ERROR: Game was not over, but playout stopped.")
else:
raise Exception("ERROR: Playout process error.")
return childr, result, starting_node
def expansion_playout_backpropagation(self, start):
node, result, starting_node = self.playout(start)
i = 0
while i == 0:
node.matches += 1
if node.player == "white":
if result == 1:
node.score += 1
if node.matches > 0:
node.winp = node.score / node.matches
else:
node.winp = 0
elif node.player == "black":
if result == -1:
node.score += 1
if node.matches > 0:
node.winp = node.score / node.matches
else:
node.winp = 0
else:
raise Exception("ERROR: Invalid player selection.")
if node.is_equal(starting_node):
i += 1
else:
node = node.parent[0]
self.results.append(self.board.result())
print(self.board.result())
self.board.reset_board()
self.total_matches += 1
def backpropagation(self, node, result, starting_node):
i = 0
while i == 0:
node.matches += 1
if node.player == "white":
if result == 1:
node.score += 1
if node.matches > 0:
node.winp = node.score / node.matches
else:
node.winp = 0
elif node.player == "black":
if result == -1:
node.score += 1
if node.matches > 0:
node.winp = node.score / node.matches
else:
node.winp = 0
else:
raise Exception("ERROR: Invalid player selection.")
if node.is_equal(starting_node):
i += 1
else:
node = node.parent[0]
self.results.append(self.board.result())
print(self.board.result())
self.board.reset_board()
self.total_matches += 1
def fitness(self, node):
p = node.winp
simulations = node.matches
parent_simulations = node.parent[0].matches if node.parent[0] != None else self.matches
c = math.sqrt(2)
if simulations > 0 and parent_simulations > 0:
formula = p + c * math.sqrt((np.log(parent_simulations)) / simulations)
else:
formula = p
return formula
Related
I try to make a binary heap in python but I get trouble printing it. I make sure that the logic in the program is right but when I want to try printing it I get the wrong result. This is what I want for program output:
Input:
6
1 2
1 3
1 7
2
3
2
Output:
7
3
command 1 to insert number
command 2 to display the highest number
command 3 to delete the highest number
This is my program:
class BinaryHeap:
def __init__(self):
self.items = []
def size(self):
return len(self.items)
def parent(self, i):
return (i - 1)//2
def left(self, i):
return 2*i + 1
def right(self, i):
return 2*i + 2
def get(self, i):
return self.items[i]
def get_max(self):
if self.size() == 0:
return None
return self.items[0]
def extract_max(self):
if self.size() == 0:
return None
largest = self.get_max()
self.items[0] = self.items[-1]
del self.items[-1]
self.max_heapify(0)
return largest
def max_heapify(self, i):
l = self.left(i)
r = self.right(i)
if (l <= self.size() - 1 and self.get(l) > self.get(i)):
largest = l
else:
largest = i
if (r <= self.size() - 1 and self.get(r) > self.get(largest)):
largest = r
if (largest != i):
self.swap(largest, i)
self.max_heapify(largest)
def swap(self, i, j):
self.items[i], self.items[j] = self.items[j], self.items[i]
def insert(self, key):
index = self.size()
self.items.append(key)
while (index != 0):
p = self.parent(index)
if self.get(p) < self.get(index):
self.swap(p, index)
index = p
bheap = BinaryHeap()
n = int(input())
for i in range (n):
operation= input('What would you like to do? ').split()
if operation == 1:
data = int(operation[1])
bheap.insert(data)
elif operation == 2:
print('Maximum value: {}'.format(bheap.get_max()))
elif operation == 3:
print('Maximum value removed: {}'.format(bheap.extract_max()))
elif operation == 4:
break
I need your opinion to fixed it.
operation is a list (you called split), but you compare it as an int in your if statements. Also, you should compare it against "1", "2", ... not 1, 2, ...
So:
operation = input('What would you like to do? ').split()
if operation[0] == "1":
data = int(operation[1])
bheap.insert(data)
elif operation[0] == "2":
print('Maximum value: {}'.format(bheap.get_max()))
elif operation[0] == "3":
print('Maximum value removed: {}'.format(bheap.extract_max()))
elif operation[0] == "4":
break
If you just want 7 and 3 in the output, and only after the input has been completely processed, then you should remove those verbose print statement (where you output phrases), and instead collect the output -- for instance in a list:
output = []
n = int(input())
for i in range(n):
operation = input('What would you like to do? ').split()
if operation[0] == "1":
data = int(operation[1])
bheap.insert(data)
elif operation[0] == "2":
output.append(bheap.get_max())
elif operation[0] == "3":
bheap.extract_max()
elif operation[0] == "4":
break
print("\n".join(map(str, output)))
I'm trying solve N Puzzle with Depth First Search using python 3.
With 3 x 3 puzzle it run good and fast but with 4 x 4 puzzle, it runs too slow and can't find solution with error: "MemoryError".
I also use "h(n) = depth + number of wrong tiles" to evaluate priority of each node.
I'm a newbie to python so hope you can help me with this
Here is my code:
import sys
import getopt
import random
import time
class State:
def __init__(self, parent, board, move, depth):
self.parent = parent
self.previousMove = move
self.board = board
self.map = ''.join(str(e) for e in board)
self.depth = depth
self.cost = self.calculateCost()
def calculateCost(self):
pos = 1
count = 0
for tile in self.board:
if tile == pos:
count += 1
pos += 1
return self.depth + 8 - count
class Puzzle:
def __init__(self, k, customBoard = None):
self.k = k
self.n = k*k - 1
self.sizeOfBoard = k*k
self.timeOfSolving = 0
self.timeOfGenerateSuccessors = 0
self.maxDeepSearch = 0
self.inititalState = State(None, self.createInitialBoard(customBoard), 'Start', 0)
self.goalBoard = self.createGoalBoard()
self.finalState = None
self.stateStorage = set() # Store states that have visited
self.path = [] # Store states that lead to goalstate
self.stack = []
def isSolvable(self, board):
# count invertion in puzzle's board
invCount = 0
for i in range(0, self.sizeOfBoard - 1):
if board[i] == 0:
continue
for j in range(i+1, self.sizeOfBoard):
if board[j] == 0:
continue
if board[i] > board[j]:
invCount += 1
# print(invCount)
if (invCount % 2 == 0):
return True
return False
def createInitialBoard(self, customBoard):
print("Creating initial state")
if customBoard is None:
board = []
lstAddSuccess = []
while 1:
board.clear()
lstAddSuccess.clear()
for count in range(0, self.k*self.k):
newTile = random.randint(0, self.n)
while newTile in lstAddSuccess:
newTile = random.randint(0, self.n)
lstAddSuccess += [newTile]
board += [newTile]
if self.isSolvable(board):
break
else:
board = [int(e) for e in customBoard]
if not self.isSolvable(board):
print("Cant find solution with this puzzle! Exiting...")
exit(-1)
return board
def createGoalBoard(self):
board = []
for count in range(1, self.n + 1):
board += [count]
board += [0]
return board
def printBoard(self, board):
for row in range(0, self.sizeOfBoard, self.k):
# for col in range(row, row + self.k):
print(board[row:row + self.k])
def generateSuccessors(self, currentState):
indexOfZero = currentState.board.index(0)
rowIndexOfZero = indexOfZero % self.k
colIndexOfZero = indexOfZero // self.k
lstSuccessors = []
# Slide to zero to up
if colIndexOfZero != 0:
newState = currentState.board.copy()
newState[indexOfZero] = newState[indexOfZero - self.k]
newState[indexOfZero - self.k] = 0
lstSuccessors.append(
State(currentState, newState, 'up', currentState.depth + 1))
# Slide zero to down
if colIndexOfZero != self.k - 1:
newState = currentState.board.copy()
newState[indexOfZero] = newState[indexOfZero + self.k]
newState[indexOfZero + self.k] = 0
lstSuccessors.append(
State(currentState, newState, 'down', currentState.depth + 1))
# slide zero to left
if rowIndexOfZero != 0:
newState = currentState.board.copy()
newState[indexOfZero] = newState[indexOfZero - 1]
newState[indexOfZero - 1] = 0
lstSuccessors.append(
State(currentState, newState, 'left', currentState.depth + 1))
# Slide zero to right
if rowIndexOfZero != self.k - 1:
newState = currentState.board.copy()
newState[indexOfZero] = newState[indexOfZero + 1]
newState[indexOfZero + 1] = 0
lstSuccessors.append(
State(currentState, newState, 'right', currentState.depth + 1))
lstSuccessorsCost = [ele.cost for ele in lstSuccessors]
lstSuccessorsInOrderOfCost = []
for i in range(0, len(lstSuccessorsCost)):
lstSuccessorsInOrderOfCost.append(lstSuccessors[lstSuccessorsCost.index(min(lstSuccessorsCost))])
lstSuccessorsCost[lstSuccessorsCost.index(min(lstSuccessorsCost))] = 100
return lstSuccessorsInOrderOfCost
def solvePuzzle(self, currentState):
self.stack.append(currentState)
self.stateStorage.add(currentState.map)
while len(self.stack) > 0:
currentState = self.stack.pop()
if currentState.board == self.goalBoard:
# find path
# self.printBoard(currentState.board)
self.finalState = currentState
print("Solving " + str(self.n) + " puzzle done!")
return
start_time_gen = time.time()
lstSuccessor = self.generateSuccessors(currentState)
end_time_gen = time.time()
timeOfGen = end_time_gen - start_time_gen
self.timeOfGenerateSuccessors += timeOfGen
for successor in lstSuccessor[::-1]:
if successor.map not in self.stateStorage:
self.stack.append(successor)
self.stateStorage.add(successor.map)
if successor.depth > self.maxDeepSearch:
self.maxDeepSearch += 1
print("Cant solve puzzle! Exiting...")
exit(-1)
def solve(self):
start_time = time.time()
self.solvePuzzle(self.inititalState)
end_time = time.time()
self.timeOfSolving = end_time - start_time
print("Running time: " + str(self.timeOfSolving))
print("Max Search Dept: " + str(self.maxDeepSearch))
print("Final State Dept: " + str(self.finalState.depth))
def printInitialBoard(self):
self.printBoard(self.inititalState.board)
def printPath(self):
if self.finalState is None:
print("No solution found!")
return
path = []
state = self.finalState
while (state is not None):
if state.previousMove is not None:
path.append(state.previousMove)
state = state.parent
print("path: "),
print(path[::-1])
def main(argv):
# if (len(argv) != 1 or int(argv[0]) not in range(1, 10000)):
# print("Input must be k of integer, which is k*k matrix of puzzle")
# exit()
# eight_puzzle = Puzzle(int(argv[0]))
k = int(input("Enter size of k * k puzzle, k = "))
while k not in range(2, 100):
print("k must be in range 2 - 100")
k = int(input("Enter size of k * k puzzle, k = "))
print("""
Choose:
1. Randome puzzle
2. Custome puzzle
""")
file = input()
if int(file) == 1:
puzzle = Puzzle(k)
elif int(file) == 2:
board = input("Enter puzzle: ")
puzzle = Puzzle(k ,list(board.split(" ")))
puzzle.printInitialBoard()
puzzle.solve()
puzzle.printPath()
if __name__ == "__main__":
main(sys.argv[1:])
Something's wrong with how the winner is determined in my poker game.
I've tried checking that the player score value is reset from the loop
and that all other logic related variables are too
TableCards = []
for Card in Flop:
TableCards.append(Card)
TableCards.append(Turn)
TableCards.append(River)
for Card in Player.Hand:
TableCards.append(Card)
TableCardValues = []
TableCardSuits = []
for Card in TableCards:
TableCardValues.append(Card.Value)
TableCardSuits.append(Card.Suit)
TableCardValues.sort()
TableCardSuits.sort()
ArrayLengthI = len(TableCardValues)
Straight = False
Flush = False
Pairs = []
ThreeOfAKinds = []
FourOfAKinds = []
StraightCount = 0
PlayerHandValues = []
DealerHandValues = []
for Card in Player.Hand:
PlayerHandValues.append(Card.Value)
for Card in Dealer.Hand:
DealerHandValues.append(Card.Value)
for X in range(ArrayLengthI - 1):
if TableCardValues[X + 1] == TableCardValues[X] + 1:
StraightCount += 1
if StraightCount >= 5:
Straight == True
for Suit in TableCardSuits:
if TableCardSuits.count(Suit) >= 5:
Flush = True
for Value in set(TableCardValues):
if TableCardValues.count(Value) == 2:
Pairs.append(Value)
elif TableCardValues.count(Value) == 3:
ThreeOfAKinds.append(Value)
elif TableCardValues.count(Value) == 4:
FourOfAKinds.append(Value)
if Straight == True or Flush == True:
if Straight == True and Flush == True:
Player.HandScore = PokerHands.StraightFlush * max(PlayerHandValues)
elif Straight == True:
Player.HandScore = PokerHands.Straight * max(PlayerHandValues)
elif Flush == True:
Player.HandScore = PokerHands.Flush * max(PlayerHandValues)
elif FourOfAKinds != []:
Player.HandScore = PokerHands.FourOfAKind * max(PlayerHandValues)
elif ThreeOfAKinds != []:
if len(Pairs) >= 1:
Player.HandScore = PokerHands.FullHouse * max(PlayerHandValues)
else:
Player.HandScore = PokerHands.ThreeOfAKind * max(PlayerHandValues)
elif len(Pairs) == 2:
Player.HandScore = PokerHands.TwoPair * max(PlayerHandValues)
elif len(Pairs) == 1:
Player.HandScore = PokerHands.OnePair * max(PlayerHandValues)
else:
Player.HandScore = PokerHands.HighCard * max(PlayerHandValues)
TableCardsDealer = []
TableCardValuesDealer = []
TableCardSuitsDealer = []
for Card in Flop:
TableCardsDealer.append(Card)
TableCardsDealer.append(Turn)
TableCardsDealer.append(River)
DealerStraight = False
DealerFlush = False
DealerStraightCount = 0
DealerPairs = []
DealerThreeOfAKinds = []
DealerFourOfAKinds = []
for Card in Dealer.Hand:
TableCardsDealer.append(Card)
for Card in TableCards:
TableCardValuesDealer.append(Card.Value)
TableCardSuitsDealer.append(Card.Suit)
TableCardSuitsDealer.sort()
TableCardValuesDealer.sort()
TableCardsDealerLength = len(TableCardSuitsDealer)
for X in range(TableCardsDealerLength - 1):
if TableCardValuesDealer[X + 1] == TableCardValuesDealer[X] + 1:
DealerStraightCount += 1
if DealerStraightCount >= 5:
DealerStraight == True
for Suit in TableCardSuitsDealer:
if TableCardSuitsDealer.count(Suit) >= 5:
DealerFlush == True
for Value in set(TableCardValuesDealer):
if TableCardValuesDealer.count(Value) == 2:
DealerPairs.append(Value)
elif TableCardValuesDealer.count(Value) == 3:
DealerThreeOfAKinds.append(Value)
elif TableCardValuesDealer.count(Value) == 4:
DealerFourOfAKinds.append(Value)
if DealerStraight == True or DealerFlush == True:
if DealerStraight == True and DealerFlush == True:
Dealer.HandScore = PokerHands.StraightFlush * max(DealerHandValues)
elif DealerStraight == True:
Dealer.HandScore = PokerHands.Straight * max(DealerHandValues)
elif DealerFlush == True:
Dealer.HandScore = PokerHands.Flush * max(DealerHandValues)
elif DealerFourOfAKinds != []:
Dealer.HandScore = PokerHands.FourOfAKind * max(DealerHandValues)
elif DealerThreeOfAKinds != []:
if len(DealerPairs) >= 1:
Dealer.HandScore = PokerHands.FullHouse * max(DealerHandValues)
else:
Dealer.HandScore = PokerHands.ThreeOfAKind * max(DealerHandValues)
elif len(DealerPairs) == 2:
Dealer.HandScore = PokerHands.TwoPair * max(DealerHandValues)
elif len(DealerPairs) == 1:
Dealer.HandScore = PokerHands.OnePair * max(DealerHandValues)
else:
Dealer.HandScore = PokerHands.HighCard * max(DealerHandValues)
if Player.HandScore > Dealer.HandScore:
print("Well Done, sir. You've won.")
Player.Money += Pot
Pot = 0
elif Player.HandScore < Dealer.HandScore:
print("I Apologise, but you've lost.")
Dealer.Money += Pot
Pot = 0
elif Player.HandScore == Dealer.HandScore:
print("You've tied")
Pot = 0
Junk = input()
Player.HandScore = 0
Dealer.HandScore = 0
system("cls")
There are no error messages, but it seems to determine the winner wrong, and I can't exactly pinpoint the reason why.
I'm trying to create a game called Connect Four with AI which uses the alpha-beta pruning algorithm in python. Here is the code I have managed to write:
# -*- coding: utf-8 -*-
import sys
class ConnectFour:
def __init__(self):
self.board = [[],[],[],[],[],[]]
for i in range(7):
for j in range(6):
self.board[j].append(" ")
self.moves = 0
self.colstack = [0,0,0,0,0,0,0]
self.node = 0
self.move = 0
def PrintGameBoard(self):
print(' 0 1 2 3 4 5 6')
for i in range(5, -1, -1):
print('|---|---|---|---|---|---|---|')
print("| ",end="")
for j in range(7):
print(self.board[i][j],end="")
if j != 6:
print(" | ",end="")
else:
print(" |")
print('`---------------------------ยด')
def CanPlay(self, col):
return self.colstack[col] < 6
def Play(self, col, board):
board[self.colstack[col]][col] = 2
self.colstack[col] += 1
self.moves += 1
return board
def IsWinning(self, currentplayer):
for i in range(6):
for j in range(4):
if self.board[i][j] == currentplayer and self.board[i][j+1] == currentplayer and self.board[i][j+2] == currentplayer and self.board[i][j+3] == currentplayer:
return True
for i in range(3):
for j in range(7):
if self.board[i][j] == currentplayer and self.board[i+1][j] == currentplayer and self.board[i+2][j] == currentplayer and self.board[i+3][j] == currentplayer:
return True
for i in range(3):
for j in range(4):
if self.board[i][j] == currentplayer and self.board[i+1][j+1] == currentplayer and self.board[i+2][j+2] == currentplayer and self.board[i+3][j+3] == currentplayer:
return True
for i in range(3,6):
for j in range(4):
if self.board[i][j] == currentplayer and self.board[i-1][j+1] == currentplayer and self.board[i-2][j+2] == currentplayer and self.board[i-3][j+3] == currentplayer:
return True
return False
def AlphaBeta(self, alpha, beta):
self.node += 1
if self.moves == 42:
return 0
for col in range(7):
if self.CanPlay(col) and self.IsWinning(2):
return (43 - self.moves)/2
max = (41 - self.moves)/2
if beta > max:
beta = max
if alpha >= beta:
return beta
for col in range(7):
if self.CanPlay(col):
self.board[self.colstack[col]][col] = 2
self.move = col
score = -self.AlphaBeta(-alpha, -beta)
if score >= beta:
return score
elif score > alpha:
alpha = score
self.board[self.colstack[col]][col] = " "
def Solve(self, table, week=False):
self.node = 0
self.board = table
if week:
self.AlphaBeta(-1, 1)
self.board = self.Play(self.move, table)
return self.board
else:
self.AlphaBeta(-21, 21)
self.board = self.Play(self.move, table)
return self.board
def PlayerMove(self, table):
self.board = table
try:
allowedmove = False
while not allowedmove:
print("Choose a column where you want to make your move (0-6): ",end="")
col =input()
if self.CanPlay(int(col)):
self.board[self.colstack[int(col)]][int(col)] = 1
self.moves += 1
self.colstack[int(col)] += 1
allowedmove = True
else:
print("This column is full")
except (NameError, ValueError, IndexError, TypeError, SyntaxError) as e:
print("Give a number as an integer between 0-6!")
else:
return self.board
def PlayerMark():
print("Player 1 starts the game")
mark = ''
while not (mark == "1" or mark == "2"):
print('Do you want to be 1 or 2: ',end="")
mark = input()
if mark == "1":
return 1
else:
return 2
def PlayAgain():
print('Do you want to play again? (yes or no) :',end="")
return input().lower().startswith('y')
def main():
sys.setrecursionlimit(2000)
print("Connect4")
while True:
mark = PlayerMark()
connectfour = ConnectFour()
if mark==1:
print("You are going to start the game\r\n\r\n")
else:
print("Computer (negamax) starts the game")
gameisgoing = True
table = [[],[],[],[],[],[]]
for i in range(7):
for j in range(6):
table[j].append(" ")
while gameisgoing:
connectfour.PrintGameBoard()
if mark == 1:
table = connectfour.PlayerMove(table)
if connectfour.IsWinning(1):
connectfour.PrintGameBoard()
print('You won the game!')
gameisgoing = False
else:
if connectfour.moves==42:
connectfour.PrintGameBoard()
print('Game is tie')
break
else:
mark = 2
else:
move = connectfour.Solve(table)
if connectfour.IsWinning(2):
connectfour.PrintGameBoard()
print('Computer won the game')
gameisgoing = False
else:
if connectfour.moves==42:
connectfour.PrintGameBoard()
print('Game is tie')
break
else:
mark = 1
if not PlayAgain():
print("Game ended")
break
if __name__ == '__main__':
main()
I'm not sure if the algorithm is working properly, but the problem is that I get RecursionError: maximum recursion depth exceeded in comparison. If I increase recursionlimit I get in around 10000 MemoryError: Stack Overflow. I think that the problem is that there is too many states to handle for my computer. That's why I changed the negamax algorithm to alpha-beta pruning, but there seem to be still to many states. Is there a effective technique that does algorithmic search, for example max depth 10 and still the computer is almost invincible? I'm waiting your solutions.
I'm trying to solve the Botclean problem on Hackerrank:
https://www.hackerrank.com/challenges/botclean
The solution I came up with will scan the board for the dirty tile with the shortest distance then navigate to it, clean it, and repeat until it's all clean.
Code:
nextD = []
def next_move(posr, posc, board):
global nextD
if nextD == []:
nextD = next_d(posr,posc,board)
if (nextD[0] == posr) and (nextD[1] == posc):
print("CLEAN")
board[posr][posc] = "-"
nextD = []
return
if posc < nextD[1]:
print("RIGHT")
return
#posc += 1
elif posc > nextD[1]:
print("LEFT")
return
#posc -= 1
if posr < nextD[0]:
print("DOWN")
return
#posr += 1
elif posr > nextD[0]:
print("UP")
return
#posr -= 1
#find closest d
def next_d(posr, posc, board):
arr = []
for i in range(len(board)):
try:
#print("Distance to: ", i, board[i].index('d'), abs(i-posc) + abs(board[i].index('d')))
vals = [abs(i-posr) + abs(board[i].index('d')-posc), i, board[i].index('d')]
arr.append(vals)
except ValueError:
pass
arr.sort()
return [arr[0][1], arr[0][2]]
# Tail starts here
if __name__ == "__main__":
pos = [int(i) for i in input().strip().split()]
board = [[j for j in input().strip()] for i in range(5)]
next_move(pos[0], pos[1], board)
I'm stuck at 17.60/17.82. My bot gets 16, 20, 34, 26 on the test cases. The best scores are 16, 25, 28 and 18. The discussion says to implement a greedy algorithm though I'm not entirely sure how in this context. Any suggestions for optimizing this problem? Did I go about it completely wrong?
edit: Time isn't a criteria. So scanning the board repeatedly isn't necessarily a problem.
If you want to watch it in action:
https://www.hackerrank.com/showgame/4843664
Thank you!
Got it
nextD = []
def next_move(posr, posc, board):
global nextD
if nextD == []:
nextD = next_d(posr,posc,board)
if (nextD[0] == posr) and (nextD[1] == posc):
print("CLEAN")
board[posr][posc] = "-"
nextD = []
elif posc < nextD[1]:
print("RIGHT")
elif posc > nextD[1]:
print("LEFT")
elif posr < nextD[0]:
print("DOWN")
elif posr > nextD[0]:
print("UP")
#find closest d
def next_d(posr, posc, board):
val = len(board) * 2
nextD = []
for i in range(len(board)):
for j in range(len(board[i])):
if board[i][j] == 'd' and abs(i - posr) + abs(j - posc) < val:
val = abs(i - posr) + abs(j - posc)
nextD = [i, j]
return nextD
# Tail starts here
if __name__ == "__main__":
pos = [int(i) for i in input().strip().split()]
board = [[j for j in input().strip()] for i in range(5)]
next_move(pos[0], pos[1], board)