UVA 253 立方体着色

直观的做法是,分别绕着三种轴旋转,每种旋转有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;
	
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值