hdu 1430 bfs + 打表 + 康拓展开

                                         魔板

                         Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
                         Total Submission(s): 3573    Accepted Submission(s): 830


Problem Description
在魔方风靡全球之后不久,Rubik先生发明了它的简化版——魔板。魔板由8个同样大小的方块组成,每个方块颜色均不相同,可用数字1-8分别表示。任一时刻魔板的状态可用方块的颜色序列表示:从魔板的左上角开始,按顺时针方向依次写下各方块的颜色代号,所得到的数字序列即可表示此时魔板的状态。例如,序列(1,2,3,4,5,6,7,8)表示魔板状态为:

1 2 3 4
8 7 6 5

对于魔板,可施加三种不同的操作,具体操作方法如下:

A: 上下两行互换,如上图可变换为状态87654321
B: 每行同时循环右移一格,如上图可变换为41236785
C: 中间4个方块顺时针旋转一格,如上图可变换为17245368

给你魔板的初始状态与目标状态,请给出由初态到目态变换数最少的变换步骤,若有多种变换方案则取字典序最小的那种。
 

Input
每组测试数据包括两行,分别代表魔板的初态与目态。
 

Output
对每组测试数据输出满足题意的变换步骤。
 

Sample Input
  
  
12345678 17245368 12345678 82754631
 

Sample Output
C
AC


刚开始这道题没有考虑打表  直接暴力交  然后各种超时  改cin cout 什么的 然而还是超时 

后来参考了下别人的代码  发先别人打表过的  这种东西是真的巧妙 

本题有一个小技巧是我第一次见

就是映射的使用

比如说  给你一个a状态 让你从这个状态到b状态  如果每次都暴力bfs的话 一定会超时 于是我们可以用一种映射的方式来解决这个问题

讲简单点就是说 如果给你一个初始状态是87654321  目标状态是12345678那么我们需要做的事情是  把8看成1把7看成2... 依此类推  也就是映射的基本原理

然后那三种操作被我写到函数中了  oper_two 和  oper_three 这两个数组是我通过映射发现的原来的数组下标和操作过后对应的数组下表的关系映射出来的一组关系

能够比较简单的完成这两种操作~~~

其他就没什么好记录的了

#include <iostream>
#include <queue>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;
static string oper_one = "12307456";
static string oper_two = "02534617";
const int N = 50000;
bool idx[N] ;
string ans[N];
struct Node
{
		string state;
		string path;
};
int fac[10];
int Fac(int n)
{
	if (n == 0)return 0;
	else if (n == 1)return 1;
	else if(fac[n])return fac[n];
	else return fac[n] = Fac(n - 1) * n;
}
int Cantor(string &str)
{
	int ans = 0;
	for (int i = 0; i < 8 ; i ++) {
		int pos = 0; 
		for (int j = i + 1; j < 8; j ++) {
			if (str[i] > str[j])pos ++;
		}
		ans += Fac(7 - i) * pos;
	}		
	return ans;
};
void op_one(Node &a)
{
	for (int i = 0; i < 4; i ++) 
		swap(a.state[i] , a.state[7 - i]);
	a.path += 'A';
	return ;
}
void op_two(Node &a)
{
	a.path += "B";
	string temp = a.state;
	for (int i = 0; i < 8; i ++) 
		a.state[oper_one[i]- '0'] = temp[i];
	return ;
}
void op_three(Node &a)
{
	a.path += "C";
	string temp = a.state;
	for (int i = 0; i < 8; i ++)
		a.state[oper_two[i] - '0'] = temp[i];
	return ;
}
void Bfs()
{
	Node start , temp;
	start.state = "12345678";
	memset(idx , false, sizeof(idx));
	int c = Cantor(start.state);
	idx[c] = true;
	queue<Node>q;
	q.push(start);
	while (!q.empty()) {
		start = temp = q.front();q.pop();
		op_one(start);
		c = Cantor(start.state);
		if (!idx[c]) {
			idx[c] = true;
			ans[c] = start.path;
			q.push(start);
		}
		start = temp;
		op_two(start);
		c = Cantor(start.state);
		if (!idx[c]) {
			idx[c] = true;
			ans[c] = start.path;
			q.push(start);
		}
		start = temp;
		op_three(start);
		c = Cantor(start.state);
		if (!idx[c]) {
			idx[c] = true;
			ans[c] = start.path;
			q.push(start);
		}
	}
	return ;
}
int main()
{
	
	memset(fac , 0, sizeof(fac));
	string init = "12345678";
	string Set, Set_end, Start, End;
	Bfs();
	while (cin >> Start >> End) {
	    for (int i = 0; i < 8; i ++) 
	    	Set[Start[i] - '0'] = i + 1 + '0';
	    for (int i = 0; i < 8; i ++) 
	    	Set_end[i] = Set[End[i] - '0'];
	    int end_cantor = Cantor(Set_end);
	    //cout << end_cantor  << endl; 
	    for (int i = 0; i < ans[end_cantor].length(); i ++) 
	    	cout << ans[end_cantor][i] ;
	    cout << endl;
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值