#include <bits/stdc++.h>
#define nah_id_win ios_base::sync_with_stdio(0); cin.tie(0)
#define ull unsigned long long
#define int long long
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define fi first
#define se second
#define pii pair<int,int>
#define all(x) (x).begin(), (x).end()
using namespace std;
const int MOD = 1e9 + 7;
int dirx[4] = {0, -1, 0, 1};
int diry[4] = {-1, 0, 1, 0};
int dir[4] = {'W', 'N', 'E', 'S'};
map <char,int> mp = {
{'W', 0},
{'N', 1},
{'E', 2},
{'S', 3},
};
void with_this_treasure_i_summon(string name){
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
signed main(){
nah_id_win;
int n, m; cin >> n >> m;
vector<vector<char>> grid(n, vector<char>(m));
vector<vector<int>> dist(n, vector<int>(m, 1e9));
vector<vector<int>> vis(n, vector<int>(m));
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
cin >> grid[i][j];
}
}
priority_queue<pair<int, pii>> pq;
dist[0][0] = 0;
pq.push({dist[0][0], {0, 0}});
while (!pq.empty()){
int d = pq.top().fi;
int ui = pq.top().se.fi;
int uj = pq.top().se.se;
pq.pop();
if (!vis[ui][uj]){
vis[ui][uj] = 1;
if (grid[ui][uj] == 'X') continue;
for (int i = 0; i < 4; i++){
int vi = ui + dirx[i];
int vj = uj + diry[i];
if (vi >= 0 && vi < n && vj >= 0 && vj < m){
int oriDir = mp[grid[ui][uj]];
int curDir = mp[dir[i]];
int cost = ((oriDir <= curDir)?
curDir - oriDir : curDir+4 - oriDir);
if (dist[ui][uj] + cost < dist[vi][vj]){
dist[vi][vj] = dist[ui][uj] + cost;
pq.push({dist[vi][vj], {vi, vj}});
}
}
}
}
}
// for (int i = 0; i < n; i++){
// for (int j = 0; j < m; j++){
// cout << dist[i][j] << " ";
// }
// cout << "\n";
// }
cout << ((dist[n-1][m-1] == 1e9)? -1 : dist[n-1][m-1]) << "\n";
}
Compilation message
adventure.cpp: In function 'int main()':
adventure.cpp:48:7: warning: unused variable 'd' [-Wunused-variable]
48 | int d = pq.top().fi;
| ^
adventure.cpp: In function 'void with_this_treasure_i_summon(std::string)':
adventure.cpp:27:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | freopen((name + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
adventure.cpp:28:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
28 | freopen((name + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 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 |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 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 |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |