Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- size = 8
- matrix = []
- queens_output = []
- king_row, king_col = None, None
- for row in range(size):
- matrix.append(input().split())
- for col in range(size):
- if matrix[row][col] == 'K':
- king_row, king_col = row, col
- break
- step = 0
- while True: # checking same row - right
- if matrix[king_row][king_col+step] == 'Q':
- queens_output.append([king_row, king_col+step])
- break
- step += 1
- if king_col + step >= size:
- break
- step = 0
- while True: # checking same row - left
- if matrix[king_row][king_col+step] == 'Q':
- queens_output.append([king_row, king_col+step])
- break
- step -= 1
- if king_col + step < 0:
- break
- step = 0
- while True: # checking same col, down
- if matrix[king_row + step][king_col] == 'Q':
- queens_output.append([king_row + step, king_col])
- break
- step += 1
- if king_row + step >= size:
- break
- step = 0
- while True: # checking same col, up
- if matrix[king_row + step][king_col] == 'Q':
- queens_output.append([king_row + step, king_col])
- break
- step -= 1
- if king_row + step < 0:
- break
- step = 0
- while True: # checking diagonal up-left
- if matrix[king_row + step][king_col + step] == 'Q':
- queens_output.append([king_row + step, king_col + step])
- break
- step -= 1
- if king_col + step < 0 or king_row + step < 0:
- break
- step = 0
- while True: # checking diagonal up-right
- if matrix[king_row + step][king_col - step] == 'Q':
- queens_output.append([king_row + step, king_col - step])
- break
- step -= 1
- if king_col - step >= size or king_row + step < 0:
- break
- step = 0
- while True: # checking diagonal down-right
- if matrix[king_row + step][king_col + step] == 'Q':
- queens_output.append([king_row + step, king_col + step])
- break
- step += 1
- if king_col + step >= size or king_row + step >= size:
- break
- step = 0
- while True: # checking diagonal down-left
- if matrix[king_row + step][king_col - step] == 'Q':
- queens_output.append([king_row + step, king_col - step])
- break
- step += 1
- if king_col - step < 0 or king_row + step >= size:
- break
- if queens_output:
- print(*queens_output, sep='\n')
- else:
- print("The king is safe!")
Advertisement
Add Comment
Please, Sign In to add comment