BFS 40AC,60WA
查看原帖
BFS 40AC,60WA
602624
___njr___楼主2022/10/19 12:41
#include <bits/stdc++.h>
using namespace std;
bool check(int i , int n) { return i>=1 && i<=n ;}

int k[210];
struct bb{
	int first ;
	int second ;
};
bitset<210> vis;
int N,n,m;
inline int bfs( int a,int b ) {
	for (int i = 0 ;i<210;++i) vis[i] = false ;
	queue<bb> q;
	q.push({a,0});
	while (q.size()) {
		bb x = q.front();
		vis[x.first] = true ;
		q.pop();
		if ( x.first == b ) return x.second ;
		int x1 = x.first + k[ x.first ] ;
		int x2 = x.first - k[ x.first ] ;
		if ( check(x1,n) && !vis[x1] ) q.push({x1,x.second+1});
		if ( check(x2,n) && !vis[x2] ) q.push({x2,x.second+1});
	}
	return-1 ;
}
int main()
{
	scanf ("%d%d%d" , &N , &n , &m) ;
	for (int i = 0 ; i < N ; ++i )
		scanf("%d" , k + i) ;
	printf("%d" , bfs ( n , m ) ) ;
	return 0;
}
2022/10/19 12:41
加载中...