P1135 询问大佬为啥只对了四个点??谢谢!!
  • 板块学术版
  • 楼主KevinHu0402
  • 当前回复1
  • 已保存回复1
  • 发布时间2022/7/12 21:38
  • 上次更新2023/10/27 20:46:03
查看原帖
P1135 询问大佬为啥只对了四个点??谢谢!!
639402
KevinHu0402楼主2022/7/12 21:38
#include <iostream>
using namespace std;
int n,a,b;
int p[200];
void bfs(int now,int time){
	if(now == b){
		cout << time;
		return;
	}
	if(now <= 0 or now > n){
		return;
	}
	if(p[now - 1] == 0){
		cout << "-1";
		return;
	}
	if(now + p[now - 1] >= 1 && now - p[now - 1] <= n){
		time++;
		int d = p[now - 1];
		p[now - 1] = 0;
		bfs(now + d,time);
		bfs(now - d,time);
	}
}
int main(){
	cin >> n >> a >> b;
	for(int i = 0;i < n;i++){
		cin >> p[i];
	}
	bfs(a,0);
	return 0;
}
2022/7/12 21:38
加载中...