#include <iostream>
#include <queue>
#include <fstream>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <utility>
#include <stack>
#include <math.h>
#include <climits>
#include <iomanip>
#include <unordered_map>
using namespace std;
#define ll long long
#define ld long double
#define ull unsigned long long
#define ub upper_bound
#define lb lower_bound
#define ff first
#define ss second
#define mpr make_pair
#define vi vector<int>
#define vll vector<ll>
#define pii pair<int,int>
#define vpi vector<pii>
#define pb push_back
#define pob pop_back
#define mii map<int,int>
#define vpl vector<pair<ll, ll>>
#define pll pair<ll,ll>
#define all(v) v.begin(),v.end()
#define sz(x) x.size()
#define clr(x) x.clear()
#define yes cout << "YES\n"
#define no cout << "NO\n"
//ifstream cin("bcount.in");
//ofstream cout("bcount.out");
void fastio() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
void setprecision(int x) {
cout.setf(ios::fixed | ios::showpoint);
cout.precision(x);
}
/*void setIO(string name = "") {
if ((int)name.size() > 0) {
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
}*/
ll gcd(ll a, ll b) {
if (a > b) {
swap(a, b);
}
if (a == 0) {
return b;
}
if (a == b) {
return a;
}
return gcd(b - a, a);
}
ll num(ll n) {
ll ans = 0;
while (n != 0) {
ans++;
n /= 10;
}
return ans;
}
ll lcm(ll a, ll b) {
return (a * b) / gcd(a, b);
}
ll MOD = 1e9 + 7;
ll MOD2 = 998244353;
ll factorial(ll n) {
ll ans = 1;
for (int i = 1; i <= n; i++) {
ans *= i;
ans %= MOD2;
}
return ans;
}
ll fact2(ll n, ll k) {
ll ans = 1;
for (int i = 1; i <= n; i++) {
if (i == k) {
ans *= (k - 1);
}
else {
ans *= i;
}
ans %= MOD2;
}
return ans;
}
bool isPrime(int n)
{
if (n <= 1) {
return false;
}
if (n == 2) {
return true;
}
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
ll bpow_mod(ll a, ll b, ll mod)
{
ll ans = 1;
while (b) {
if ((b & 1) == 1) {
ans *= a;
ans %= mod;
}
b >>= 1;
a *= a;
a %= mod;
}
return ans;
}
bool sortbysec(const pair<int, int>& a,
const pair<int, int>& b)
{
return (a.second < b.second);
}
bool ask(vector<int> vertices) {
cout << "? " << vertices.size();
for(int x: vertices) {
cout << " " << x;
}
cout << endl << flush;
string answer;
cin >> answer;
return (answer == "YES");
}
void precalc(){
return;
}
const int N = 1e6 + 10;
char c[502][502];
vpi g[N];
int m, n;
int dist[N];
bool check(int i, int j){
if(i <= m && i >= 1 && j <= n && j >= 1){
return true;
}
return false;
}
int f(int i, int j){
return (i - 1) * n + j;
}
void solve() {
cin >> m >> n;
for(int i = 1; i <= m; ++i){
for(int j = 1; j <= n; ++j){
cin >> c[i][j];
}
}
for(int i = 1; i <= m; ++i){
for(int j = 1; j <= n; ++j){
dist[f(i, j)] = 1e9;
if(c[i][j] == 'E'){
if(check(i, j + 1)){
g[f(i, j)].pb({f(i, j + 1), 0});
}
if(check(i + 1, j)){
g[f(i, j)].pb({f(i + 1, j), 1});
}
if(check(i, j - 1)){
g[f(i, j)].pb({f(i, j - 1), 2});
}
if(check(i - 1, j)){
g[f(i, j)].pb({f(i - 1, j), 3});
}
}
if(c[i][j] == 'S'){
if(check(i, j + 1)){
g[f(i, j)].pb({f(i, j + 1), 3});
}
if(check(i + 1, j)){
g[f(i, j)].pb({f(i + 1, j), 0});
}
if(check(i, j - 1)){
g[f(i, j)].pb({f(i, j - 1), 1});
}
if(check(i - 1, j)){
g[f(i, j)].pb({f(i - 1, j), 2});
}
}
if(c[i][j] == 'W'){
if(check(i, j + 1)){
g[f(i, j)].pb({f(i, j + 1), 2});
}
if(check(i + 1, j)){
g[f(i, j)].pb({f(i + 1, j), 3});
}
if(check(i, j - 1)){
g[f(i, j)].pb({f(i, j - 1), 0});
}
if(check(i - 1, j)){
g[f(i, j)].pb({f(i - 1, j), 1});
}
}
if(c[i][j] == 'N'){
if(check(i, j + 1)){
g[f(i, j)].pb({f(i, j + 1), 1});
}
if(check(i + 1, j)){
g[f(i, j)].pb({f(i + 1, j), 2});
}
if(check(i, j - 1)){
g[f(i, j)].pb({f(i, j - 1), 3});
}
if(check(i - 1, j)){
g[f(i, j)].pb({f(i - 1, j), 0});
}
}
// cout << sz(g[f(i, j)]) << ' ' << i << ' ' << j << endl;
}
}
// cout << sz(g[5]) << endl;
priority_queue<pii>pq;
pq.push({0, 1});
dist[1] = 0;
while(!pq.empty()){
int k = -pq.top().ff;
int u = pq.top().ss;
pq.pop();
if(k > dist[u]){
continue;
}
for(auto i : g[u]){
if(dist[i.ff] > i.ss + dist[u]){
dist[i.ff] = i.ss + dist[u];
pq.push({-dist[i.ff], i.ff});
}
}
}
int v = n * m;
if(dist[n * m] == 1e9){
cout << -1 << endl;return;
}
cout << dist[n * m] << endl;
}
void cases() {
int t;
cin >> t;
while (t--) {
solve();
}
}
int main() {
fastio();
precalc();
//cases();
solve();
return 0;
}
Compilation message
adventure.cpp: In function 'void solve()':
adventure.cpp:269:9: warning: unused variable 'v' [-Wunused-variable]
269 | int v = n * m;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
23900 KB |
Output is correct |
2 |
Correct |
10 ms |
25432 KB |
Output is correct |
3 |
Correct |
12 ms |
25436 KB |
Output is correct |
4 |
Correct |
13 ms |
25436 KB |
Output is correct |
5 |
Correct |
11 ms |
23900 KB |
Output is correct |
6 |
Correct |
10 ms |
25436 KB |
Output is correct |
7 |
Correct |
11 ms |
23900 KB |
Output is correct |
8 |
Correct |
11 ms |
25640 KB |
Output is correct |
9 |
Correct |
17 ms |
25432 KB |
Output is correct |
10 |
Correct |
10 ms |
25436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
23900 KB |
Output is correct |
2 |
Correct |
10 ms |
25432 KB |
Output is correct |
3 |
Correct |
12 ms |
25436 KB |
Output is correct |
4 |
Correct |
13 ms |
25436 KB |
Output is correct |
5 |
Correct |
11 ms |
23900 KB |
Output is correct |
6 |
Correct |
10 ms |
25436 KB |
Output is correct |
7 |
Correct |
11 ms |
23900 KB |
Output is correct |
8 |
Correct |
11 ms |
25640 KB |
Output is correct |
9 |
Correct |
17 ms |
25432 KB |
Output is correct |
10 |
Correct |
10 ms |
25436 KB |
Output is correct |
11 |
Correct |
10 ms |
25432 KB |
Output is correct |
12 |
Correct |
13 ms |
25600 KB |
Output is correct |
13 |
Correct |
11 ms |
25428 KB |
Output is correct |
14 |
Correct |
10 ms |
25436 KB |
Output is correct |
15 |
Correct |
11 ms |
23900 KB |
Output is correct |
16 |
Correct |
12 ms |
25432 KB |
Output is correct |
17 |
Correct |
11 ms |
23900 KB |
Output is correct |
18 |
Correct |
11 ms |
25436 KB |
Output is correct |
19 |
Correct |
13 ms |
23848 KB |
Output is correct |
20 |
Correct |
11 ms |
23900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
25436 KB |
Output is correct |
2 |
Correct |
9 ms |
23728 KB |
Output is correct |
3 |
Correct |
9 ms |
25436 KB |
Output is correct |
4 |
Correct |
9 ms |
25424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23900 KB |
Output is correct |
2 |
Correct |
12 ms |
25528 KB |
Output is correct |
3 |
Correct |
11 ms |
23900 KB |
Output is correct |
4 |
Correct |
13 ms |
25436 KB |
Output is correct |
5 |
Correct |
10 ms |
25436 KB |
Output is correct |
6 |
Correct |
11 ms |
25432 KB |
Output is correct |
7 |
Correct |
12 ms |
25436 KB |
Output is correct |
8 |
Correct |
11 ms |
25436 KB |
Output is correct |
9 |
Correct |
10 ms |
25436 KB |
Output is correct |
10 |
Correct |
10 ms |
25512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
23900 KB |
Output is correct |
2 |
Correct |
10 ms |
25432 KB |
Output is correct |
3 |
Correct |
12 ms |
25436 KB |
Output is correct |
4 |
Correct |
13 ms |
25436 KB |
Output is correct |
5 |
Correct |
11 ms |
23900 KB |
Output is correct |
6 |
Correct |
10 ms |
25436 KB |
Output is correct |
7 |
Correct |
11 ms |
23900 KB |
Output is correct |
8 |
Correct |
11 ms |
25640 KB |
Output is correct |
9 |
Correct |
17 ms |
25432 KB |
Output is correct |
10 |
Correct |
10 ms |
25436 KB |
Output is correct |
11 |
Correct |
10 ms |
25432 KB |
Output is correct |
12 |
Correct |
13 ms |
25600 KB |
Output is correct |
13 |
Correct |
11 ms |
25428 KB |
Output is correct |
14 |
Correct |
10 ms |
25436 KB |
Output is correct |
15 |
Correct |
11 ms |
23900 KB |
Output is correct |
16 |
Correct |
12 ms |
25432 KB |
Output is correct |
17 |
Correct |
11 ms |
23900 KB |
Output is correct |
18 |
Correct |
11 ms |
25436 KB |
Output is correct |
19 |
Correct |
13 ms |
23848 KB |
Output is correct |
20 |
Correct |
11 ms |
23900 KB |
Output is correct |
21 |
Correct |
10 ms |
25436 KB |
Output is correct |
22 |
Correct |
9 ms |
23728 KB |
Output is correct |
23 |
Correct |
9 ms |
25436 KB |
Output is correct |
24 |
Correct |
9 ms |
25424 KB |
Output is correct |
25 |
Correct |
11 ms |
23900 KB |
Output is correct |
26 |
Correct |
12 ms |
25528 KB |
Output is correct |
27 |
Correct |
11 ms |
23900 KB |
Output is correct |
28 |
Correct |
13 ms |
25436 KB |
Output is correct |
29 |
Correct |
10 ms |
25436 KB |
Output is correct |
30 |
Correct |
11 ms |
25432 KB |
Output is correct |
31 |
Correct |
12 ms |
25436 KB |
Output is correct |
32 |
Correct |
11 ms |
25436 KB |
Output is correct |
33 |
Correct |
10 ms |
25436 KB |
Output is correct |
34 |
Correct |
10 ms |
25512 KB |
Output is correct |
35 |
Correct |
18 ms |
26460 KB |
Output is correct |
36 |
Correct |
11 ms |
23812 KB |
Output is correct |
37 |
Correct |
17 ms |
24924 KB |
Output is correct |
38 |
Correct |
15 ms |
27228 KB |
Output is correct |
39 |
Correct |
77 ms |
36288 KB |
Output is correct |
40 |
Correct |
78 ms |
36084 KB |
Output is correct |
41 |
Correct |
39 ms |
36160 KB |
Output is correct |
42 |
Correct |
88 ms |
34820 KB |
Output is correct |
43 |
Correct |
114 ms |
40568 KB |
Output is correct |
44 |
Correct |
35 ms |
36180 KB |
Output is correct |