2018蓝桥杯国赛c++b组-第四题-调手表

每次只能走一步或k步
从原点开始bfs即可
#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=100000+5;
int d[maxn];
int main(){
	int n,k;
	while(cin>>n>>k){
		memset(d,-1,sizeof(d));
		queue<int> q;
		q.push(0);
		d[0]=0;
		while(!q.empty()){
			int p=q.front();
			q.pop();
			if(d[(p+1)%n]==-1){
				q.push((p+1)%n);
				d[(p+1)%n]=d[p]+1;
			}
			if(d[(p+k)%n]==-1){
				q.push((p+k)%n);
				d[(p+k)%n]=d[p]+1;
			}
		}
		int max=0;
		for(int i=0;i<n;i++){
			if(d[i]>max)
			max=d[i];
		}
		cout<<max<<endl;
	}
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/qq_41093189/article/details/80620157
今日推荐