2016 Blue Bridge National - one step away

Problem Description:


Waking up from a coma, Xiao Ming finds himself locked in an abandoned minecart on Planet X. The minecart is parked on a straight, abandoned track. In front of him are two buttons with "F" and "B" written on them.

Xiao Ming suddenly remembered that these two buttons could control the minecart to move forward and backward on the track. Press F to advance 97 meters. Press B to go back 127 meters. Through the dim light, Xiao Ming saw that there was a surveillance probe 1 meter in front of him. He had to find a way to get the minecart parked just below the camera to get a chance to get help from his companions. Perhaps, by manipulating F and B multiple times.

The power on the minecart is not enough, and the yellow warning light is blinking silently... Every time you perform an F or B operation, a certain amount of energy is consumed. Xiao Ming quickly calculated how many operations it would take to park the minecart exactly 1 meter ahead.

Please fill in the minimum number of operations required to achieve the goal


The first thing you should think of is the formula 1-i*97+j*127=0 (this is equivalent to taking the target position as the origin, the forward direction is the negative direction, and the backward direction is the positive direction), you can convert the formula to i *97+j*127=1, you can know that i+j is the required answer, so the easiest way is to use a double loop to solve it.

#include<iostream>
using namespace std;
int main(){
	int i,j,flag=0;
	for(i=0;i<101;i++){
		for(j=0;j<101;j++){
			if(i*97-j*127==1){
				printf("%d %d %d\n",i,j,i+j);
				flag=1;
				break;
			}
		}
		if(flag==1)break;
	}
	return 0;
}

This problem can also be done with queues, but some operations are required to save space.

 
 

#include<iostream> using namespace std; struct Queue{ int dis; int step; }; struct Queue que[1000]; int last=-1,top=-1; int book[100000]; int main(){ last++ ; que[last].step=0; que[last].dis=1; //The initial distance from the target position is 1 book[1]=1;//Mark the array to prevent the same dis from entering the queue while(last>top ){//When the queue is not empty top++;//equivalent to top pointing to the head of the queue//forward 97 meters if(que[top].dis-97>-100&&book[que[top].dis-97]== 0){//Because the final result has nothing to do with the arrangement of forward and backward, so this is to save space

book[que[top].dis-97]==1; last++; que[last].dis=que[top].dis-97; que[last].step=que[top].step+1; } if(que[last].dis==0){//If the distance from the back (behind the target point) to the target point is greater than 128, then it should also be discarded (not added to the queue), for the same reason as above printf("% d\n",que[last].step); break; } //Back 127 meters if(que[top].dis+127<130&&book[que[top].dis+127]==0){ book[ que[top].dis+127]==1; last++; que[last].dis=que[top].dis+127; que[last].step=que[top].step+1; } if( que[last].dis==0){ printf("%d\n",que[last].step); break; } } return 0; }

According to the explanation of the first method, the solution (i, j) of i*97-j*127=1 can be solved, and the extended Euclidean algorithm can be thought of here.

#include<iostream>
using namespace std;
int Extgcd(int a,int b,int &x,int &y){
	if(!b){
		x=1;
		y=0;
		return a;
	}
	else{
		int num=Extgcd(b,a%b,y,x);
		y-=(a/b)*x;
		return num;
	}
}
int main(){
	int x,y,num;
	num=Extgcd(97,-127,x,y);
	cout<<x+y<<endl;
	return 0;
}



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324599895&siteId=291194637