1 /********************************
2 啊哈!算法
3 深度优先搜索算法
4 迷宫问题
5 输入:
6 5 4
7 0 0 1 0
8 0 0 0 0
9 0 0 1 0
10 0 1 0 0
11 0 0 0 1
12 1 1 4 3
13
14 输出:7
15 *********************************/
16 #include<iostream>
17 #include<ctime>
18
19 using namespace std;
20 int count=0;
21 int endx,endy,m,n;
22 void dfs(int **pos,bool **book,int sx,int sy,int step,int &min)
23 {
24 if(sx==endx && sy==endy)
25 {
26 if(step<=min)
27 min=step;
28 cout<<"第"<<count++<<"方案:"<<step<<endl;
29 return;//出口
30 }
31 int move[4][2]={
32 {0,1},
33 {0,-1},
34 {1,0},
35 {-1,0}
36 };
37
38 int tx,ty;
39 for(int i=0;i<4;i++)
40 {
41 tx=sx+move[i][0];
42 ty=sy+move[i][1];
43 if(tx>m || tx<1 || ty>n || ty<1)
44 continue;
45 if(pos[tx][ty]==0 && book[tx][ty]==0)
46 {
47 book[tx][ty]=true;
48 dfs(pos,book,tx,ty,step+1,min);
49 book[tx][ty]=0;//**尝试结束,取消这个点的标记
50 }
51 }
52 }
53
54
55 int main()
56 {
57 int i,j;
58 cout<<"输入迷宫的行列:";
59 cin>>m>>n;
60 int **pos=new int *[m+1];//迷宫。指针数组
61 bool **book=new bool*[m+1];//记录是否走过
62 for(i=1;i<m+1;i++)
63 {
64 pos[i]=new int[n+1];
65 book[i]=new bool[n+1];
66 }
67
68 cout<<"输入迷宫(1表示障碍物,0表示通道,空格隔开):"<<endl;
69 for(i=1;i<m+1;i++)
70 {
71 for(j=1;j<n+1;j++)
72 {
73
74 cin>>pos[i][j];
75 while(!cin.good())
76 {
77 cin.clear();
78 while(cin.get()!='\n')
79 continue;
80 cout<<"在第"<<i+1<<"行"<<"第"<<j+1<<"列"<<"输入错误"<<endl;
81 cout<<"重新输入:"<<endl;
82 cin>>pos[i][j];
83 }
84 }
85 }
86
87 cout<<endl<<"迷宫:"<<endl;
88 for(i=1;i<m+1;i++)
89 {
90 for(j=1;j<n+1;j++)
91 cout<<pos[i][j]<<" ";
92 cout<<endl;
93 }
94
95
96 for(i=1;i<m+1;i++)
97 for(j=1;j<n+1;j++)
98 book[i][j]=0;
99
100 int startx,starty;
101 cout<<"输入起始点: ";
102 cin>>startx>>starty;
103 book[startx][starty]=true;
104
105 cout<<"输入终点: ";
106 cin>>endx>>endy;
107
108 int step=0,min=99999;
109
110 dfs(pos,book,startx,starty,step,min);
111 if (min<99999)
112 cout<<"最短步数是: "<<min<<endl;
113 else cout<<"不存在路径"<<endl;
114
115
116 for(i=1;i<m+1;i++)
117 {
118 delete [] pos[i];
119 delete [] book[i];
120 }
121 delete [] pos;
122 delete [] book;
123 return 0;
124 }
1 /**********************
2 BFS
3 *************/
4 #include<iostream>
5 using namespace std;
6
7 struct node
8 {
9 int x;
10 int y;
11 int f;//记录路径
12 int s;//记录步长
13 };
14
15 struct stack
16 {
17 int st[100];
18 int top;
19 };
20 int main()
21 {
22 node queue[2500];
23
24 bool book[51][51]={false};
25 int m,n,sx,sy,ex,ey;
26 cout<<"row and column:";
27 cin>>m>>n;
28 int i,j;
29 int **pos=new int*[m+1];
30 for(i=0;i<m+1;i++)
31 pos[i]=new int[n+1];
32
33 cout<<"screat map:"<<endl;
34 for(i=1;i<m+1;i++)
35 for(j=1;j<n+1;j++)
36 cin>>pos[i][j];
37 cout<<"start and end:";
38 cin>>sx>>sy>>ex>>ey;
39 book[sx][sy]=1;
40 int head=0, tail=0;
41 queue[head].x=sx; //定义后初始化只能以这种方式,出发点
42 queue[head].y=sy;
43 queue[head].f=head;
44 queue[head].s=0;
45 tail++;//tail超前队列最后一个元素一位
46
47 int move[][2]={
48 {0,1},{-1,0},{0,-1},{1,0}
49 };
50
51 int goal=0;
52 while(head!=tail)
53 {
54 if( queue[head].x==ex && queue[head].y==ey)
55 {
56 goal=head;
57 cout<<"最短路径:"<<queue[head].s<<endl;
58 head++;
59 break; //广度优先搜索最先找到的就是最短的
60 }
61 for(i=0;i<4;i++)
62 {
63 node t={queue[head].x+move[i][0],queue[head].y+move[i][1],head,queue[head].s+1};
64 //遍历四个方向如果合法且没被标记则入队
65 if(t.x>m || t.x<1 || t.y>n || t.y<1)
66 continue;
67 if(pos[t.x][t.y]==0 && book[t.x][t.y]==false)
68 {
69 queue[tail]=t;//结构体可整体复制
70 tail++;
71 book[t.x][t.y]=true;//注意走过的路要标记
72 }
73 }
74 head++;
75
76 }
77 //打印路径
78 cout<<"队列中的位置是:"<<endl;
79 i=goal;cout<<goal<<endl;
80 stack re={{0},0};
81 while(queue[i].s>=0)//直到回溯到起始点
82 {
83 re.st[re.top++]=i;//反着从终点到起点入栈
84 i=queue[i].f;//这里错了,不要i--,i记录上一个位置的前任(在队列中的位置)
85 if(i==0)//起始点的前任是它自己,要标记退出,不然死循环
86 break;
87
88 }
89 while(re.top>=0)
90 {
91 cout<<queue[re.st[re.top]].x<<" "<<queue[re.st[re.top]].y;//先进后出,出栈,正着打印
92 if(re.top!=0)
93 cout<<"->";
94 re.top--;
95 }
96 cout<<endl;
97
98 for(i=0;i<m+1;i++)
99 delete [] pos[i];
100 delete [] pos;
101 return 0;
102 }