To solve this problem optimally, we'll use Dynamic Programming (DP). The goal is to efficiently calculate the sum of the squared costs of all possible paths from the top-left corner to the bottom-right corner of a grid, where each cell contains either 0 or 1. The cost of a path is defined as the square of the number of consecutive 1s (11 pairs) along that path.
Step-by-Step Solution:
-
Understanding the Problem:
- You have an
H × W grid.
- Each cell contains either
0 or 1.
- A path moves only right or down from
(1,1) to (H,W).
- The cost of a path is the square of the number of
11 pairs along the path.
- You need to calculate the sum of the costs of all possible paths.
-
Dynamic Programming Approach:
-
States:
dp_cnt[i][j]: Number of paths from (1,1) to (i,j).
dp_sum[i][j]: Sum of c_p (number of 11 pairs) for all paths to (i,j).
dp_sum_sq[i][j]: Sum of c_p² for all paths to (i,j).
-
Initialization:
- Start at
(1,1):
dp_cnt[1][1] = 1 (only one path starting here).
dp_sum[1][1] = 0 (no pairs yet).
dp_sum_sq[1][1] = 0 (no pairs yet).
-
Transition:
- For each cell
(i,j), compute values based on the cell above (i-1,j) and the cell to the left (i,j-1).
- From the Top
(i-1,j):
- If both
(i-1,j) and (i,j) contain 1, increment the 11 pair count.
- Update
dp_cnt, dp_sum, and dp_sum_sq accordingly.
- From the Left
(i,j-1):
- Similar to the top, check for
11 pairs and update DP tables.
-
Final Answer:
- The desired sum of squared costs is
dp_sum_sq[H][W].
-
Implementation Details:
- Use 2D arrays for DP tables.
- Ensure that all updates are done using 64-bit integers to handle large numbers.
- Iterate through each cell in row-major order, updating the DP tables based on the previous computations.
-
Example for Clarity:
- Consider a
2x2 grid with all 1s:
1 1
1 1
- There are two paths:
- Right → Down:
11 and 11 → cost = (2)^2 = 4
- Down → Right:
11 and 11 → cost = (2)^2 = 4
- Total sum = 4 + 4 = 8, which matches our DP approach.
Final Implementation:
Here's the Python code implementing the described DP approach:
import sys
import sys
import sys
def main():
import sys
import sys
sys.setrecursionlimit(1 << 25)
H, W = map(int, sys.stdin.readline().split())
grid = []
for _ in range(H):
row = sys.stdin.readline().strip()
grid.append([int(c) for c in row.split()])
# Adjust to 1-based indexing
grid = [[0]*(W+1)] + [[0]+row for row in grid]
# Initialize DP tables
dp_cnt = [[0]*(W+1) for _ in range(H+1)]
dp_sum = [[0]*(W+1) for _ in range(H+1)]
dp_sum_sq = [[0]*(W+1) for _ in range(H+1)]
dp_cnt[1][1] = 1
dp_sum[1][1] = 0
dp_sum_sq[1][1] = 0
for i in range(1, H+1):
for j in range(1, W+1):
if i ==1 and j ==1:
continue
# From top
if i >1:
add = 1 if grid[i-1][j] ==1 and grid[i][j]==1 else 0
dp_cnt[i][j] += dp_cnt[i-1][j]
dp_sum[i][j] += dp_sum[i-1][j] + add * dp_cnt[i-1][j]
dp_sum_sq[i][j] += dp_sum_sq[i-1][j] + 2 * add * dp_sum[i-1][j] + add * dp_cnt[i-1][j]
# From left
if j >1:
add = 1 if grid[i][j-1] ==1 and grid[i][j]==1 else 0
dp_cnt[i][j] += dp_cnt[i][j-1]
dp_sum[i][j] += dp_sum[i][j-1] + add * dp_cnt[i][j-1]
dp_sum_sq[i][j] += dp_sum_sq[i][j-1] + 2 * add * dp_sum[i][j-1] + add * dp_cnt[i][j-1]
print(dp_sum_sq[H][W])
if __name__ == "__main__":
main()
Explanation of the Code:
-
Input Handling:
- Read
H (height) and W (width) of the grid.
- Read the grid, converting each cell to an integer (
0 or 1).
- Adjust the grid to 1-based indexing for easier DP transitions.
-
DP Tables Initialization:
dp_cnt: Initialized to 0 for all cells except (1,1) which is 1.
dp_sum and dp_sum_sq: Initialized to 0 for all cells.
-
DP Transition:
- Iterate through each cell
(i,j).
- From the Top
(i-1,j):
- If both
(i-1,j) and (i,j) are 1, increment the add flag.
- Update
dp_cnt, dp_sum, and dp_sum_sq based on the predecessor.
- From the Left
(i,j-1):
- Similar to the top, check for
11 pairs and update DP tables.
-
Output:
- After filling the DP tables, output the value at
dp_sum_sq[H][W], which represents the sum of squared costs of all paths.
Complexity Analysis:
- Time Complexity:
O(H × W) since we iterate through each cell once.
- Space Complexity:
O(H × W) for the DP tables.
This approach ensures that the problem is solved efficiently within the given time and memory constraints.