답안 #959909

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
959909 2024-04-09T10:03:24 Z Mr_Husanboy 로봇 (APIO13_robots) C++17
10 / 100
1 ms 348 KB
//#pragma GCC optimize("Ofast,O3")
//#pragma GCC target("avx,avx2")
#include <bits/stdc++.h>
using namespace std;



#ifdef LOCAL
#include "debugger.cpp"
#else
#define debug(...)
#endif
template<class T>
int len(T &a){
    return a.size();
}

using ll = long long;
using pii = pair<int,int>;
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
string fileio = "";

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const int MOD = 1e9 + 7;
const int inf = 1e9;
const ll infl = 1e18;
const int maxn = 1e5 + 1;





void solve(){
    int k, n, m; cin >> k >> n >> m;
    swap(n, m);

    vector<string> grid(n);
    vector<int> st(k);

    for(int i = 0; i < n; i ++){
        cin >> grid[i];
        for(int j = 0; j < m; j ++){
            if(grid[i][j] >= '1' && grid[i][j] <= '9') st[grid[i][j] - '1'] = i * m + j;
        }
    }

    vector memo(n, vector(m, vector(4, pair{-1, -1}))); 

    vector used(n, vector(m, vector(4, false)));

    auto find = [&](auto &find, int i, int j, int dir)->pair<int,int>{
        if(memo[i][j][dir].ff != -1) return memo[i][j][dir];
        //if(used[i][j][dir]) return {inf,inf};
        //used[i][j][dir] = 1;
        if(grid[i][j] == 'A') dir --;
        if(grid[i][j] == 'C') dir ++;
        dir = (dir + 4) % 4;
        pair<int,int> res = {i, j};
        if(dir == 0){
            if(i > 0 && grid[i - 1][j] != 'x') res = find(find, i - 1, j, dir);
        }else
        if(dir == 1){
            if(j + 1 < m && grid[i][j + 1] != 'x') res = find(find, i, j + 1, dir);
        }else
        if(dir == 2){
            if(i + 1 < n && grid[i + 1][j] != 'x') res = find(find, i + 1, j, dir);
        }else
            if(j > 0 && grid[i][j - 1] != 'x') res = find(find, i, j - 1, dir);

        return memo[i][j][dir] = res;
    };

    vector<vector<int>> g(n * m);
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j ++){
            if(grid[i][j] == 'x') continue;
            for(int dir = 0; dir < 4; dir ++){

                auto [ii, jj] = find(find, i, j, dir);
                if(ii == inf){
                    debug(i, j, dir);
                }
                if((ii == i && j == jj) || ii == inf) continue;
                //debug(i, j, dir, ii, jj);
                g[i * m + j].push_back(ii * m + jj);
            }
        }
    }

    vector<vector<int>> dis(k, vector<int> (n * m, inf));
    int vis[n * m];
    //debug(dis);
    for(int i = 0; i < k; i ++){
        queue<int> q; q.push(st[i]);
        memset(vis, 0, sizeof(vis));
        debug(i);
        vis[st[i]] = 1;
        dis[i][st[i]] = 0;
        while(!q.empty()){
            int t = q.front(); q.pop();
            //debug(t);
            for(auto u : g[t]){
                //debug(u);
                if(!vis[u]){
                    vis[u] = 1;
                    dis[i][u] = dis[i][t] + 1;
                    q.push(u);
                }
            }
        }
    }

    int mn = inf;
    for(int i = 0; i < n * m; i ++){
        mn = min(mn, dis[0][i] + dis[1][i]);
    }
    if(mn == inf){
        cout << -1;
    }else cout << mn;
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    auto start = chrono::high_resolution_clock::now();
    #ifndef LOCAL
    if(fileio.size()){
        freopen((fileio + ".in").c_str(), "r", stdin);
        freopen((fileio + ".out").c_str(), "w", stdout);
    }
    #endif
    int testcases = 1;
    #ifdef Tests
    cin >> testcases;
    #endif
    while(testcases --){
        solve();
        if(testcases) cout << '\n';
        #ifdef LOCAL
        else cout << '\n';
        cout << "_________________________" << endl;
        #endif
    }
    #ifdef LOCAL
    auto duration = chrono::duration_cast<chrono::microseconds>(chrono::high_resolution_clock::now() - start);
    
    cout << "time: " << (double)duration.count()/1000.0 << " milliseconds" << endl;
    #endif
    return 0;
}       

Compilation message

robots.cpp: In function 'int main()':
robots.cpp:126:10: warning: variable 'start' set but not used [-Wunused-but-set-variable]
  126 |     auto start = chrono::high_resolution_clock::now();
      |          ^~~~~
robots.cpp:129:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  129 |         freopen((fileio + ".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
robots.cpp:130:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  130 |         freopen((fileio + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Incorrect 0 ms 348 KB Output isn't correct
10 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Incorrect 0 ms 348 KB Output isn't correct
10 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Incorrect 0 ms 348 KB Output isn't correct
10 Halted 0 ms 0 KB -