这道题是一道简单的DFS遍历图问题。
细节的注意问题都写在注释里了~
#include <iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
bool check[25][45]; //false表示未走过,false表示走过
int n, ans = 0, cur = -1;//当前步数
void Dfs(int x, int y)
{
if(check[x][y] || cur == n) //该点已经走过
return;
check[x][y] = true; //标记为旧点
cur++;
Dfs(x, y-1); //向西
Dfs(x, y+1); //向东
Dfs(x-1, y); //向北
if(cur == n) //找到一种答案,回溯
ans++;
cur--;
check[x][y] = false;
}
int main()
{
scanf("%d",&n);
Dfs(20, 20); //题中要求位置
printf("%d\n",ans);
return 0;
}