-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze.cpp
152 lines (141 loc) · 2.89 KB
/
maze.cpp
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <iostream>
#include <vector>
using namespace std;
#define N 3
typedef pair<int,int> point;
vector<vector<pair<int,int>>> paths;
vector<vector<char>> board;
void intialize();
void run();
bool check(point a);
bool is_visited(vector <pair<int,int>> path , pair <int,int> new_point);
void print_path();
void dfs(vector <pair<int,int>> path);
int main(void)
{
run();
}
void intialize()
{
for(int i =0 ; i<N ; i++)
{
vector <char> tmp;
for (int j = 0 ; j < N ; j++)
{
tmp.push_back(' ');
}
board.push_back(tmp);
}
board[0][0] = 'A';
board[N-1][N-1] = 'C';
}
void print_map()
{
for(int i =0 ; i<N ; i++)
{
cout<<"|";
for (int j = 0 ; j < N ; j++)
{
cout<<board[i][j]<<"|";
}
cout<<endl;
for (int j = 0 ; j < 2*N ; j++)
{
cout<<"-";
}
cout<<endl;
}
}
void run()
{
intialize();
print_map();
vector<pair<int,int>> tmp;
tmp.push_back({0,0});
dfs(tmp);
print_path();
}
bool check(pair<int,int> a){
if (a.first < 0 || a.first >= N)
{
return false;
}
if (a.second < 0 || a.second >= N)
{
return false;
}
return true;
}
bool is_visited(vector <pair<int,int>> path , pair <int,int> new_point)
{
for (auto point : path)
{
if (point == new_point)
return false;
}
return true;
}
void dfs(vector <pair<int,int>> path){
pair <int,int> point_n = path[path.size() - 1];
if (board[point_n.first][point_n.second] == 'C')
{
paths.push_back(path);
return;
}
pair <int,int> point_next = point_n;
point_next.second +=1;
if (check(point_next))
{
if(is_visited(path , point_next))
{
path.push_back(point_next);
dfs(path);
path.pop_back();
}
}
point_next = point_n;
point_next.second -=1;
if (check(point_next))
{
if(is_visited(path , point_next))
{
path.push_back(point_next);
dfs(path);
path.pop_back();
}
}
point_next = point_n;
point_next.first -=1;
if (check(point_next))
{
if(is_visited(path , point_next))
{
path.push_back(point_next);
dfs(path);
path.pop_back();
}
}
point_next = point_n;
point_next.first +=1;
if (check(point_next))
{
if(is_visited(path , point_next))
{
path.push_back(point_next);
dfs(path);
path.pop_back();
}
}
}
void print_path()
{
for(int i =0 ; i<paths.size() ; i++)
{
cout<<"Paht_number = "<<i+1<<endl;
for (int j = 0 ; j < paths[i].size() ; j++)
{
cout<<"i = "<<paths[i][j].first<<"j = "<<paths[i][j].second<<endl;
}
cout<<endl;
}
}