標題: 振興中華
小明參加了學校的趣味運動會,其中的一個項目是:跳格子。
地上畫著一些格子,每個格子里寫一個字,如下所示:(也可參見p1.jpg) 從我做起振 我做起振興 做起振興中 起振興中華
比賽時,先站在左上角的寫著“從”字的格子里,可以橫向或縱向跳到相鄰的格子里,但不能跳到對角的格子或其它位置。一直要跳到“華”字結束。
要求跳過的路線剛好構成“從我做起振興中華”這句話。
請你幫助小明算一算他一共有多少種可能的跳躍路線呢? 答案是一個整數,請通過瀏覽器直接提交該數字。
注意:不要提交解答過程,或其它輔助說明類的內容。
一共有 (8分)種可能的跳躍路線
#include <cstdio>#include <cstring>#include <iostream>using namespace std;const string a[4][8] = {{"從","我","做","起","振"}, {"我","做","起","振","興"}, {"做","起","振","興","中"}, {"起","振","興","中","華"}};int count;string val[8];int check(string val[]){ if(val[0] == "從" && val[1] == "我" && val[2] == "做" && val[3] == "起" && val[4] == "振" && val[5] == "興" && val[6] == "中" && val[7] == "華" ) return 1; else return 0;}// "從我做起振興中華" void dfs(int step, int x, int y){ if(x > 3){ return; } if(y > 4){ return; } if(step > 7){ return; } val[step] = a[x][y]; if(step == 7){ if(check(val)){ count++; } return; } dfs(step+1,x+1,y); dfs(step+1,x,y+1);}int main(){ count = 0; dfs(0,0,0); cout << count << endl; return 0;}count == 35;
新聞熱點
疑難解答