【无标题】

7-2 最短路径 (10 分)
用BFS(广度优先算法写的)

在n*n的方格中找出从起点到终点的最短路径。

提示:每个节点的状态为该节点在地图中的坐标。每个节点扩展时最多有上下左右四个孩子节点。当扩展的节点为目标节点的作标时,遍历结束,最优解即为该节点在这个解空间树中的深度

输入格式:
第一行一个数字n 后面n行为方格地图,s表示起点,e表示终点,*表示可以通过,#表示不能通过

输出格式:
一个数字,表示最短路径。如果没有通路,输出-1

输入样例:
6
s#**
#
###

#
*
##*e
*#

输出样例:
在这里给出相应的输出。例如:

10

//广度优先算法
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
char wall[101][101];
int n;
struct point{
	int x;
	int y;
	int sum;
    vector<point> rrparents;
};
//vector<point> parents;
int bestx = 1000;
queue<point> all_first;
point first;
point end1;
bool is_parent(int x1,int y1,vector<point> parents){
	for(int i = 0; i < parents.size(); i++){
		if(parents[i].x == x1 && parents[i].y == y1){
			return false;
		}
	}
	return true;
}
void show(vector<point> parents){
	for(int i = 0; i < parents.size(); i++){
		cout << parents[i].x << " " << parents[i].y << "   ";
	}
}
void BFS(){
	while( !all_first.empty() ){
		point parent = all_first.front();
		all_first.pop();
		if (parent.x == end1.x && parent.y == end1.y){
			if(parent.sum < bestx){
				bestx = parent.sum;
			}
		}
		else{
			if(parent.y > 1 && is_parent(parent.x,parent.y-1,parent.rrparents) && wall[parent.x][parent.y-1] != '#'){
				point cleft;
				cleft.x = parent.x ;
				cleft.y = parent.y - 1;
				cleft.sum = parent.sum + 1;
				//parents.push_back(parent);	
				vector<point> m_v = parent.rrparents;
				m_v.push_back(parent);
                cleft.rrparents = m_v;
                all_first.push(cleft);
			}
			if(parent.x > 1 &&  is_parent(parent.x-1,parent.y,parent.rrparents) && wall[parent.x-1][parent.y] != '#'){
				point ctop;
				ctop.x = parent.x - 1;
				ctop.y = parent.y;
				ctop.sum = parent.sum + 1;
				//parents.push_back(parent);	
				vector<point> m_v = parent.rrparents;
				m_v.push_back(parent);
                ctop.rrparents = m_v;
                all_first.push(ctop);
			}
			if(parent.y < n &&  is_parent(parent.x,parent.y+1,parent.rrparents) && wall[parent.x][parent.y+1] != '#'){
				point cright;
				cright.x = parent.x ;
				cright.y = parent.y + 1;
				cright.sum = parent.sum + 1;
				//parents.push_back(parent);	
				vector<point> m_v = parent.rrparents;
				m_v.push_back(parent);
                cright.rrparents = m_v;
				all_first.push(cright);
			}
			if(parent.x < n &&  is_parent(parent.x+1,parent.y,parent.rrparents) && wall[parent.x+1][parent.y] != '#'){
				point cbottom;
				cbottom.x = parent.x + 1;
				cbottom.y = parent.y;
				cbottom.sum = parent.sum + 1;
				//parents.push_back(parent);
				vector<point> m_v = parent.rrparents;
				m_v.push_back(parent);
                cbottom.rrparents = m_v;
                all_first.push(cbottom);
			}
		}
	}
}
int main(){
	cin >> n;
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <=n ; j++){
			char a;
			cin >> a;
			if(a == 's'){
				first.x = i;
				first.y = j;
				first.sum = 0;
			}
			else if(a == 'e'){
				end1.x = i;
				end1.y = j;
			}
			wall[i][j] = a;
		}
	}
	all_first.push(first);
	BFS();
	if(bestx == 1000){
		cout << -1 << endl;
	}
	else{
		cout << bestx << endl; 
	}
	return 0;
	
			
	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值