直观的做法是,分别绕着三种轴旋转,每种旋转有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;
else
cout << "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;
else
cout << "FALSE" << endl;
}
///
return 0;
}