# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
47629 | fallingstar | Riddick's Cube (IZhO13_riddicks) | C++17 | 7 ms | 620 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int NO_SOLUTION = 100500;
int n, m, a[5][5], b[5][5];
int complexity = NO_SOLUTION;
void ShiftCols(int);
void ShiftRows(int row, int steps)
{
if (row == m)
{
auto check = [&] {
for (int i = 0; i < n; ++i)
for (int j = 1; j < m; ++j)
if (b[j][i] != b[0][i]) return false;
return true;
};
if (check())
complexity = min(complexity, steps);
ShiftCols(steps);
}
else
for (int i = 0; i < n; ++i) // position of row "row"
{
copy(a[row], a[row] + n, b[row]);
rotate(b[row], b[row] + i, b[row] + n);
ShiftRows(row + 1, steps + min(i, n - i));
}
}
int c[5][5];
void ShiftCols(int steps) // swap x and y again -> actually shifting rows
{
for (int i = 0; i < m; ++i) // position of first row
{
// row 0 of c is col 0 of b
for (int j = 0; j < m; ++j)
c[0][j] = b[j][0];
rotate(c[0], c[0] + i, c[0] + m);
auto calc = [&] {
int tot_cost = 0; // cost for this fixed first row
for (int i = 1; i < n; ++i) // for each row 1 -> n - 1
{
int row_cost = NO_SOLUTION; // cost for i-th row
for (int j = 0; j < m; ++j) // position of i-th row
{
// row i of c is col i of b
for (int k = 0; k < m; ++k)
c[i][k] = b[k][i];
rotate(c[i], c[i] + j, c[i] + m);
if (equal(c[0], c[0] + m, c[i]))
row_cost = min(row_cost, min(j, m - j));
}
if (row_cost == NO_SOLUTION)
return NO_SOLUTION;
tot_cost += row_cost;
}
return tot_cost;
};
complexity = min(complexity, steps + min(i, m - i) + calc());
}
}
int main()
{
cin >> n >> m;
vector<int> values;
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
{
cin >> a[j][i]; // swap x and y -> shift rows then columns
values.push_back(a[j][i]);
}
sort(values.begin(), values.end());
values.resize(unique(values.begin(), values.end()) - values.begin());
if (values.size() > max(n, m))
{
cout << NO_SOLUTION;
return 0;
}
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
{
a[i][j] = lower_bound(values.begin(), values.end(), a[i][j]) - values.begin();
b[i][j] = a[i][j]; // copy of a
}
ShiftRows(0, 0);
cout << complexity;
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |