小明参加了学校的趣味运动会,其中的一个项目是:跳格子。
地上画着一些格子,每个格子里写一个字,如下所示:(也可参见p1.jpg)
从我做起振
我做起振兴
做起振兴中
起振兴中华
比赛时,先站在左上角的写着“从”字的格子里,可以横向或纵向跳到相邻的格子里,但不能跳到对角的格子或其它位置。一直要跳到“华”字结束。
要求跳过的路线刚好构成“从我做起振兴中华”这句话。
请你帮助小明算一算他一共有多少种可能的跳跃路线呢?
答案是一个整数,请通过浏览器直接提交该数字。
注意:不要提交解答过程,或其它辅助说明类的内容。
答案: 35
代码:
#include<iostream>
#include<algorithm>
using namespace std;
int cnt=0;
string a[10][10]=
{
{"从","我","做","起","振" },
{"我","做","起","振","兴" },
{"做","起","振","兴","中" },
{"起","振","兴","中","华" },
};
string val[10];
int check(string val[])
{
if(val[0]=="从"&&val[1]=="我"&&val[2]=="做"&&val[3]=="起"&&val[4]=="振"&&val[5]=="兴"&&val[6]=="中"&&val[7]=="华")
return 1;
return 0;
}
void dfs(int x,int y,int step)
{
if(x>3||y>4||step>7) return;
val[step]=a[x][y];
if(step==7)
{
if(check(val))
{
cnt++;
}
return;
}
dfs(x,y+1,step+1);
dfs(x+1,y,step+1);
}
int main()
{
dfs(0,0,0);
cout<<cnt<<endl;
return 0;
}