UVALive - 3401 Colored Cubes 枚举

题目大意:有n个带颜色的立方体,每个面都涂有一种颜色,要求重新涂尽量少的面,使得所有的立方体都相同,两个立方体相同的含义是:存在一种旋转方式,使得两个立方体对应的颜色相同

解题思路:枚举n个立方体的每种旋转后的状态,然后再统计需要重新涂的面

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;

const int dice[24][6] = {{0,1,2,3,4,5},{0,3,1,4,2,5},{0,2,4,1,3,5},{0,4,3,2,1,5},{1,2,0,5,3,4},{1,5,2,3,0,4},{1,0,3,2,5,4},{1,3,5,0,2,4},{2,1,5,0,4,3},{2,0,1,4,5,3},{2,4,0,5,1,3},{2,5,4,1,0,3},{3,4,5,0,1,2},{3,5,1,4,0,2},{3,1,0,5,4,2},{3,0,4,1,5,2},{4,2,5,0,3,1},{4,0,2,3,5,1},{4,5,3,2,0,1},{4,3,0,5,2,1},{5,2,1,4,3,0},{5,4,2,3,1,0},{5,1,3,2,4,0},{5,3,4,1,2,0}};

#define maxn 10
int start[maxn][6], end[maxn][6], r[maxn], num , N , MIN, num_color[maxn*6];
vector<string> color;

int change(const char *t) {
	string tmp = (string) t;
	int size = color.size();

	for(int i = 0; i < size; i++)
		if(tmp == color[i])
			return i;
	color.push_back(tmp);
	return size;
}

void check() {
	for(int i = 0; i < N; i++)
		for(int j = 0; j < 6; j++)
			end[i][dice[r[i]][j]] = start[i][j];


	int sum = 0;
	for(int i = 0; i < 6; i++) {
		memset(num_color,0,sizeof(num_color));
		int MAX = 0;
		for(int j = 0; j < N; j++)
			MAX = max(MAX,++num_color[end[j][i]]);
		sum += N - MAX;
	}
	MIN = min(MIN,sum);
}

void dfs(int cur) {
	if(cur == N)
		check();
	else 
		for(int i = 0; i < 24; i++) {
			r[cur] = i;
			dfs(cur+1);
		}
}

int main() {
	while(scanf("%d",&N) == 1 && N) {
		color.clear();	
		char temp[30];
		for(int i = 0; i < N; i++)
			for(int j = 0; j < 6; j++) {
				scanf("%s",temp);
				start[i][j] = change(temp);
			}
		MIN = 6 * N;
		dfs(1);
		printf("%d\n",MIN);
	}

	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值