Submission #365067

# Submission time Handle Problem Language Result Execution time Memory
365067 2021-02-10T20:39:46 Z ACmachine Robots (APIO13_robots) C++17
30 / 100
1500 ms 126248 KB
#include <bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T, typename U> using ordered_map = tree<T, U, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef long long ll;
typedef long double ld;
typedef double db;
typedef string str;

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

typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<str> vstr;

#define FOR(i,j,k,in) for(int i=(j); i < (k);i+=in)
#define FORD(i,j,k,in) for(int i=(j); i >=(k);i-=in)
#define REP(i,b) FOR(i,0,b,1)
#define REPD(i,b) FORD(i,b,0,1)
#define pb push_back 
#define mp make_pair
#define ff first
#define ss second
#define all(x) begin(x), end(x)
#define rsz resize 
#define MANY_TESTS int tcase; cin >> tcase; while(tcase--)
	
const double EPS = 1e-9;
const int MOD = 1e9+7; // 998244353;
const ll INFF = 1e18;
const int INF = 1e9;
const ld PI = acos((ld)-1);
const vi dy = {1, 0, -1, 0, -1, 1, 1, -1};
const vi dx = {0, 1, 0, -1, -1, 1, -1, 1};

#ifdef DEBUG
#define DBG if(1)
#else
#define DBG if(0)
#endif

