用递归和栈找迷宫最短路

其实没什么东西,就是搜索,用的话记得给博主点个赞哦,我会持续更新,关注不迷路 

#include "iostream"
#include "vector" 
#include "queue"
#include "string.h"
using namespace std;
typedef long long ll;
const ll N = 1e4;
int dir[8][2] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}};
string s[8] = {"向左", "向右", "向上", "向下", "向左上", "向右上","向左下","向右下"};
typedef pair<pair<ll, ll> , string> PII;
typedef pair<ll, ll>  PI;
vector<PII> P;
vector<ll> v[N];
vector<PII> t;  
vector<char> v1[N]; 
int vis[N][N];
priority_queue<ll, vector<ll>,  greater<ll> > p;
ll n, m, z ,count = 0,ann = 0, xx, yy, x, y, minSize;
void dfs(ll x, ll y){
	if(x == xx && y == yy){
		if(t.size() < minSize) {
			minSize = t.size() ;
			P.clear() ;
			for(ll i = 0; i < t.size(); i++)
				P.push_back(t[i]);

		}
		 
		
		
	}
//	cout<<x<<" "<<y<<"vvvvv"<<endl; 
	ann ++;  vis[x][y] = 1;
	for(int i = 0; i < 8; i ++){
		ll x1 = x + dir[i][0], y1 = y + dir[i][1];
		if(x1 < 0 || x1 >= n | y1 < 0 || y1 >= m || v[x1][y1] == 1 || vis[x1][y1] ) continue;
		t.push_back({{x1, y1}, s[i]});
		dfs(x1, y1);
		t.pop_back();
	
	}
	vis[x][y] = 0;
	ann  --;
}
int main(){
	minSize = 1e10;
	memset(vis, 0, sizeof(vis));
	cin>>n>>m;
	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j ++){
			cin>>z;
			getchar();
			char s = z + '0';
			v[i].push_back(z);
			v1[i].push_back(s);
		}
	}
	cin>> x>>y>>xx>>yy;	
	v1[x][y] = '^', v1[xx][yy] = '^';
	dfs(x, y);
	if(P.size() == 0){
	cout<<"该道路不通"<<endl;
	return 0;	
	} 
	 
	cout<<"从起点("<<x<<","<<y <<")开始"<<endl;
	for(ll i =0; i < P.size(); i++){
		cout<<P[i].second<<"到";
		cout<<"点("<<P[i].first.first<<","<<P[i].first.second<<")"<<endl;
		v1[P[i].first.first][P[i].first.second] = '*';
	}
	v1[xx][yy] = '^';
	cout<<"路线图如下图所示:"<<endl;
	for( ll i = 0; i < n; i++){
		
	
		for(ll j = 0; j < m; j ++)
			cout<<v1[i][j]<<" ";
			cout<<endl; 
	}
	return 0;
}
#include "iostream"
#include "vector" 
#include "queue"
#include "string.h"
#include "stack"
using namespace std;
typedef long long ll;
const ll N = 1e4;
int dir[8][2] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}};
string s[8] = {"向左", "向右", "向上", "向下", "向左上", "向右上","向左下","向右下"};
typedef pair<pair<ll, ll> , string> PII;
 typedef pair<pair<ll, ll> , ll> PIII;
typedef pair<ll, ll>  PI;
vector<PII> P;
vector<ll> v[N];
vector<char> v1[N];
vector<PII> t;   
int vis[N][N];
stack<PIII> st;
ll minSize;
int i;
priority_queue<PI, vector<PI>,  greater<PI> > p;
ll n, m, z ,count = 0,ann = 0, xx, yy, x, y;

void judge(){ 

		x = st.top().first.first, y = st.top().first.second;
		if(x == xx &&y  == yy){ 
		count ++; 
//		cout<<"%$^&#"<<t.size()<<endl;
//			for(ll j = 0; j < t.size(); j++){
//				 	cout<<t[j].first.first<<" "<<t[j].first.second<<t[j].second<<"--->";
//				}
//				cout<<endl;  //输出全部路线 
			if(t.size() < minSize){
				minSize = t.size();
				P.clear() ;
				 for(ll j = 0; j < t.size(); j++){

					P.push_back(t[j]);
				}
//				cout<<endl;
			} 
			
		  vis[x][y] = 0;
		  st.pop();
		  t.pop_back();
		if(!st.empty())	x = st.top().first.first, y = st.top().first.second;
		
	}
}
void solve(){
  	
  while(!st.empty()){
	judge();
	ll x1, y1;
	for(i = st.top().second; i < 8; i ++){
		 x1 = x + dir[i][0], y1 = y + dir[i][1];
		if(x1 < 0 || x1 >= n || y1 < 0 || y1 >= m || v[x1][y1] == 1 || vis[x1][y1] ) continue;
		else break;	                                               
	}  
	if(i < 8){
		st.top().second = i + 1; 
		t.push_back({{x1, y1}, s[i]}); 
		st.push({{x1, y1}, 0});
		vis[x1][y1] = 1;
	}
	else { 
		if(st.size() == 1) break;
		vis[x][y] = 0;
		st.pop();
		t.pop_back();
		
	}
	
} 	
	
}
int main(){
	minSize = 1e4;
	memset(vis, 0, sizeof(vis));
	cin>>n>>m;
	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j ++){
			cin>>z;  
			getchar();
			char s = z + '0';  
			v[i].push_back(z);
			v1[i].push_back(s); 
		}
	}
	cin>> x>>y>>xx>>yy;
	vis[x][y] = 1;
	v1[x][y] = '^';	
	st.push({{x,y}, 0});
	solve();
	if(P.size() == 0){
	cout<<"该道路不通"<<endl;
	return 0;	
	} 
	
	cout<<"从起点("<<x<<","<<y <<")开始"<<endl;
	for(ll i =0; i < P.size(); i++){
		cout<<P[i].second<<"到";
		cout<<"点("<<P[i].first.first<<","<<P[i].first.second<<")"<<endl;
		v1[P[i].first.first][P[i].first.second] = '*';
		 
	}
	 v1[xx][yy] = '^';
	 cout<<"共有"<<count<<"条线路"<<endl;
	cout<<"路线图如下图所示:"<<endl;
	for( i = 0; i < n; i++){
		
	
		for(ll j = 0; j < m; j ++)
			cout<<v1[i][j]<<" ";
			cout<<endl; 
	}
	return 0;
}

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小竹子14

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值