生活随笔
收集整理的這篇文章主要介紹了
跳格子。。
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
小明參加了學校的趣味運動會,其中的一個項目是:跳格子。
比賽時,先站在左上角的寫著“從”字的格子里,可以橫向或縱向跳相鄰的格子里,但不能跳到對角的格子里。格子中寫的字如下(或參看p1.jpg):
從我做起振
我做起振興
做起振興中
起振興中華
要求跳過的路線剛好構成“從我做起振興中華”這句話。
請你幫助小明算一算他一共有多少種可能的跳躍路線呢?
答案是一個整數,請通過瀏覽器直接提交該數字。
注意:不要提交解答過程,或其它輔助說明類的內容。
DFS
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std
;
int a
[4][5] = {0,1,2,3,4,1,2,3,4,5,2,3,4,5,6,3,4,5,6,7};
int vis
[10][10];
int dx
[4] = {0,1,0,-1};
int dy
[4] = {1,0,-1,0};
int res
= 0;
void dfs(int r
,int c
)
{vis
[r
][c
] = 1;if(a
[r
][c
] == 7){res
++;return;}for(int i
=0;i
<4;i
++){int x
,y
;x
= r
+ dx
[i
];y
= c
+ dy
[i
];if(!vis
[x
][y
] && x
>=0 && x
<=3 && y
>=0 && y
<=4 && a
[r
][c
] +1 ==a
[x
][y
]){dfs(x
,y
);vis
[x
][y
] = 0;}}
}
int main()
{dfs(0,0);cout
<<res
;
}
總結
以上是生活随笔為你收集整理的跳格子。。的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。