답안 #316033

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
316033 2020-10-24T21:43:16 Z Temmie Riddick's Cube (IZhO13_riddicks) C++17
100 / 100
39 ms 384 KB
#include <bits/stdc++.h>
 
int n, m, ans = 100500, g[10][10], a[10];
 
void dfs(int x) {
	
	if(x == m) {
		int gg[6][6], cnt = 0;
		for (int i = 0; i < m; i++) {
			cnt += std::min(a[i], n - a[i]);
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				gg[i][j] = g[(i + a[j]) % n][j];
			}
		}
		bool good = true;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if(gg[i][j] != gg[i][0]) {
					good = false;
				}
			}
		}
		if(good) {
			ans = std::min(ans, cnt);
		}
		int count = 0;
		std::map <std::vector <int>, int> mp;
		int z[6][6], dp[6][6];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				std::vector <int> cur;
				for (int k = 0; k < m; k++) {
					cur.push_back(gg[i][(j + k) % m]);
				}
				if(mp.find(cur) == mp.end()) {
					mp[cur] = ++count;
				}
				z[i][j] = mp[cur];
			}
		}
		for (int j = 0; j < m; j++) {
			dp[0][j] = std::min(j, m - j);
		}
		for(int i = 1; i < n; i++) {
			for (int j = 0; j < m; j++) {
				dp[i][j] = 1e9;
			}
			for (int j = 0; j < m; j++) {
				for (int k = 0; k < m; k++) {
					if(z[i][j] == z[i - 1][k]) {
						dp[i][j] = std::min(dp[i][j], dp[i - 1][k] + std::min(j, m - j));
					}
				}
			}
		}
		cnt += *std::min_element(dp[n - 1], dp[n - 1] + m);
		ans = std::min(ans, cnt);
		return;
	}
	
	for (int i = 0; i < n; i++) {
		a[x] = i;
		dfs(x + 1);
	}
	
}
 
int main() {
	//std::ios::sync_with_stdio(0); std::cin.tie(0);
	
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			scanf("%d", g[i] + j);
		}
	}
	dfs(0);
	//std::cout << ans << "\n";
	printf("%d\n", ans);
	//std::cout.flush(); std::cin >> n;
	
}

Compilation message

riddicks.cpp: In function 'int main()':
riddicks.cpp:73:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   73 |  scanf("%d%d", &n, &m);
      |  ~~~~~^~~~~~~~~~~~~~~~
riddicks.cpp:76:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   76 |    scanf("%d", g[i] + j);
      |    ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 1 ms 256 KB Output is correct
7 Correct 1 ms 256 KB Output is correct
8 Correct 2 ms 256 KB Output is correct
9 Correct 10 ms 372 KB Output is correct
10 Correct 6 ms 256 KB Output is correct
11 Correct 10 ms 256 KB Output is correct
12 Correct 6 ms 384 KB Output is correct
13 Correct 38 ms 256 KB Output is correct
14 Correct 39 ms 376 KB Output is correct
15 Correct 39 ms 256 KB Output is correct
16 Correct 39 ms 256 KB Output is correct
17 Correct 39 ms 368 KB Output is correct
18 Correct 39 ms 384 KB Output is correct
19 Correct 39 ms 256 KB Output is correct
20 Correct 39 ms 256 KB Output is correct