forked from lulufa390/Parallel-A-Star-Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.h
177 lines (148 loc) · 3.82 KB
/
map.h
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//
// Created by Jikai Lu on 4/24/20.
//
#ifndef FINAL_MAP_H
#define FINAL_MAP_H
#include <iostream>
#include <queue>
#include <vector>
#include <unordered_set>
#include <cmath>
#include <utility>
#include <fstream>
using namespace std;
class TestResult
{
public:
int shortest;
int* thread_explore;
TestResult (int thread_count) {
thread_explore = new int[thread_count];
}
~TestResult() {
delete thread_explore;
}
};
class Node
{
public:
int node_id;
// for bidirectional
// int g2_value[2];
// int f2_value[2];
vector<pair<Node *, int>> adjacent_list;
// Node *path_parent;
// extra information
public:
int x;
int y;
public:
Node(int id)
{
node_id = id;
// path_parent = nullptr;
x = y = -1;
}
int compute_heuristic(Node *node)
{
return abs(node->x - this->x) + abs(node->y - this->y);
}
};
class Map
{
public:
Node *start;
Node *goal;
vector<Node *> node_set;
int width, height;
public:
Map(ifstream& input, bool block_goal = false)
{
// ifstream input(fileName, ifstream::in);
// int height, width;
input >> width >> height;
node_set = vector<Node *>(width * height);
int startX, startY, destX, destY;
input >> startX >> startY >> destX >> destY;
vector<vector<Node *>> node_matrix(height, vector<Node *>(width, nullptr));
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
Node *node = new Node(width * i + j);
node_set[node->node_id] = node;
node_matrix[i][j] = node;
node->x = i;
node->y = j;
}
}
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
int x, y;
input >> x >> y;
int count;
input >> count;
while (count--)
{
char dir;
input >> dir;
int newX, newY;
switch (dir)
{
case 'N':
newX = x;
newY = y - 1;
break;
case 'S':
newX = x;
newY = y + 1;
break;
case 'E':
newX = x + 1;
newY = y;
break;
case 'W':
newX = x - 1;
newY = y;
break;
default:
throw string("unknow direction");
}
if (!block_goal || ((newY != destY || newX != destX) && (y != destY || x != destX)))
{
node_matrix[y][x]->adjacent_list.push_back({node_matrix[newY][newX], 1});
}
}
}
}
start = node_matrix[startY][startX];
goal = node_matrix[destY][destX];
}
// Map(const Map &map)
// {
// start = map.start;
// goal = map.goal;
// for (Node *node : map.node_set)
// {
// node_set.insert(node);
// }
// }
// void reverse()
// {
// Node *start_tmp = start;
// start = goal;
// goal = start_tmp;
// is_reverse = true;
// }
// int compute_heuristic(Node *node)
// {
// return abs(node->x - goal->x) + abs(node->y - goal->y);
// }
// int compute_heuristic_reverse(Node *node)
// {
// return abs(node->x - start->x) + abs(node->y - start->y);
// }
};
#endif //FINAL_MAP_H