Submission #365089

# Submission time Handle Problem Language Result Execution time Memory
365089 2021-02-10T21:28:40 Z ACmachine Robots (APIO13_robots) C++17
60 / 100
469 ms 163844 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<int, 2> nul2 = {-2, -2};
    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<bool, 4> bi = {false, false, false, false};
    vector<vector<array<bool, 4>>> visited(h, vector<array<bool, 4>>(w, bi)); 
    
    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(visited[row][col][d]) return nxt[row][col][d] = nul2;
        visited[row][col][d] = true;
        int ny = row + dy[d]; int nx = col + dx[d];
        if(ny < 0 || ny >= h || nx < 0 || nx >= w || grid[ny][nx] == 'x')
            return nxt[row][col][d] = {row, col};
        else
            return nxt[row][col][d] = get_last(ny, nx, d); 
    };
    REP(i, h){
        REP(j, w){
            if(grid[i][j] == 'x') continue;
            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;
            }
        }
    }
    vector<queue<array<int, 2>>> qv(h * w + 10);         
    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]);
                    }
                }
            }
            REP(ind, qv.size()) 
                while(!qv[ind].empty()) qv[ind].pop();
            REP(row, h){
                REP(col, w){
                    if(grid[row][col] == 'x') continue;
                    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){ 
                        REP(dir, 4){
                            auto nx = nxt[v[0]][v[1]][dir];
                            if(nx == nul2) continue;
                            if(dp[nx[0]][nx[1]][st][en] > j + 1){ 
                                dp[nx[0]][nx[1]][st][en] = j + 1;
                                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 function 'int main()':
robots.cpp:26:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::queue<std::array<int, 2> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   26 | #define FOR(i,j,k,in) for(int i=(j); i < (k);i+=in)
      |                                        ^
robots.cpp:28:18: note: in expansion of macro 'FOR'
   28 | #define REP(i,b) FOR(i,0,b,1)
      |                  ^~~
robots.cpp:142:13: note: in expansion of macro 'REP'
  142 |             REP(ind, qv.size())
      |             ^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 0 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 0 ms 364 KB Output is correct
3 Correct 0 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 0 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 0 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 0 ms 364 KB Output is correct
3 Correct 0 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 0 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 0 ms 364 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 356 ms 97756 KB Output is correct
12 Correct 77 ms 97536 KB Output is correct
13 Correct 181 ms 99436 KB Output is correct
14 Correct 469 ms 98156 KB Output is correct
15 Correct 299 ms 97644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 0 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 0 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 0 ms 364 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 356 ms 97756 KB Output is correct
12 Correct 77 ms 97536 KB Output is correct
13 Correct 181 ms 99436 KB Output is correct
14 Correct 469 ms 98156 KB Output is correct
15 Correct 299 ms 97644 KB Output is correct
16 Runtime error 154 ms 163844 KB Execution killed with signal 9
17 Halted 0 ms 0 KB -