viligen

checkmate_2

Feb 16th, 2022
728
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.96 KB | None | 0 0
  1. def queen_left(k_row, k_col, field_, step=1):
  2.     current_row, current_col = k_row, k_col
  3.     while current_col > 0:
  4.         current_col -= step
  5.         if field_[current_row][current_col] == 'Q':
  6.             return current_row, current_col
  7.     return False
  8.  
  9.  
  10. def queen_right(k_row, k_col, field_, step=1):
  11.     current_row, current_col = k_row, k_col
  12.     while current_col < size - 1:
  13.         current_col += step
  14.         if field_[current_row][current_col] == 'Q':
  15.             return current_row, current_col
  16.     return False
  17.  
  18.  
  19. def queen_up(k_row, k_col, field_, step=1):
  20.     current_row, current_col = k_row, k_col
  21.     while current_row > 0:
  22.         current_row -= step
  23.         if field_[current_row][current_col] == 'Q':
  24.             return current_row, current_col
  25.     return False
  26.  
  27.  
  28. def queen_down(k_row, k_col, field_, step=1):
  29.     current_row, current_col = k_row, k_col
  30.     while current_row < size - 1:
  31.         current_row += step
  32.         if field_[current_row][current_col] == 'Q':
  33.             return current_row, current_col
  34.     return False
  35.  
  36.  
  37. def queen_up_right(k_row, k_col, field_, step=1):
  38.     current_row, current_col = k_row, k_col
  39.     while current_row > 0 and current_col < size - 1:
  40.         current_row -= step
  41.         current_col += step
  42.         if field_[current_row][current_col] == 'Q':
  43.             return current_row, current_col
  44.     return False
  45.  
  46.  
  47. def queen_up_left(k_row, k_col, field_, step=1):
  48.     current_row, current_col = k_row, k_col
  49.     while current_row > 0 and current_col > 0:
  50.         current_row -= step
  51.         current_col -= step
  52.         if field_[current_row][current_col] == 'Q':
  53.             return current_row, current_col
  54.     return False
  55.  
  56.  
  57. def queen_down_right(k_row, k_col, field_, step=1):
  58.     current_row, current_col = k_row, k_col
  59.     while current_row < size - 1 and current_col < size - 1:
  60.         current_row += step
  61.         current_col += step
  62.         if field_[current_row][current_col] == 'Q':
  63.             return current_row, current_col
  64.     return False
  65.  
  66.  
  67. def queen_down_left(k_row, k_col, field_, step=1):
  68.     current_row, current_col = k_row, k_col
  69.     while current_row < size - 1 and current_col > 0:
  70.         current_row += step
  71.         current_col -= step
  72.         if field_[current_row][current_col] == 'Q':
  73.             return current_row, current_col
  74.     return False
  75.  
  76.  
  77. size = 8
  78. king_row, king_col = None, None
  79. field = []
  80. queens = []
  81. for row in range(size):
  82.     field.append(input().split())
  83.     if 'K' in field[row]:
  84.         king_row, king_col = row, field[row].index('K')
  85.  
  86. functions_reference_list = [queen_left, queen_right, queen_up, queen_down, queen_down_left, queen_down_right,
  87.                             queen_up_left, queen_up_right]
  88.  
  89. for function in functions_reference_list:
  90.     if function(king_row, king_col, field):
  91.         queens.append(list(function(king_row, king_col, field)))
  92.  
  93. if not queens:
  94.     print('The king is safe!')
  95. else:
  96.     print(*queens, sep='\n')
  97.  
Advertisement
Add Comment
Please, Sign In to add comment