NeFut Logo NeFut
Admin Login

State Space Design and Geometric Mapping in Dynamic Programming

Published at: 2026-05-27 09:17 Last updated: 2026-06-06 13:04
#C++ #Tutorial

Core Logic and Mathematical Principles

The state space is the collection of all possible states, representing a complete discretization and abstraction of the phased characteristics of real-world problems. The essence of dynamic programming is to find the longest or shortest path on a directed acyclic graph (DAG) formed by the state space.

When considering states as grid points in a high-dimensional space, the transition equations represent the geometric vector mappings between these grid points:

  1. Dimensions and Axes: The dimensions of the state array correspond to the number of control variables. For example, $f[i][j]$ represents a two-dimensional state space, where the horizontal and vertical axes represent "the first $i$ phases" and "the constraint $j$" respectively.
  2. Geometric Topology of the DAG: Any valid state transition equation geometrically manifests as a directed edge moving from lower-dimensional/lower-index grid points to higher-dimensional/higher-index grid points. Due to the no-backtracking constraint, these directed edges never form cycles in space.
  3. Spatial Step Size and Locality: The index changes in the transition equations (such as $i-1, j-v$) define the step size of movement within the state space grid. Efficient DP algorithms often exhibit good spatial locality, which is the geometric basis for rolling arrays to compress spatial dimensions.

State Design and Algorithm Derivation

Under the geometric mapping of the DAG, we need to translate complex boundaries and constraints into control at grid points on the axes. Taking the "path counting on a grid with obstacles" extended model as an example:

1. State Space Design

Let state $f[i][j]$ represent the number of valid path plans from the starting point $(1, 1)$ to the coordinate $(i, j)$ along the grid lines. If the current coordinate $(i, j)$ is an obstacle, then that grid point is invalid in the state space, and its value is forcibly set to zero.

2. Transition Equations and Geometric Mapping

Since each step can only move right or down, this means that the predecessor grid points of the target grid point $(i, j)$ can only be the left $(i, j-1)$ and the top $(i-1, j)$. This geometrically manifests as two directed edges converging at $(i, j)$:

$$f[i][j] = \begin{cases} 0 & \text{if } \text{grid}[i][j] \text{ is obstacle} \\ f[i-1][j] + f[i][j-1] & \text{otherwise} \end{cases}$$

3. Boundaries and Topological Order


C++ Standard Source Code (NOIP Style)

Below is the C++ source code for counting paths in a two-dimensional space based on DAG geometric mapping, including obstacle handling, strictly compatible with NOIP Linux (g++ -O2) environment.

#include <iostream>
#include <vector>

using namespace std;

const int MAXN = 1005;
const int MOD = 1000000007; // Prevent overflow, modulo as required by the problem

int grid[MAXN][MAXN];
int f[MAXN][MAXN];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n, m;
    if (!(cin >> n >> m)) return 0;

    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            cin >> grid[i][j]; // 1 represents empty space, 0 represents obstacle
        }
    }

    // Boundary condition initialization
    if (grid[1][1] == 1) {
        f[1][1] = 1;
    }

    // Strictly calculate the state space in topological order (double-increasing row and column loops)
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            if (i == 1 && j == 1) continue; // Skip the starting point

            if (grid[i][j] == 0) {
                f[i][j] = 0; // Important: The number of paths at obstacle points must be forcibly set to zero, do not overlook
                continue;
            }

            // State transition: relies on the geometric states above and to the left
            f[i][j] = (f[i - 1][j] + f[i][j - 1]) % MOD;
        }
    }

    cout << f[n][m] << "\n";

    return 0;
}

NOIP Practical Pitfall Guide

1. Ignoring Explicit Zeroing of Obstacle States

2. Topological Order of State Space and Reversed Loop Order


Classic NOIP/Luogu Problems

1. Luogu P1002 [NOIP2002 Popular Group] River Crossing Soldier

2. Luogu P1508 Likecloud - Eating Candy

Original Source: local://13.2

[h] Back to Home