#define dbg(x) cout << "(" << #x << " : " << x << ")" << endl;
// ostreams
template <class T, class U>
ostream& operator<<(ostream& out, const pair<T, U> &par) {out << "[" << par.first << ";" << par.second << "]"; return out;}
template <class T>
ostream& operator<<(ostream& out, const set<T> &cont) { out << "{"; for( const auto &x:cont) out << x << ", "; out << "}"; return out; }
template <class T, class U>
ostream& operator<<(ostream& out, const map<T, U> &cont) {out << "{"; for( const auto &x:cont) out << x << ", "; out << "}"; return out; }
template<class T>
ostream& operator<<(ostream& out, const vector<T> &v){ out << "[";  REP(i, v.size()) out << v[i] << ", ";  out << "]"; return out;}
// istreams
template<class T>
istream& operator>>(istream& in, vector<T> &v){ for(auto &x : v) in >> x; return in; }
template<class T, class U>
istream& operator>>(istream& in, pair<T, U> &p){ in >> p.ff >> p.ss; return in; }
//searches
template<typename T, typename U>
T bsl(T lo, T hi, U f){ hi++; T mid; while(lo < hi){ mid = (lo + hi)/2; f(mid) ? hi = mid : lo = mid+1; } return lo; }
template<typename U>
double bsld(double lo, double hi, U f, double p = 1e-9){ int r = 3 + (int)log2((hi - lo)/p); double mid; while(r--){ mid = (lo + hi)/2; f(mid) ? hi = mid : lo = mid; } return (lo + hi)/2; }
template<typename T, typename U>
T bsh(T lo, T hi, U f){ lo--; T mid; while(lo < hi){ mid = (lo + hi + 1)/2; f(mid) ? lo = mid : hi = mid-1; } return lo; }
template<typename U>
double bshd(double lo, double hi, U f, double p = 1e-9){ int r = 3+(int)log2((hi - lo)/p); double mid; while(r--){ mid = (lo + hi)/2; f(mid) ? lo = mid : hi = mid; } return (lo + hi)/2; }
// some more utility functions
template<typename T>
pair<T, int> get_min(vector<T> &v){ typename vector<T> :: iterator it = min_element(v.begin(), v.end()); return mp(*it, it - v.begin());}
template<typename T>
pair<T, int> get_max(vector<T> &v){ typename vector<T> :: iterator it = max_element(v.begin(), v.end()); return mp(*it, it - v.begin());}        
template<typename T> bool ckmin(T& a, const T& b){return b < a ? a = b , true : false;}
template<typename T> bool ckmax(T& a, const T& b){return b > a ? a = b, true : false;}

    
int main(){
 	ios_base::sync_with_stdio(false);
 	cin.tie(NULL); cout.tie(NULL);
	int n, w, h;
    cin >> n >> w >> h;
    vector<string> grid(h);
    cin >> grid;
    array<int, 2> nul = {-1, -1};
    /* array<array<int, 2>, 4> nlll; fill(all(nlll), nul); */
    /* vector<vector<array<array<int, 2>, 4>>> nxt(h, vector<array<array<int, 2>, 4>>(w, nlll)); */
    array<int, 2> nxt[500][500][4];
    REP(i, h) REP(j, w) REP(d, 4) nxt[i][j][d] = nul; 
    auto wall = [&](int row, int col){
        return (row < 0 || row >= h || col < 0 || col >= w);
    };
    function<array<int, 2>(int, int, int)> get_last = [&](int row, int col, int d){
        if(grid[row][col] == 'A') d = (d + 3) % 4;
        if(grid[row][col] == 'C') d = (d + 1) % 4;
        if(nxt[row][col][d] != nul) return nxt[row][col][d];
        if(d == 0){
            if(wall(row - 1, col) || grid[row - 1][col] == 'x'){
                return nxt[row][col][d] = {row, col};
            }
            else
                return nxt[row][col][d] = get_last(row - 1, col, d);
        }   
        else if(d == 1){
            if(wall(row, col + 1) || grid[row][col + 1] == 'x'){
                return nxt[row][col][d] = {row, col};
            }
            else
                return nxt[row][col][d] = get_last(row, col + 1, d);
        }
        else if(d == 2){
            if(wall(row + 1, col) || grid[row + 1][col] == 'x'){
                return nxt[row][col][d] = {row, col};
            }
            else
                return nxt[row][col][d] = get_last(row + 1, col, d);
        }
        else if(d == 3){
            if(wall(row, col - 1) || grid[row][col - 1] == 'x'){
                return nxt[row][col][d] = {row, col};
            }
            else
                return nxt[row][col][d] = get_last(row, col - 1, d);
        }
    };
    REP(i, h){
        REP(j, w){
            REP(k, 4){
                get_last(i, j, k);
            }
        }
    }
    int dp[501][501][10][10];
    //vector<vector<array<array<int, 10>, 10>>> dp(h, vector<array<array<int, 10>, 10>>(w));
    //vector<vector<vector<vi>>> dp(h, vector<vector<vi>>(w, vector<vi>(10, vi(10, INF))));
    REP(i, h) REP(j, w) REP(k, 10) REP(g, 10) dp[i][j][k][g] = INF; 
    REP(i, h){
        REP(j, w){
            if(grid[i][j] > '0' && grid[i][j] <= '9'){
                int num = grid[i][j] - '0';
                dp[i][j][num][num] = 0;
            }
        }
    }
    REP(len, n){
        FOR(st, 1, n - len + 1, 1){
            int en = st + len;
            REP(row, h){
                REP(col, w){
                    FOR(mid, st, en, 1){
                        dp[row][col][st][en] = min(dp[row][col][st][en], dp[row][col][st][mid] + dp[row][col][mid + 1][en]);
                    }
                }
            }

            vector<queue<array<int, 2>>> qv(h * w + 10);
            REP(row, h){
                REP(col, w){
                    if(dp[row][col][st][en] < (int)qv.size())
                        qv[dp[row][col][st][en]].push({row, col});
                }
            }
            REP(j, (int)qv.size() - 5){
                while(!qv[j].empty()){
                    array<int, 2> v = qv[j].front();
                    qv[j].pop();
                    if(dp[v[0]][v[1]][st][en] >= j){
                        dp[v[0]][v[1]][st][en] = j;
                        REP(dir, 4){
                            qv[j + 1].push(nxt[v[0]][v[1]][dir]);
                        }
                    }
                }
            }
        }
    }
    int ans = INF;
    REP(row, h){
        REP(col, w){
            ans = min(ans, dp[row][col][1][n]);
        }
    }
    if(ans > 300000){
        cout << -1 << "\n";
    }
    else{
        cout << ans << "\n";
    }
	
    return 0;
}

Compilation message

robots.cpp: In lambda function:
robots.cpp:132:5: warning: control reaches end of non-void function [-Wreturn-type]
  132 |     };
      |     ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 492 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 0 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 492 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 0 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Execution timed out 1592 ms 126248 KB Time limit exceeded
12 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 492 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 0 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Execution timed out 1592 ms 126248 KB Time limit exceeded
12 Halted 0 ms 0 KB -