第四届蓝桥杯JAVA B组省赛真题-振兴中华

标题: 振兴中华

小明参加了学校的趣味运动会,其中的一个项目是:跳格子。

地上画着一些格子,每个格子里写一个字,如下所示:(也可参见p1.jpg)

从我做起振
我做起振兴
做起振兴中
起振兴中华

比赛时,先站在左上角的写着“从”字的格子里,可以横向或纵向跳到相邻的格子里,但不能跳到对角的格子或其它位置。一直要跳到“华”字结束。

要求跳过的路线刚好构成“从我做起振兴中华”这句话。

请你帮助小明算一算他一共有多少种可能的跳跃路线呢?

答案是一个整数,请通过浏览器直接提交该数字。
注意:不要提交解答过程,或其它辅助说明类的内容。
根据题意可知,这道题考察的就是递归方法,就是将这里面的所有可能情况进行遍历一遍,只要到达二维数组的末尾进行判断是否满足该条件,只要满足就计数+1,不满足继续遍历直到完成为止

package com.lanqiao.four.Test;

public class Demo03 {
    public static int temp=0;
    public static void main(String[] args) {
        String[][] str= {{"从","我","做","起","振"},
                         {"我","做","起","振","兴"},
                         {"做","起","振","兴","中"},
                         {"起","振","兴","中","华"}};
        digui(str,0,0,"从");
        System.out.println(temp);
    }
    public static void digui(String[][] str, int i, int j, String result) {
        // TODO Auto-generated method stub
        if(i==3&&j==4)
        {
            if(result.equals("从我做起振兴中华"))
            {
                temp++;
            }

        }else if(i>4||j>4) {

        }else {
            if(i+1<str.length)
            {
                digui(str,i+1,j,result+str[i+1][j]);
            }
            if(j+1<str[i].length)
            {
                digui(str,i,j+1,result+str[i][j+1]);
            }
        }
    }
}

结果是:35

猜你喜欢

转载自blog.csdn.net/technology_liu/article/details/79734442