欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

LA 3401 Colored Cubes

程序员文章站 2022-04-17 21:21:51
...

题目:Colored Cubes


思路:枚举每一个正方体的状态,把所有正方体的每一个面都变成在这个面上出现最多的颜色。


注意:

1、不用枚举第一个正方体。

2、不要写错变量名。


代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <cstring>
#include <map>
using namespace std;

#define maxn 4
#define m 6
#define maxch 24

const int chg[maxch+5][m+5]= {{0},{0,3,2,6,1,5,4},{0,3,1,2,5,6,4},{0,3,5,1,6,2,4},{0,3,6,5,2,1,4},{0,5,3,6,1,4,2},{0,6,3,2,5,4,1},{0,2,3,1,6,4,5},{0,1,3,5,2,4,6},{0,1,2,3,4,5,6},{0,5,1,3,4,6,2},{0,6,5,3,4,2,1},{0,2,6,3,4,1,5},{0,6,2,4,3,5,1},{0,2,1,4,3,6,5},{0,1,5,4,3,2,6},{0,5,6,4,3,1,2},{0,2,4,6,1,3,5},{0,1,4,2,5,3,6},{0,5,4,1,6,3,2},{0,6,4,5,2,3,1},{0,4,5,6,1,2,3},{0,4,6,2,5,1,3},{0,4,2,1,6,5,3},{0,4,1,5,2,6,3}};

int n;
map<string,int> mp;
int cnt;

int a[maxn+5][m+5];

int ans=0;

void init() {
	mp.clear();
	cnt=0;
	ans=(1<<30);
}

vector<int> st;

int find(){
	int b[maxn+5][m+5];
	memcpy(b[1],a[1],sizeof(a[1]));
	for(int i=2;i<=n;i++){
		for(int j=1;j<=m;j++){
			b[i][j]=a[i][chg[st[i-2]][j]];
		}
	}
	int sum=0;
	for(int j=1;j<=m;j++){
		int col[maxn*m+5]={0};
		for(int i=1;i<=n;i++){
			col[b[i][j]]++;
		}
		int s=0,w=0;
		for(int i=1;i<=cnt;i++){
			if(col[i]>s) s=col[i],w=i;
		}
		for(int i=1;i<=n;i++){
			if(b[i][j]!=w) sum++;
		}
	}
	return sum;
}

void dfs(int x){
	if(x==n) {
		ans=min(ans,find());
		return ;
	}
	for(int i=1;i<=maxch;i++){
		st.push_back(i);
		dfs(x+1);
		st.pop_back();
	}
}

int main() {
	while(~scanf("%d",&n)&&n) {
		init();
		for(int i=1; i<=n; i++) {
			for(int j=1; j<=m; j++) {
				string x;
				cin>>x;
				if(!mp.count(x)) mp[x]=++cnt;
				a[i][j]=mp[x];
			}
		}
		dfs(1);
		printf("%d\n",ans);
	}
	return 0;
}


相关标签: 蓝书 UVaLive