Submission #226566

# Submission time Handle Problem Language Result Execution time Memory
226566 2020-04-24T10:51:19 Z MKopchev Portals (BOI14_portals) C++14
70 / 100
1000 ms 26140 KB
#include<bits/stdc++.h>
using namespace std;
const int nmax=1e3+42;

int n,m;

char inp[nmax][nmax];

char get_in()
{
    char c=getchar();
    while(c!='.'&&c!='#'&&c!='S'&&c!='C')c=getchar();
    return c;
}

bool in(int i,int j)
{
    return 1<=i&&i<=n&&1<=j&&j<=m;
}
int nxt_left[nmax][nmax],nxt_right[nmax][nmax],nxt_up[nmax][nmax],nxt_down[nmax][nmax];

int dist[nmax][nmax];

priority_queue< pair<int, pair<int,int> > > pq;

int x_move[4]={1,-1,0,0};
int y_move[4]={0,0,1,-1};

int calc_dist(pair<int,int> a,pair<int,int> b)
{
    return abs(a.first-b.first)+abs(a.second-b.second);
}
int main()
{
    scanf("%i%i",&n,&m);

    for(int i=0;i<=n+1;i++)
        for(int j=0;j<=m+1;j++)
        {
            if(in(i,j)==0)inp[i][j]='#';
            else inp[i][j]=get_in();
        }

    for(int i=0;i<=n+1;i++)
        for(int j=0;j<=m+1;j++)
        {
            if(inp[i][j]=='#')nxt_left[i][j]=j;
            else nxt_left[i][j]=nxt_left[i][j-1];

            if(inp[i][j]=='#')nxt_up[i][j]=i;
            else nxt_up[i][j]=nxt_up[i-1][j];
        }

    for(int i=n+1;i>=0;i--)
        for(int j=m+1;j>=0;j--)
        {
            if(inp[i][j]=='#')nxt_right[i][j]=j;
            else nxt_right[i][j]=nxt_right[i][j+1];

            if(inp[i][j]=='#')nxt_down[i][j]=i;
            else nxt_down[i][j]=nxt_down[i+1][j];
        }
    /*
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            cout<<i<<" "<<j<<" -> "<<nxt_left[i][j]<<" "<<nxt_right[i][j]<<" "<<nxt_down[i][j]<<" "<<nxt_up[i][j]<<endl;
        }
    */


    memset(dist,-1,sizeof(dist));

    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            if(inp[i][j]=='S')pq.push({0,{i,j}});

    while(pq.size())
    {
        pair<int, pair<int,int> > tp=pq.top();

        pq.pop();

        int d_now=-tp.first;

        int x=tp.second.first;
        int y=tp.second.second;

        if(dist[x][y]!=-1)continue;

        if(in(x,y)==0)continue;

        //cout<<x<<" "<<y<<" -> "<<d_now<<endl;

        dist[x][y]=d_now;

        if(inp[x][y]=='C')
        {
            printf("%i\n",d_now);
            return 0;
        }

        if(inp[x][y]=='#')continue;

        //move adjacent
        for(int t=0;t<4;t++)
        {
            int x_=x+x_move[t];
            int y_=y+y_move[t];

            if(in(x_,y_))pq.push({-(d_now+1),{x_,y_}});
        }

        //use portals
        pair<int,int> go[4];

        go[0]={x,nxt_left[x][y]+1};
        go[1]={x,nxt_right[x][y]-1};
        go[2]={nxt_up[x][y]+1,y};
        go[3]={nxt_down[x][y]-1,y};

        for(int move_1=0;move_1<=3;move_1++)
            for(int move_2=0;move_2<=3;move_2++)
            {
                //move (i,j) -> move_1 -> ( teleport ) move_2
                pq.push({-(d_now+calc_dist({x,y},go[move_1])+1),go[move_2]});
            }

    }

    assert(0==1);
    return 0;
}

Compilation message

portals.cpp: In function 'int main()':
portals.cpp:35:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%i%i",&n,&m);
     ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4608 KB Output is correct
