路径计数
题目链接
https://www.lanqiao.cn/problems/817/learning/
题目来源
蓝桥云课
思路:
把沿边走看成点到点,把出发点设成特殊数,不然开始会被返回,dfs上下左右各走一次,判断边界
#include <bits/stdc++.h>
using namespace std;
int a[100][100];
int ans;
void dfs(int x,int y ,int s)//s 代表步数
{
if(x<1||y<1||x>5||y>5) return; //图边界
if(s>13) return ;//步数极限
if(a[x][y]==1) return ;
if(x==1&&y==1&&s>3){//s>3 一圈至少4步 x==1 && y==1 回到原点
ans++;
return ;
}
if(x!=1&&y!=1)
a[x][y] = 1;
dfs(x+1,y,s+1);
dfs(x-1,y,s+1);
dfs(x,y+1,s+1);
dfs(x,y-1,s+1);
return;
}
int main()
{
a[1][1]=2;//出发点设成特殊数
dfs(1,1,0);//从一出发
cout << ans;
return 0;
}