답안 #969852

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
969852 2024-04-25T16:56:47 Z steveonalex 로봇 (APIO13_robots) C++17
60 / 100
1500 ms 163840 KB
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
 
#define ALL(v) (v).begin(), (v).end()
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
 
// mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng(1);
ll rngesus(ll l, ll r){return ((ull) rng()) % (r - l + 1) + l;}
 
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
 
ll LASTBIT(ll mask){return mask & (-mask);}
ll pop_cnt(ll mask){return __builtin_popcountll(mask);}
ll ctz(ll mask){return __builtin_ctzll(mask);}
ll clz(ll mask){return __builtin_clzll(mask);}
ll logOf(ll mask){return 63 - clz(mask);}
 
template <class T1, class T2>
    bool minimize(T1 &a, T2 b){
        if (a > b){a = b; return true;}
        return false;
    }
template <class T1, class T2>
    bool maximize(T1 &a, T2 b){
        if (a < b){a = b; return true;}
        return false;
    }
template <class T>
    void printArr(T& a, string separator = " ", string finish = "\n", ostream& out = cout){
        for(auto i: a) out << i << separator;
        out << finish;
    }
template <class T>
    void remove_dup(vector<T> &a){
        sort(ALL(a));
        a.resize(unique(ALL(a)) - a.begin());
    }
 
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
 
const int N = 505, K = 9, INF = 1e9 + 69;
 
enum GridObj{none=0, obstacle=1, anti_clockwise=2, clockwise=3};
 
int n, w, h;
int dp[K][K][N][N];
int dis[N][N][4];
bool visited[N][N][4];
 
 
GridObj grid[N][N];
pair<int, int> pos[K];
 
vector<array<short, 3>> q[N * N / 2];
 
void dijkstra(int l, int r){
    memset(dis, 63, sizeof dis);
    memset(visited, 0, sizeof visited);
    int cnt = 0;
    if (l < r){
        for(int mid = l; mid < r; ++mid){
            for(int i = 1; i<=w; ++i) for(int j = 1;j <=h; ++j) 
                minimize(dp[l][r-l][i][j], dp[l][mid-l][i][j] + dp[mid+ 1][r-mid-1][i][j]);
        }
    }
    for(int i = 1; i<=w; ++i) for(int j = 1;j <=h; ++j) if (dp[l][r-l][i][j] < INF) {
        for(int x = 0; x < 4; ++x){
            dis[i][j][x] = dp[l][r-l][i][j];
            q[dp[l][r-l][i][j]].push_back({{i, j, x}});
            cnt++;
        }
    }
 
    for(int distance = 0; cnt > 0; ++distance){
        while(q[distance].size()){
            array<short, 3> u = q[distance].back(); q[distance].pop_back();
            cnt--;
 
            // cout << distance << " "; printArr(u); cout.flush();
 
            if (visited[u[0]][u[1]][u[2]]) continue;
            visited[u[0]][u[1]][u[2]] = true;
            
            array<short, 3> v = {{u[0] + dx[u[2]], u[1]+ dy[u[2]], u[2]}};
            if (grid[v[0]][v[1]] == anti_clockwise) v[2] = (v[2] + 1) % 4;
            else if (grid[v[0]][v[1]] == clockwise) v[2] = (v[2] + 3) % 4;
 
            if (grid[v[0]][v[1]] == obstacle){
                minimize(dp[l][r-l][u[0]][u[1]], distance + 1);
                for(int x = 0; x < 4; ++x){
                    array<short, 3> v = {{u[0] + dx[x], u[1] + dy[x], x}};
                    if (grid[v[0]][v[1]] == anti_clockwise) v[2] = (v[2] + 1) % 4;
                    else if (grid[v[0]][v[1]] == clockwise) v[2] = (v[2] + 3) % 4;
                    if (grid[v[0]][v[1]] != obstacle && minimize(dis[v[0]][v[1]][v[2]], distance + 1)) {
                        q[distance + 1].push_back(v);
                        cnt++;
                    }
                }
            }
            else {
                if (minimize(dis[v[0]][v[1]][v[2]], distance)) {
                    q[distance].push_back(v);
                    cnt++;
                }
            }
        }
    }
 
    // for(int i = 1; i<=w; ++i) {
    //     for(int j = 1; j<=h; ++j) cout << dp[l][r][i][j] << " ";
    //     cout << "\n";
    // }
    // cout << "\n";
}
 
int main(void){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
 
    cin >> n >> w >> h;
    swap(w, h);
    if (n == 1){
        cout << 0 << "\n";
        return 0;
    }
    for(int i = 0; i<=w+1; ++i) 
    for(int j = 0; j<=h+1; ++j) grid[i][j] = obstacle;
    for(int i = 0; i<w; ++i){
        string s; cin >> s;
        for(int j = 0; j<h; ++j){
            if (s[j] == '.') grid[i+1][j+1]=none;
            else if (s[j] == 'x') grid[i+1][j+1] = obstacle;
            else if (s[j] == 'A') grid[i+1][j+1] = anti_clockwise;
            else if (s[j] == 'C') grid[i+1][j+1] = clockwise;
            else {
                grid[i+1][j+1]=none;
                pos[s[j] - '0' - 1] = {i + 1, j + 1};
            }
        }
    }
 
    memset(dp, 63, sizeof dp);
 
    for(int i = 0; i<n; ++i){
        dp[i][0][pos[i].first][pos[i].second] = 0;
    }
 
    for(int i = n-1; i>=0; --i)
    for(int j = i; j<n; ++j){
        dijkstra(i, j);
    }
    
    int ans = INF;
    for(int i = 1; i<=w; ++i) for(int j = 1; j<=w; ++j) 
        minimize(ans, dp[0][n-1][i][j]);
 
    if (ans == INF) ans = -1;
    cout << ans << "\n";
 
 
    return 0;
}

