#include <bits/stdc++.h>
using namespace std;
class Q
{
public:
int row, col, nasoka, cost;
Q(int r, int c, int n, int co):row(r), col(c), nasoka(n), cost(co){}
bool operator<(Q b) const
{
return cost>b.cost;
}
};
void add(priority_queue<Q>&q, int row, int col, int nasoka, int cost)
{
if(nasoka==0)
{
q.push(Q(row-1, col, 0, cost));
q.push(Q(row, col+1, 1, cost+1));
q.push(Q(row+1, col, 2, cost+2));
q.push(Q(row, col-1, 3, cost+3));
}
if(nasoka==1)
{
q.push(Q(row, col+1, 1, cost));
q.push(Q(row+1, col, 2, cost+1));
q.push(Q(row, col-1, 3, cost+2));
q.push(Q(row-1, col, 0, cost+3));
}
if(nasoka==2)
{
q.push(Q(row+1, col, 2, cost));
q.push(Q(row, col-1, 3, cost+1));
q.push(Q(row-1, col, 0, cost+2));
q.push(Q(row, col+1, 1, cost+3));
}
if(nasoka==3)
{
q.push(Q(row, col-1, 3, cost));
q.push(Q(row-1, col, 0, cost+1));
q.push(Q(row, col+1, 1, cost+2));
q.push(Q(row+1, col, 2, cost+3));
}
}
int main()
{
int n, m;
cin>>n>>m;
int a[n][m];
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
char aa;
cin>>aa;
if(aa=='N')
a[i][j]=0;
if(aa=='E')
a[i][j]=1;
if(aa=='S')
a[i][j]=2;
if(aa=='W')
a[i][j]=3;
if(aa=='X')
a[i][j]=-1;
}
}
if(a[0][0]==-1)
{
cout<<-1;
return 0;
}
priority_queue<Q>q;
q.push(Q(0, 0, a[0][0], 0));
int visited[n][m];
memset(visited, -1, sizeof(visited));
while(!q.empty())
{
Q w=q.top();
q.pop();
if(w.row>=0 && w.row<n && w.col>=0 && w.col<m)
{
if(visited[w.row][w.col]!=-1)
continue;
visited[w.row][w.col]=w.cost;
if(a[w.row][w.col]==-1)
continue;
add(q, w.row, w.col, a[w.row][w.col], w.cost);
}
}
cout<<visited[n-1][m-1];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 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 |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 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 |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
5 ms |
604 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
6 ms |
532 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
58 ms |
2504 KB |
Output is correct |
40 |
Correct |
59 ms |
2396 KB |
Output is correct |
41 |
Correct |
9 ms |
2140 KB |
Output is correct |
42 |
Correct |
66 ms |
2428 KB |
Output is correct |
43 |
Correct |
67 ms |
19396 KB |
Output is correct |
44 |
Correct |
10 ms |
1368 KB |
Output is correct |