#include <iostream>
#include <vector>
#include <set>
using namespace std;
struct Info {
int node;
int cost;
Info() = default;
Info(int node, int cost) {
this->node = node;
this->cost = cost;
}
bool operator<(Info a2) const {
if(cost == a2.cost) {
return node<a2.node;
}
return cost<a2.cost;
}
};
int n,m;
vector<vector<char>> ma;
vector<vector<Info>> graph;
void read() {
cin>>n>>m;
ma.resize(n+1, vector<char>(m+1));
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
cin>>ma[i][j];
}
}
}
void form_graph() {
graph.resize(n*m+1);
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
vector<int> delta;
if(ma[i][j] == 'N') {
delta = {0, 1, 2, 3};
}
else if(ma[i][j] == 'E') {
delta = {3, 0, 1, 2};
}
else if(ma[i][j] == 'S') {
delta = {2, 3, 0, 1};
}
else {
delta = {1, 2, 3, 0};
}
int curr_node = (i-1)*m + j;
int node_dr = (i-1)*m + j + 1;
int node_st = (i-1)*m + j - 1;
int node_jos = i * m + j;
int node_sus = (i-2)*m + j;
if(i-1>0 && ma[i-1][j]!='X') {
graph[curr_node].push_back(*new Info(node_sus, delta[0]));
}
if(j+1<=m && ((i==n && j==m-1) || ma[i][j+1]!='X')) {
graph[curr_node].push_back(*new Info(node_dr, delta[1]));
}
if(i+1<=n && ((i==n-1 && j==m) || ma[i+1][j]!='X')) {
graph[curr_node].push_back(*new Info(node_jos, delta[2]));
}
if(j-1>0 && ma[i][j-1]!='X') {
graph[curr_node].push_back(*new Info(node_st, delta[3]));
}
}
}
}
int dijkstra() {
vector<int> dist(n*m+1, 1e9);
set<Info> st;
st.insert(*new Info(1, 0));
dist[1] = 0;
Info tp;
while(!st.empty()) {
tp = (*st.begin());
st.erase(st.begin());
for(auto it : graph[tp.node]) {
if(dist[it.node] > dist[tp.node] + it.cost) {
dist[it.node] = dist[tp.node] + it.cost;
st.insert(*new Info(it.node, dist[it.node]));
}
}
}
if(dist[n*m]==1e9 || ma[1][1]=='X') {
dist[n*m]=-1;
}
return dist[n*m];
}
void solve() {
form_graph();
cout<<dijkstra();
}
int main() {
read();
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
432 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
432 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
436 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
428 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
432 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
436 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
428 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
10 ms |
5012 KB |
Output is correct |
36 |
Correct |
1 ms |
860 KB |
Output is correct |
37 |
Correct |
11 ms |
5724 KB |
Output is correct |
38 |
Correct |
20 ms |
8796 KB |
Output is correct |
39 |
Correct |
129 ms |
51156 KB |
Output is correct |
40 |
Correct |
128 ms |
51004 KB |
Output is correct |
41 |
Correct |
81 ms |
43604 KB |
Output is correct |
42 |
Correct |
127 ms |
51028 KB |
Output is correct |
43 |
Correct |
182 ms |
77580 KB |
Output is correct |
44 |
Correct |
132 ms |
51056 KB |
Output is correct |