北大算法基础-八数码(优化)数据预处理

练习代码,已经测试.

#include<iostream> 
#include<bitset>
using namespace std;

int goalStatus;//目标序号
bitset<362880> Flags;//标记已用序号
const int MAXS = 370000;
struct Node
{
	int status;//排列序号
	int father;//父序号
	char move;//移动到当前序号的动作
	Node(int s, int f, int m) :status(s), father(f), move(m) {}
	Node() {}
};
Node myQueue[MAXS];//状态队列
int qHead; int qTail;//队头,队尾的'指针'

char sz4Moves[] = "udrl";//四个移动动作
unsigned int factorial[21];//阶乘的值数组


unsigned int GetPermutationNumForInt(int* perInt, int len) {
	//从数值排列perInt计算序号
	bool numUsed[21];//数字使用标记
	unsigned int num = 0;
	memset(numUsed, 0, sizeof(numUsed));
	for (int i = 0; i < len; i++)//循环每一位数
	{
		int n = 0;
		numUsed[perInt[i]] = 1;//标记第i位数已使用
		for (int j = 0; j < perInt[i]; j++) {
			if (!numUsed[j])
				++n;
		}
		num += n * factorial[len - 1 - i];
	}
	return num;//从0开始所以不加上它自身的状态
}

template<class T>
unsigned int GetPermutationNum(T s1, T s2, int len) {
	//给定任意排列求序号,s1是第一个排列,s2待求的排列
	int* perInt = new int[len];
	for (int i = 0; i < len; i++)//把任意排列转为数值排列perInt
		for (int j = 0; j < len; j++)
			if (*(s2 + i) == *(s1 + j))
			{
				perInt[i] = j;
				break;
			}
	unsigned int num = GetPermutationNumForInt(perInt, len);
	delete[] perInt;//清空
	return num;//返回序号
}

template<class T>
void GetPermutationByNum(T s1, T s2, int len, unsigned int No) {
	//根据序号求任意排列,s1是第一个排列
	int* perInt = new int[len];//存排列的数值
	bool* numUsed = new bool[len];//已使用标记
	memset(numUsed, 0, sizeof(bool) * len);//numUsed是指针不能放在第三位,注意
	for (int i = 0; i < len; i++) {	//第i位
		int j;
		for (j = 0; j < len; j++) {
			if (!numUsed[j])
			{
				if (factorial[len - 1 - i] >= No + 1)
					break;
				else
					No -= factorial[len - 1 - i];
			}
		}
		perInt[i] = j;//找到的数放入排列
		numUsed[j] = 1;
	}

	for (int i = 0; i < len; i++) {
		*(s2 + i) = *(s1 + perInt[i]);//转换成任意形式的排列s2
	}
	delete[] perInt;
	delete[] numUsed;
}

unsigned int StrStatusToIntStatus(const char* strStatus) {
	//从字串排列求序号
	return GetPermutationNum("012345678", strStatus, 9);

}

void IntStatusToStrStatus(int n, char* strStatus) {
	//从序号求字串排列
	GetPermutationByNum((char*)"012345678", strStatus, 9, n);//为了类型统一,加上(char*)
}

int NewStatus(int nStatus, char cMove) {
	//从序号nStatus,通过cMove移动为新排列序号
	char szTmp[20];//保存序号对应的字串排列
	int nZeroPos;//0的位置
	IntStatusToStrStatus(nStatus, szTmp);//序号求字符排列
	for (int i = 0; i < 9; i++)//找0位
		if (szTmp[i] == '0') {
			nZeroPos = i;
			break;
		}
	switch (cMove) {//移动0的位置
	case'u':if (nZeroPos < 3)return -1;//最上排,不能上移
		   else {
		szTmp[nZeroPos] = szTmp[nZeroPos - 3];
		szTmp[nZeroPos - 3] = '0';
		break;
	}
	case'd':if (nZeroPos > 5)return -1;
		   else {
		szTmp[nZeroPos] = szTmp[nZeroPos + 3];
		szTmp[nZeroPos + 3] = '0';
		break;
	}
	case'r':if (nZeroPos % 3 == 2) return -1;
		   else {
		szTmp[nZeroPos] = szTmp[nZeroPos + 1];
		szTmp[nZeroPos + 1] = '0';
		break;
	}
	case'l':if (nZeroPos % 3 == 0) return -1;
		   else {
		szTmp[nZeroPos] = szTmp[nZeroPos - 1];
		szTmp[nZeroPos - 1] = '0';
	}
	}
	return StrStatusToIntStatus(szTmp);
}

