#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <cassert>
#include <string>
#include <cstring>
#include <bitset>
#include <random>
#include <chrono>
#define FOR(i, a, b) for(int i = a; i < (int) b; i++)
#define F0R(i, a) FOR (i, 0, a)
#define ROF(i, a, b) for(int i = a; i >= (int) b; i--)
#define R0F(i, a) ROF(i, a, 0)
#define GO(i, a) for (auto i : a)
#define rsz resize
#define eb emplace_back
#define pb push_back
#define sz(x) (int) x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define f first
#define s second
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef vector<vpii> vvpii;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef vector<pi64> vpi64;
/*
const int dr[] = {+1, -1, +0, +0, +1, -1, +1, -1};
const int dc[] = {+0, +0, +1, -1, +1, -1, -1, +1};
*/
const int ms[] = {+31, +29, +31, 30, +31, +30, +31, +31, +30, +31, +30, +31};
const int dr[] = {-1, +0, +1, +0};
const int dc[] = {+0, +1, +0, -1};
const char dir[] = {'N', 'E', 'S', 'W'};
const int N = 5e2 + 5;
char g [N][N];
int dist [N][N];
int main () {
ios::sync_with_stdio(0);
cin.tie(0);
int r, c;
cin >> r >> c;
if (r == 1 && c == 1) {
cout << 0 << '\n';
return 0;
}
F0R (i, r) F0R (j, c) cin >> g[i][j];
F0R (i, r) F0R (j, c) dist[i][j] = 1e9;
dist[0][0] = 0;
priority_queue < pair<int, pii> , vector<pair<int, pii> >, greater < pair<int, pii> > > pq;
pq.push({0, {0, 0}});
auto Get = [&] (char cur) {
F0R (i, 4) if (dir[i] == cur) return i;
return -1;
};
while (!pq.empty()) {
int cost = pq.top().f;
int linha = pq.top().s.f;
int coluna = pq.top().s.s;
pq.pop();
if (cost > dist[linha][coluna]) continue;
int cur = Get (g[linha][coluna]);
if (cur == -1) continue;
F0R (i, 4) {
if (linha + dr[i] < 0 || coluna + dc[i] < 0 || linha + dr[i] >= r || coluna + dc[i] >= c) continue;
int w = 0;
while ((cur + w) % 4 != i) ++w;
if (dist[linha + dr[i]][coluna + dc[i]] > dist[linha][coluna] + w) {
dist[linha + dr[i]][coluna + dc[i]] = dist[linha][coluna] + w;
pq.push({dist[linha + dr[i]][coluna + dc[i]], {linha + dr[i], coluna + dc[i]}});
}
}
}
cout << dist[r - 1][c - 1] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Incorrect |
4 ms |
384 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |