王道考研复试机试题解 2020版--第九章搜索

1.Catch That Cow

#include<iostream>
#include<queue>
using namespace std;

const int maxn=100001;

struct status{
	int time; //所需时间
	int position; //当前位置
	//bool isFind=false; 
};
queue<status> wait;

int findCow(status p,int cowPos,bool find[]){  //查找结点相关的下一个 
    if(p.position==cowPos)return p.time;
	if(p.position>0&&!find[p.position-1]){
		find[p.position-1]=true;
		if(p.position-1==cowPos)return p.time+1;
		else {
			status next;
			next.position=p.position-1;
			next.time=p.time+1;
			wait.push(next);
			//cout<<next.position<<endl;
		}
	}
	 if(p.position<maxn&&!find[p.position+1]){
		find[p.position+1]=true;
		if(p.position+1==cowPos)
		{
			//cout<<p.time+1<<endl;
		    return p.time+1;	
		}
		else {
			status next;
			next.position=p.position+1;
			next.time=p.time+1;
			wait.push(next);
			//cout<<next.position<<endl;
		}
	}
	 if(p.position*2<=maxn&&!find[p.position*2]){
		find[p.position*2]=true;
		if(p.position*2==cowPos)return p.time+1;
		else{
			status next;
			next.position=p.position*2;
			next.time=p.time+1;
			wait.push(next);
			//cout<<next.position<<endl;
			
		}
	}
	return -1; 
}


int main(){
	int n,k;
	while(cin>>n>>k){
		while (!wait.empty()) wait.pop();
		
		bool find[maxn]={false};
		
		status farmer;
		farmer.position=n;
		farmer.time=0;
		
		if(k==n)cout<<0<<endl;
		else wait.push(farmer);
		while(!wait.empty()){
			
			int jud=findCow(wait.front(),k,find);
			
			if(jud>0)
			{
				cout<<jud<<endl;
				break;
			}
			else{
				wait.pop();
			}
		}
		
		
	}
	return 0;
}

2.Find The Mutiple

#include<iostream>
#include<queue>
using namespace std;

void BFS(int n){
	queue<long long> myQueue;
	myQueue.push(1);
	while(!myQueue.empty()){
		long long current=myQueue.front();
		myQueue.pop();
		if(current%n==0){
			cout<<current<<endl;
			return;
		}
		myQueue.push(current*10);
		myQueue.push(current*10+1);
		
	}
}

int main(){
	
	int n;
	while(cin>>n&&n!=0){
		BFS(n);
	}
		
	
	return 0;
}

3.玛雅人的密码

#include<iostream>
#include<map>
#include<string>
#include<queue>
using namespace std;
map<string,int> M;//M[str]表示str经历的交换次数
queue<string> Q;

string Swap(string str, int i){
    //将字符串i位与i+1位呼唤
    string newStr=str;
    char tmp=newStr[i];
    newStr[i]=newStr[i+1];
    newStr[i+1]=tmp;
    return newStr;
}

bool Judge(string str){
    if(str.find("2012",0)==string::npos)return false;
    else return true;
}
int BFS(string str){
    //广度优先搜索
    M.clear();//清空map
    while(!Q.empty())Q.pop();//清空队列
    Q.push(str);
    M[str]=0;
    while(!Q.empty()){
        str=Q.front();
        Q.pop();
        for(int i=0;i<str.size()-1;i++){
            string newStr=Swap(str,i);
            if(M.find(newStr)==M.end()){
                M[newStr]=M[str]+1;
                if(Judge(newStr)==true)return M[newStr];
                else Q.push(newStr);
            }
            else continue;
        }
    }return -1;
}
int main(){
    int n;
    string str;
    while(cin>>n>>str){
        if(Judge(str)==true)cout<<0<<endl;
        else{
            cout<<BFS(str)<<endl;
        }
       
    }
     return 0;
}

4. A Knight's Journey

 

 

发布了53 篇原创文章 · 获赞 12 · 访问量 7925

猜你喜欢

转载自blog.csdn.net/mid_Faker/article/details/104701100