Submission #890778

# Submission time Handle Problem Language Result Execution time Memory
890778 2023-12-22T03:04:23 Z Kutan Robots (APIO13_robots) C++14
100 / 100
1192 ms 50776 KB
// 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

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 time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 600 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 600 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 213 ms 18408 KB Output is correct
12 Correct 12 ms 4308 KB Output is correct
13 Correct 112 ms 13388 KB Output is correct
14 Correct 399 ms 18644 KB Output is correct
15 Correct 193 ms 18388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 600 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 213 ms 18408 KB Output is correct
12 Correct 12 ms 4308 KB Output is correct
13 Correct 112 ms 13388 KB Output is correct
14 Correct 399 ms 18644 KB Output is correct
15 Correct 193 ms 18388 KB Output is correct
16 Correct 36 ms 4444 KB Output is correct
17 Correct 1192 ms 49984 KB Output is correct
18 Correct 588 ms 49724 KB Output is correct
19 Correct 547 ms 50776 KB Output is correct
20 Correct 803 ms 49816 KB Output is correct