Advertisement
GeorgiLukanov87

03. Knight Game - Multidimensional Lists - Exercise 2

Sep 3rd, 2022 (edited)
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.84 KB | None | 0 0
  1. # 03. Knight Game
  2.  
  3. # Multidimensional Lists - Exercise 2
  4. # https://judge.softuni.org/Contests/Practice/Index/3194#2
  5.  
  6.  
  7. def is_inside_func(current_row, current_col, default_M_size):
  8.     return 0 <= current_row < default_M_size and 0 <= current_col < default_M_size
  9.  
  10.  
  11. size = int(input())
  12. M = []
  13. for _ in range(size):
  14.     M.append(list(input()))
  15.  
  16. removed_knights = 0
  17.  
  18. while True:
  19.     table = {}
  20.  
  21.     for row in range(len(M)):
  22.         for col in range(len(M)):
  23.             if M[row][col] == '0':
  24.                 continue
  25.  
  26.             if is_inside_func(row - 2, col + 1, size) and M[row - 2][col + 1] == 'K':  # DIAGONAL TOP RIGHT #1
  27.                 if (row, col) not in table:
  28.                     table[(row, col)] = 0
  29.                 table[(row, col)] += 1
  30.  
  31.             if is_inside_func(row - 1, col + 2, size) and M[row - 1][col + 2] == 'K':  # DIAGONAL TOP RIGHT #2
  32.                 if (row, col) not in table:
  33.                     table[(row, col)] = 0
  34.                 table[(row, col)] += 1
  35.  
  36.             if is_inside_func(row + 1, col + 2, size) and M[row + 1][col + 2] == 'K':  # DIAGONAL BOTTOM RIGHT #1
  37.                 if (row, col) not in table:
  38.                     table[(row, col)] = 0
  39.                 table[(row, col)] += 1
  40.  
  41.             if is_inside_func(row + 2, col + 1, size) and M[row + 2][col + 1] == 'K':  # DIAGONAL BOTTOM RIGHT #2
  42.                 if (row, col) not in table:
  43.                     table[(row, col)] = 0
  44.                 table[(row, col)] += 1
  45.  
  46.             if is_inside_func(row + 2, col - 1, size) and M[row + 2][col - 1] == 'K':  # DIAGONAL BOTTOM LEFT #1
  47.                 if (row, col) not in table:
  48.                     table[(row, col)] = 0
  49.                 table[(row, col)] += 1
  50.  
  51.             if is_inside_func(row + 1, col - 2, size) and M[row + 1][col - 2] == 'K':  # DIAGONAL BOTTOM LEFT #2
  52.                 if (row, col) not in table:
  53.                     table[(row, col)] = 0
  54.                 table[(row, col)] += 1
  55.  
  56.             if is_inside_func(row - 1, col - 2, size) and M[row - 1][col - 2] == 'K':  # DIAGONAL TOP LEFT #1
  57.                 if (row, col) not in table:
  58.                     table[(row, col)] = 0
  59.                 table[(row, col)] += 1
  60.  
  61.             if is_inside_func(row - 2, col - 1, size) and M[row - 2][col - 1] == 'K':  # DIAGONAL TOP LEFT #2
  62.                 if (row, col) not in table:
  63.                     table[(row, col)] = 0
  64.                 table[(row, col)] += 1
  65.  
  66.     if len(table) == 0:
  67.         break
  68.  
  69.     most_attacks = 0
  70.     knight_row, knight_col = 0, 0
  71.     for position, attacks in table.items():
  72.         if attacks > most_attacks:
  73.             most_attacks = attacks
  74.             knight_row = position[0]
  75.             knight_col = position[1]
  76.  
  77.     M[knight_row][knight_col] = '0'  # Remove the most danger Knight
  78.     removed_knights += 1
  79.  
  80. print(removed_knights)
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement