请大佬么么看看这个代码哪里错了, 奇怪的电梯,P1135,谢谢!
  • 板块学术版
  • 楼主gaoyunlang1
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/6/13 17:00
  • 上次更新2024/6/13 20:23:17
查看原帖
请大佬么么看看这个代码哪里错了, 奇怪的电梯,P1135,谢谢!
1345921
gaoyunlang1楼主2024/6/13 17:00
#include<bits/stdc++.h>
using namespace std;
int n,a,b;
const int NN=205;
int arr[NN];
int q[NN];//res:几次 
bool ch[NN];//x:当前位置;
int cnt=1e9;
void dfs(int x,int res){
	if(x<1 || x>n){
		res=0;
		return ;
	}
	if(x==b){
		cnt=min(cnt,res);
		res=0;
	}
	for(int i=a;i<=n;i++){
		if(!ch[i]){
			ch[i]=true;
			arr[x]=i;
			if(x+q[x]<=n && !ch[i+q[x]]){
				dfs(x+q[x],res+1);
			}
			if(x-q[x]>0 && !ch[i-q[x]]){
				dfs(x-q[x],res+1);
			}
			arr[x]=0;
		}
	}
	return ;
} 
int main(){
	cin>>n>>a>>b;
	for(int i=1;i<=n;i++){
		cin>>q[i];
	}
	dfs(1,0);
	if(cnt==1000000000) return cout<<"-1",0;
	cout<<cnt;
	return 0;
}
2024/6/13 17:00
加载中...