Submission #890778

#TimeUsernameProblemLanguageResultExecution timeMemory
890778KutanRobots (APIO13_robots)C++14
100 / 100
1192 ms50776 KiB
// Cao Quang Hung
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>

using namespace std;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define pii pair<int,int>
#define pll pair<long long , long long>
#define vi vector<int>
#define vpii vector<pii>
#define SZ(x) ((int)(x.size()))
#define fi first
#define se second
#define IN(x,y) ((y).find((x))!=(y).end())
#define ALL(t) t.begin(),t.end()
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
#define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
#define FOR(i, n) for (int (i) = 0; (i) < (n); ++(i))
#define dem(x) __builtin_popcount(x)
#define Mask(x) (1LL << (x))
#define BIT(x, i) ((x) >> (i) & 1)
#define ln '\n'
#define io_faster ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
///mt19937 rnd(time(0));

const int INF = 1e9 , mod = 1e9 + 7;

template <class T1, class T2>
inline T1 mul(T1& x, const T2 &k){ return x = (1LL * x * k) % mod; }

template <class T1 , class T2>
inline T1 pw(T1 x, T2 k){T1 res = 1; for (; k ; k >>= 1){ if (k & 1) mul(res, x); mul(x, x); } return res;}

template <class T>
inline bool minimize(T &x, const T &y){ if (x > y){x = y; return 1;} return 0; }

template <class T>
inline bool maximize(T &x, const T &y){ if (x < y){x = y; return 1;} return 0; }

template <class T>
inline void add(T &x , const T &y){ if ((x += y) >= mod) x -= mod; }

template <class T>
inline T product (const T &x , const T &y) { return 1LL * x * y % mod; }

#define PROB "a"
void file(){
    if(fopen(PROB".inp", "r")){
        freopen(PROB".inp","r",stdin);
        freopen(PROB".out","w",stdout);
    }
}
void sinh_(){
//    srand(time(0));
//    freopen(PROB".inp" , "w" , stdout);
//    int n;
}

typedef long long ll;
typedef double db;
const int N = 5e2 + 5;

// Counter clockwise
const int dx[4] = {+1, 0, -1, 0};
const int dy[4] = {0, +1, 0, -1};

int n, m, numRobot;
char a[N][N];
int id[N][N] = {}, cnt = 0;

bool inside(int x, int y) {
    return x >= 1 && x <= n && y >= 1 && y <= m && a[x][y] != 'x';
}

vector<vi> adj;

void readip(){
    cin >> numRobot >> m >> n;
    REP(i, 1, n) REP(j, 1, m) cin >> a[i][j];

    /// Find the important cells that can be the place that the robots can stop
    REP(i, 1, n) REP(j, 1, m) if (inside(i, j)) {
        bool important = 0;
        if (a[i][j] >= '0' && a[i][j] <= '9') important = true;
        
        if (!important) {
            FOR(d, 4) if (!inside(i + dx[d], j + dy[d])) {
                important = true;
                break;
            }
        }

        if (important) id[i][j] = ++cnt;

        // if (id[i][j] > 0) cout << id[i][j] << ' ' << i << ' ' << j << ln;
    }

    adj.assign(cnt + 1, vi());

    REP(i, 1, n) REP(j, 1, m) if (id[i][j] > 0) { // go from an important cell
        FOR(d, 4) if (!inside(i + dx[d], j + dy[d])) {
            int u = i, v = j, cur_d = d;
            while(inside(u, v)) {
                if (id[u][v] > 0 && id[u][v] != id[i][j]) {
                    adj[id[u][v]].eb(id[i][j]);
                    // cout << id[u][v] << " -> " << id[i][j] << ln;
                }

                if (a[u][v] == 'A') cur_d = (cur_d + 3) % 4;
                else if (a[u][v] == 'C') cur_d = (cur_d + 1) % 4;

                u -= dx[cur_d];
                v -= dy[cur_d];
            }       
        }
    }
}