bool Bfs(int startStatus) {//广搜
	int nNewStatus;
	int Status;
	Flags.reset();
	qHead = 0;	qTail = 1;
	myQueue[qHead] = Node(startStatus, -1, 0);
	Flags.set(startStatus, 1);//被使用

	while (qHead != qTail)
	{
		Status = myQueue[qHead].status;
		for (int i = 0; i < 4; i++)//四个方向移动
		{
			nNewStatus = NewStatus(Status, sz4Moves[i]);
			if (nNewStatus == -1) continue;//不能移
			if (Flags[nNewStatus]) continue;//状态已存在 
			Flags.set(nNewStatus, 1);
			myQueue[qTail++] = Node(nNewStatus, qHead, sz4Moves[i]);
		}
		qHead++;
	}
	return false;//没有找到
}

int main() {
	factorial[0] = factorial[1] = 1;
	for (int i = 2; i < 21; i++)
		factorial[i] = i * factorial[i - 1];
	goalStatus = StrStatusToIntStatus("123456780");
	Bfs(goalStatus);//预置,反推结果的所有可能状态
	char szLine[50];
	char szLine2[20];
	while (cin.getline(szLine, 48)) {//清洗字符放入szLine2
		int j = 0;
		for (int i = 0; szLine[i]; i++) {
			if (szLine[i] != ' ') {//跳过空字符
				if (szLine[i] == 'x')szLine2[j++] = '0';//替换字符x,
				else szLine2[j++] = szLine[i];
			}
		}
		szLine2[j] = 0;//字符串最后一个字符为0
		int sumGoal = 0;
		for (int i = 0; i < 8; i++)//"123456780"的奇偶性
			sumGoal += i;
		int sumOri = 0;
		for (int i = 0; i < 9; i++)//判断输入奇偶性
			for (int j = 0; j < i; j++)
				if (szLine2[j] < szLine2[i] && szLine2[i] != '0' && szLine2[j] != '0')//奇偶性必须忽略0
				{
					++sumOri;
					continue;
				}

		if (sumGoal % 2 != sumOri % 2)//判断输入与结果的奇偶性
		{
			cout << "unsolvable" << endl;
			continue;
		}		
		
		int nStatus = StrStatusToIntStatus(szLine2);
		int nPos;
		if (Flags[nStatus] == 1) {//输入的序号是不是已经在队列中,如果有找到位置
			{for (int i = 0; i < qTail; ++i)
				if (myQueue[i].status == nStatus)
					nPos = i;
			}

			do {
				switch (myQueue[nPos].move)//反转移动方向
				{
				case 'u': {cout << 'd'; break; }
				case 'd': {cout << 'u'; break; }
				case 'l': {cout << 'r'; break; }
				case 'r': {cout << 'l'; break; }
				}
				nPos = myQueue[nPos].father;
			} while (nPos);
		}
		else {
			cout << "unsolvable" << endl;
			continue;
		}
	}

	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
北京理工大学的数据结构与算法设计课程是乐学的。数据结构与算法是计算机科学与技术专业中非常重要的一门基础课程,对于提升学生的编程能力和解决实际问题的能力具有重要作用。 在这门课程中,学生将学习各种数据结构,如数组、链表、栈、队列、树、图等,以及常用的算法,如排序算法、搜索算法、图算法等。通过学习这些数据结构和算法,学生可以更好地理解数据的组织和处理方式,掌握问题的分析与解决方法。 北京理工大学的数据结构与算法设计课程注重实践和动手能力的培养。学生在课程中会完成一系列的编程实验,通过实践来巩固理论知识,并掌握如何运用数据结构和算法来解决实际问题。实验内容涵盖了各种常见的算法和数据结构,让学生能够从实际问题中得到锻炼和提升。 此外,北京理工大学的这门课程还注重培养学生的团队协作能力和创新思维。学生会在团队中完成一些课程项目,通过合作来解决更加复杂的问题,提升了学生的合作与沟通能力。同时,学生也会有机会钻研相关领域的前沿研究,培养创新思维和科研能力。 综上所述,北京理工大学的数据结构与算法设计课程是乐学的。通过这门课程的学习,学生不仅可以掌握数据结构和算法的基本知识,还能够提升编程能力和解决问题的能力,培养团队协作能力和创新思维。这些都对于学生未来的学习和职业发展都具有重要意义。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值