标签:os io for sp html on amp c size
没啥好说的,注意一下,走过的楼层不能再走,否则会陷入循环
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list> #include <set> #include <queue> #include <stack> using namespace std; #define maxn 210 int num[maxn],n,a,b; int tot; int step[maxn],vis[maxn]; bool bfs() { memset(vis,0,sizeof(vis)); queue<int>q; q.push(a); vis[a]=1; while(!q.empty()) { int u=q.front(); q.pop(); for(int i=0;i<=1;i++) { int v=u+(i?num[u]:-num[u]); if(v<0||v>n) continue; if(vis[v]==0) { vis[v]=1; q.push(v); step[v]=step[u]+1; } if(v==b) { return true; } } } return false; } int main() { while(scanf("%d",&n)!=EOF) { if(n==0) break; tot=0; memset(step,0,sizeof(step)); scanf("%d%d",&a,&b); for(int i=1;i<=n;i++) scanf("%d",&num[i]); if(bfs()) printf("%d\n",step[b]); else printf("-1\n"); } return 0; }
标签:os io for sp html on amp c size
原文地址:http://blog.csdn.net/u013748887/article/details/39028989