Compilation message

robots.cpp: In function 'void dijkstra(int, int)':
robots.cpp:77:45: warning: narrowing conversion of 'i' from 'int' to 'short int' [-Wnarrowing]
   77 |             q[dp[l][r-l][i][j]].push_back({{i, j, x}});
      |                                             ^
robots.cpp:77:48: warning: narrowing conversion of 'j' from 'int' to 'short int' [-Wnarrowing]
   77 |             q[dp[l][r-l][i][j]].push_back({{i, j, x}});
      |                                                ^
robots.cpp:77:51: warning: narrowing conversion of 'x' from 'int' to 'short int' [-Wnarrowing]
   77 |             q[dp[l][r-l][i][j]].push_back({{i, j, x}});
      |                                                   ^
robots.cpp:92:40: warning: narrowing conversion of '(((int)u.std::array<short int, 3>::operator[](0)) + dx[((int)u.std::array<short int, 3>::operator[](2))])' from 'int' to 'short int' [-Wnarrowing]
   92 |             array<short, 3> v = {{u[0] + dx[u[2]], u[1]+ dy[u[2]], u[2]}};
robots.cpp:92:56: warning: narrowing conversion of '(((int)u.std::array<short int, 3>::operator[](1)) + dy[((int)u.std::array<short int, 3>::operator[](2))])' from 'int' to 'short int' [-Wnarrowing]
   92 |             array<short, 3> v = {{u[0] + dx[u[2]], u[1]+ dy[u[2]], u[2]}};
robots.cpp:99:48: warning: narrowing conversion of '(((int)u.std::array<short int, 3>::operator[](0)) + dx[x])' from 'int' to 'short int' [-Wnarrowing]
   99 |                     array<short, 3> v = {{u[0] + dx[x], u[1] + dy[x], x}};
robots.cpp:99:62: warning: narrowing conversion of '(((int)u.std::array<short int, 3>::operator[](1)) + dy[x])' from 'int' to 'short int' [-Wnarrowing]
   99 |                     array<short, 3> v = {{u[0] + dx[x], u[1] + dy[x], x}};
robots.cpp:99:71: warning: narrowing conversion of 'x' from 'int' to 'short int' [-Wnarrowing]
   99 |                     array<short, 3> v = {{u[0] + dx[x], u[1] + dy[x], x}};
      |                                                                       ^
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 90204 KB Output is correct
2 Correct 12 ms 90204 KB Output is correct
3 Correct 13 ms 90296 KB Output is correct
4 Correct 13 ms 90204 KB Output is correct
5 Correct 12 ms 90200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 90204 KB Output is correct
2 Correct 12 ms 90204 KB Output is correct
3 Correct 13 ms 90296 KB Output is correct
4 Correct 13 ms 90204 KB Output is correct
5 Correct 12 ms 90200 KB Output is correct
6 Correct 14 ms 90204 KB Output is correct
7 Correct 12 ms 90456 KB Output is correct
8 Correct 12 ms 90204 KB Output is correct
9 Correct 13 ms 90128 KB Output is correct
10 Correct 13 ms 90200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 90204 KB Output is correct
2 Correct 12 ms 90204 KB Output is correct
3 Correct 13 ms 90296 KB Output is correct
4 Correct 13 ms 90204 KB Output is correct
5 Correct 12 ms 90200 KB Output is correct
6 Correct 14 ms 90204 KB Output is correct
7 Correct 12 ms 90456 KB Output is correct
8 Correct 12 ms 90204 KB Output is correct
9 Correct 13 ms 90128 KB Output is correct
10 Correct 13 ms 90200 KB Output is correct
11 Correct 119 ms 101524 KB Output is correct
12 Correct 2 ms 4700 KB Output is correct
13 Correct 69 ms 90456 KB Output is correct
14 Correct 247 ms 106984 KB Output is correct
15 Correct 67 ms 93204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 90204 KB Output is correct
2 Correct 12 ms 90204 KB Output is correct
3 Correct 13 ms 90296 KB Output is correct
4 Correct 13 ms 90204 KB Output is correct
5 Correct 12 ms 90200 KB Output is correct
6 Correct 14 ms 90204 KB Output is correct
7 Correct 12 ms 90456 KB Output is correct
8 Correct 12 ms 90204 KB Output is correct
9 Correct 13 ms 90128 KB Output is correct
10 Correct 13 ms 90200 KB Output is correct
11 Correct 119 ms 101524 KB Output is correct
12 Correct 2 ms 4700 KB Output is correct
13 Correct 69 ms 90456 KB Output is correct
14 Correct 247 ms 106984 KB Output is correct
15 Correct 67 ms 93204 KB Output is correct
16 Correct 60 ms 90536 KB Output is correct
17 Execution timed out 2646 ms 163840 KB Time limit exceeded
18 Halted 0 ms 0 KB -