64分求大佬调教qwq
查看原帖
64分求大佬调教qwq
1261471
Ceciliajie楼主2024/12/29 17:48
/*
*
分析:每一楼层只能到达指定的楼层,数为当前楼层加上对应数字。从起点出发,先将起点入队,搜索该点可以到达的楼层,楼层访问标记,并将这些点入队,直到访问到指定楼层为止。
*
* 
数据结构设计:
1.三个一维数组,分别保存初始数据、访问标记、爬到该楼层的按键数,
*/
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;

#define NMAX 210
int n, s, d, ans = 0, flag = 0;
int a[NMAX], b[NMAX], c[NMAX];
queue<int> q;

int main(void)
{
	cin >> n >> s >> d;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	memset(b, 0, sizeof(b));
	memset(c, 0, sizeof(c));
	//起点入队
	q.push(s); b[s] = 1; c[s] = 0;
	while (!q.empty())
	{
		int loc = q.front(); q.pop();
		if (loc == d) {
			flag = 1;
			break;
		}
		int x1 = loc + a[loc], x2 = loc - a[loc];
		if (x1 >= 1 && x1 <= n && b[x1] == 0) {
			q.push(x1);
			c[x1] = c[loc] + 1;
			b[x1] = 1;
		}
		if (x2 >= 1 && x2 <= n && b[x2] == 0) {
			q.push(x2);
			c[x2] = c[loc] + 1;
			b[x1] = 1;
		}
		
	}
	if (flag)	cout << c[d]-1 << endl;
	else cout << -1 << endl;
	return 0;
}
//1-5WA,查看第一个测试点,我的结果比他多了一,我应该检查什么地方
2024/12/29 17:48
加载中...