#include <bits/stdc++.h>
#define INF 100000000000000000LL
using namespace std;
int tabla[550][550];
long long d[250010];
vector<int> g[250010], w[250010];
bool visited[250010];
int n, m;
string s;
void dijkstra()
{
for (int i=0; i<n*m; i++) d[i]=-INF;
d[0]=0;
for (int i=0; i<n*m; i++) visited[i]=false;
priority_queue<pair<int, int>> pq;
pq.push({0, 0});
while (!pq.empty())
{
int t=-pq.top().first; int u=pq.top().second; pq.pop();
if (visited[u]) continue;
d[u]=t;
visited[u]=true;
for (int i=0; i<g[u].size(); i++) pq.push({-t-w[u][i], g[u][i]});
}
}
int dist(int x, int y)
{
return (y+4-x)%4;
}
int main()
{
cin>>n>>m;
for (int i=0; i<n; i++)
{
cin>>s;
for (int j=0; j<m; j++)
{
if (s[j]=='N')
tabla[i][j]=1;
else if (s[j]=='E')
tabla[i][j]=2;
else if (s[j]=='S')
tabla[i][j]=3;
else if (s[j]=='W')
tabla[i][j]=4;
else
tabla[i][j]=-1;
}
}
for (int i=0; i<n; i++)
{
for (int j=0; j<m; j++)
{
if (tabla[i][j]==-1) continue;
int pos=i*m+j;
if (i==0 && j==0)
{
g[pos].push_back(i*m+j+1);
w[pos].push_back(dist(tabla[i][j], 2));
g[pos].push_back((i+1)*m+j);
w[pos].push_back(dist(tabla[i][j], 3));
}
else if (i==n-1 && j==m-1)
{
g[pos].push_back(i*m+j-1);
w[pos].push_back(dist(tabla[i][j], 4));
g[pos].push_back((i-1)*m+j);
w[pos].push_back(dist(tabla[i][j], 1));
}
else if (i==0 && j==m-1)
{
g[pos].push_back(i*n+j-1);
w[pos].push_back(dist(tabla[i][j], 4));
g[pos].push_back((i+1)*m+j);
w[pos].push_back(dist(tabla[i][j], 3));
}
else if (j==0 && i==n-1)
{
g[pos].push_back(i*m+j+1);
w[pos].push_back(dist(tabla[i][j], 2));
g[pos].push_back((i-1)*m+j);
w[pos].push_back(dist(tabla[i][j], 1));
}
else if (i==0)
{
g[pos].push_back(i*m+j+1);
w[pos].push_back(dist(tabla[i][j], 2));
g[pos].push_back(i*m+j-1);
w[pos].push_back(dist(tabla[i][j], 4));
g[pos].push_back((i+1)*m+j);
w[pos].push_back(dist(tabla[i][j], 3));
}
else if (j==0)
{
g[pos].push_back(i*m+j+1);
w[pos].push_back(dist(tabla[i][j], 2));
g[pos].push_back((i-1)*m+j);
w[pos].push_back(dist(tabla[i][j], 1));
g[pos].push_back((i+1)*m+j);
w[pos].push_back(dist(tabla[i][j], 3));
}
else if (i==n-1)
{
g[pos].push_back(i*m+j+1);
w[pos].push_back(dist(tabla[i][j], 2));
g[pos].push_back(i*m+j-1);
w[pos].push_back(dist(tabla[i][j], 4));
g[pos].push_back((i-1)*m+j);
w[pos].push_back(dist(tabla[i][j], 1));
}
else if (j==m-1)
{
g[pos].push_back(i*m+j-1);
w[pos].push_back(dist(tabla[i][j], 4));
g[pos].push_back((i-1)*m+j);
w[pos].push_back(dist(tabla[i][j], 1));
g[pos].push_back((i+1)*m+j);
w[pos].push_back(dist(tabla[i][j], 3));
}
else
{
g[pos].push_back(i*m+j-1);
w[pos].push_back(dist(tabla[i][j], 4));
g[pos].push_back(i*m+j+1);
w[pos].push_back(dist(tabla[i][j], 2));
g[pos].push_back((i-1)*m+j);
w[pos].push_back(dist(tabla[i][j], 1));
g[pos].push_back((i+1)*m+j);
w[pos].push_back(dist(tabla[i][j], 3));
}
}
}
dijkstra();
/*for (int i=0; i<n; i++)
{
for (int j=0; j<m; j++)
cout<<d[m*i+j]<<" ";
cout<<endl;
}*/
if (d[n*m-1]==-INF) cout<<-1;
else cout<<d[n*m-1];
}
/// FAKESOLVE
/*for (int i=0; i<549; i++)
{
for (int j=0; j<549; j++) dp[i][j]=INF;
}
cin>>n>>m;
for (int i=0; i<n; i++)
{
string s;
cin>>s;
for (int j=0; j<m; j++)
{
if (s[j]=='N')
matra[i][j]=0;
else if (s[j]=='E')
matra[i][j]=1;
else if (s[j]=='S')
matra[i][j]=2;
else if (s[j]=='W')
matra[i][j]=3;
else
matra[i][j]=-1;
}
}
for (int i=0; i<n; i++)
{
for (int j=0; j<m; j++)
{
if (i==0 && j==0) dp[i][j]=0;
else if (i==0)
{
if (matra[i][j-1]==-1) dp[i][j]=INF;
else dp[i][j]=dp[i][j-1]+(5-matra[i][j-1])%4;
}
else if (j==0)
{
if (matra[i-1][j]==-1) dp[i][j]=INF;
else dp[i][j]=dp[i-1][j]+(6-matra[i-1][j])%4;
}
else
{
if (matra[i-1][j]==-1) dp[i][j]=INF;
else dp[i][j]=dp[i-1][j]+(6-matra[i-1][j])%4;
if (matra[i][j-1]!=-1) dp[i][j]=min(dp[i][j], dp[i][j-1]+(6-matra[i][j-1])%4);
}
}
}
for (int i=0; i<n; i++)
{
for (int j=0; j<m; j++)
cout<<dp
}
cout<<dp[n-1][m-1];*/
Compilation message
adventure.cpp: In function 'void dijkstra()':
adventure.cpp:28:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | for (int i=0; i<g[u].size(); i++) pq.push({-t-w[u][i], g[u][i]});
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12032 KB |
Output is correct |
2 |
Correct |
8 ms |
12032 KB |
Output is correct |
3 |
Correct |
9 ms |
12160 KB |
Output is correct |
4 |
Correct |
9 ms |
12160 KB |
Output is correct |
5 |
Correct |
9 ms |
12160 KB |
Output is correct |
6 |
Correct |
8 ms |
12032 KB |
Output is correct |
7 |
Correct |
8 ms |
12032 KB |
Output is correct |
8 |
Correct |
9 ms |
12160 KB |
Output is correct |
9 |
Correct |
8 ms |
12160 KB |
Output is correct |
10 |
Correct |
8 ms |
12032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12032 KB |
Output is correct |
2 |
Correct |
8 ms |
12032 KB |
Output is correct |
3 |
Correct |
9 ms |
12160 KB |
Output is correct |
4 |
Correct |
9 ms |
12160 KB |
Output is correct |
5 |
Correct |
9 ms |
12160 KB |
Output is correct |
6 |
Correct |
8 ms |
12032 KB |
Output is correct |
7 |
Correct |
8 ms |
12032 KB |
Output is correct |
8 |
Correct |
9 ms |
12160 KB |
Output is correct |
9 |
Correct |
8 ms |
12160 KB |
Output is correct |
10 |
Correct |
8 ms |
12032 KB |
Output is correct |
11 |
Correct |
8 ms |
12032 KB |
Output is correct |
12 |
Correct |
8 ms |
12160 KB |
Output is correct |
13 |
Correct |
9 ms |
12160 KB |
Output is correct |
14 |
Correct |
9 ms |
12032 KB |
Output is correct |
15 |
Correct |
8 ms |
12032 KB |
Output is correct |
16 |
Correct |
8 ms |
12160 KB |
Output is correct |
17 |
Correct |
9 ms |
12160 KB |
Output is correct |
18 |
Correct |
8 ms |
12032 KB |
Output is correct |
19 |
Correct |
8 ms |
12032 KB |
Output is correct |
20 |
Correct |
8 ms |
12160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12032 KB |
Output is correct |
2 |
Correct |
8 ms |
12032 KB |
Output is correct |
3 |
Correct |
8 ms |
12032 KB |
Output is correct |
4 |
Correct |
8 ms |
12032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12160 KB |
Output is correct |
2 |
Correct |
8 ms |
12032 KB |
Output is correct |
3 |
Correct |
8 ms |
12160 KB |
Output is correct |
4 |
Correct |
9 ms |
12160 KB |
Output is correct |
5 |
Correct |
9 ms |
12160 KB |
Output is correct |
6 |
Correct |
8 ms |
12160 KB |
Output is correct |
7 |
Correct |
8 ms |
12160 KB |
Output is correct |
8 |
Correct |
8 ms |
12032 KB |
Output is correct |
9 |
Correct |
8 ms |
12032 KB |
Output is correct |
10 |
Correct |
8 ms |
12032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12032 KB |
Output is correct |
2 |
Correct |
8 ms |
12032 KB |
Output is correct |
3 |
Correct |
9 ms |
12160 KB |
Output is correct |
4 |
Correct |
9 ms |
12160 KB |
Output is correct |
5 |
Correct |
9 ms |
12160 KB |
Output is correct |
6 |
Correct |
8 ms |
12032 KB |
Output is correct |
7 |
Correct |
8 ms |
12032 KB |
Output is correct |
8 |
Correct |
9 ms |
12160 KB |
Output is correct |
9 |
Correct |
8 ms |
12160 KB |
Output is correct |
10 |
Correct |
8 ms |
12032 KB |
Output is correct |
11 |
Correct |
8 ms |
12032 KB |
Output is correct |
12 |
Correct |
8 ms |
12160 KB |
Output is correct |
13 |
Correct |
9 ms |
12160 KB |
Output is correct |
14 |
Correct |
9 ms |
12032 KB |
Output is correct |
15 |
Correct |
8 ms |
12032 KB |
Output is correct |
16 |
Correct |
8 ms |
12160 KB |
Output is correct |
17 |
Correct |
9 ms |
12160 KB |
Output is correct |
18 |
Correct |
8 ms |
12032 KB |
Output is correct |
19 |
Correct |
8 ms |
12032 KB |
Output is correct |
20 |
Correct |
8 ms |
12160 KB |
Output is correct |
21 |
Correct |
8 ms |
12032 KB |
Output is correct |
22 |
Correct |
8 ms |
12032 KB |
Output is correct |
23 |
Correct |
8 ms |
12032 KB |
Output is correct |
24 |
Correct |
8 ms |
12032 KB |
Output is correct |
25 |
Correct |
8 ms |
12160 KB |
Output is correct |
26 |
Correct |
8 ms |
12032 KB |
Output is correct |
27 |
Correct |
8 ms |
12160 KB |
Output is correct |
28 |
Correct |
9 ms |
12160 KB |
Output is correct |
29 |
Correct |
9 ms |
12160 KB |
Output is correct |
30 |
Correct |
8 ms |
12160 KB |
Output is correct |
31 |
Correct |
8 ms |
12160 KB |
Output is correct |
32 |
Correct |
8 ms |
12032 KB |
Output is correct |
33 |
Correct |
8 ms |
12032 KB |
Output is correct |
34 |
Correct |
8 ms |
12032 KB |
Output is correct |
35 |
Correct |
22 ms |
13568 KB |
Output is correct |
36 |
Correct |
10 ms |
12160 KB |
Output is correct |
37 |
Correct |
26 ms |
13952 KB |
Output is correct |
38 |
Correct |
23 ms |
15104 KB |
Output is correct |
39 |
Correct |
196 ms |
28152 KB |
Output is correct |
40 |
Correct |
192 ms |
28024 KB |
Output is correct |
41 |
Correct |
86 ms |
28024 KB |
Output is correct |
42 |
Correct |
195 ms |
28024 KB |
Output is correct |
43 |
Correct |
253 ms |
39396 KB |
Output is correct |
44 |
Correct |
86 ms |
28024 KB |
Output is correct |