#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
using namespace std;
using llong = long long;
int ans = 100500;
int a[5][5];
int n, m;
inline int Val(const vector<int>& col, const vector<int>& row)
{
static int b[5][5];
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
b[i][j] = a[i][j];
int res = 0;
for (int j = 0; j < m; ++j)
{
res += min(col[j], n - col[j]);
vector<int> cur(n);
for (int i = 0; i < n; ++i) cur[i] = b[i][j];
rotate(cur.begin(), cur.begin() + col[j], cur.end());
for (int i = 0; i < n; ++i) b[i][j] = cur[i];
}
for (int i = 0; i < n; ++i)
{
res += min(row[i], m - row[i]);
vector<int> cur(m);
for (int j = 0; j < m; ++j) cur[j] = b[i][j];
rotate(cur.begin(), cur.begin() + row[i], cur.end());
for (int j = 0; j < m; ++j) b[i][j] = cur[j];
}
bool ans1 = true, ans2 = true;
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
{
if (b[i][j] != b[i][0])
ans1 = false;
if (b[i][j] != b[0][j])
ans2 = false;
}
if (!ans1 && !ans2)
return 100500;
return res;
}
void Solve(vector<int>& col, vector<int>& row)
{
if (col.size() == m && row.size() == n)
{
ans = min(ans, Val(col, row));
return;
}
if (col.size() == m)
{
row.push_back(0);
for (row.back() = 0; row.back() < m; ++row.back())
Solve(col, row);
row.pop_back();
}
else
{
col.push_back(0);
for (col.back() = 0; col.back() < n; ++col.back())
Solve(col, row);
col.pop_back();
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
cin >> a[i][j];
vector<int> col, row;
Solve(col, row);
cout << ans;
}
Compilation message
riddicks.cpp: In function 'void Solve(std::vector<int>&, std::vector<int>&)':
riddicks.cpp:51:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (col.size() == m && row.size() == n)
~~~~~~~~~~~^~~~
riddicks.cpp:51:39: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (col.size() == m && row.size() == n)
~~~~~~~~~~~^~~~
riddicks.cpp:56:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (col.size() == m)
~~~~~~~~~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
256 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
6 ms |
384 KB |
Output is correct |
7 |
Correct |
6 ms |
384 KB |
Output is correct |
8 |
Correct |
33 ms |
384 KB |
Output is correct |
9 |
Correct |
350 ms |
256 KB |
Output is correct |
10 |
Correct |
343 ms |
504 KB |
Output is correct |
11 |
Correct |
344 ms |
384 KB |
Output is correct |
12 |
Correct |
348 ms |
504 KB |
Output is correct |
13 |
Execution timed out |
2091 ms |
384 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |