#include<bits/stdc++.h>
using namespace std;
int n,ans;
int a[1001];
int get_frist(int x)
{
int res=0;
while(x)
{
res=x%10;
x/=10;
}
return res;
}
int get_final(int x)
{
return x%10;
}
void dfs(int i,int cnt,int last)//last 表示选了方案数中的最后一位数
{
if(i>=n)
{
ans=max(ans,cnt);
return;
}
//选第i个数 //为什么last等于-1 因为第一个数是随便选的 last==-1 想选什么选什么
if(last==-1||get_final(last)==get_frist(a[i]))//选的这个数的第一位与前面一个数的最后一位相同
dfs(i+1,cnt+1,a[i]);
//不选
dfs(i+1,cnt,last);
}
int main()
{
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
dfs(0,0,-1);
cout<<n-ans<<endl;
return 0;
}
接龙数列 c++ dfs 只过四分一的测试
于 2024-03-25 19:40:36 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)