To solve this problem, we need to determine the maximum number of souvenirs that can be collected from a grid such that no two collected souvenirs are adjacent in any direction (horizontally, vertically, or diagonally). This is a classic maximum weight independent set problem on a grid graph.
Approach
The problem can be approached using dynamic programming (DP) where we track the state of selected cells in each row. The key insight is to model the problem by considering the constraints between consecutive rows. Specifically, if a cell is selected in a row, the cells in the next row that are adjacent (including diagonally) cannot be selected.
We use a bitmask to represent the selected cells in each row. For each row, we generate all possible valid selections (subsets of columns where no two selected columns are adjacent). We then transition between valid selections of consecutive rows, ensuring that no cells in the current row's selection are adjacent to any cells in the previous row's selection.
Solution Code
python
def main():
import sys
H, W = map(int, sys.stdin.readline().split())
grid = []
for _ in range(H):
grid.append(list(map(int, sys.stdin.readline().split())))
# Precompute all valid masks for a row (no two adjacent cells)
valid_masks = []
for mask in range(1 << W):
valid = True
for i in range(W-1):
if (mask & (1 << i)) and (mask & (1 << (i+1))):
valid = False
break
if valid:
valid_masks.append(mask)
# Precompute the sum for each valid mask
row_sums = []
for i in range(H):
sum_dict = {}
for mask in valid_masks:
total = 0
for j in range(W):
if mask & (1 << j):
total += grid[i][j]
sum_dict[mask] = total
row_sums.append(sum_dict)
# DP: prev_dp is a dictionary {prev_mask: max_sum}
prev_dp = {0: 0}
for i in range(H):
curr_dp = {}
for curr_mask in valid_masks:
curr_sum = row_sums[i][curr_mask]
max_prev = 0
for prev_mask, prev_sum in prev_dp.items():
# Check if curr_mask and prev_mask have adjacent cells
# For each bit set in curr_mask, check if prev_mask has bits in j-1, j, j+1
conflict = False
for j in range(W):
if (curr_mask & (1 << j)) == 0:
continue
# Check prev_mask's j-1, j, j+1
for dj in [-1, 0, 1]:
jj = j + dj
if 0 <= jj < W and (prev_mask & (1 << jj)):
conflict = True
break
if conflict:
break
if not conflict:
if prev_sum > max_prev:
max_prev = prev_sum
total = curr_sum + max_prev
if curr_mask in curr_dp:
if total > curr_dp[curr_mask]:
curr_dp[curr_mask] = total
else:
curr_dp[curr_mask] = total
prev_dp = curr_dp
print(max(prev_dp.values()) if prev_dp else 0)
if __name__ == '__main__':
main()
Explanation
Precompute Valid Masks: For each row, generate all possible valid selections (masks) where no two selected cells are adjacent. This is done using bitmasking.
Row Sums Calculation: For each row and each valid mask, precompute the sum of the selected cells.
Dynamic Programming Transition: Use a DP approach where prev_dp keeps track of the maximum sums achievable for each valid mask of the previous row. For each current row's valid mask, check all valid masks of the previous row to ensure no adjacency conflicts and compute the maximum possible sum.
Result Extraction: The maximum value in the final DP dictionary gives the solution, representing the maximum number of souvenirs that can be collected without any two being adjacent.