UVA 253 立方体着色

来源:互联网 发布:java 时间校验 编辑:程序博客网 时间:2024/05/02 03:03

直观的做法是,分别绕着三种轴旋转,每种旋转有4种状态

然后枚举解空间,先写了个递归也能运行,但是速度没有那么快

#include <iostream>#include <sstream>#include <cstdio>#include <cstring>#include <cmath>#include <string>#include <vector>#include <set>#include <cctype>#include <algorithm>#include <cmath>#include <deque>#include <map>using namespace std;///#define INF 0xffffff7#define MAXN 200 ///string state1, state2;int rotate1[] = {1, 4, 2, 5, 3, 6};int rotate2[] = {5, 1, 3, 4, 6, 2};int rotate3[] = {4, 2, 1, 6, 5, 3};void rotateFunc(int *method, string &content){int i, j;string exchange("");for (i = 0; i < 6; i++){exchange += content[method[i] - 1];}content = exchange;}bool dfs(string state1, int times){if (times == 9)return false;if (state1 == state2){return true;}else{times++;string temp = state1;rotateFunc(rotate1, state1);if (dfs(state1, times))return true;state1 = temp;rotateFunc(rotate2, state1);if (dfs(state1, times))return true;state1 = temp;rotateFunc(rotate3, state1);if (dfs(state1, times))return true;return false;}}int main(){///int i, j;string line;while (getline(cin, line)){state1 = line.substr(0, 6);state2 = line.substr(6, 12);if (dfs(state1, 0))cout << "TRUE" << endl;elsecout << "FALSE" << endl;}    ///    return 0;}


参考了一下其他解法

发现只要对面的颜色匹配,就一定可以找到旋转的方法

好残暴的解法

#include <iostream>#include <sstream>#include <cstdio>#include <cstring>#include <cmath>#include <string>#include <vector>#include <set>#include <cctype>#include <algorithm>#include <cmath>#include <deque>#include <map>using namespace std;///#define INF 0xffffff7#define MAXN 200 ///int main(){///int i, j;string line;while (getline(cin, line)){string line1, line2;line1 = line.substr(0, 6);line2 = line.substr(6, 12);bool IsOk = true;//对于每个对面进行比较for (i = 0; i < 3; i++){bool flag = false;//是否找到第二个cube对面与第一个匹配的for (j = 0; j < 6; j++){if (line1[i] == line2[j] && line1[5 - i] == line2[5 - j]){flag = true;line2[j]  = '0';line2[5 - j] = '0';break;}}if (!flag){IsOk = false;break;}}//3个都能找到则肯定能旋转到,好残暴的解法if (IsOk)cout << "TRUE" << endl;elsecout << "FALSE" << endl;}    ///    return 0;}


 

原创粉丝点击