Submission #50347

#TimeUsernameProblemLanguageResultExecution timeMemory
50347dualityRobots (APIO13_robots)C++11
100 / 100
796 ms54776 KiB
#define DEBUG 0

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

#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}

// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
    public:
        template<typename T>
        _Debug& operator,(T val) {
            cout << val << endl;
            return *this;
        }
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif

// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back

// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;

// ---------- END OF TEMPLATE ----------

int dx[4] = {-1,0,1,0};
int dy[4] = {0,-1,0,1};
char grid[502][502];
pii fin[502][502][4];
pii doDFS(int x,int y,int d) {
    if (fin[y][x][d] != mp(0,0)) return fin[y][x][d];
    int d2 = d;
    if (grid[y][x] == 'A') d2 = (d+3) % 4;
    else if (grid[y][x] == 'C') d2 = (d+1) % 4;
    if (grid[y+dy[d2]][x+dx[d2]] == 'x') return fin[y][x][d] = mp(x,y);
    else return fin[y][x][d] = doDFS(x+dx[d2],y+dy[d2],d2);
}
int dp[9][9][502][502];
queue<pii> Q;
vector<pair<int,pii> > v;
int main() {
    int i,j;
    int n,w,h;
    scanf("%d %d %d",&n,&w,&h);
    memset(grid,'x',sizeof(grid));
    for (i = 1; i <= h; i++) {
        for (j = 1; j <= w; j++) scanf(" %c",&grid[i][j]);
    }

    int k,l,p,q;
    for (i = 1; i <= h; i++) {
        for (j = 1; j <= w; j++) {
            for (k = 0; k < 4; k++) {
                if (grid[i][j] != 'x') doDFS(j,i,k);
            }
        }
    }
    for (i = 0; i < n; i++) {
        for (j = 1; j <= h; j++) {
            for (k = 1; k <= w; k++) {
                if (grid[j][k] == '1'+i) {
                    dp[i][i][j][k] = 0;
                    Q.push(mp(k,j));
                }
                else dp[i][i][j][k] = MAX_VAL;
            }
        }
        while (!Q.empty()) {
            int x = Q.front().first,y = Q.front().second;
            Q.pop();

            for (j = 0; j < 4; j++) {
                int xx = fin[y][x][j].first,yy = fin[y][x][j].second;
                if (dp[i][i][yy][xx] == MAX_VAL) {
                    dp[i][i][yy][xx] = dp[i][i][y][x]+1;
                    Q.push(mp(xx,yy));
                }
            }
        }
    }
    for (i = 2; i <= n; i++) {
        for (j = 0; j <= n-i; j++) {
            k = j+i-1;
            for (p = 1; p <= h; p++) {
                for (q = 1; q <= w; q++) {
                    dp[j][k][p][q] = MAX_VAL;
                    for (l = j; l < k; l++) dp[j][k][p][q] = min(dp[j][k][p][q],dp[j][l][p][q]+dp[l+1][k][p][q]);
                    if (dp[j][k][p][q] < MAX_VAL) v.pb(mp(dp[j][k][p][q],mp(q,p)));
                }
            }
            l = 0;
            sort(v.begin(),v.end());
            while ((l < v.size()) && (v[l].first == v[0].first)) Q.push(v[l++].second);
            while (!Q.empty()) {
                int x = Q.front().first,y = Q.front().second;
                Q.pop();

                for (p = 0; p < 4; p++) {
                    int xx = fin[y][x][p].first,yy = fin[y][x][p].second;
                    if (dp[j][k][y][x]+1 < dp[j][k][yy][xx]) {
                        dp[j][k][yy][xx] = dp[j][k][y][x]+1;
                        Q.push(mp(xx,yy));
                    }
                }
                while ((l < v.size()) && (v[l].first == dp[j][k][y][x]+1)) Q.push(v[l++].second);
            }
            v.clear();
        }
    }
    int ans = MAX_VAL;
    for (i = 1; i <= h; i++) {
        for (j = 1; j <= w; j++) ans = min(ans,dp[0][n-1][i][j]);
    }
    if (ans == MAX_VAL) printf("-1\n");
    else printf("%d\n",ans);

    return 0;
}

Compilation message (stderr)

robots.cpp: In function 'int main()':
robots.cpp:125:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             while ((l < v.size()) && (v[l].first == v[0].first)) Q.push(v[l++].second);
                     ~~^~~~~~~~~~
robots.cpp:137:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                 while ((l < v.size()) && (v[l].first == dp[j][k][y][x]+1)) Q.push(v[l++].second);
                         ~~^~~~~~~~~~
robots.cpp:76: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:79:39: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         for (j = 1; j <= w; j++) scanf(" %c",&grid[i][j]);
                                  ~~~~~^~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...