#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()
#define skibidi pair<int, pii>
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<skibidi, vector<skibidi>, greater<skibidi>> 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 << "\n";
}
}
// 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:49:7: warning: unused variable 'd' [-Wunused-variable]
49 | int d = pq.top().fi;
| ^
adventure.cpp: In function 'void with_this_treasure_i_summon(std::string)':
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 + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
adventure.cpp:29:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
29 | freopen((name + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
600 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
600 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
424 KB |
Output is correct |
13 |
Correct |
0 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
500 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
600 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
424 KB |
Output is correct |
13 |
Correct |
0 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
500 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
5 ms |
600 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
6 ms |
860 KB |
Output is correct |
38 |
Correct |
1 ms |
1112 KB |
Output is correct |
39 |
Correct |
57 ms |
4732 KB |
Output is correct |
40 |
Correct |
54 ms |
4724 KB |
Output is correct |
41 |
Correct |
4 ms |
4444 KB |
Output is correct |
42 |
Correct |
60 ms |
4720 KB |
Output is correct |
43 |
Correct |
84 ms |
12092 KB |
Output is correct |
44 |
Correct |
4 ms |
4440 KB |
Output is correct |