2 Correct 6 ms 4736 KB Output is correct
3 Correct 7 ms 4736 KB Output is correct
4 Correct 7 ms 4736 KB Output is correct
5 Correct 7 ms 4736 KB Output is correct
6 Correct 7 ms 4736 KB Output is correct
7 Correct 7 ms 4864 KB Output is correct
8 Correct 7 ms 4864 KB Output is correct
9 Correct 7 ms 4736 KB Output is correct
10 Correct 7 ms 4736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 4608 KB Output is correct
2 Correct 7 ms 4736 KB Output is correct
3 Correct 7 ms 4864 KB Output is correct
4 Correct 7 ms 4736 KB Output is correct
5 Correct 7 ms 4864 KB Output is correct
6 Correct 8 ms 4864 KB Output is correct
7 Correct 7 ms 4736 KB Output is correct
8 Correct 7 ms 4864 KB Output is correct
9 Correct 11 ms 6012 KB Output is correct
10 Correct 10 ms 5952 KB Output is correct
11 Correct 11 ms 5504 KB Output is correct
12 Correct 12 ms 5504 KB Output is correct
13 Correct 13 ms 5632 KB Output is correct
14 Correct 7 ms 4736 KB Output is correct
15 Correct 11 ms 5824 KB Output is correct
16 Correct 8 ms 4736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4608 KB Output is correct
2 Correct 6 ms 4736 KB Output is correct
3 Correct 6 ms 4736 KB Output is correct
4 Correct 7 ms 4864 KB Output is correct
5 Correct 76 ms 8192 KB Output is correct
6 Correct 101 ms 8440 KB Output is correct
7 Correct 72 ms 8492 KB Output is correct
8 Correct 37 ms 8184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4608 KB Output is correct
2 Correct 8 ms 4736 KB Output is correct
3 Correct 7 ms 4864 KB Output is correct
4 Correct 7 ms 4736 KB Output is correct
5 Correct 8 ms 4864 KB Output is correct
6 Correct 8 ms 4736 KB Output is correct
7 Correct 7 ms 4864 KB Output is correct
8 Correct 8 ms 4864 KB Output is correct
9 Correct 11 ms 6080 KB Output is correct
10 Correct 11 ms 5952 KB Output is correct
11 Correct 11 ms 5504 KB Output is correct
12 Correct 12 ms 5504 KB Output is correct
13 Correct 13 ms 5632 KB Output is correct
14 Correct 73 ms 8312 KB Output is correct
15 Correct 97 ms 8320 KB Output is correct
16 Correct 70 ms 8508 KB Output is correct
17 Correct 93 ms 8632 KB Output is correct
18 Correct 108 ms 9840 KB Output is correct
19 Correct 11 ms 8640 KB Output is correct
20 Correct 37 ms 14440 KB Output is correct
21 Correct 73 ms 8312 KB Output is correct
22 Correct 78 ms 8192 KB Output is correct
23 Correct 90 ms 8320 KB Output is correct
24 Correct 126 ms 14440 KB Output is correct
25 Correct 7 ms 4736 KB Output is correct
26 Correct 11 ms 5824 KB Output is correct
27 Correct 7 ms 4736 KB Output is correct
28 Correct 34 ms 8112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4608 KB Output is correct
2 Correct 7 ms 4736 KB Output is correct
3 Correct 7 ms 4864 KB Output is correct
4 Correct 7 ms 4736 KB Output is correct
5 Correct 8 ms 4864 KB Output is correct
6 Correct 8 ms 4864 KB Output is correct
7 Correct 7 ms 4736 KB Output is correct
8 Correct 7 ms 4864 KB Output is correct
9 Correct 11 ms 6080 KB Output is correct
10 Correct 10 ms 6080 KB Output is correct
11 Correct 11 ms 5504 KB Output is correct
12 Correct 12 ms 5504 KB Output is correct
13 Correct 14 ms 5632 KB Output is correct
14 Correct 75 ms 8192 KB Output is correct
15 Correct 99 ms 8320 KB Output is correct
16 Correct 71 ms 8448 KB Output is correct
17 Correct 96 ms 8632 KB Output is correct
18 Correct 109 ms 9840 KB Output is correct
19 Correct 12 ms 8640 KB Output is correct
20 Correct 39 ms 14440 KB Output is correct
21 Correct 74 ms 8312 KB Output is correct
22 Correct 76 ms 8192 KB Output is correct
23 Correct 90 ms 8440 KB Output is correct
24 Execution timed out 1054 ms 26140 KB Time limit exceeded
25 Halted 0 ms 0 KB -