답안 #50347

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
50347 2018-06-10T17:34:51 Z duality 로봇 (APIO13_robots) C++11
100 / 100
796 ms 54776 KB
#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

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]);
                                  ~~~~~^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 632 KB Output is correct
2 Correct 4 ms 632 KB Output is correct
3 Correct 2 ms 696 KB Output is correct
4 Correct 3 ms 932 KB Output is correct
5 Correct 3 ms 1028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 632 KB Output is correct
2 Correct 4 ms 632 KB Output is correct
3 Correct 2 ms 696 KB Output is correct
4 Correct 3 ms 932 KB Output is correct
5 Correct 3 ms 1028 KB Output is correct
6 Correct 3 ms 1028 KB Output is correct
7 Correct 3 ms 1124 KB Output is correct
8 Correct 3 ms 1124 KB Output is correct
9 Correct 3 ms 1124 KB Output is correct
10 Correct 3 ms 1124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 632 KB Output is correct
2 Correct 4 ms 632 KB Output is correct
3 Correct 2 ms 696 KB Output is correct
4 Correct 3 ms 932 KB Output is correct
5 Correct 3 ms 1028 KB Output is correct
6 Correct 3 ms 1028 KB Output is correct
7 Correct 3 ms 1124 KB Output is correct
8 Correct 3 ms 1124 KB Output is correct
9 Correct 3 ms 1124 KB Output is correct
10 Correct 3 ms 1124 KB Output is correct
11 Correct 130 ms 31232 KB Output is correct
12 Correct 16 ms 31232 KB Output is correct
13 Correct 48 ms 31232 KB Output is correct
14 Correct 298 ms 31760 KB Output is correct
15 Correct 93 ms 31760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 632 KB Output is correct
2 Correct 4 ms 632 KB Output is correct
3 Correct 2 ms 696 KB Output is correct
4 Correct 3 ms 932 KB Output is correct
5 Correct 3 ms 1028 KB Output is correct
6 Correct 3 ms 1028 KB Output is correct
7 Correct 3 ms 1124 KB Output is correct
8 Correct 3 ms 1124 KB Output is correct
9 Correct 3 ms 1124 KB Output is correct
10 Correct 3 ms 1124 KB Output is correct
11 Correct 130 ms 31232 KB Output is correct
12 Correct 16 ms 31232 KB Output is correct
13 Correct 48 ms 31232 KB Output is correct
14 Correct 298 ms 31760 KB Output is correct
15 Correct 93 ms 31760 KB Output is correct
16 Correct 150 ms 53976 KB Output is correct
17 Correct 796 ms 54364 KB Output is correct
18 Correct 187 ms 54364 KB Output is correct
19 Correct 129 ms 54776 KB Output is correct
20 Correct 605 ms 54776 KB Output is correct