Submission #759712

# Submission time Handle Problem Language Result Execution time Memory
759712 2023-06-16T15:43:31 Z ivaziva Awesome Arrowland Adventure (eJOI19_adventure) C++14
100 / 100
87 ms 32316 KB
#include <bits/stdc++.h>

using namespace std;

#define MAXM 510
#define MAXN 250010

long long n;
long long m;
char matrica[MAXM][MAXM];
vector<pair<long long,long long>> adj[MAXN];
long long dist[MAXN];

void dikstra(long long poc)
{
    for (long long i=0;i<MAXN;i++) dist[i]=LLONG_MAX;
    dist[poc]=0;
    priority_queue<pair<long long,long long>,vector<pair<long long,long long>>,greater<pair<long long,long long>>> pq;
    pq.push({0,poc});
    while (pq.empty()==false)
    {
        long long node0=pq.top().second;
        long long dist0=pq.top().first;
        pq.pop();
        if (dist0!=dist[node0]) continue;
        long long s=adj[node0].size();
        for (long long i=0;i<s;i++)
        {
            long long node=adj[node0][i].first;
            long long distt=adj[node0][i].second;
            if (dist0+distt<dist[node])
            {
                dist[node]=distt+dist0;
                pq.push({dist[node],node});
            }
        }
    }
}

int main()
{
    cin>>n>>m;
    for (long long i=1;i<=n;i++)
    {
        for (long long j=1;j<=m;j++) cin>>matrica[i][j];
    }
    for (long long i=1;i<=n;i++)
    {
        for (long long j=1;j<=m;j++)
        {
            if (i==1)
            {
                if (j==1)
                {
                    if (matrica[i][j]=='E')
                    {
                        adj[(i-1)*m+j].push_back({(i-1)*m+j+1,0});
                        adj[(i-1)*m+j].push_back({(i-1+1)*m+j,1});
                    }
                    if (matrica[i][j]=='S')
                    {
                        adj[(i-1)*m+j].push_back({(i-1)*m+j+1,3});
                        adj[(i-1)*m+j].push_back({(i-1+1)*m+j,0});
                    }
                    if (matrica[i][j]=='W')
                    {
                        adj[(i-1)*m+j].push_back({(i-1)*m+j+1,2});
                        adj[(i-1)*m+j].push_back({(i-1+1)*m+j,3});
                    }
                    if (matrica[i][j]=='N')
                    {
                        adj[(i-1)*m+j].push_back({(i-1)*m+j+1,1});
                        adj[(i-1)*m+j].push_back({(i-1+1)*m+j,2});
                    }
                }
                else if (j==m)
                {
                    if (matrica[i][j]=='E')
                    {
                        adj[(i-1)*m+j].push_back({(i-1)*m+j-1,2});
                        adj[(i-1)*m+j].push_back({(i-1+1)*m+j,1});
                    }
                    if (matrica[i][j]=='S')
                    {
                        adj[(i-1)*m+j].push_back({(i-1)*m+j-1,1});
                        adj[(i-1)*m+j].push_back({(i-1+1)*m+j,0});
                    }
                    if (matrica[i][j]=='W')
                    {
                        adj[(i-1)*m+j].push_back({(i-1)*m+j-1,0});
                        adj[(i-1)*m+j].push_back({(i-1+1)*m+j,3});
                    }
                    if (matrica[i][j]=='N')
                    {
                        adj[(i-1)*m+j].push_back({(i-1)*m+j-1,3});
                        adj[(i-1)*m+j].push_back({(i-1+1)*m+j,2});
                    }
                }
                else
                {
                    if (matrica[i][j]=='E')
                    {
                        adj[(i-1)*m+j].push_back({(i-1)*m+j+1,0});
                        adj[(i-1)*m+j].push_back({(i-1)*m+j-1,2});
                        adj[(i-1)*m+j].push_back({(i-1+1)*m+j,1});
                    }
                    if (matrica[i][j]=='S')
                    {
                        adj[(i-1)*m+j].push_back({(i-1)*m+j+1,3});
                        adj[(i-1)*m+j].push_back({(i-1)*m+j-1,1});
                        adj[(i-1)*m+j].push_back({(i-1+1)*m+j,0});
                    }
                    if (matrica[i][j]=='W')
                    {
                        adj[(i-1)*m+j].push_back({(i-1)*m+j+1,2});
                        adj[(i-1)*m+j].push_back({(i-1)*m+j-1,0});
                        adj[(i-1)*m+j].push_back({(i-1+1)*m+j,3});
                    }
                    if (matrica[i][j]=='N')
                    {
                        adj[(i-1)*m+j].push_back({(i-1)*m+j+1,1});
                        adj[(i-1)*m+j].push_back({(i-1)*m+j-1,3});
                        adj[(i-1)*m+j].push_back({(i-1+1)*m+j,2});
                    }
                }
            }
            else if (i==n)
            {
                if (j==1)
                {
                    if (matrica[i][j]=='E')
                    {
                        adj[(i-1)*m+j].push_back({(i-1)*m+j+1,0});
                        adj[(i-1)*m+j].push_back({(i-1-1)*m+j,3});
                    }
                    if (matrica[i][j]=='S')
                    {
                        adj[(i-1)*m+j].push_back({(i-1)*m+j+1,3});
                        adj[(i-1)*m+j].push_back({(i-1-1)*m+j,2});
                    }
                    if (matrica[i][j]=='W')
                    {
                        adj[(i-1)*m+j].push_back({(i-1)*m+j+1,2});
                        adj[(i-1)*m+j].push_back({(i-1-1)*m+j,1});
                    }
                    if (matrica[i][j]=='N')
                    {
                        adj[(i-1)*m+j].push_back({(i-1)*m+j+1,1});
                        adj[(i-1)*m+j].push_back({(i-1-1)*m+j,0});
                    }
                }
                else if (j==m)
                {
                    if (matrica[i][j]=='E')
                    {
                        adj[(i-1)*m+j].push_back({(i-1)*m+j-1,2});
                        adj[(i-1)*m+j].push_back({(i-1-1)*m+j,3});
                    }
                    if (matrica[i][j]=='S')
                    {
                        adj[(i-1)*m+j].push_back({(i-1)*m+j-1,1});
                        adj[(i-1)*m+j].push_back({(i-1-1)*m+j,2});
                    }
                    if (matrica[i][j]=='W')
                    {
                        adj[(i-1)*m+j].push_back({(i-1)*m+j-1,0});
                        adj[(i-1)*m+j].push_back({(i-1-1)*m+j,1});
                    }
                    if (matrica[i][j]=='N')
                    {
                        adj[(i-1)*m+j].push_back({(i-1)*m+j-1,3});
                        adj[(i-1)*m+j].push_back({(i-1-1)*m+j,0});
                    }
                }
                else
                {
                    if (matrica[i][j]=='E')
                    {
                        adj[(i-1)*m+j].push_back({(i-1)*m+j+1,0});
                        adj[(i-1)*m+j].push_back({(i-1)*m+j-1,2});
                        adj[(i-1)*m+j].push_back({(i-1-1)*m+j,3});
                    }
                    if (matrica[i][j]=='S')
                    {
                        adj[(i-1)*m+j].push_back({(i-1)*m+j+1,3});
                        adj[(i-1)*m+j].push_back({(i-1)*m+j-1,1});
                        adj[(i-1)*m+j].push_back({(i-1-1)*m+j,2});
                    }
                    if (matrica[i][j]=='W')
                    {
                        adj[(i-1)*m+j].push_back({(i-1)*m+j+1,2});
                        adj[(i-1)*m+j].push_back({(i-1)*m+j-1,0});
                        adj[(i-1)*m+j].push_back({(i-1-1)*m+j,1});
                    }
                    if (matrica[i][j]=='N')
                    {
                        adj[(i-1)*m+j].push_back({(i-1)*m+j+1,1});
                        adj[(i-1)*m+j].push_back({(i-1)*m+j-1,3});
                        adj[(i-1)*m+j].push_back({(i-1-1)*m+j,0});
                    }
                }
            }
            else
            {
                if (j==1)
                {
                    if (matrica[i][j]=='E')
                    {
                        adj[(i-1)*m+j].push_back({(i-1)*m+j+1,0});
                        adj[(i-1)*m+j].push_back({(i-1-1)*m+j,3});
                        adj[(i-1)*m+j].push_back({(i-1+1)*m+j,1});
                    }
                    if (matrica[i][j]=='S')
                    {
                        adj[(i-1)*m+j].push_back({(i-1)*m+j+1,3});
                        adj[(i-1)*m+j].push_back({(i-1-1)*m+j,2});
                        adj[(i-1)*m+j].push_back({(i-1+1)*m+j,0});
                    }
                    if (matrica[i][j]=='W')
                    {
                        adj[(i-1)*m+j].push_back({(i-1)*m+j+1,2});
                        adj[(i-1)*m+j].push_back({(i-1-1)*m+j,1});
                        adj[(i-1)*m+j].push_back({(i-1+1)*m+j,3});
                    }
                    if (matrica[i][j]=='N')
                    {
                        adj[(i-1)*m+j].push_back({(i-1)*m+j+1,1});
                        adj[(i-1)*m+j].push_back({(i-1-1)*m+j,0});
                        adj[(i-1)*m+j].push_back({(i-1+1)*m+j,2});
                    }
                }
                else if (j==m)
                {
                    if (matrica[i][j]=='E')
                    {
                        adj[(i-1)*m+j].push_back({(i-1)*m+j-1,2});
                        adj[(i-1)*m+j].push_back({(i-1-1)*m+j,3});
                        adj[(i-1)*m+j].push_back({(i-1+1)*m+j,1});
                    }
                    if (matrica[i][j]=='S')
                    {
                        adj[(i-1)*m+j].push_back({(i-1)*m+j-1,1});
                        adj[(i-1)*m+j].push_back({(i-1-1)*m+j,2});
                        adj[(i-1)*m+j].push_back({(i-1+1)*m+j,0});
                    }
                    if (matrica[i][j]=='W')
                    {
                        adj[(i-1)*m+j].push_back({(i-1)*m+j-1,0});
                        adj[(i-1)*m+j].push_back({(i-1-1)*m+j,1});
                        adj[(i-1)*m+j].push_back({(i-1+1)*m+j,3});
                    }
                    if (matrica[i][j]=='N')
                    {
                        adj[(i-1)*m+j].push_back({(i-1)*m+j-1,3});
                        adj[(i-1)*m+j].push_back({(i-1-1)*m+j,0});
                        adj[(i-1)*m+j].push_back({(i-1+1)*m+j,2});
                    }
                }
                else
                {
                    if (matrica[i][j]=='E')
                    {
                        adj[(i-1)*m+j].push_back({(i-1)*m+j+1,0});
                        adj[(i-1)*m+j].push_back({(i-1)*m+j-1,2});
                        adj[(i-1)*m+j].push_back({(i-1-1)*m+j,3});
                        adj[(i-1)*m+j].push_back({(i-1+1)*m+j,1});
                    }
                    if (matrica[i][j]=='S')
                    {
                        adj[(i-1)*m+j].push_back({(i-1)*m+j+1,3});
                        adj[(i-1)*m+j].push_back({(i-1)*m+j-1,1});
                        adj[(i-1)*m+j].push_back({(i-1-1)*m+j,2});
                        adj[(i-1)*m+j].push_back({(i-1+1)*m+j,0});
                    }
                    if (matrica[i][j]=='W')
                    {
                        adj[(i-1)*m+j].push_back({(i-1)*m+j+1,2});
                        adj[(i-1)*m+j].push_back({(i-1)*m+j-1,0});
                        adj[(i-1)*m+j].push_back({(i-1-1)*m+j,1});
                        adj[(i-1)*m+j].push_back({(i-1+1)*m+j,3});
                    }
                    if (matrica[i][j]=='N')
                    {
                        adj[(i-1)*m+j].push_back({(i-1)*m+j+1,1});
                        adj[(i-1)*m+j].push_back({(i-1)*m+j-1,3});
                        adj[(i-1)*m+j].push_back({(i-1-1)*m+j,0});
                        adj[(i-1)*m+j].push_back({(i-1+1)*m+j,2});
                    }
                }
            }
        }
    }
    dikstra(1);
    if (dist[n*m]==LLONG_MAX) cout<<-1<<endl;
    else cout<<dist[n*m]<<endl;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8148 KB Output is correct
2 Correct 4 ms 8148 KB Output is correct
3 Correct 4 ms 8148 KB Output is correct
4 Correct 4 ms 8148 KB Output is correct
5 Correct 4 ms 8148 KB Output is correct
6 Correct 3 ms 8104 KB Output is correct
7 Correct 3 ms 8020 KB Output is correct
8 Correct 4 ms 8148 KB Output is correct
9 Correct 3 ms 8148 KB Output is correct
10 Correct 4 ms 8020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8148 KB Output is correct
2 Correct 4 ms 8148 KB Output is correct
3 Correct 4 ms 8148 KB Output is correct
4 Correct 4 ms 8148 KB Output is correct
5 Correct 4 ms 8148 KB Output is correct
6 Correct 3 ms 8104 KB Output is correct
7 Correct 3 ms 8020 KB Output is correct
8 Correct 4 ms 8148 KB Output is correct
9 Correct 3 ms 8148 KB Output is correct
10 Correct 4 ms 8020 KB Output is correct
11 Correct 3 ms 8020 KB Output is correct
12 Correct 3 ms 8100 KB Output is correct
13 Correct 4 ms 8100 KB Output is correct
14 Correct 4 ms 8020 KB Output is correct
15 Correct 3 ms 8148 KB Output is correct
16 Correct 4 ms 8148 KB Output is correct
17 Correct 4 ms 8148 KB Output is correct
18 Correct 3 ms 8020 KB Output is correct
19 Correct 4 ms 8108 KB Output is correct
20 Correct 5 ms 8148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8020 KB Output is correct
2 Correct 4 ms 8020 KB Output is correct
3 Correct 3 ms 8020 KB Output is correct
4 Correct 3 ms 8020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8148 KB Output is correct
2 Correct 4 ms 8020 KB Output is correct
3 Correct 4 ms 8116 KB Output is correct
4 Correct 3 ms 8148 KB Output is correct
5 Correct 4 ms 8108 KB Output is correct
6 Correct 4 ms 8148 KB Output is correct
7 Correct 4 ms 8148 KB Output is correct
8 Correct 4 ms 8020 KB Output is correct
9 Correct 4 ms 8148 KB Output is correct
10 Correct 4 ms 8088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8148 KB Output is correct
2 Correct 4 ms 8148 KB Output is correct
3 Correct 4 ms 8148 KB Output is correct
4 Correct 4 ms 8148 KB Output is correct
5 Correct 4 ms 8148 KB Output is correct
6 Correct 3 ms 8104 KB Output is correct
7 Correct 3 ms 8020 KB Output is correct
8 Correct 4 ms 8148 KB Output is correct
9 Correct 3 ms 8148 KB Output is correct
10 Correct 4 ms 8020 KB Output is correct
11 Correct 3 ms 8020 KB Output is correct
12 Correct 3 ms 8100 KB Output is correct
13 Correct 4 ms 8100 KB Output is correct
14 Correct 4 ms 8020 KB Output is correct
15 Correct 3 ms 8148 KB Output is correct
16 Correct 4 ms 8148 KB Output is correct
17 Correct 4 ms 8148 KB Output is correct
18 Correct 3 ms 8020 KB Output is correct
19 Correct 4 ms 8108 KB Output is correct
20 Correct 5 ms 8148 KB Output is correct
21 Correct 4 ms 8020 KB Output is correct
22 Correct 4 ms 8020 KB Output is correct
23 Correct 3 ms 8020 KB Output is correct
24 Correct 3 ms 8020 KB Output is correct
25 Correct 3 ms 8148 KB Output is correct
26 Correct 4 ms 8020 KB Output is correct
27 Correct 4 ms 8116 KB Output is correct
28 Correct 3 ms 8148 KB Output is correct
29 Correct 4 ms 8108 KB Output is correct
30 Correct 4 ms 8148 KB Output is correct
31 Correct 4 ms 8148 KB Output is correct
32 Correct 4 ms 8020 KB Output is correct
33 Correct 4 ms 8148 KB Output is correct
34 Correct 4 ms 8088 KB Output is correct
35 Correct 9 ms 9556 KB Output is correct
36 Correct 4 ms 8276 KB Output is correct
37 Correct 10 ms 9816 KB Output is correct
38 Correct 9 ms 10780 KB Output is correct
39 Correct 70 ms 24284 KB Output is correct
40 Correct 73 ms 24340 KB Output is correct
41 Correct 34 ms 24236 KB Output is correct
42 Correct 71 ms 24332 KB Output is correct
43 Correct 87 ms 32316 KB Output is correct
44 Correct 35 ms 24204 KB Output is correct