其实没什么东西,就是搜索,用的话记得给博主点个赞哦,我会持续更新,关注不迷路
#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;
}