답안 #339324

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
339324 2020-12-25T04:53:17 Z tengiz05 Riddick's Cube (IZhO13_riddicks) C++17
100 / 100
1935 ms 492 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define FASTIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define pii pair<int, int>
#define ff first
#define ss second
#define PI acos(-1)
#define ld long double
template<class T> bool ckmin(T& a, const T& b) {return a>b? a=b, true:false;}
template<class T> bool ckmax(T& a, const T& b) {return a<b? a=b, true:false;}
const int mod = 1e9+7, N = 4005;
int msb(int val){return sizeof(int)*8-__builtin_clzll(val)-1;}
int n, m, k;
int a[10][10];
int ans = 100500;
vector<int> opscol, opsrow;
int res[10][10];
int ops;
inline int shiftcol(int i, int j){
	int tt = min(j, n-j);
	for(int c=0;c<j;c++){
		res[n][i] = res[0][i];
		for(int I=0;I<n;I++){
			res[I][i] = res[I+1][i];
		}
	}return tt;
}

inline int shiftrow(int i, int j){
	int tt = min(j, m-j);
	for(int c=0;c<j;c++){
		res[i][m] = res[i][0];
		for(int I=0;I<m;I++){
			res[i][I] = res[i][I+1];
		}
	}return tt;
}

void row(int i){
	if(i == n){
		int tmp = 0;
		for(int j=0;j<n;j++){
			tmp += shiftrow(j, opsrow[j]);
		}ops += tmp;
		bool is1 = true, is2 = true;
		for(int i=0;i<n;i++){
			for(int j=1;j<m;j++)if(res[i][j] != res[i][j-1])is1 = false;
		}
		for(int i=0;i<m;i++){
			for(int j=1;j<n;j++)if(res[j][i] != res[j-1][i])is2 = false;
		}
		for(int j=0;j<n;j++){
			shiftrow(j, m-opsrow[j]);
		}
		if(is1 || is2)ckmin(ans, ops);
		ops -= tmp;
		return;
	}
	for(int j=0;j<m;j++){
		opsrow.pb(j);
		row(i+1);
		opsrow.pop_back();
	}
}

void col(int i){
	if(i == m){
		for(int c=0;c<n;c++)for(int r=0;r<m;r++)res[c][r] = a[c][r];
		int tmp = 0;
		for(int j=0;j<m;j++){
			tmp += shiftcol(j, opscol[j]);
		}
		ops += tmp;
		row(0);
		ops -= tmp;
		return;
	}
	for(int j=0;j<n;j++){
		opscol.pb(j);
		col(i+1);
		opscol.pop_back();
	}
}

void solve(int test_case){
	int i, j;
	cin >> n >> m;
	for(i=0;i<n;i++){
		for(j=0;j<m;j++){
			cin >> a[i][j];
		}
	}
	col(0);
	cout << ans << '\n';
	return;
}

signed main(){
	FASTIO;
#define MULTITEST 0
#if MULTITEST
	int _T;
	cin >> _T;
	for(int T_CASE = 1; T_CASE <= _T; T_CASE++)
		solve(T_CASE);
#else
	solve(1);
#endif
	return 0;
}




# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 9 ms 364 KB Output is correct
9 Correct 107 ms 492 KB Output is correct
10 Correct 102 ms 364 KB Output is correct
11 Correct 108 ms 364 KB Output is correct
12 Correct 105 ms 364 KB Output is correct
13 Correct 1927 ms 392 KB Output is correct
14 Correct 1920 ms 492 KB Output is correct
15 Correct 1919 ms 492 KB Output is correct
16 Correct 1935 ms 492 KB Output is correct
17 Correct 1907 ms 364 KB Output is correct
18 Correct 1920 ms 392 KB Output is correct
19 Correct 1929 ms 492 KB Output is correct
20 Correct 1920 ms 392 KB Output is correct