dfs即可,代码有注释.
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int a[N],b[N],st[N],vis[N],flag=0,n;
char ans[N];
//确认过眼神是一个难难的题目Hhhh
bool dfs(int u,int step)//能不能用step步到第u个点.
{
if(u>n||u<1) return false;//假如不合法直接返回.
if(u==n)
{
ans[step]='\0';
return true;
}
if(vis[u])
{
st[u]=1;
return false;//不可能一直重复.会t的
}
vis[u]=1;
//选择走第一种
if(dfs(u+a[u],step+1))
{
ans[step]='a';
if(st[u]) flag=1;
return true;
}
//选择走第二种
if(dfs(u+b[u],step+1))
{
ans[step]='b';
if(st[u]) flag=1;
return true;
}
return false;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(int i=1;i<=n;i++) scanf("%d",&b[i]);
//起始在1号小区,终点在n号小区
if(dfs(1,0))
{
if(flag) puts("Infinity!");
else puts(ans);
}
else
{
puts("No solution!");
}
return 0;
}
京公网安备 11010502036488号