题目要求

需求:从一个方格中A点,按要求移动到B点。
要求:每次只能走上下左右,每次只能走一次,每次是轮换穿越’+‘,’-'两个,否则就会能量失衡,发生爆炸。

使用的算法:这题典型的就是使用BFS算法,DFS也是可以的,不过BFS明显最为简单。

bfs算法,优先广度搜索

int bfs(std::pair<int,int>s,std::pair<int,int>t)
{
	std::map<  std::pair<int,int>, std::pair<int,int>>processor;//记录前驱与后继节点进行回溯的 
	int  visted[5][5];
	memset(visted,0,sizeof(visted));
	int orient[4][2]={{-1,0},{1,0},{0,-1},{0,1}};//定义上下左右移动的方向
	std::queue<std::pair<int,int>> que; //定义一个队列
	que.push(s);
	visted[s.first][s.second]=1; 
	while(!que.empty()) 
	{
		//取出第一个元素
		auto temp=que.front();
		que.pop();//弹出队列第一个位置元素 
		if( temp.first == t.first && temp.second == t.second )
			break ;
		for(int i=0;i<4;i++)
		{  
		
			std::pair<int,int> next =std::make_pair(temp.first+orient[i][0],temp.second+orient[i][1]);
			if(next.first >=0 && next.first <=4 && 
			next.second >=0 && next.second <=4 && 
			visted[ next.first][next.second] !=1 &&
			map_info[temp.first][temp.second] != map_info[next.first][next.second] )
			{
				visted[next.first][next.second] =1;//表示已经走过了 
				que.push(next);
				processor[next]=temp;
				
				cout<<temp.first<<"  "<<temp.second<<"       "<<next.first << "  "<<next.second<<endl;
			}
		}
		cout<<"--------------------------"<<endl;
	}
	
	
//反向回溯路径	
	std::vector<std::pair<int,int>> ops;
	for(std::pair<int,int> at =t;at!=s;at=processor[at])
		{
			if(processor.find(at) ==processor.end())
				return -1;
			ops.push_back(at);
		}
		
		
//只是为了显示结果,打印出来	
	for(auto it=processor.begin();it!=processor.end();++it)
	{
		std::cout << "Key: (" << it->first.first << ", " << it->first.second << "), Value: (" 
                  << it->second.first << ", " << it->second.second << ")" << std::endl;

	}
	return ops.size();
	
}

全部代码如下

#include<bits/stdc++.h>
using namespace std;

char map_info[5][5]={
{'A','+','-','+','-'},
{'-','+','-','-','+'},
{'-','+','+','+','-'},
{'+','-','+','-','+'},
{'B','+','-','+','-'}
};

int bfs(std::pair<int,int>s,std::pair<int,int>t)
{
	std::map<  std::pair<int,int>, std::pair<int,int>>processor;//记录前驱与后继节点进行回溯的 
	int  visted[5][5];
	memset(visted,0,sizeof(visted));
	int orient[4][2]={{-1,0},{1,0},{0,-1},{0,1}};//定义上下左右移动的方向
	std::queue<std::pair<int,int>> que; //定义一个队列
	que.push(s);
	visted[s.first][s.second]=1; 
	while(!que.empty()) 
	{
		//取出第一个元素
		auto temp=que.front();
		que.pop();//弹出队列第一个位置元素 
		if( temp.first == t.first && temp.second == t.second )
			break ;
		for(int i=0;i<4;i++)
		{  
		
			std::pair<int,int> next =std::make_pair(temp.first+orient[i][0],temp.second+orient[i][1]);
			if(next.first >=0 && next.first <=4 && 
			next.second >=0 && next.second <=4 && 
			visted[ next.first][next.second] !=1 &&
			map_info[temp.first][temp.second] != map_info[next.first][next.second] )
			{
				visted[next.first][next.second] =1;//表示已经走过了 
				que.push(next);
				processor[next]=temp;
				
				cout<<temp.first<<"  "<<temp.second<<"       "<<next.first << "  "<<next.second<<endl;
			}
		}
		cout<<"--------------------------"<<endl;
	}
	
	
	
	std::vector<std::pair<int,int>> ops;
	for(std::pair<int,int> at =t;at!=s;at=processor[at])
		{
			if(processor.find(at) ==processor.end())
				return -1;
			ops.push_back(at);
		}
		
		
	
	for(auto it=processor.begin();it!=processor.end();++it)
	{
		std::cout << "Key: (" << it->first.first << ", " << it->first.second << "), Value: (" 
                  << it->second.first << ", " << it->second.second << ")" << std::endl;

	}
	return ops.size();
	
}



signed main()
{
	
	
	std::pair<int,int> s0=std::make_pair(0,0);
	std::pair<int,int> s1=std::make_pair(4,0);
	
cout<<	bfs(s0,s1);

	return 0;
}   

输出结果

更多推荐