The provided code is a C++ program that calculates the minimum "move" cost based on input costs arranged in a 3x3 matrix, using permutations of a set of types (0, 1, 2). Here's a step-by-step explanation of the code:
Includes and Usings
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
- This block includes necessary header files.
iostream for input/output operations, algorithm for functions such as swap and min, and vector to use dynamic arrays (vectors).
Global Variables
#define endl "\n";
vector<int> types = {0, 1, 2};
vector<int> types_res;
- A preprocessor directive defines
endl to output a newline.
types is initialized globally with three integers 0, 1, 2, which represent different types.
types_res is a global vector that will store the result of the permutations of types.
Function: hoan_vi
void hoan_vi(vector<int> &types, int l, int r) {
if (l == r) types_res.assign(types.begin(), types.end());
else {
for (int i = l; i <= r; i++) {
swap(types[l], types[i]);
hoan_vi(types, l + 1, r);
swap(types[l], types[i]);
}
}
}
- This is a recursive function that generates all permutations of the
types vector.
- When
l equals r, it assigns the current permutation to types_res.
- For
l less than r, it iterates through the elements, swapping each element with the current position l, recursively calling itself to generate all permutations for the remaining elements, then swaps back to restore the original order for the next iteration.
Main Function
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int sum = 0;
vector<vector<int>> a(3, vector<int>(3));
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cin >> a[i][j];
sum += a[i][j];
}
}
- The main function starts with fast input/output settings.
- Declares a 2D vector
a to store costs associated with each combination of types and space.
- It computes the sum of all input costs while reading values into the 3x3 matrix
a.
Finding Minimum Moves
int min_move = INT_MAX;
int i = 0;
while (i <= 6) {
hoan_vi(types, 0, 2);
int move = 0;
for (int spc = 0; spc < 3; spc++) {
for (int type = 0; type < 3; type++) {
if (type != types_res[spc]) {
move += a[spc][type];
}
}
}
min_move = min(min_move, move);
i++;
}
cout << min_move;
}
min_move is initialized to a large value to ensure any calculated move will be less.
- A loop runs exactly 7 times (covering all permutations), calling the
hoan_vi function to get a permutation of types.
- Within this loop, another nested loop calculates the cost of aligning types with their corresponding spaces based on the
types_res from permutations.
- It sums costs for mismatching types and updates
min_move if a lower cost is found.
- Finally, the minimum move cost is printed to the console.
Summary
The program is designed to compute the minimum cost of rearranging types (represented as columns in the cost matrix) such that types align with specific spaces in a manner that minimizes the total move cost assessed from the cost matrix. The use of permutation generation allows exploring all possible configurations to ensure finding the optimal solution.