-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
132 lines (108 loc) · 2.84 KB
/
main.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
//pathfinding algorithm
#include <iostream>
#include <vector>
#define coord std::pair<int, int>
const int width = 10;
const int height = 10;
coord start(0, 0);
coord end(7, 7);
std::vector<coord> stack;
void move(coord pos, const char* notdir);
int arr[width][height] =
{
{14, 13, 9, 8, 9, 8, 8, 9, 8, 11},
{ 6, 15, 12, 3, 12, 3, 6, 13, 0, 11},
{ 6, 12, 3, 12, 3, 13, 1, 9, 0, 11},
{ 4, 3, 12, 3, 12, 9, 10, 12, 0, 11},
{ 6, 12, 3, 12, 1, 8, 3, 6, 4, 11},
{ 4, 3, 12, 3, 15, 6, 14, 6, 4, 11},
{ 6, 14, 6, 15, 12, 2, 5, 0, 0, 10},
{ 6, 6, 4, 9, 1, 1, 11, 7, 6, 7},
{ 6, 7, 6, 12, 9, 9, 9, 10, 4, 11},
{ 5, 9, 1, 1, 9, 9, 9, 1, 3, 15}
};
bool visited(coord pos)
{
for(int i = 0; i < stack.size(); i++){
if(stack[i] == pos)
return true;
}
return false;
}
bool check_right_wall(coord pos)
{
return ((arr[pos.first][pos.second] % 4) >= 2);
}
bool check_left_wall(coord pos)
{
return ((arr[pos.first][pos.second] % 8) >= 4);
}
bool check_up_wall(coord pos)
{
return ((arr[pos.first][pos.second] % 1) >= 8);
}
bool check_down_wall(coord pos)
{
return ((arr[pos.first][pos.second] % 2) >= 1);
}
void checkmove(coord pos, const char* dir)
{
if(dir == "right"){
if(!check_right_wall(pos))
if(!visited(coord(pos.first, pos.second + 1)))
move(coord(pos.first, pos.second + 1), "left");
}
else if(dir == "left"){
if(!check_left_wall(pos))
if(!visited(coord(pos.first, pos.second - 1)))
move(coord(pos.first, pos.second - 1), "right");
}
else if(dir == "up"){
if(!check_up_wall(pos))
if(!visited(coord(pos.first - 1, pos.second)))
move(coord(pos.first - 1, pos.second), "down");
}
else if(dir == "down"){
if(!check_down_wall(pos))
if(!visited(coord(pos.first + 1, pos.second)))
move(coord(pos.first + 1, pos.second), "up");
}
}
bool found = false;
void move(coord pos, const char* notdir)
{
stack.push_back(pos);
if(pos == end)
{
found = true;
return;
}
if(pos.first < 0 || pos.first >= width || pos.second < 0 || pos.second >= height)
return;
if(notdir != "down")
checkmove(pos, "down");
if(found)
return;
if(notdir != "left")
checkmove(pos, "left");
if(found)
return;
if(notdir != "right")
checkmove(pos, "right");
if(found)
return;
if(notdir != "up")
checkmove(pos, "up");
if(found)
return;
stack.pop_back();
}
int main()
{
move(start, "nodir");
for(int i = 0; i < stack.size(); i++)
{
std::cout << "(" << stack[i].first << "," << stack[i].second << ") ";
}
return 0;
}