地上畫着一些格子,每個格子裏寫一個字,如下所示:(也可參見p1.jpg)
從我做起振 1 2 3 4 5
我做起振興 2 3 4 5 6
做起振興中 3 4 5 6 7
起振興中華 4 5 6 7 8
比賽時,先站在左上角的寫着“從”字的格子裏,可以橫向或縱向跳到相鄰
的格子裏,但不能跳到對角的格子或其它位置。一直要跳到“華”字結束。
要求跳過的路線剛好構成“從我做起振興中華”這句話。
請你幫助小明算一算他一共有多少種可能的跳躍路線呢?
答案是一個整數,請通過瀏覽器直接提交該數字。
注意:不要提交解答過程,或其它輔助說明類的內容。
法一:dfs
#include <cstdio>
#include <algorithm>
using namespace std;
int tx[4]={0,1};
int ty[4]={1,0};
int ans=0;
int mp[4][5]={{1,2,3,4,5},{2,3,4,5,6},{3,4,5,6,7},{4,5,6,7,8}};
bool vis[4][5];
void dfs(int x,int y,int t) {
if (x>=4 || y>=5) return;
if (x==3 && y==4 && t==8) {
ans++;
return;
}
for (int i=0;i<2;i++) {
int xx=x+tx[i];
int yy=y+ty[i];
if (!vis[xx][yy] && mp[xx][yy]==t+1) {
vis[xx][yy]=true;
dfs(xx,yy,t+1);
vis[xx][yy]=false;
}
}
return;
}
int main()
{
vis[0][0]=true;
dfs(0,0,1);
printf ("%d\n",ans);
return 0;
}
法二:數學
首先到達終點必須要走七步,在這七步裏一定有四步是橫着走,三步豎着走,順序隨。從七步裏任選3步橫向走,因此有c(7,3)種可能