#include <bits/stdc++.h>
#define NMAX 200001
#define int long long
#define MAX 1001
#define pb push_back
#define eb emplace_back
#define MOD 1000000007
#define nl '\n'
#define INF 1000000007
#define LLONG_MAX 9223372036854775807
#define pii pair<int,int>
#define tpl tuple<int,int,int,int>
#pragma GCC optimize("O3")
using namespace std;
ifstream fin("aib.in");
ofstream fout("aib.out");
int n,m;
int mat[501][501];
bool vis[501][501];
int dp[501][501];
bool inmat(int i,int j)
{
return i>=1 && i<=n && j>=1 && j<=m;
}
void run(int i,int j)
{
queue<tpl>q;
for(int d=1;d<=4;++d)
{
int r;
if(d<mat[i][j])
r=4-mat[i][j]+d;
else
r=d-mat[i][j];
q.push({i,j,d,r});
}
while(!q.empty())
{
auto [x,y,dir,rot]=q.front();
q.pop();
dp[x][y]=min(dp[x][y],rot);
int inou=x;
int jnou=y;
if(mat[x][y]==5)
continue;
if(dir==1)
{
inou--;
if(inmat(inou,jnou))
{
for(int d=1;d<=4;++d)
{
int r;
if(d<mat[inou][jnou])
r=4-mat[inou][jnou]+d;
else
r=d-mat[inou][jnou];
if(dp[inou][jnou]>rot+r)
q.push({inou,jnou,d,rot+r});
}
}
}
else if(dir==2)
{
jnou++;
if(inmat(inou,jnou))
{
for(int d=1;d<=4;++d)
{
int r;
if(d<mat[inou][jnou])
r=4-mat[inou][jnou]+d;
else
r=d-mat[inou][jnou];
if(dp[inou][jnou]>rot+r)
q.push({inou,jnou,d,rot+r});
}
}
}
else if(dir==3)
{
inou++;
if(inmat(inou,jnou))
{
for(int d=1;d<=4;++d)
{
int r;
if(d<mat[inou][jnou])
r=4-mat[inou][jnou]+d;
else
r=d-mat[inou][jnou];
if(dp[inou][jnou]>rot+r)
q.push({inou,jnou,d,rot+r});
}
}
}
else
{
jnou--;
if(inmat(inou,jnou))
{
for(int d=1;d<=4;++d)
{
int r;
if(d<mat[inou][jnou])
r=4-mat[inou][jnou]+d;
else
r=d-mat[inou][jnou];
if(dp[inou][jnou]>rot+r)
q.push({inou,jnou,d,rot+r});
}
}
}
}
}
signed main()
{
cin>>n>>m;
for(int i=1;i<=n;++i)
{
for(int j=1;j<=m;++j)
{
dp[i][j]=INF;
char x;
cin>>x;
if(x=='N')
mat[i][j]=1;
else if(x=='E')
mat[i][j]=2;
else if(x=='S')
mat[i][j]=3;
else if(x=='W')
mat[i][j]=4;
else
mat[i][j]=5;
}
}
if(mat[1][1]==5)
{
cout<<-1;
return 0;
}
run(1,1);
if(dp[n][m]==INF)
cout<<-1;
else
cout<<dp[n][m];
return 0;
}
Compilation message
adventure.cpp:10: warning: "LLONG_MAX" redefined
10 | #define LLONG_MAX 9223372036854775807
|
In file included from /usr/lib/gcc/x86_64-linux-gnu/10/include/limits.h:195,
from /usr/lib/gcc/x86_64-linux-gnu/10/include/syslimits.h:7,
from /usr/lib/gcc/x86_64-linux-gnu/10/include/limits.h:34,
from /usr/include/c++/10/climits:42,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:39,
from adventure.cpp:1:
/usr/include/limits.h:135: note: this is the location of the previous definition
135 | # define LLONG_MAX __LONG_LONG_MAX__
|
adventure.cpp: In function 'void run(long long int, long long int)':
adventure.cpp:39:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
39 | auto [x,y,dir,rot]=q.front();
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
380 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
448 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
380 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
448 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Incorrect |
1 ms |
856 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
380 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
448 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Incorrect |
1 ms |
856 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |