Advertisement
viligen

knights_game

Jan 25th, 2022
878
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.29 KB | None | 0 0
  1. def next_moves(k_row, k_col, size):
  2.     next_possible_moves = []
  3.     valid_range = range(0, size)
  4.     if k_row - 1 in valid_range and k_col - 2 in valid_range:
  5.         next_possible_moves.append((k_row - 1, k_col - 2))
  6.  
  7.     if k_row - 2 in valid_range and k_col - 1 in valid_range:
  8.         next_possible_moves.append((k_row - 2, k_col - 1))
  9.  
  10.     if k_row - 2 in valid_range and k_col + 1 in valid_range:
  11.         next_possible_moves.append((k_row - 2, k_col + 1))
  12.  
  13.     if k_row - 1 in valid_range and k_col + 2 in valid_range:
  14.         next_possible_moves.append((k_row - 1, k_col + 2))
  15.  
  16.     if k_row + 1 in valid_range and k_col - 2 in valid_range:
  17.         next_possible_moves.append((k_row + 1, k_col - 2))
  18.  
  19.     if k_row + 2 in valid_range and k_col - 1 in valid_range:
  20.         next_possible_moves.append((k_row + 2, k_col - 1))
  21.  
  22.     if k_row + 2 in valid_range and k_col + 1 in valid_range:
  23.         next_possible_moves.append((k_row + 2, k_col + 1))
  24.  
  25.     if k_row + 1 in valid_range and k_col + 2 in valid_range:
  26.         next_possible_moves.append((k_row + 1, k_col + 2))
  27.  
  28.     return next_possible_moves
  29.  
  30.  
  31. rows = int(input())
  32.  
  33. knights_coordinates = []
  34. removed_knights = 0
  35. max_knight = {}
  36.  
  37. matrix = []
  38. for row in range(rows):
  39.     current_row = list(input())
  40.     matrix.append(current_row)
  41.     for col in range(len(current_row)):
  42.         if matrix[row][col] == "K":
  43.             knights_coordinates.append((row, col))
  44.  
  45. while True:
  46.     max_knight = {}
  47.     max_knight_hits = 0
  48.     max_knight_row, max_knight_col = None, None
  49.  
  50.     for knight_row, knight_col in knights_coordinates:
  51.         hit_knights = 0
  52.  
  53.         next_possible_move = next_moves(knight_row, knight_col, rows)
  54.         for next_row, next_col in next_possible_move:
  55.             if matrix[next_row][next_col] == "K":
  56.                 hit_knights += 1
  57.         if hit_knights:
  58.             max_knight[(knight_row, knight_col)] = hit_knights
  59.             if hit_knights > max_knight_hits:
  60.                 max_knight_hits = hit_knights
  61.                 max_knight_row, max_knight_col = knight_row, knight_col
  62.     if max_knight:
  63.         knights_coordinates.remove((max_knight_row, max_knight_col))
  64.         removed_knights += 1
  65.         matrix[max_knight_row][max_knight_col] = "0"
  66.     else:
  67.         break
  68.  
  69. print(removed_knights)
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement