Submission #755948

# Submission time Handle Problem Language Result Execution time Memory
755948 2023-06-10T18:13:54 Z thimote75 Robots (APIO13_robots) C++14
30 / 100
1500 ms 8188 KB
#include <bits/stdc++.h>

using namespace std;

using idata = vector<int>;

template<typename T>
using  grid = vector<vector<T>>;
using igrid = grid<int>;

using pos = pair<int, int>;
using graph = grid<vector<pos>>;

using dist_pos = pair<int, pos>;
using dist_pqp = priority_queue<dist_pos, vector<dist_pos>, less<dist_pos>>;

const int WALL    = 0;
const int EMPTY   = 1;
const int WHEEL_C = 2; // clockwise
const int WHEEL_A = 3; // counter clockwise

const int INF = 1e9;

template<typename T>
void resize (grid<T> &data_grid, int width, int height, T def) {
    data_grid.resize(width, vector<T>(height, def));
}
template<typename T>
T get (grid<T> &data, pos pos) {
    return data[pos.first][pos.second];
}

template<typename T>
void show (T &o) {
    cout << o;
}
void show (int &x) {
    if (x == INF) cout << "-";
    else cout << x;
}
template<typename U, typename V>
void show (pair<U, V> &p) {
    cout << "(";
    show(p.first);
    cout << ", ";
    show(p.second);
    cout << ")";
}
template<typename T>
void show(vector<T> &vector) {
    cout << "[";
    for (T u : vector) {
        show(u);
        cout << ", ";
    }
    cout << "]";
}
template<typename T>
void show(grid<T> &data_grid, int width, int height) {
    for (int y = 0; y < height; y ++) {
        for (int x = 0; x < width; x ++)
            show(data_grid[x][y]);
        cout << endl;
    }
}

int nbRobots, width, height;
igrid type_grid;
graph roads;

bool is_valid (pos node) {
    int x = node.first;
    int y = node.second;

    return 0 <= x && x < width
        && 0 <= y && y < height;
}
pos get_next (pos node, int direction) {
    int x = node.first;
    int y = node.second;

    if (direction == 0) return { x + 1, y };
    if (direction == 1) return { x, y - 1 };
    if (direction == 2) return { x - 1, y };
    if (direction == 3) return { x, y + 1 };

    return node;
}
void dfs (pos start, pos node, int direction) {
    int type = get(type_grid, node);
    if (type == WALL) return ;
    if (type == WHEEL_A) direction --;
    if (type == WHEEL_C) direction ++;

    if (direction  < 0) direction += 4;
    if (direction == 4) direction  = 0;

    pos next = get_next(node, direction);

    if (node.first != start.first || node.second != start.second)
        roads[node.first][node.second].push_back(start);

    if (!is_valid(next)) return ;

    dfs(start, next, direction);
}
void create   (igrid &grid, pos start) {
    resize(grid, width, height, INF);

    if (start.first == -1 && start.second == -1) return ;
    grid[start.first][start.second] = 0;
}
void dijkstra (igrid &grid) {
    dist_pqp queue;
    
    for (int y = 0; y < height; y ++) {
        for (int x = 0; x < width; x ++) {
            if (grid[x][y] == INF) continue ;

            queue.push({ grid[x][y], { x, y } });
        }
    }

    while (queue.size() != 0) {
        dist_pos curr = queue.top();
        queue.pop();

        pos rpos = curr.second;
        int cost = curr.first;

        if (get(grid, rpos) != cost) continue ;

        cost ++;

        for (pos next : get(roads, rpos)) {
            if (get(grid, next) <= cost) continue ;

            grid[next.first][next.second] = cost;
            queue.push({ cost, next });
        }
    }
}
void merge (igrid &a, igrid &b, igrid &target) {
    for (int y = 0; y < height; y ++) {
        for (int x = 0; x < width; x ++) {
            target[x][y] = min(
                target[x][y],
                a[x][y] + b[x][y]
            );
        }
    }
}

vector<vector<igrid>> grids;

int main () {
    cin >> nbRobots >> width >> height;

    resize(type_grid, width, height, -1);
    resize(roads, width, height, vector<pos>());

    vector<pos> robots(nbRobots);

    for (int y = 0; y < height; y ++) {
        string buffer;
        cin >> buffer;

        for (int x = 0; x < width; x ++) {
            char c = buffer[x];

            int type = EMPTY;
            if (c == 'x') type = WALL;
            else if (c == 'A') type = WHEEL_A;
            else if (c == 'C') type = WHEEL_C;
            else if (c != '.') {
                int robot_id = (int) (c - '1');

                robots[robot_id] = { x, y };
            }

            type_grid[x][y] = type;
        }
    }

    int i = 0;
    for (int y = 0; y < height; y ++) {
        for (int x = 0; x < width; x ++) {
            for (int dir = 0; dir < 4; dir ++) {
                int opp = (2 + dir) % 4;

                pos node = { x, y };
                pos wall = get_next(node, opp);
                bool is_wall = (!is_valid(wall)) || (get(type_grid, wall) == WALL);

                if (!is_wall) continue ;

                dfs(node, node, dir);
            }
        }
    }

    grids.resize(nbRobots);

    for (int iR = 0; iR < nbRobots; iR ++) {
        grids[iR].resize(nbRobots - iR);

        create(grids[iR][0], robots[iR]);
        dijkstra(grids[iR][0]);
    }

    for (int size = 1; size < nbRobots; size ++) {
        for (int iR = 0; iR + size < nbRobots; iR ++) {
            create(grids[iR][size], { -1, -1 });

            for (int pivot_size = 0; pivot_size < size; pivot_size ++) {
                int other_size = size - 1 - pivot_size;

                merge( grids[iR][pivot_size], grids[iR + pivot_size + 1][other_size], grids[iR][size] );
            }

            dijkstra(grids[iR][size]);
        }
    }

    igrid &last_grid = grids[0][nbRobots - 1];

    int min_value = INF;
    for (int y = 0; y < height; y ++) {
        for (int x = 0; x < width; x ++) {
            min_value = min(min_value, last_grid[x][y]);
        }
    }

    if (min_value == INF) min_value = -1;
    cout << min_value << endl;
}

Compilation message

robots.cpp: In function 'int main()':
robots.cpp:186:9: warning: unused variable 'i' [-Wunused-variable]
  186 |     int i = 0;
      |         ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 304 KB Output is correct
8 Correct 1 ms 300 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 304 KB Output is correct
8 Correct 1 ms 300 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Execution timed out 1572 ms 8188 KB Time limit exceeded
12 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 304 KB Output is correct
8 Correct 1 ms 300 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Execution timed out 1572 ms 8188 KB Time limit exceeded
12 Halted 0 ms 0 KB -