C++计算最短的路线所用时间

题目中输入的4*4的数组表示走过这个点所用的时间,求最短时间。

在这里插入图片描述

//Author:PanDaoxi
#include <iostream>
using namespace std;
//a=地图
//n,m=地图的宽和高
//mins,maxs=最短时间
int a[101][101],book[101][101],n,m,mins=100000,maxs=0;
//sx,sy=起点坐标
//ex,ey=终点坐标
int sx,sy,ex,ey;
//搜索:上右下左
int f[4][2]={
    
    {
    
    -1,0},{
    
    0,1},{
    
    1,0},{
    
    0,-1}};
//深搜函数
void dfs(int x,int y){
    
    
	//假如到终点
	if(x==ex&&y==ey){
    
    
		mins=(maxs<mins?maxs:mins);
		return;
	}
	//搜索 
	for(int i=0;i<4;i++){
    
    
		int nx=x+f[i][0],ny=y+f[i][1];
		//在地图中且没被搜索过 
		if(nx>=1&&nx<=n&&ny>=1&&ny<=m&&book[nx][ny]==0){
    
    
			book[nx][ny]=1;
			maxs+=a[nx][ny]; //累加时间 
			dfs(nx,ny); //递归 
			//回溯
			book[nx][ny]=0;
			maxs-=a[nx][ny]; 
		} 
	}	
} 
int main(){
    
    
	cin>>n>>m;
	cin>>sx>>sy>>ex>>ey;
	for(int i=1;i<=n;i++){
    
    
		for(int j=1;j<=m;j++){
    
    
			cin>>a[i][j];
		}
	}
	book[sx][sy]=1;
	maxs=a[sx][sy];
	dfs(sx,sy);
	cout<<mins<<endl;
	return 0;
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/PanDaoxi2020/article/details/121191078