-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsrc.cpp
More file actions
65 lines (63 loc) · 1.64 KB
/
src.cpp
File metadata and controls
65 lines (63 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<bits/stdc++.h>
using namespace std;
int n,m,sx,sy,ex,ey;//存储输入的x、y坐标,开始坐标和结束坐标
int a[110][110];
int head=1,tail=1,step=0;//存储迷宫的数组,初始化头指针和尾指针,路径
int q[40000][4];//存储队列
int fx[5]={0,1,0,-1,0};
int fy[5]={0,0,1,0,-1};
int tx,ty;//将要去的坐标
void print(int k){
if(q[k][3]!=0) print(q[k][3]);
a[q[k][1]][q[k][2]]=2;
cout<<"("<<q[k][1]<<","<<q[k][2]<<")";
if(k!=tail) cout<<"->";
step++;
}
void welcome(){
cout<<"Welcome to Y-Maze!An automatic maze solve program.\nDeveloped by YURLAK,2023."<<endl;
}
int main(){
welcome();
printf("\033[32m%s\033[0m","[INPUT x AND y]");
cin>>n>>m;
printf("\033[32m%s\033[0m\n","[INPUT YOUR MAZE BELOW]");
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>a[i][j];
}
}
printf("\033[32m%s\033[0m","[INPUT START]");
cin>>sx>>sy;
printf("\033[32m%s\033[0m","[INPUT END]");
cin>>ex>>ey;
q[1][1]=sx;q[1][2]=sy;q[1][3]=0;//初始化队列
while(head<=tail){
//枚举四个方向(坐标)
for(int i=1;i<=4;i++){
tx=q[head][1]+fx[i];
ty=q[head][2]+fy[i];
if(tx>=1&&tx<=n&&ty>=1&&ty<=m&&a[tx][ty]==0){//判断是否越界
a[tx][ty]=3;//标记为已走过
tail++;
q[tail][1]=tx;
q[tail][2]=ty;
q[tail][3]=head;
if(tx==ex&&ty==ey) break;
}
}
head++;
}
printf("\033[32m%s\033[0m\n","[RESULT OUTPUT]");
print(tail);
printf("\n\033[32m%s\033[0m\n","[MAZE]");
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(a[i][j]==2) printf("\033[31m%d\033[0m",a[i][j]);
else if(a[i][j]==3) cout<<0;
else cout<<a[i][j];
}
cout<<endl;
}
printf("\033[32mSteps:%d\033[0m\n",step-1);
}