To solve the maze problem where you need to determine the minimal number of distinct digits and the corresponding minimal cost for a path from any cell in the first column to any cell in the last column, we can employ a Dijkstra-like algorithm with a specialized state management approach. Here's a comprehensive C++ solution to address the problem:
Algorithm Overview
-
Input Parsing:
- Read the size of the maze
N.
- Read the costs associated with each digit from
0 to 9.
- Read the maze grid, which consists of
N strings each containing N digits.
-
State Definition:
- Each state in our search will be defined by the current cell's coordinates
(x, y), a bitmask representing the set of distinct digits encountered so far, and the total cost incurred up to that point.
-
Priority Queue:
- Use a priority queue to always expand the state with the smallest number of distinct digits first. If two states have the same number of distinct digits, prioritize the one with the lower cost.
-
Visited Array:
- Maintain a 3D visited array to keep track of the minimum cost required to reach a particular cell with a specific set of digits.
-
Dijkstra-like Expansion:
- Initialize the queue with all cells from the first column.
- Continuously expand the state with the highest priority (fewest digits, lowest cost).
- For each neighboring cell (up, down, left, right), calculate the new mask and cost.
- If this new state offers a better path (fewer digits or lower cost), add it to the queue.
-
Termination:
- Once a state reaches the last column, output the number of distinct digits and the total cost, then terminate.
C++ Implementation
#include <bits/stdc++.h>
using namespace std;
// Structure to represent each state in the priority queue
struct State {
int k; // Number of distinct digits used
int c; // Total cost incurred
int x; // Current row
int y; // Current column
int mask; // Bitmask representing the set of distinct digits
};
// Comparator for the priority queue to prioritize states with fewer digits and lower cost
struct Compare {
bool operator()(const State& a, const State& b) const {
if (a.k != b.k)
return a.k > b.k; // Prioritize fewer digits
return a.c > b.c; // If digits are equal, prioritize lower cost
}
};
int main(){
// File I/O setup
ifstream fin("MAZE2024.INP");
ofstream fout("MAZE2024.OUT");
int N;
fin >> N;
int v[10];
for(int i = 0; i < 10; i++) fin >> v[i];
vector<string> grid(N);
for(int i = 0; i < N; i++) fin >> grid[i];
// Initialize the visited array with INF
const int INF = 1e9;
static int visited_arr[100][100][1024];
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
for(int m = 0; m < 1024; m++) {
visited_arr[i][j][m] = INF;
}
}
}
// Priority queue setup
priority_queue<State, vector<State>, Compare> pq;
for(int x = 0; x < N; x++) {
int y = 0;
int digit = grid[x][y] - '0';
int mask = 1 << digit;
int c = v[digit];
int k = 1; // Initially, one distinct digit
visited_arr[x][y][mask] = c;
State s;
s.k = __builtin_popcount(mask);
s.c = c;
s.x = x;
s.y = y;
s.mask = mask;
pq.push(s);
}
// Directions: up, down, left, right
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
// Dijkstra-like search
while(!pq.empty()){
State current = pq.top();
pq.pop();
// Skip if a better state has already been processed
if(current.c > visited_arr[current.x][current.y][current.mask])
continue;
// Check if the current state has reached the last column
if(current.y == N-1){
fout << current.k << "\n" << current.c;
return 0;
}
// Explore all possible directions
for(int dir = 0; dir < 4; dir++) {
int nx = current.x + dx[dir];
int ny = current.y + dy[dir];
// Ensure the new position is within bounds
if(nx >= 0 && nx < N && ny >=0 && ny < N){
int digit = grid[nx][ny] - '0';
int new_mask = current.mask | (1 << digit);
int new_c = current.c + v[digit];
// If this path offers a better cost, update and add to the queue
if(new_c < visited_arr[nx][ny][new_mask]){
visited_arr[nx][ny][new_mask] = new_c;
State new_state;
new_state.k = __builtin_popcount(new_mask);
new_state.c = new_c;
new_state.x = nx;
new_state.y = ny;
new_state.mask = new_mask;
pq.push(new_state);
}
}
}
}
// If no path is found, output -1 (though the problem guarantees a path)
fout << "-1\n-1";
}
Explanation of the Example
Consider the following example input:
6
8 1 2 1 9 14 8 8 6 9
287566
123444
983070
071311
548739
353665
- Minimal Distinct Digits: 3
- Minimal Cost: 9
Path Explanation: One such path with 3 distinct digits and a total cost of 9 is highlighted in the problem statement's explanation. This path efficiently navigates through the maze by selecting cells that contribute minimally to both the distinct digit count and the total cost.
Conclusion
This solution efficiently navigates the maze by prioritizing paths with the fewest distinct digits and, among those, the lowest cost. The use of a bitmask to track distinct digits and a priority queue to manage state exploration ensures that the solution is both optimal and performant within the given constraints.