Submission #155749

# Submission time Handle Problem Language Result Execution time Memory
155749 2019-09-30T08:46:18 Z arnold518 Robots (APIO13_robots) C++14
60 / 100
1500 ms 115384 KB
#pragma GCC optimize ("Ofast")

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 500;
const int INF = 987654321;
const int MAXVAL = 1e6;

const int dy[]={-1, 1, 0, 0};
const int dx[]={0, 0, -1, 1};
const int dA[]={2, 3, 1, 0};
const int dC[]={3, 2, 0, 1};

int N, W, H;
char A[MAXN+10][MAXN+10];
pii dest[MAXN+10][MAXN+10][4];
int dp[MAXN+10][MAXN+10][10][10];

void dfs(int y, int x, int dir)
{
    if(dest[y][x][dir]!=pii(-1, -1)) return;
    int ndir;
    if(A[y][x]=='A') ndir=dA[dir];
    else if(A[y][x]=='C') ndir=dC[dir];
    else ndir=dir;

    int ny=y+dy[ndir], nx=x+dx[ndir];
    if(A[ny][nx]=='x')
    {
        dest[y][x][dir]={y, x};
        return;
    }
    dfs(ny, nx, ndir);
    dest[y][x][dir]=dest[ny][nx][ndir];
}

struct Queue
{
    int y, x, w;
    bool operator < (const Queue &p) const { return w>p.w; }
};

struct dequeue
{
    Queue arr[MAXVAL];
    int l, r;
    void init() { l=r=MAXVAL/2; }
    Queue front() { return arr[l]; }
    void push_back(Queue x) { arr[r++]=x; }
    void push_front(Queue x) { arr[--l]=x; }
    void pop_front () { l++; }
    bool empty() { return l==r; }
};
dequeue Q;

int main()
{
    int i, j, k, p, q;

    scanf("%d%d%d", &N, &W, &H);
    for(i=1; i<=H; i++) scanf("%s", A[i]+1);
    for(i=0; i<=H+1; i++) A[i][0]=A[i][W+1]='x';
    for(j=0; j<=W+1; j++) A[0][j]=A[H+1][j]='x';

    for(i=1; i<=H; i++) for(j=1; j<=W; j++) for(k=0; k<4; k++) dest[i][j][k]={-1, -1};
    for(i=1; i<=H; i++) for(j=1; j<=W; j++) for(k=0; k<4; k++)
    {
        if(dest[i][j][k]!=pii(-1, -1)) continue;
        dfs(i, j, k);
    }
    //for(i=1; i<=H; i++) { for(j=1; j<=W; j++) printf("(%d %d / %d %d / %d %d / %d %d) ", dest[i][j][0].first, dest[i][j][0].second, dest[i][j][1].first, dest[i][j][1].second, dest[i][j][2].first, dest[i][j][2].second, dest[i][j][3].first, dest[i][j][3].second); printf("\n"); }

    for(q=1; q<=N; q++) for(p=q; p>=1; p--)
    {
        vector<Queue> PQ;
        for(i=1; i<=H; i++) for(j=1; j<=W; j++) dp[i][j][p][q]=INF;
        if(p==q)
        {
            for(i=1; i<=H; i++) for(j=1; j<=W; j++)
            {
                int now;
                if(A[i][j]=='0'+p) now=0;
                else now=INF;
                PQ.push_back({i, j, now});
            }
        }
        else
        {
            for(i=1; i<=H; i++) for(j=1; j<=W; j++)
            {
                int now=INF;
                for(k=p; k<q; k++)
                {
                    now=min(now, dp[i][j][p][k]+dp[i][j][k+1][q]);
                }
                PQ.push_back({i, j, now});
            }
        }
        sort(PQ.begin(), PQ.end());
        Q.init();
        Q.push_back(PQ.back()); PQ.pop_back();

        while(!Q.empty())
        {
            while(!PQ.empty() && PQ.back().w==Q.front().w) Q.push_front(PQ.back()), PQ.pop_back();
            Queue T=Q.front(); Q.pop_front();
            //printf("%d %d %d\n", T.y, T.x, T.w);
            if(dp[T.y][T.x][p][q]<=T.w) continue;
            dp[T.y][T.x][p][q]=T.w;
            for(k=0; k<4; k++) if(dp[dest[T.y][T.x][k].first][dest[T.y][T.x][k].second][p][q]>T.w+1) Q.push_back({dest[T.y][T.x][k].first, dest[T.y][T.x][k].second, T.w+1});//, printf("?%d %d %d\n", dest[T.y][T.x][k].first, dest[T.y][T.x][k].second, T.w+1);
        }
        //printf("=======%d %d========\n", p, q);
        //for(i=1; i<=H; i++) { for(j=1; j<=W; j++) printf("%d ", dp[i][j][p][q]); printf("\n"); }
    }
    int ans=INF;
    for(i=1; i<=H; i++) for(j=1; j<=W; j++) ans=min(ans, dp[i][j][1][N]);
    if(ans==INF) printf("-1");
    else printf("%d", ans);
}

Compilation message

robots.cpp: In function 'int main()':
robots.cpp:65:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d%d", &N, &W, &H);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~
robots.cpp:66:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(i=1; i<=H; i++) scanf("%s", A[i]+1);
                         ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 504 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 504 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 504 KB Output is correct
11 Correct 476 ms 43384 KB Output is correct
12 Correct 43 ms 41840 KB Output is correct
13 Correct 240 ms 43604 KB Output is correct
14 Correct 598 ms 43616 KB Output is correct
15 Correct 428 ms 44164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 504 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 504 KB Output is correct
11 Correct 476 ms 43384 KB Output is correct
12 Correct 43 ms 41840 KB Output is correct
13 Correct 240 ms 43604 KB Output is correct
14 Correct 598 ms 43616 KB Output is correct
15 Correct 428 ms 44164 KB Output is correct
16 Correct 1196 ms 114684 KB Output is correct
17 Execution timed out 1582 ms 115384 KB Time limit exceeded
18 Halted 0 ms 0 KB -