一下是分析:
测试样例如下:
5 5
.....
.*.*.
.*S*.
.***.
...T*
2 2 4 3
5 5
.....
.*.*.
.*S*.
.*.*.
...T.
2 2 4 3
5 5
.....
.*.*.
.*S*.
.***.
...T.
2 2 4 3
#include<iostream>
#include<string>
#include<algorithm>
#include<bits/stdc++.h>
#include<stack>
#include<set>
#include<vector>
#include<map>
#include<queue>
#include<deque>
#include<cctype>
#include<unordered_set>
#include<unordered_map>
#include<fstream>
using namespace std;
const int maxn=100;
struct Node{
int x,y;
int step=0;
}B,T,node;//B为起点,T为终点,node是临时节点
int n,m;
char maze[maxn][maxn];//棋盘
bool inq[maxn][maxn];//判断该点是不是入过队
int X[4]={0,0,-1,1};//用于存放当前点的上下左右坐标
int Y[4]={1,-1,0,0};
bool text(int x,int y){//判断该点是否有效
if(x>=n||x<0||y>=m||y<0) return false;//超出棋盘边界,无效
if(maze[x][y]=='*') return false;//是墙壁,无效
if(inq[x][y]==true) return false;//如果队,不能重复走,无效
return true;//有效点
}
int Bfs(){
queue<Node>qu;//广度优先搜索,用队列实现
qu.push(B);
while(!qu.empty()){
Node top=qu.front();//top为正确路线节点
qu.pop();
if(top.x==T.x&&top.y==T.y){
return top.step;//终点,返回步数
}
for(int i=0;i<4;i++){
int newx=top.x+X[i];//新坐标
int newy=top.y+Y[i];
if(text(newx,newy)){//判断是否合理
node.x=newx;//合理就将赋值给临时节点
node.y=newy;
node.step=top.step+1;//临时节点的步数为当前最短节点步数加一
qu.push(node);
inq[newx][newy]=true;//入过队
}
}
}
return -1;//无法到达终点
}
int main(){
cin>>n>>m;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>maze[i][j];
}
getchar();//换行符
maze[i][m]='\0';
}
cin>>B.x>>B.y>>T.x>>T.y;//输入
B.step=0;//初始化起点为0,即从s走到s走了0步
cout<<Bfs();
return 0;
}
用深度优先搜索实现,Dfs,分析如下
不过有个问题,如果是这样的
5 5
.....
.*.*.
.*S*.
.*.*.
...T.
2 2 4 3
在第一次走的时候,会因为4,2的点在第一次深搜已经走过了,所以没法求出最短的,很烦,我还得再想想
#include<iostream>
#include<string>
#include<algorithm>
#include<bits/stdc++.h>
#include<stack>
#include<set>
#include<vector>
#include<map>
#include<queue>
#include<deque>
#include<cctype>
#include<unordered_set>
#include<unordered_map>
#include<fstream>
using namespace std;
const int maxn=1000;
int n,m;
char maze[maxn][maxn];
bool inq[maxn][maxn];
int X[4]={0,0,-1,1};
int Y[4]={1,-1,0,0};
int minstep=10000;
int X1,Y1,X2,Y2;
bool text(int x,int y){
if(x>=n||x<0||y>=m||y<0) return false;
if(maze[x][y]=='*') return false;
if(inq[x][y]==true) return false;
return true;
}
void Dfs(int x,int y,int step){//只需要修改这一部分,将函数设置为无返回值的
if(x==X2&&y==Y2){//如果找到终点且步数最少,将minstep赋值
if(step<minstep){
minstep=step;
}
return ;
}
for(int i=0;i<4;i++){
int xx=x+X[i];
int yy=y+Y[i];//同上,找出上下左右的点
if(text(xx,yy)){//满足条件
inq[xx][yy]=true;//设置为true
Dfs(xx,yy,step+1);//继续递归遍历下一个点
}
}
}
int main(){
cin>>n>>m;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>maze[i][j];
}
getchar();
}
cin>>X1>>Y1>>X2>>Y2;
Dfs(X1,Y1,0);
if(minstep==10000){//如果没找到,输出error
cout<<"error";
return 0;
}
cout<<minstep;
return 0;
}
解决完后代码:return那里还是没想明白,
#include<iostream>
#include<string>
#include<algorithm>
#include<bits/stdc++.h>
#include<stack>
#include<set>
#include<vector>
#include<map>
#include<queue>
#include<deque>
#include<cctype>
#include<unordered_set>
#include<unordered_map>
#include<fstream>
using namespace std;
const int maxn=1000;
int n,m;
char maze[maxn][maxn];
bool inq[maxn][maxn];
int X[4]={0,0,-1,1};
int Y[4]={1,-1,0,0};
int minstep=10000;
int X1,Y1,X2,Y2;
bool text(int x,int y){
if(x>=n||x<0||y>=m||y<0) return false;
if(maze[x][y]=='*') return false;
if(inq[x][y]==true) return false;
return true;
}
void Dfs(int x,int y,int step){
if(x==X2&&y==Y2){
if(step<minstep){
minstep=step;
}
return ;
}
for(int i=0;i<4;i++){
int xx=x+X[i];
int yy=y+Y[i];
if(text(xx,yy)){
inq[xx][yy]=true;
Dfs(xx,yy,step+1);
inq[xx][yy]=false; //此时还要将该点设为false!!!
}
}
}
int main(){
cin>>n>>m;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>maze[i][j];
}
getchar();
}
cin>>X1>>Y1>>X2>>Y2;
Dfs(X1,Y1,0);
if(minstep==10000){
cout<<"error";
return 0;
}
cout<<minstep;
return 0;
}