Submission #12717

# Submission time Handle Problem Language Result Execution time Memory
12717 2015-01-01T18:47:23 Z baneling100 Robots (APIO13_robots) C++
100 / 100
428 ms 149468 KB
#include <stdio.h>
#include <vector>
#define INF 0x7fffffff

using namespace std;

typedef pair <int,int> ppair;
vector <ppair> A[250001];
int N, W, H, Ynext[501][501][4], Xnext[501][501][4], Yloc[10], Xloc[10];
int Ydir[4]={0,1,0,-1}, Xdir[4]={1,0,-1,0}, D[10][10][501][501], Ans=INF;
int Check[501][501][4];
char Room[501][502];

void DFS(int y, int x, int dir) {

    int ty, tx, tdir=dir;

    if(Check[y][x][dir])
        return;
    if(Room[y][x]=='A') {
        tdir=dir-1;
        if(tdir<0)
            tdir=3;
    }
    else if(Room[y][x]=='C') {
        tdir=dir+1;
        tdir%=4;
    }
    ty=y+Ydir[tdir];
    tx=x+Xdir[tdir];
    if(!(ty>=1 && ty<=H && tx>=1 && tx<=W) || Room[ty][tx]=='x') {
        Ynext[y][x][dir]=y;
        Xnext[y][x][dir]=x;
    }
    else {
        DFS(ty,tx,tdir);
        Ynext[y][x][dir]=Ynext[ty][tx][tdir];
        Xnext[y][x][dir]=Xnext[ty][tx][tdir];
    }
    Check[y][x][dir]=1;
}

void renew(int p, int q) {

    int i, j, k, m, l=INF, r=0, y, x, ok;

    for(i=1 ; i<=H ; i++)
        for(j=1 ; j<=W ; j++)
            if(D[p][q][i][j]!=INF) {
                A[D[p][q][i][j]].push_back(make_pair(i,j));
                if(l>D[p][q][i][j])
                    l=D[p][q][i][j];
                if(r<D[p][q][i][j])
                    r=D[p][q][i][j];
            }
    for(i=l ; i<=r ; i++) {
        k=A[i].size();
        ok=0;
        for(j=0 ; j<k ; j++) {
            y=A[i][j].first;
            x=A[i][j].second;
            if(D[p][q][y][x]!=i)
                continue;
            for(m=0 ; m<=3 ; m++)
                if(D[p][q][Ynext[y][x][m]][Xnext[y][x][m]]>i+1) {
                    D[p][q][Ynext[y][x][m]][Xnext[y][x][m]]=i+1;
                    A[i+1].push_back(make_pair(Ynext[y][x][m],Xnext[y][x][m]));
                    ok=1;
                }
        }
        if(i==r && ok)
            r++;
        A[i].clear();
    }
}

int main(void) {

    int i, j, k, l, y, x, dir, py, px;

    scanf("%d %d %d",&N,&W,&H);
    for(i=1 ; i<=H ; i++) {
        scanf("%s",&Room[i][1]);
        for(j=1 ; j<=W ; j++)
            if('1'<=Room[i][j] && Room[i][j]<='9') {
                Yloc[Room[i][j]-'0']=i;
                Xloc[Room[i][j]-'0']=j;
                Room[i][j]='.';
            }
    }
    for(i=1 ; i<=H ; i++)
        for(j=1 ; j<=W ; j++)
            if(Room[i][j]!='x')
                for(k=0 ; k<=3 ; k++)
                    DFS(i,j,k);
    for(k=1 ; k<=N ; k++) {
        for(i=1 ; i<=H ; i++)
            for(j=1 ; j<=W ; j++)
                D[k][k][i][j]=INF;
        D[k][k][Yloc[k]][Xloc[k]]=0;
        renew(k,k);
    }
    for(i=1 ; i<=N-1 ; i++)
        for(j=i+1 ; j<=N ; j++)
            for(y=1 ; y<=H ; y++)
                for(x=1 ; x<=W ; x++)
                    D[i][j][y][x]=INF;
    for(l=1 ; l<=N-1 ; l++)
        for(i=1 ; i<=N-l ; i++) {
            j=i+l;
            for(k=i ; k<=j-1 ; k++)
                for(y=1 ; y<=H ; y++)
                    for(x=1 ; x<=W ; x++)
                        if(D[i][k][y][x]!=INF && D[k+1][j][y][x]!=INF && D[i][j][y][x]>D[i][k][y][x]+D[k+1][j][y][x])
                            D[i][j][y][x]=D[i][k][y][x]+D[k+1][j][y][x];
            renew(i,j);
        }
    for(i=1 ; i<=H ; i++)
        for(j=1 ; j<=W ; j++)
            if(Ans>D[1][N][i][j])
                Ans=D[1][N][i][j];
    if(Ans==INF)
        printf("-1");
    else
        printf("%d",Ans);
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 117128 KB Output is correct
2 Correct 0 ms 117128 KB Output is correct
3 Correct 4 ms 117128 KB Output is correct
4 Correct 0 ms 117128 KB Output is correct
5 Correct 0 ms 117128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 117128 KB Output is correct
2 Correct 0 ms 117128 KB Output is correct
3 Correct 0 ms 117128 KB Output is correct
4 Correct 4 ms 117128 KB Output is correct
5 Correct 0 ms 117128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 121100 KB Output is correct
2 Correct 0 ms 117656 KB Output is correct
3 Correct 16 ms 117128 KB Output is correct
4 Correct 132 ms 126104 KB Output is correct
5 Correct 36 ms 118364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 88 ms 117128 KB Output is correct
2 Correct 428 ms 149468 KB Output is correct
3 Correct 104 ms 121580 KB Output is correct
4 Correct 68 ms 117128 KB Output is correct
5 Correct 252 ms 134416 KB Output is correct