To solve this problem, we need to determine the number of ways to place K non-attacking kings on an N x N grid. A king attacks all 8 adjacent cells (horizontally, vertically, and diagonally), so we need to ensure no two kings are adjacent in any direction.
Approach
- Valid Masks: Generate all valid masks for a single row. A valid mask is a binary representation where no two 1s (kings) are adjacent (since kings in the same row can't be next to each other).
- Compatibility Check: For two consecutive rows, their masks must be compatible. This means no two kings are in the same column or adjacent columns (to avoid vertical or diagonal attacks).
- Dynamic Programming (DP): Use DP to count the number of ways to place K kings up to each row. The DP state
dp[i][j][mask]represents the number of ways to place j kings in the first i rows with the i-th row having the given mask. We optimize space by using two 2D arrays (previous and current row states).
Solution Code
def get_valid_masks(n):
masks = []
for mask in range(0, 1 << n):
if (mask & (mask << 1)) == 0:
masks.append(mask)
return masks
def main():
import sys
input = sys.stdin.read().split()
n = int(input[0])
k = int(input[1])
valid_masks = get_valid_masks(n)
m = len(valid_masks)
mask_to_idx = {mask: idx for idx, mask in enumerate(valid_masks)}
cnt = [bin(mask).count('1') for mask in valid_masks]
# Initialize DP: prev_dp[j][idx] = ways to get j kings up to previous row with mask valid_masks[idx]
prev_dp = [[0] * m for _ in range(k + 1)]
for idx in range(m):
c = cnt[idx]
if c <= k:
prev_dp[c][idx] = 1
for row in range(2, n + 1):
curr_dp = [[0] * m for _ in range(k + 1)]
for curr_idx in range(m):
curr_mask = valid_masks[curr_idx]
curr_c = cnt[curr_idx]
if curr_c > k:
continue
# Check all previous masks for compatibility
for prev_idx in range(m):
prev_mask = valid_masks[prev_idx]
# Compatibility condition: no overlapping or adjacent in previous row
if (prev_mask & (curr_mask | (curr_mask << 1) | (curr_mask >> 1))) != 0:
continue
# Update current DP state
for j in range(curr_c, k + 1):
curr_dp[j][curr_idx] += prev_dp[j - curr_c][prev_idx]
prev_dp = curr_dp
# Sum all ways to get exactly k kings in the last row
total = sum(prev_dp[k][idx] for idx in range(m))
print(total)
if __name__ == "__main__":
main()
Explanation
- Valid Masks: We generate all masks for a row where no two 1s are adjacent using bitwise operations. For example, for N=3, valid masks are 000, 001, 010, 100, and 101.
- Compatibility Check: For two masks to be compatible, the bitwise AND of the previous mask with the current mask (and its left/right shifts) must be zero. This ensures no vertical or diagonal adjacency.
- DP Transition: For each row, we compute the number of ways to place kings by considering all valid masks and their compatible previous masks. We update the DP state for the current row based on the previous row's state.
- Result: The final result is the sum of all ways to place exactly K kings in the last row, considering all valid masks for that row.
This approach efficiently handles the constraints and provides the correct count of non-attacking king placements. The complexity is manageable due to the small number of valid masks for N up to 10.


作者声明:本文包含人工智能生成内容。