标题: 振兴中华
小明参加了学校的趣味运动会,其中的一个项目是:跳格子。
地上画着一些格子,每个格子里写一个字,如下所示:(也可参见p1.jpg)
从我做起振
我做起振兴
做起振兴中
起振兴中华
比赛时,先站在左上角的写着“从”字的格子里,可以横向或纵向跳到相邻的格子里,但不能跳到对角的格子或其它位置。一直要跳到“华”字结束。
要求跳过的路线刚好构成“从我做起振兴中华”这句话。
请你帮助小明算一算他一共有多少种可能的跳跃路线呢?
答案是一个整数,请通过浏览器直接提交该数字。
注意:不要提交解答过程,或其它辅助说明类的内容。
同样的dfs,将文字“从我做起振兴中华”改为12345678,只要能够走完全的1-8即可记为一条可行路线
-
public class Main {
-
-
static int dot[][] = new int[7][7];
-
static int routine = 0;
-
static char road[] = new char[10];
-
-
public static void dfs(int x, int y, int n) {
-
if (n == 8) {
-
routine++;
-
-
-
-
-
} else {
-
if (dot[x][y + 1] == n + 1) {
-
road[n] = ‘→‘;
-
dfs(x, y + 1, n + 1);
-
}
-
if (dot[x + 1][y] == n + 1) {
-
road[n] = ‘↓‘;
-
dfs(x + 1, y, n + 1);
-
}
-
}
-
}
-
-
public static void main(String[] args) {
-
for (int i = 1; i <= 4; i++) {
-
for (int j = 1; j <= 5; j++) {
-
dot[i][j] = j + i - 1;
-
}
-
}
-
dfs(1, 1, 1);
-
System.out.println(routine);
-
}
-
}
一开始看成5x5的方格了,输出了70,改为4x5的方格后输出正确答案是35[JAVA][2013蓝桥杯预赛 JAVA本科B组][振兴中华]
原文:http://blog.csdn.net/szhielelp/article/details/41511435