#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ar array
#define f first
#define s second
#define mpr make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define FOR(i, a, b) for(auto i = a; i < b; i++)
#define FORD(i, a, b) for(auto i = a - 1; i >= b; i--)
#define trav(x, v) for(auto x : v)
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define fileio freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
// --------------- SOLUTION --------------- //
const int INF = 1e6;
int n, m;
char a[501][501];
int d[501][501];
set <pair<int, pair<int, int>>> s;
char dir[4] = {'W', 'S', 'E', 'N'};
bool valid(int x, int y) {
return (x >= 0 && x < n && y >= 0 && y < m);
}
void solve() {
cin >> n >> m;
FOR(i, 0, n) {
FOR(j, 0, m) {
cin >> a[i][j];
}
}
FOR(i, 0, n) {
FOR(j, 0, m) {
d[i][j] = INF;
}
}
d[0][0] = 0;
s.insert({0, {0, 0}});
while (!s.empty()) {
auto it = s.begin();
int dist = (*it).f;
int x = (*it).s.f;
int y = (*it).s.s;
s.erase(it);
if (a[x][y] == 'X') {
continue;
}
FOR(i, 0, 3) {
int cnt = 0;
int xx = 0, yy = 0;
if (a[x][y] == dir[i]) cnt = 0;
else if (a[x][y] == dir[(i + 1) % 4]) cnt = 1;
else if (a[x][y] == dir[(i + 2) % 4]) cnt = 2;
else cnt = 3;
switch (dir[i]) {
case 'W': xx = 0, yy = -1; break;
case 'S': xx = 1, yy = 0; break;
case 'E': xx = 0, yy = 1; break;
case 'N': xx = -1, yy = 0; break;
}
if (valid(x + xx, y + yy) && d[x + xx][y + yy] > dist + cnt) {
s.erase({d[x + xx][y + yy], {x + xx, y + yy}});
d[x + xx][y + yy] = dist + cnt;
s.insert({d[x + xx][y + yy], {x + xx, y + yy}});
}
}
}
if(d[n - 1][m - 1] != INF)
cout << d[n - 1][m - 1] << endl;
else
cout << -1 << endl;
}
int main() {
fastio;
fileio;
int tests = 1;
// cin >> tests;
while (tests--) {
solve();
}
return 0;
}
Compilation message
adventure.cpp: In function 'int main()':
adventure.cpp:20:23: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
20 | #define fileio freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
adventure.cpp:87:5: note: in expansion of macro 'fileio'
87 | fileio;
| ^~~~~~
adventure.cpp:20:57: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
20 | #define fileio freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
adventure.cpp:87:5: note: in expansion of macro 'fileio'
87 | fileio;
| ^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |