This repository was archived by the owner on Jun 20, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathcivilization.cpp
More file actions
130 lines (113 loc) · 3.09 KB
/
Copy pathcivilization.cpp
File metadata and controls
130 lines (113 loc) · 3.09 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
//
// Created by Bobur on 2.25.2021
//
#include <bits/stdc++.h>
#define vec std::vector
#define pair std::pair
#define make std::make_pair
#define queue std::priority_queue
const int infty = INT_MAX;
const int size = 1000;
int n, m, a, b, c, d;
short int map[size][size];
vec<vec<int>> w(size, vec<int>(size, infty));
bool visited[size][size];
// dijkstra's algorithm
void dijkstra(int x, int y)
{
queue<pair<int, pair<int, int>>> que;
que.push(make((w[x][y] = 0), make(x, y)));
while (!que.empty())
{
x = que.top().second.first;
y = que.top().second.second;
que.pop();
if (visited[x][y]) continue;
visited[x][y] = 1;
if (x > 0 && !visited[x - 1][y] && map[x - 1][y]) {
if (w[x][y] + map[x - 1][y] < w[x - 1][y]) {
w[x - 1][y] = w[x][y] + map[x - 1][y];
}
que.push(make(-w[x - 1][y], make(x - 1, y)));
}
if (x < n - 1 && !visited[x + 1][y] && map[x + 1][y]) {
if (w[x][y] + map[x + 1][y] < w[x + 1][y]) {
w[x + 1][y] = w[x][y] + map[x + 1][y];
}
que.push(make(-w[x + 1][y], make(x + 1, y)));
}
if (y > 0 && !visited[x][y - 1] && map[x][y - 1]) {
if (w[x][y] + map[x][y - 1] < w[x][y - 1]) {
w[x][y - 1] = w[x][y] + map[x][y - 1];
}
que.push(make(-w[x][y - 1], make(x, y - 1)));
}
if (y < m - 1 && !visited[x][y + 1] && map[x][y + 1]) {
if (w[x][y] + map[x][y + 1] < w[x][y + 1]) {
w[x][y + 1] = w[x][y] + map[x][y + 1];
}
que.push(make(-w[x][y + 1], make(x, y + 1)));
}
}
}
int cost = 0;
void print_direction(int x, int y)
{
if (w[x][y] == 0) return;
else cost = w[x][y];
char sign;
if (x > 0 && cost > w[x - 1][y]) {
sign = 'S';
cost = w[x - 1][y];
}
if (x < n - 1 && cost > w[x + 1][y]) {
sign = 'N';
cost = w[x + 1][y];
}
if (y > 0 && cost > w[x][y - 1]) {
sign = 'E';
cost = w[x][y - 1];
}
if (y < m - 1 && cost > w[x][y + 1]) {
sign = 'W';
cost = w[x][y + 1];
}
switch (sign) {
case 'S':
print_direction(x - 1, y);
break;
case 'N':
print_direction(x + 1, y);
break;
case 'E':
print_direction(x, y - 1);
break;
case 'W':
print_direction(x, y + 1);
break;
}
putchar(sign);
}
int main()
{
std::cin >> n >> m >> a >> b >> c >> d;
std::string s;
for (int i = 0; i < n; i++) {
std::cin >> s;
for (int j = 0; j < m; j++) {
/* is field */
if (s[j] == '.') map[i][j] = 1;
/* is forest */
if (s[j] == 'W') map[i][j] = 2;
}
}
dijkstra(a - 1, b - 1);
if (w[c - 1][d - 1] == infty) {
std::cout << -1 << std::endl;
} else {
std::cout << w[c - 1][d - 1] << std::endl;
print_direction(c - 1, d - 1);
std::cout << std::endl;
}
return 0;
}