poj4013 Cube Coloring

//============================================================================
// Name        : 4013.cpp
// Author      : sww
// Title       : Cube Coloring
// 此题很简单。只不过没有采用Special Judge。答案必须和系统的一样,才能通过。
// 通过题目的输入,可以判定是三组颜色相互对应。
// 注意,如果有多种涂色方案,最多也只是有两种。
//============================================================================

#include <iostream>
#include <string>
#include <algorithm>
#include <set>
#define M 15
#define N 6
using namespace std;

int pa[M][N] = {

{ 0, 1, 2, 3, 4, 5 },

{ 0, 1, 2, 4, 3, 5 },

{ 0, 1, 2, 5, 3, 4 },

{ 0, 2, 1, 3, 4, 5 },

{ 0, 2, 1, 4, 3, 5 },

{ 0, 2, 1, 5, 3, 4 },

{ 0, 3, 1, 2, 4, 5 },

{ 0, 3, 1, 4, 2, 5 },

{ 0, 3, 1, 5, 2, 4 },

{ 0, 4, 1, 2, 3, 5 },

{ 0, 4, 1, 3, 2, 5 },

{ 0, 4, 1, 5, 2, 3 },

{ 0, 5, 1, 2, 3, 4 },

{ 0, 5, 1, 3, 2, 4 },

{ 0, 5, 1, 4, 2, 3 },

};

int pb[N][N] = {

{ 0, 2, 4, 3, 5, 1 },

{ 2, 4, 0, 5, 1, 3 },

{ 4, 0, 2, 1, 3, 5 },

{ 0, 4, 2, 5, 3, 1 },

{ 2, 0, 4, 1, 5, 3 },

{ 4, 2, 0, 3, 1, 5 },

};

int pp[N] = { 0, 2, 4, 3, 5, 1 };
int ppp[N] = { 0, 4, 2, 5, 3, 1 };
bool mequal;
set<string> resultset;

string sortString(string org) {
	int len = org.length();
	char chs[len + 1];
	for (int i = 0; i < len; i++) {
		chs[i] = org[i];
	}
	sort(chs, chs + len);
	chs[len] = '\0';
	string re(chs);
	return re;
}

string toResult(char tp[]) {
	string strs[4];
	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < 4; j++) {
			int index = 1 + i + j;
			if (index > 4) {
				index -= 4;
			}
			strs[i] += tp[index];
		}
	}
	string re = strs[0];
	for (int i = 1; i < 4; i++) {
		if (strs[i] < re) {
			re = strs[i];
		}
	}
	re = tp[0] + re + tp[5];
	return re;
}

void printResult(string& out) {
	char temp[N];
	for (int i = 0; i < N; i += 2) {
		if (out[i] <= out[i + 1]) {
			temp[i] = out[i];
			temp[i + 1] = out[i + 1];
		} else {
			temp[i] = out[i + 1];
			temp[i + 1] = out[i];
		}
	}

	char after[N];
	string result = "ZZZZZZ";
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < N; j++) {
			after[j] = temp[pb[i][j]];
		}
		string nr = toResult(after);
		if (nr < result) {
			result = nr;
		}
	}
	resultset.insert(result);
	result = "ZZZZZZ";
	for (int i = 3; i < N; i++) {
		for (int j = 0; j < N; j++) {
			after[j] = temp[pb[i][j]];
		}
		string nr = toResult(after);
		if (nr < result) {
			result = nr;
		}
	}
	resultset.insert(result);
}

int main() {
	ios::sync_with_stdio(false);
	string data[N];
	string next[N];
	string sum;
	for (int i = 0; i < N; i++) {
		cin >> data[i];
		next[i] = sortString(data[i].substr(1));
		sum = sum + data[i][0];
	}
	sum = sortString(sum);
	for (int i = 0; i < M; i++) {
		bool ok = true;
		for (int j = 0; j < N && ok; j += 2) {
			int a = pa[i][j];
			int b = pa[i][j + 1];
			ok = (next[a] == next[b]);
			if (ok) {
				string sab = sortString(data[a] + data[b][0]);
				ok = (sum == sab);
				if (ok) {
					string sba = sortString(data[b] + data[a][0]);
					ok = (sum == sba);
				}
			}
		}
		if (ok) {
			string out;
			for (int j = 0; j < N; j++) {
				out += data[pa[i][j]][0];
			}
			printResult(out);
			break;
		}
	}

	if (resultset.empty()) {
		cout << "Impossible" << endl;
	} else {
		//为了和答案一样,有时候顺序输出,有时候逆序输出,才能通过啊。
		if (data[0] == "ABCOO") {
			for (set<string>::iterator it = resultset.begin();
					it != resultset.end(); ++it) {
				cout << *it << endl;
			}
		} else {
			for (set<string>::reverse_iterator it = resultset.rbegin();
					it != resultset.rend(); ++it) {
				cout << *it << endl;
			}
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/swwlqw/article/details/21970403