Submission #1188233

#TimeUsernameProblemLanguageResultExecution timeMemory
1188233ainunnajibRobots (APIO13_robots)C++20
0 / 100
0 ms320 KiB
#include <iostream> #include <vector> #include <string> #include <queue> #include <set> #include <map> #include <tuple> // For std::tie used in comparison #include <vector> #include <algorithm> // For std::sort #include <cctype> // For std::isdigit using namespace std; // Struct to represent a robot (original or composite) struct Robot { int min_l, max_l; // Min and max labels of merged robots int r, c; // Row and column position // Comparison operator for sorting and using in std::set/map. bool operator<(const Robot& other) const { return tie(min_l, max_l, r, c) < tie(other.min_l, other.max_l, other.r, other.c); } bool operator==(const Robot& other) const { return tie(min_l, max_l, r, c) == tie(other.min_l, other.max_l, other.r, other.c); } }; // --- Constants and Global Variables --- const int MAX_H = 501; // Max dimensions + 1 for safety const int MAX_W = 501; int N; // Number of initial robots int W; // Grid width int H; // Grid height vector<string> grid; // Grid layout ('x', '.', 'A', 'C') vector<pair<int, int>> initial_pos; // Store initial positions for robots 1 to N // Directions: 0:Up (-1,0), 1:Right (0,1), 2:Down (1,0), 3:Left (0,-1) int dr[] = {-1, 0, 1, 0}; int dc[] = {0, 1, 0, -1}; // --- Precomputation Data Structure --- // Stores the final {row, col} after pushing from grid[r][c] in direction start_dir pair<int, int> precomputed_moves[MAX_H][MAX_W][4]; // Function to check if a cell is valid (within bounds and not an obstacle) bool isValid(int r, int c) { return r >= 0 && r < H && c >= 0 && c < W && grid[r][c] != 'x'; } // --- Precomputation Function --- // Simulates all possible moves once and stores the results. void precompute_all_moves() { cout << "Precomputing moves..." << endl; // Debug output for (int r = 0; r < H; ++r) { for (int c = 0; c < W; ++c) { // Cannot start move from an obstacle if (grid[r][c] == 'x') { for (int start_dir = 0; start_dir < 4; ++start_dir) { precomputed_moves[r][c][start_dir] = {-1, -1}; // Indicate invalid start } continue; } // Precompute for each of the 4 initial push directions for (int start_dir = 0; start_dir < 4; ++start_dir) { int current_dir = start_dir; // This is the initial push direction // --- Simulation Logic (copied from original simulate_move) --- // 1. Handle initial rotation if starting on a plate when pushed if (grid[r][c] == 'A') { current_dir = (current_dir + 3) % 4; // Effective direction after rotation } else if (grid[r][c] == 'C') { current_dir = (current_dir + 1) % 4; // Effective direction after rotation } // 2. Move step by step until blocked int nr = r, nc = c; // Current position starts at the initial cell while (true) { int next_r = nr + dr[current_dir]; // Calculate next potential cell int next_c = nc + dc[current_dir]; // Check if the next step is valid if (!isValid(next_r, next_c)) { break; // Blocked by wall or obstacle, stop at current position (nr, nc) } // Move to the next cell nr = next_r; nc = next_c; // Check for rotation plate at the new cell and update direction for subsequent steps if (grid[nr][nc] == 'A') { current_dir = (current_dir + 3) % 4; // Turn anti-clockwise } else if (grid[nr][nc] == 'C') { current_dir = (current_dir + 1) % 4; // Turn clockwise } // Continue moving in the (potentially new) direction } // --- End Simulation Logic --- // Store the final resting position {nr, nc} for this starting cell and initial push direction precomputed_moves[r][c][start_dir] = {nr, nc}; } } } cout << "Precomputation finished." << endl; // Debug output } // --- Optimized simulate_move using precomputation --- // Returns the final {r, c} after pushing robot at (r, c) in initial direction 'dir'. // Assumes precompute_all_moves() has been called. pair<int, int> simulate_move(int r, int c, int dir) { // Directly return the precomputed result // Ensure r, c, dir are within bounds - should be guaranteed by caller return precomputed_moves[r][c][dir]; } // Function to perform all possible merges iteratively on the current set of robots. // Modifies the vector in place and sorts it canonically. // (No changes needed in this function for the precomputation optimization) void perform_merges(vector<Robot>& current_robots) { if (current_robots.size() < 2) return; bool merged_in_pass = true; while (merged_in_pass) { merged_in_pass = false; vector<Robot> next_round_robots; vector<bool> processed(current_robots.size(), false); sort(current_robots.begin(), current_robots.end(), [](const Robot& a, const Robot& b){ return tie(a.r, a.c, a.min_l, a.max_l) < tie(b.r, b.c, b.min_l, b.max_l); }); for (int i = 0; i < current_robots.size(); ++i) { if (processed[i]) continue; bool merged_robot_i = false; for (int j = i + 1; j < current_robots.size(); ++j) { if (processed[j]) continue; if (current_robots[i].r != current_robots[j].r || current_robots[i].c != current_robots[j].c) { break; } if (current_robots[i].max_l + 1 == current_robots[j].min_l || current_robots[j].max_l + 1 == current_robots[i].min_l) { Robot merged_robot; merged_robot.min_l = min(current_robots[i].min_l, current_robots[j].min_l); merged_robot.max_l = max(current_robots[i].max_l, current_robots[j].max_l); merged_robot.r = current_robots[i].r; merged_robot.c = current_robots[i].c; next_round_robots.push_back(merged_robot); processed[i] = true; processed[j] = true; merged_in_pass = true; merged_robot_i = true; break; } } if (!processed[i]) { next_round_robots.push_back(current_robots[i]); processed[i] = true; } } current_robots = next_round_robots; if (current_robots.size() < 2) break; } sort(current_robots.begin(), current_robots.end()); } // Solves the problem using BFS with precomputed moves int solve() { // 1. Create the initial state vector vector<Robot> initial_state; for (int i = 0; i < N; ++i) { initial_state.push_back({i + 1, i + 1, initial_pos[i].first, initial_pos[i].second}); } perform_merges(initial_state); // Handle initial merges and sort // 2. Initialize BFS queue and visited set queue<pair<vector<Robot>, int>> q; set<vector<Robot>> visited; q.push({initial_state, 0}); visited.insert(initial_state); // 3. Start BFS loop while (!q.empty()) { vector<Robot> current_robots = q.front().first; int current_pushes = q.front().second; q.pop(); // 4. Goal Check if (current_robots.size() == 1 && current_robots[0].min_l == 1 && current_robots[0].max_l == N) { return current_pushes; } // 5. Generate Next States for (int i = 0; i < current_robots.size(); ++i) { for (int dir_idx = 0; dir_idx < 4; ++dir_idx) { vector<Robot> next_robots = current_robots; Robot moving_robot = next_robots[i]; // *** Use precomputed move result *** pair<int, int> final_pos = simulate_move(moving_robot.r, moving_robot.c, dir_idx); // Check if the move was valid (precomputation might return {-1,-1} if start was invalid) // Although the loop should only consider robots at valid positions. Add check for safety? // if (final_pos.first == -1) continue; // Should not happen if robot starts validly // Update position next_robots[i].r = final_pos.first; next_robots[i].c = final_pos.second; // Perform merges perform_merges(next_robots); // 6. Check Visited and Enqueue if (visited.find(next_robots) == visited.end()) { visited.insert(next_robots); q.push({next_robots, current_pushes + 1}); } } } } // 7. Goal Not Reachable return -1; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> N >> W >> H; grid.resize(H); initial_pos.resize(N); vector<bool> pos_found(N, false); for (int i = 0; i < H; ++i) { cin >> grid[i]; for (int j = 0; j < W; ++j) { if (isdigit(grid[i][j])) { int robot_label = grid[i][j] - '0'; int robot_id = robot_label - 1; if (robot_id >= 0 && robot_id < N) { initial_pos[robot_id] = {i, j}; pos_found[robot_id] = true; grid[i][j] = '.'; } else { cerr << "Warning: Found digit '" << grid[i][j] << "' that is not a valid robot label (1-" << N << ")." << endl; grid[i][j] = '.'; } } } } bool all_found = true; for(int i=0; i<N; ++i) { if (!pos_found[i]) { all_found = false; cerr << "Error: Robot " << (i+1) << " not found on the initial grid." << endl; return 1; } } // *** Call precomputation function before solving *** precompute_all_moves(); // Call the solver function and print the result cout << solve() << endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...