βœ…Knight game(Contest)

Knight game easy Time Limit: 2 sec Memory Limit: 128000 kB

Problem Statement :

You are given a 100*100 chessboard. A knight is placed on cell (i, j). You have to find the number of blocks on the chessboard that the knight can be at in exactly N moves. Input Input contains three integers i j N.

Constraints

1 <= i, j <= 100 1 <= N <= 10 Output Output a single integer, the number of blocks on the chessboard that the knight can be at in exactly N moves. Example sample input 1 1 1 1

sample output 1 2

sample input 2 50 50 1

sample input 2 8

link:https://my.newtonschool.co/playground/code/3rnba6jbh2x0/

# Your code here
def get_next_position(pos):
    moves = [(2,1),(2,-1),(-2,1),(-2,-1),(1,2),(-1,2),(1,-2),(-1,-2)]
    i,j = pos
    position = set()
    for(x,y)in moves:
      pos_x = i+x
      pos_y = j+y
      if(1<=pos_x)and (pos_x<=100) and (1<=pos_y) and (pos_y<=100):
             position.add((pos_x,pos_y))
    return position


i,j,n = [int(x)for x in input().split()]
curr_positions=set([(i,j)])
for i in range(n):
         new_position=set()
         for pos in curr_positions:
             new_position|=get_next_position(pos)
         curr_positions=new_position
print(len(curr_positions))

Last updated