踩方格 OpenJ_Bailian - 4103(DFS)

这道题是一道简单的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;
}

猜你喜欢

转载自blog.csdn.net/a1097304791/article/details/82115151