振兴中华

来源:互联网 发布:centos boot repair 编辑:程序博客网 时间:2024/06/18 03:54

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

地上画着一些格子,每个格子里写一个字,如下所示:


从 我 做 起 振 

我 做 起 振 兴 

做 起 振 兴 中

起 振 兴 中 华


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

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

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

public class ZhenXing {private  static char [] [] a={{'从','我','做','起','振'},                      {'我','做','起','振','兴'},                      {'做','起','振','兴','中'},                      {'起','振','兴','中','华'}};private static int count;  //计数器public static void main(String[] args) { count=0; char [] b=new char[8]; judge(0, 0, 0, b); System.out.println(count);}/*     模拟小明跳跃格子的递归方法 *     step 跳格子经过的步数,范围是0-7 *     x 向下跳的位置,范围是0-3  *     y 向右跳的位置,范围是0-4 *     b 暂存跳过路线的一维数组  */private static void judge(int step,int x,int y,char [] b){if(x>3){return;}if(y>4){return;}if(step>7){return;}b[step]=a[x][y];  //记录跳过的字if(step==7){              //刚好走到第八个位置if(check(b)){count++;}return;}//有两种情况,向下跳或者向右跳judge(step+1, x+1, y, b);  judge(step+1, x, y+1, b);  }//检查数组内容是“从我做起振兴中华”private static boolean check(char[] b){ if(String.valueOf(b).equals("从我做起振兴中华")){  return true;  }else{  return false; }}}




原创粉丝点击