Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check if a king can move a valid move or not when N nights are there in a modified chessboard in Python
Suppose we have an infinite chessboard with the same rules as chess. Given N knights and a king's position, we need to check whether the king is in checkmate or not. The coordinate system is bounded by large values like (-10^9 <= x, y <= 10^9).
So, if the input is like knights position = [[2,1],[1,3],[3,6],[5,5],[6,1],[7,3]] and king position: [4,3], then the output will be True, as the king has no valid move and is in checkmate.
Algorithm
To solve this problem, we follow these steps −
- Create a dictionary to mark all squares attacked by knights
- For each knight position, mark all 8 possible knight attack squares
- Also mark the knight's current position as occupied
- Check all 8 possible king moves (adjacent squares)
- If any adjacent square is not attacked or occupied, king can escape
- If all adjacent squares are attacked or occupied, it's checkmate
Knight Attack Pattern
A knight attacks 8 squares in an L-shaped pattern. From position (x, y), a knight can attack ?
def get_knight_attacks(x, y):
"""Returns all squares attacked by a knight at position (x, y)"""
attacks = [
(x - 2, y + 1), (x - 2, y - 1), # Left L-moves
(x + 2, y + 1), (x + 2, y - 1), # Right L-moves
(x + 1, y + 2), (x + 1, y - 2), # Up L-moves
(x - 1, y + 2), (x - 1, y - 2) # Down L-moves
]
return attacks
# Example: Knight at (3, 3)
knight_pos = (3, 3)
attacks = get_knight_attacks(3, 3)
print("Knight at", knight_pos, "attacks:")
for attack in attacks:
print(attack)
Knight at (3, 3) attacks: (1, 4) (1, 2) (5, 4) (5, 2) (4, 5) (4, 1) (2, 5) (2, 1)
Complete Solution
def is_checkmate(knights, king_pos):
"""Check if king is in checkmate given knight positions"""
attacked_squares = {}
# Mark all squares attacked by knights
for knight in knights:
x, y = knight[0], knight[1]
# Mark knight's position as occupied
attacked_squares[(x, y)] = 1
# Mark all squares this knight attacks
knight_attacks = [
(x - 2, y + 1), (x - 2, y - 1),
(x + 2, y + 1), (x + 2, y - 1),
(x + 1, y + 2), (x + 1, y - 2),
(x - 1, y + 2), (x - 1, y - 2)
]
for attack_x, attack_y in knight_attacks:
attacked_squares[(attack_x, attack_y)] = 1
# Check all 8 possible king moves
king_x, king_y = king_pos[0], king_pos[1]
for dx in [-1, 0, 1]:
for dy in [-1, 0, 1]:
if dx == 0 and dy == 0: # Skip king's current position
continue
new_x = king_x + dx
new_y = king_y + dy
# If this square is not attacked, king can escape
if (new_x, new_y) not in attacked_squares:
return False
# All adjacent squares are attacked - checkmate!
return True
# Test with the given example
knights = [[2,1],[1,3],[3,6],[5,5],[6,1],[7,3]]
king_position = [4, 3]
result = is_checkmate(knights, king_position)
print(f"Knights: {knights}")
print(f"King position: {king_position}")
print(f"Is checkmate? {result}")
Knights: [[2, 1], [1, 3], [3, 6], [5, 5], [6, 1], [7, 3]] King position: [4, 3] Is checkmate? True
How It Works
The algorithm works by:
- Mapping attacks: For each knight, we calculate all 8 squares it can attack
- Marking occupancy: Knight positions themselves are also marked as unavailable
- Testing escapes: We check all 8 adjacent squares around the king
- Determining checkmate: If no adjacent square is safe, the king is in checkmate
Test with Different Scenarios
# Test case 1: King can escape
knights1 = [[1, 1], [3, 3]]
king1 = [2, 2]
print(f"Test 1 - Checkmate: {is_checkmate(knights1, king1)}")
# Test case 2: No knights (king is safe)
knights2 = []
king2 = [0, 0]
print(f"Test 2 - Checkmate: {is_checkmate(knights2, king2)}")
# Test case 3: Original example
knights3 = [[2,1],[1,3],[3,6],[5,5],[6,1],[7,3]]
king3 = [4, 3]
print(f"Test 3 - Checkmate: {is_checkmate(knights3, king3)}")
Test 1 - Checkmate: False Test 2 - Checkmate: False Test 3 - Checkmate: True
Conclusion
This solution efficiently determines if a king is in checkmate by mapping all knight attack patterns and checking if the king has any valid escape moves. The algorithm has O(N) time complexity where N is the number of knights.
