-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpath_to_cell.cpp
More file actions
193 lines (137 loc) · 4.45 KB
/
Copy pathpath_to_cell.cpp
File metadata and controls
193 lines (137 loc) · 4.45 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<char> vc;
typedef pair<int,int> pii;
#define endl '\n'
#define _ ios::sync_with_stdio(0);cin.tie(0);
struct Agent {
char id;
int x;
int y;
};
struct Step {
int x;
int y;
int cost; // Costo real + heuristica
bool operator<(const Step& other) const {
return cost > other.cost;
}
};
const vector<pii> moves = {
{0, 1}, {1, 0}, {-1,0}, {0,-1}, {-1, 1}, {-1, -1}, {1, 1}, {1, -1}
};
int heuristic(int x1, int y1, int x2, int y2){
int dx = abs(x1 - x2);
int dy = abs(y1 - y2);
return dx*dx + dy*dy;
}
// Algoritmo de A*
// Complejidad temporal: O(n*mlog(n*m)) en el peor caso
vector<pii> path_to_cell(Agent &agent, pii &destiny, vector<vector<char>> &grid){
const ll MAX = 1e3;
// Penalizamos pasar por casillas ya cosechadas y premiamos las casillas con cultivo
const int CROP_COST = 1;
const int EMPTY_COST = 2;
const int RE_ENTER_COST = 10;
int n = grid.size();
int m = grid[0].size();
int xf = destiny.first;
int yf = destiny.second;
vector<vector<ll>> costs(n, vector<ll>(m, MAX));
priority_queue<Step> pq;
pq.push({
agent.x,
agent.y,
0
});
costs[agent.y][agent.x] = 0;
while(!pq.empty()){
Step c = pq.top();
pq.pop();
// Primer cultivo encontrado (cultivo óptimo)
if(c.x == destiny.first && c.y == destiny.second){
for(int y = 0; y < n; y++){
for(int x = 0; x < m; x++) cout << costs[y][x] << " ";
cout << endl;
}
// Reconstrucción de camino
int x = c.x;
int y = c.y;
vector<pii> path;
path.push_back({x, y});
while (x != agent.x || y != agent.y) {
for (auto &mov : moves) {
int nx = x + mov.first;
int ny = y + mov.second;
if (nx < 0 || nx >= m) continue;
if (ny < 0 || ny >= n) continue;
if (costs[ny][nx] == costs[y][x] -
(grid[y][x] == 'W' ? CROP_COST :
grid[y][x] == '_' ? RE_ENTER_COST :
EMPTY_COST)) {
x = nx;
y = ny;
path.push_back({x, y});
break;
}
}
}
reverse(path.begin(), path.end());
return path;
}
for(auto &mov : moves){
int nx = c.x + mov.first;
int ny = c.y + mov.second;
if(nx < 0 || nx >= m) continue;
if(ny < 0 || ny >= n) continue;
if(grid[ny][nx]== '#') continue; // No pasar por obstaculos
int cost = 1;
switch(grid[ny][nx]){
case '.': cost = EMPTY_COST; // No hay cultivo (vacio)
break;
case 'W': cost = CROP_COST; // Hay cultivo
break;
case '_': cost = RE_ENTER_COST; // El cultivo ya fué cosechado
break;
}
int newCost = costs[c.y][c.x] + cost;
if(costs[ny][nx] > newCost){
costs[ny][nx] = newCost;
pq.push({
nx,
ny,
newCost + heuristic(nx,ny,xf,yf)
});
}
}
}
return {};
}
int main(){
vector<vector<char>> grid = {
{'#','#','#','#','#','#','#','#','#','#','#','#'},
{'#','.','_','_','.','.','#','.','.','W','.','#'},
{'#','_','#','#','.','W','#','.','#','#','.','#'},
{'#','_','#','.','.','.','.','.','.','#','W','#'},
{'#','.','#','.','#','#','#','#','.','#','.','#'},
{'#','.','_','.','W','.','W','.','.','.','.','#'},
{'#','.','#','.','#','#','#','.','#','#','#','#'},
{'#','W','.','.','#','_','_','.','.','W','.','#'},
{'#','.','#','#','#','_','_','#','#','#','.','#'},
{'#','#','#','#','#','#','#','#','#','#','#','#'}
};
Agent agent = {
'A',
1, // x
1 // y
};
pii destiny = {
8, // x
5 // y
};
vector<pii> path = path_to_cell(agent, destiny, grid);
for(auto c : path) cout << c.first << " " << c.second << endl;
}