#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 = 4e6 + 10;
char c[502][502];
vpi g[N];
int m, n;
int dist[N];
int par[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:270:9: warning: unused variable 'v' [-Wunused-variable]
270 | int v = n * m;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
94552 KB |
Output is correct |
2 |
Correct |
42 ms |
94544 KB |
Output is correct |
3 |
Correct |
41 ms |
94548 KB |
Output is correct |
4 |
Correct |
40 ms |
94544 KB |
Output is correct |
5 |
Correct |
42 ms |
94548 KB |
Output is correct |
6 |
Correct |
43 ms |
94548 KB |
Output is correct |
7 |
Correct |
42 ms |
94552 KB |
Output is correct |
8 |
Correct |
42 ms |
94556 KB |
Output is correct |
9 |
Correct |
41 ms |
94756 KB |
Output is correct |
10 |
Correct |
40 ms |
94512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
94552 KB |
Output is correct |
2 |
Correct |
42 ms |
94544 KB |
Output is correct |
3 |
Correct |
41 ms |
94548 KB |
Output is correct |
4 |
Correct |
40 ms |
94544 KB |
Output is correct |
5 |
Correct |
42 ms |
94548 KB |
Output is correct |
6 |
Correct |
43 ms |
94548 KB |
Output is correct |
7 |
Correct |
42 ms |
94552 KB |
Output is correct |
8 |
Correct |
42 ms |
94556 KB |
Output is correct |
9 |
Correct |
41 ms |
94756 KB |
Output is correct |
10 |
Correct |
40 ms |
94512 KB |
Output is correct |
11 |
Correct |
38 ms |
94548 KB |
Output is correct |
12 |
Correct |
42 ms |
94548 KB |
Output is correct |
13 |
Correct |
46 ms |
94800 KB |
Output is correct |
14 |
Correct |
41 ms |
94548 KB |
Output is correct |
15 |
Correct |
42 ms |
94552 KB |
Output is correct |
16 |
Correct |
44 ms |
94556 KB |
Output is correct |
17 |
Correct |
43 ms |
94640 KB |
Output is correct |
18 |
Correct |
41 ms |
94576 KB |
Output is correct |
19 |
Correct |
42 ms |
94548 KB |
Output is correct |
20 |
Correct |
40 ms |
94556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
94548 KB |
Output is correct |
2 |
Correct |
43 ms |
94544 KB |
Output is correct |
3 |
Correct |
42 ms |
94544 KB |
Output is correct |
4 |
Correct |
46 ms |
94800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
94556 KB |
Output is correct |
2 |
Correct |
53 ms |
94548 KB |
Output is correct |
3 |
Correct |
41 ms |
94548 KB |
Output is correct |
4 |
Correct |
42 ms |
94572 KB |
Output is correct |
5 |
Correct |
41 ms |
94548 KB |
Output is correct |
6 |
Correct |
40 ms |
94548 KB |
Output is correct |
7 |
Correct |
43 ms |
94548 KB |
Output is correct |
8 |
Correct |
41 ms |
94576 KB |
Output is correct |
9 |
Correct |
40 ms |
94556 KB |
Output is correct |
10 |
Correct |
41 ms |
94712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
94552 KB |
Output is correct |
2 |
Correct |
42 ms |
94544 KB |
Output is correct |
3 |
Correct |
41 ms |
94548 KB |
Output is correct |
4 |
Correct |
40 ms |
94544 KB |
Output is correct |
5 |
Correct |
42 ms |
94548 KB |
Output is correct |
6 |
Correct |
43 ms |
94548 KB |
Output is correct |
7 |
Correct |
42 ms |
94552 KB |
Output is correct |
8 |
Correct |
42 ms |
94556 KB |
Output is correct |
9 |
Correct |
41 ms |
94756 KB |
Output is correct |
10 |
Correct |
40 ms |
94512 KB |
Output is correct |
11 |
Correct |
38 ms |
94548 KB |
Output is correct |
12 |
Correct |
42 ms |
94548 KB |
Output is correct |
13 |
Correct |
46 ms |
94800 KB |
Output is correct |
14 |
Correct |
41 ms |
94548 KB |
Output is correct |
15 |
Correct |
42 ms |
94552 KB |
Output is correct |
16 |
Correct |
44 ms |
94556 KB |
Output is correct |
17 |
Correct |
43 ms |
94640 KB |
Output is correct |
18 |
Correct |
41 ms |
94576 KB |
Output is correct |
19 |
Correct |
42 ms |
94548 KB |
Output is correct |
20 |
Correct |
40 ms |
94556 KB |
Output is correct |
21 |
Correct |
40 ms |
94548 KB |
Output is correct |
22 |
Correct |
43 ms |
94544 KB |
Output is correct |
23 |
Correct |
42 ms |
94544 KB |
Output is correct |
24 |
Correct |
46 ms |
94800 KB |
Output is correct |
25 |
Correct |
39 ms |
94556 KB |
Output is correct |
26 |
Correct |
53 ms |
94548 KB |
Output is correct |
27 |
Correct |
41 ms |
94548 KB |
Output is correct |
28 |
Correct |
42 ms |
94572 KB |
Output is correct |
29 |
Correct |
41 ms |
94548 KB |
Output is correct |
30 |
Correct |
40 ms |
94548 KB |
Output is correct |
31 |
Correct |
43 ms |
94548 KB |
Output is correct |
32 |
Correct |
41 ms |
94576 KB |
Output is correct |
33 |
Correct |
40 ms |
94556 KB |
Output is correct |
34 |
Correct |
41 ms |
94712 KB |
Output is correct |
35 |
Correct |
45 ms |
95572 KB |
Output is correct |
36 |
Correct |
41 ms |
94808 KB |
Output is correct |
37 |
Correct |
45 ms |
95824 KB |
Output is correct |
38 |
Correct |
45 ms |
96604 KB |
Output is correct |
39 |
Runtime error |
57 ms |
102400 KB |
Execution killed with signal 9 |
40 |
Halted |
0 ms |
0 KB |
- |