vector<int> dp[10][10];

void solve(){   
    REP(i, 1, numRobot) REP(j, 1, numRobot) dp[i][j].assign(cnt + 1, INF);
    REP(i, 1, n) REP(j, 1, m) if (a[i][j] >= '0' && a[i][j] <= '9')
        dp[a[i][j] - '0'][a[i][j] - '0'][id[i][j]] = 0;
    
    REPD(l, numRobot, 1) REP(r, l, numRobot) {
        vector<pii> v;
        priority_queue<pii, vector<pii> , greater<pii>> heap;
        REP(i, 1, cnt) {
            REP(k, l, r - 1) minimize(dp[l][r][i] , dp[l][k][i] + dp[k + 1][r][i]);

            heap.push({dp[l][r][i], i});
            // v.eb(dp[l][r][i], i);
            // cout << dp[l][r][i] << ' ' << i << ln;
        }


        while(!heap.empty())  {
            auto [du, u] = heap.top(); heap.pop();
            if (du > dp[l][r][u]) continue;
            // cout << u << ' ' << du << ln;
            
        //     cout << l << ' ' << r  << ' ' << u << ' ' << du << ln;
            for (int v : adj[u])  if (minimize(dp[l][r][v], du + 1)) {
                heap.push({dp[l][r][v], v});
                // cout << l << ' ' << r << ' ' << v << ln;
            }
        }
        // sort(ALL(v));
        // vector<int> cur, nxt;
        // int ptr = 0;
        // while(!cur.empty() || ptr < v.size()) {
        //     if (cur.empty()) {
        //         if (dp[l][r][v[ptr].second] == v[ptr].first) cur.eb(v[ptr].second);
        //         ++ptr;
        //         continue;
        //     }
            
        //     while(ptr < v.size() && dp[l][r][v[ptr].second] == dp[l][r][cur.back()]) {
        //         cur.eb(v[ptr].second);
        //         ++ptr;
        //     }

        //     nxt.clear();
        //     while(!cur.empty()) {
        //         int u = cur.back(); cur.pop_back();
        //         for (int v : adj[u]) if (minimize(dp[l][r][v] , dp[l][r][u] + 1))  nxt.eb(v);
        //     }   

        //     swap(cur, nxt);
        // }
    }
    int res = INF;
    REP(i, 1, cnt) minimize(res, dp[1][numRobot][i]);
    if (res == INF) cout << "-1";
    else cout << res << ln;
}   

int main(){
    sinh_();
    io_faster
    file();
    int t = 1;
//    cin >> t;
    while (t--){
        readip();
        solve();
    }
}

Compilation message (stderr)

robots.cpp: In function 'void readip()':
robots.cpp:92:28: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   92 | #define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
      |                            ^
robots.cpp:155:5: note: in expansion of macro 'REP'
  155 |     REP(i, 1, n) REP(j, 1, m) cin >> a[i][j];
      |     ^~~
robots.cpp:92:28: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   92 | #define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
      |                            ^
robots.cpp:155:18: note: in expansion of macro 'REP'
  155 |     REP(i, 1, n) REP(j, 1, m) cin >> a[i][j];
      |                  ^~~
robots.cpp:92:28: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   92 | #define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
      |                            ^
robots.cpp:158:5: note: in expansion of macro 'REP'
  158 |     REP(i, 1, n) REP(j, 1, m) if (inside(i, j)) {
      |     ^~~
robots.cpp:92:28: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   92 | #define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
      |                            ^
robots.cpp:158:18: note: in expansion of macro 'REP'
  158 |     REP(i, 1, n) REP(j, 1, m) if (inside(i, j)) {
      |                  ^~~
robots.cpp:94:28: warning: unnecessary parentheses in declaration of 'd' [-Wparentheses]
   94 | #define FOR(i, n) for (int (i) = 0; (i) < (n); ++(i))
      |                            ^
robots.cpp:163:13: note: in expansion of macro 'FOR'
  163 |             FOR(d, 4) if (!inside(i + dx[d], j + dy[d])) {
      |             ^~~
robots.cpp:92:28: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   92 | #define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
      |                            ^
robots.cpp:176:5: note: in expansion of macro 'REP'
  176 |     REP(i, 1, n) REP(j, 1, m) if (id[i][j] > 0) { // go from an important cell
      |     ^~~
robots.cpp:92:28: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   92 | #define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
      |                            ^
robots.cpp:176:18: note: in expansion of macro 'REP'
  176 |     REP(i, 1, n) REP(j, 1, m) if (id[i][j] > 0) { // go from an important cell
      |                  ^~~
robots.cpp:94:28: warning: unnecessary parentheses in declaration of 'd' [-Wparentheses]
   94 | #define FOR(i, n) for (int (i) = 0; (i) < (n); ++(i))
      |                            ^
robots.cpp:177:9: note: in expansion of macro 'FOR'
  177 |         FOR(d, 4) if (!inside(i + dx[d], j + dy[d])) {
      |         ^~~
robots.cpp: In function 'void solve()':
robots.cpp:92:28: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   92 | #define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
      |                            ^
robots.cpp:198:5: note: in expansion of macro 'REP'
  198 |     REP(i, 1, numRobot) REP(j, 1, numRobot) dp[i][j].assign(cnt + 1, INF);
      |     ^~~
robots.cpp:92:28: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   92 | #define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
      |                            ^
robots.cpp:198:25: note: in expansion of macro 'REP'
  198 |     REP(i, 1, numRobot) REP(j, 1, numRobot) dp[i][j].assign(cnt + 1, INF);
      |                         ^~~
robots.cpp:92:28: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   92 | #define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
      |                            ^
robots.cpp:199:5: note: in expansion of macro 'REP'
  199 |     REP(i, 1, n) REP(j, 1, m) if (a[i][j] >= '0' && a[i][j] <= '9')
      |     ^~~
robots.cpp:92:28: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   92 | #define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
      |                            ^
robots.cpp:199:18: note: in expansion of macro 'REP'
  199 |     REP(i, 1, n) REP(j, 1, m) if (a[i][j] >= '0' && a[i][j] <= '9')
      |                  ^~~
robots.cpp:93:29: warning: unnecessary parentheses in declaration of 'l' [-Wparentheses]
   93 | #define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
      |                             ^
robots.cpp:202:5: note: in expansion of macro 'REPD'
  202 |     REPD(l, numRobot, 1) REP(r, l, numRobot) {
      |     ^~~~
robots.cpp:92:28: warning: unnecessary parentheses in declaration of 'r' [-Wparentheses]
   92 | #define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
      |                            ^
robots.cpp:202:26: note: in expansion of macro 'REP'
  202 |     REPD(l, numRobot, 1) REP(r, l, numRobot) {
      |                          ^~~
robots.cpp:92:28: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   92 | #define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
      |                            ^
robots.cpp:205:9: note: in expansion of macro 'REP'
  205 |         REP(i, 1, cnt) {
      |         ^~~
robots.cpp:92:28: warning: unnecessary parentheses in declaration of 'k' [-Wparentheses]
   92 | #define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
      |                            ^
robots.cpp:206:13: note: in expansion of macro 'REP'
  206 |             REP(k, l, r - 1) minimize(dp[l][r][i] , dp[l][k][i] + dp[k + 1][r][i]);
      |             ^~~
robots.cpp:215:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  215 |             auto [du, u] = heap.top(); heap.pop();
      |                  ^
robots.cpp:92:28: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   92 | #define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
      |                            ^
robots.cpp:250:5: note: in expansion of macro 'REP'
  250 |     REP(i, 1, cnt) minimize(res, dp[1][numRobot][i]);
      |     ^~~
robots.cpp: In function 'void file()':
robots.cpp:125:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  125 |         freopen(PROB".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
robots.cpp:126:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  126 |         freopen(PROB".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...