Yung Venuz
Yung Venuz

Reputation: 1

Finding an element in the matrix with a least amount of steps

I have a W x H matrix, random starting position x0,y0, and the target with unknown coordinates. On every step I get information of direction to the target (above-below, left-right) and then have to move to the next position. The goal is to move to the target in the least amount of steps. Currently I'm doing it by having two lists: possible X and possible Y of the target, and removing any impossible coordinates (according to the direction I got) every step, then going to the middle of what is left. Is there a way to do it in less steps?

w, h = [int(i) for i in input().split()]
n = int(input())  # maximum number of turns before game over.
x0, y0 = [int(i) for i in input().split()]
currentx = x0
currenty = y0
xes = [i for i in range(w)]
yes = [i for i in range(h)]
while True:
    bomb_dir = input() #(U, UR, R, DR, D, DL, L or UL)
    for x in xes:
        if 'R' in bomb_dir and x <= currentx: del xes[:xes.index(x)]
        if 'L' in bomb_dir and x >= currentx: del xes[xes.index(x):]
    for y in yes:
        if 'U' in bomb_dir and y >= currenty: del yes[yes.index(y):]
        if 'D' in bomb_dir and y <= currenty: del yes[:yes.index(y)]
    currentx = xes[round(len(xes) / 2)]
    currenty = yes[round(len(yes) / 2)]
    print(currentx, currenty)

Upvotes: 0

Views: 28

Answers (0)

Related Questions