python - Optimizing a robot vacuum's path -
python - Optimizing a robot vacuum's path -
i'm trying solve botclean problem on hackerrank: https://www.hackerrank.com/challenges/botclean
the solution came scan board dirty tile shortest distance navigate it, clean it, , repeat until it's clean.
code:
nextd = [] def next_move(posr, posc, board): global nextd if nextd == []: nextd = next_d(posr,posc,board) if (nextd[0] == posr) , (nextd[1] == posc): print("clean") board[posr][posc] = "-" nextd = [] homecoming if posc < nextd[1]: print("right") homecoming #posc += 1 elif posc > nextd[1]: print("left") homecoming #posc -= 1 if posr < nextd[0]: print("down") homecoming #posr += 1 elif posr > nextd[0]: print("up") homecoming #posr -= 1 #find closest d def next_d(posr, posc, board): arr = [] 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() homecoming [arr[0][1], arr[0][2]] # tail starts here if __name__ == "__main__": pos = [int(i) in input().strip().split()] board = [[j j in input().strip()] in range(5)] next_move(pos[0], pos[1], board)
i'm stuck @ 17.60/17.82. bot gets 16, 20, 34, 26 on test cases. best scores 16, 25, 28 , 18. give-and-take says implement greedy algorithm though i'm not exclusively sure how in context. suggestions optimizing problem? did go wrong?
edit: time isn't criteria. scanning board repeatedly isn't problem.
if want watch 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) , (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 = [] in range(len(board)): j in range(len(board[i])): if board[i][j] == 'd' , abs(i - posr) + abs(j - posc) < val: val = abs(i - posr) + abs(j - posc) nextd = [i, j] homecoming nextd # tail starts here if __name__ == "__main__": pos = [int(i) in input().strip().split()] board = [[j j in input().strip()] in range(5)] next_move(pos[0], pos[1], board)
python algorithm artificial-intelligence bots
Comments
Post a Comment