owo哪里有问题了,求助
查看原帖
owo哪里有问题了,求助
372454
AsadChen楼主2021/8/21 20:34
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;

int n, a, b, ans;
int floo[205], vis[205];
struct node {
	int fl; //层数
	int cnt; //次数
	int i; //当前该匹配的数组下标 
	node (int _fl, int _cnt, int _i) {
		fl = _fl;
		cnt = _cnt;
		i = _i;
	}
};
queue<node> q;

bool in(int x) {
	return x >= 1 && x <= n;
}

int bfs() {
	q.push(node(1, 0, 1));
	vis[1] = true;
	while(!q.empty()) {
		node now = q.front();
		node v = node(0, 0, 0);
		q.pop();
		if (now.fl == b) {
			return now.cnt;
		}
		v.fl = now.fl + floo[now.i];
		if (in(v.fl) && !vis[v.fl]) {
			vis[v.fl] = true;
			v.cnt = now.cnt + 1;
			v.i = now.i + 1;
			q.push(v);
		}
	
		v.fl = now.fl - floo[now.i];
		if (in(v.fl) && !vis[v.fl]) {
			vis[v.fl] = true;
			v.cnt = now.cnt + 1;
			v.i = now.i + 1;
			q.push(v);
		}
	}
	return -1;
}

int main() {
	cin >> n >> a >> b;
	for (int i = 1; i <= n; i++) {
		cin >> floo[i];
	}
	cout << bfs() << endl;
	return 0;
}
2021/8/21 20:34
加载中...