题解 | #走方格的方案数#
走方格的方案数
https://www.nowcoder.com/practice/e2a22f0305eb4f2f9846e7d644dba09b
首先初始化"边上"的格子,把它们初始化为1
然后开始初始化中间的格子,每个格子的值等于水平方向上左边一格格子的值加上竖直方向上向上一格的值的和
最后dp[n][m]即为所得
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int[][] dp = new int[n + 1][m + 1]; for (int i = 0; i <= n; i++) { dp[i][0] = 1; } for (int i = 0; i <= m; i++) { dp[0][i] = 1; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { dp[i][j] = dp[i][j - 1] + dp[i - 1][j]; } } System.out.println(dp[n][m]); } }