文章
1
粉丝
36
获赞
2
访问
1.1k
#include <iostream>
#include<vector>
#include <string>
#include <algorithm>
#include <queue>
#include <string.h>
using namespace std;
//数组的下标从1开始,而非0
const int MAX = 205;
int n, a, b;
int nums[MAX];
int dir[2] = { 1,-1 };
int dis[MAX];
bool visit[MAX] = { false };
bool jud(int floor) {
if (floor < 0 || floor > n) {
return false;
}
if (visit[floor]) {
return false;
}
return true;
}
//BFS模板
int BFS() {
queue<int>q;
q.push(a);
while (!q.empty()) {
int cur = q.front();
q.pop();
//找到出口了
if (cur == b)
return dis[cur];
for (int i = 0; i < 2; i++) {
int newfloor = cur + dir[i]*nums[cur];
if (jud(newfloor)){
q.push(newfloor);
visit[newfloor] = true;
dis[newfloor] = dis[cur] + 1;
}
}
}
return -1;
}
int main() {
cin >> n >> a >> b;
for (int i = 1; i <= n; i++) {
cin>>nums[i];
}
if (a == b) {
...
登录后发布评论
暂无评论,来抢沙发