Advertisement
exDotaPro

checkmate

Oct 24th, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.89 KB | None | 0 0
  1. def find_king(matrix):
  2.     for i in range(8):
  3.         for j in range(8):
  4.             if matrix[i][j] == 'K':
  5.                 return i, j
  6.  
  7.  
  8. def is_valid(row, col):
  9.     return row in range(8) and col in range(8)
  10.  
  11.  
  12. def output(matrix):
  13.     king_pos = find_king(matrix)
  14.     directions = [[-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1], [-1, -1]]
  15.     result = []
  16.  
  17.     for i in range(8):
  18.         pos_1, pos_2 = king_pos[0] + directions[i][0], king_pos[1] + directions[i][1]
  19.         while is_valid(pos_1, pos_2):
  20.             if matrix[pos_1][pos_2] == 'Q':
  21.                 result.append([pos_1, pos_2])
  22.                 break
  23.             pos_1 += directions[i][0]
  24.             pos_2 += directions[i][1]
  25.  
  26.     if result:
  27.         return '\n'.join([str(x) for x in result])
  28.     return 'The king is safe!'
  29.  
  30.  
  31. board = [[x for x in input().split()] for _ in range(8)]
  32. print(output(board))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement