Submission #890767

# Submission time Handle Problem Language Result Execution time Memory
890767 2023-12-22T02:42:23 Z Kutan Robots (APIO13_robots) C++14
100 / 100
613 ms 51412 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;
        REP(i, 1, cnt) {
            REP(k, l, r - 1) minimize(dp[l][r][i] , dp[l][k][i] + dp[k + 1][r][i]);
            v.eb(dp[l][r][i], i);
        }
        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:204:9: note: in expansion of macro 'REP'
  204 |         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:205:13: note: in expansion of macro 'REP'
  205 |             REP(k, l, r - 1) minimize(dp[l][r][i] , dp[l][k][i] + dp[k + 1][r][i]);
      |             ^~~
robots.cpp:211:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  211 |         while(!cur.empty() || ptr < v.size()) {
      |                               ~~~~^~~~~~~~~~
robots.cpp:218:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  218 |             while(ptr < v.size() && dp[l][r][v[ptr].second] == dp[l][r][cur.back()]) {
      |                   ~~~~^~~~~~~~~~
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:233:5: note: in expansion of macro 'REP'
  233 |     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 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 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 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 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 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 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 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 464 KB Output is correct
11 Correct 117 ms 18732 KB Output is correct
12 Correct 8 ms 4308 KB Output is correct
13 Correct 114 ms 13656 KB Output is correct
14 Correct 137 ms 18472 KB Output is correct
15 Correct 98 ms 18472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 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 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 464 KB Output is correct
11 Correct 117 ms 18732 KB Output is correct
12 Correct 8 ms 4308 KB Output is correct
13 Correct 114 ms 13656 KB Output is correct
14 Correct 137 ms 18472 KB Output is correct
15 Correct 98 ms 18472 KB Output is correct
16 Correct 25 ms 4700 KB Output is correct
17 Correct 415 ms 50376 KB Output is correct
18 Correct 276 ms 49968 KB Output is correct
19 Correct 613 ms 51412 KB Output is correct
20 Correct 369 ms 49976 KB Output is correct