-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
129 lines (105 loc) · 2.7 KB
/
main.cpp
File metadata and controls
129 lines (105 loc) · 2.7 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
#include "maze.h"
#include "Stack.cpp"
#include "Item.h"
#define LENGTH 10
using namespace std;
enum dir{N,NE,E,SE,S,SW,W,NW};;
struct offset{
int x;
int y;
};
offset moves[8];
int main()
{
moves[N]={-1,0};
moves[NE]={-1,1};
moves[E]={0,1};
moves[SE]={1,1};
moves[S]={1,0};
moves[SW]={1,-1};
moves[W]={0,-1};
moves[NW]={-1,-1};
cout << "N_X: " << moves[N].x << endl;
int **mazeMap;
int **mark;
Item temp;
mazeMap = new int*[LENGTH+1];
mark = new int*[LENGTH+1];
/**(1.1)~(10.10)**/
int i,j,d;
Stack<Item> S;
for(i = 1 ; i <= LENGTH ; i++){
mazeMap[i]= new int[LENGTH+1];
mark[i]= new int[LENGTH+1];
}
for(i = 1 ; i <= LENGTH ; i++){
for( j = 1 ; j <= LENGTH ; j++){
mazeMap[i][j]=is_valid(i,j);
mark[i][j] = 0;
cout << mazeMap[i][j];
}
cout << endl;
}
mark[1][1] = 1;
temp.x = 1;
temp.y = 1;
temp.dir = E;
//cout << "PUSH:(" << temp.x << "," << temp.y << "," << temp.dir << ")" << endl;
int done=0;
S.Add(temp);
while(!S.IsEmpty() && !done ){
temp = *S.Delete(temp);
int i = temp.x;
int j = temp.y;
int d = temp.dir;
// cout << "POP:(" << i << "," << j << "," << d << ")" << endl;
while( d < 8 ){
int g = i + moves[d].x;
int h = j + moves[d].y;
cout <<h-moves[d].y <<"!!!!"<< endl;
if((g == 10)&&( h == 10)){
S.Add(temp);
done = 1;
break;
}
if((mazeMap[g][h]==1)&&(mark[g][h]==0)){
//cout << "forward!" << endl;
mark[g][h] = 1;
temp.x = i;
temp.y = j;
temp.dir = d;
S.Add(temp);
// cout << "PUSH:(" << temp.x << "," << temp.y << "," << temp.dir << ")" << endl;
i = g;
j = h;
d = N;
}else d++;
}
}
if(!done)
cout << "no path!" <<endl;
else{
cout << "done!" << endl;
cout << "route:(10,10,x)" << endl;
mazeMap[10][10]=8;
while(!S.IsEmpty()){
temp = *S.Delete(temp);
int i = temp.x;
int j = temp.y;
cout << "(" << temp.x << "," << temp.y << "," << temp.dir << ")" << endl;
mazeMap[i][j]=8;
}
}
for(i = 1 ; i <= LENGTH ; i++){
for( j = 1 ; j <= LENGTH ; j++){
cout << mazeMap[i][j];
}
cout << endl;
}
for(i = 1 ; i <= LENGTH ; i++){
delete []mazeMap[i];
}
delete []mazeMap;
return 0;
}