题解 | #走方格的方案数#
走方格的方案数
http://www.nowcoder.com/practice/e2a22f0305eb4f2f9846e7d644dba09b
DFS搜索
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
int m = sc.nextInt();
int count = 0;
dfs(0, 0, n, m, count);
System.out.println(dfs(0, 0, n, m, count));
}
}
private static int dfs(int i, int j, int n, int m, int count) {
if (i == n && j == m) {
return count+1;
}
if (i < n) {
count = dfs(i+1, j, n, m, count);
}
if (j < m) {
count = dfs(i, j+1, n, m, count);
}
return count;
}
}