PKU-1077 Eight (八数码之双向BFS)

原题链接 http://poj.org/problem?id=1077

Eight

Description

The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've seen it.  It is constructed with 15 sliding tiles, each with a number from 1 to 15 on it, and all packed into a 4 by 4 frame with one tile missing.  Let's call the missing tile 'x'; the object of the puzzle is to arrange the tiles so that they are ordered as:
 1  2  3  4 

 5  6  7  8 

 9 10 11 12 

13 14 15  x 

where the only legal operation is to exchange 'x' with one of the tiles with which it shares an edge.  As an example, the following sequence of moves solves a slightly scrambled puzzle:
 1  2  3  4    1  2  3  4    1  2  3  4    1  2  3  4 

 5  6  7  8    5  6  7  8    5  6  7  8    5  6  7  8 

 9  x 10 12    9 10  x 12    9 10 11 12    9 10 11 12 

13 14 11 15   13 14 11 15   13 14  x 15   13 14 15  x 

           r->           d->           r-> 

The letters in the previous row indicate which neighbor of the 'x' tile is swapped with the 'x' tile at each step; legal values are 'r','l','u' and 'd', for right, left, up, and down, respectively.

Not all puzzles can be solved; in 1870, a man named Sam Loyd was famous for distributing an unsolvable version of the puzzle, and
frustrating many people.  In fact, all you have to do to make a regular puzzle into an unsolvable one is to swap two tiles (not counting the missing 'x' tile, of course).

In this problem, you will write a program for solving the less well-known 8-puzzle, composed of tiles on a three by three
arrangement.

Input

You will receive  a description of a configuration of the 8 puzzle.  The description is just a list of the tiles in their initial positions, with the rows listed from top to bottom, and the tiles listed from left to right within a row, where the tiles are represented by numbers 1 to 8, plus 'x'.  For example, this puzzle
 1  2  3 

 x  4  6 

 7  5  8 

is described by this list:
 1 2 3 x 4 6 7 5 8 

Output

You will print to standard output either the word ``unsolvable'', if the puzzle has no solution, or a string consisting entirely of the letters 'r', 'l', 'u' and 'd' that describes a series of moves that produce a solution.  The string should include no spaces and start at the beginning of the line.

Sample Input

 2  3  4  1  5  x  7  6  8 

Sample Output

ullddrurdllurdruldr

Source Code

/*
双向BFS, 从初始状态和目标状态同时开始搜索,当遇到有已经被搜索过的状态时停止
双向BFS优化了单次查询时间,但如果多次查询效率没有反向搜索好(因为目标状态固定)
*/

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

#define Max 365000   // 9! = 362880

//x,y记录当前X所在位置,state保存当前排列状态
//cantorValue记录当前状态对应的康托展开值
struct node {
	int x, y;
	char state[10];
	int cantorValue;
};

//searched保存当前状态是否出现过
//parent保存前一状态
//opration保存前一状态到当前状的操作(上下左右)
int searched[Max], parent[Max], opration[Max];
int index;
//Q负责初始状态,Q1负责目标状态
queue<node> Q, Q1;
node Next, Top;
int dir[4][2] = { 1,0, -1,0, 0,1, 0,-1};

/*
康托展开:X=a[n]*(n-1)!+a[n-1]*(n-2)!+...+a[i]*(i-1)!+...+a[1]*0!
例:3 5 7 4 1 2 9 6 8 展开为 98884。因为X=2*8!+3*7!+4*6!+2*5!+0*4!+0*3!+2*2!+0*1!+0*0!=98884.
解释: 
排列的第一位是3,3以后的序列中比3小的数有2个1,2,以这样的数开始的排列有8!个,因此第一项为2*8!	
排列的第二位是5,5以后的序列中比5小的数有3个4、1、2,这样的排列有7!个,因此第二项为3*7!	  
…………
以此类推,直至0*0!
*/
int Cantor(char * buf, int len) {
     
	//bit记录之后的序列中比当前位小的值的个数
	int cantor[9] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320};
	int i, j, bit;
	int result = 0;

	for (i = 0; i < len; i ++) {
		bit = 0;
		for (j = i + 1; j < len; j++) {
			if (buf[i] > buf[j]) {
				bit ++;
			}
		}
		result += (bit * cantor[len - i - 1]);
	}
	return result;
}

//判断是否越界
bool isInside(int x, int y) {
	if (x < 0 || y < 0 || x > 2 || y > 2) {
		return false;
	}
	return true;
} 

bool BFS(node start, node end) {

	int i, j, nextX, nextY;
	int a, b, step[30];
	char temp;

	while (!Q.empty()) {
		Q.pop();
	}

	while (!Q1.empty()) {
		Q1.pop();
	}

	Q.push(start);
	Q1.push(end);

	while (!Q.empty() || !Q1.empty()) {
		
		//初始状态开始的BFS
		if (!Q.empty()) {
			Top = Q.front();
			Q.pop();	
			
			for (i = 0; i < 4; i++) {
				nextX = Top.x + dir[i][0];
				nextY = Top.y + dir[i][1];
				if (!isInside(nextX, nextY)) {
					continue;
				}
				
				//下一个状态
				Next.x = nextX;
				Next.y = nextY;
				for (j = 0; j < 9; j ++) {
					Next.state[j] = Top.state[j];
				}
				a = Next.x * 3 + Next.y;
				b = Top.x * 3 + Top.y;
				temp = Next.state[a];
				Next.state[a] =  Next.state[b];
				Next.state[b] = temp;	
				Next.cantorValue = Cantor(Next.state, 9);
				if (!searched[Next.cantorValue]) {
					searched[Next.cantorValue] = 1;
					parent[Next.cantorValue] = Top.cantorValue;
					opration[Next.cantorValue] = i;
					Q.push(Next);
				} else if (searched[Next.cantorValue] == 2) {   
					int len = 0;

					//top到next的这一步
					step[len ++] = i;

					int cur = Top.cantorValue;
					while (parent[cur] != -1) {
						step[len ++] = opration[cur];
						cur = parent[cur];
					}
					for (j = len - 1; j >= 0; j--) {
						switch (step[j])
						{
							case 0:
								printf("d");
								break;
							case 1:
								printf("u");
								break;
							case 2:
								printf("r");
								break;
							case 3:
								printf("l");
								break;
						}
					}

					cur = Next.cantorValue;
					while (parent[cur] != -1) {
						switch (opration[cur])
						{
							case 0:
								printf("u");
								break;
							case 1:
								printf("d");
								break;
							case 2:
								printf("l");
								break;
							case 3:
								printf("r");
								break;
						}
						cur = parent[cur];
					}
					printf("\n");
					return true;
				}
			}
		}

		//目标状态开始的BFS
		if (!Q1.empty()) {
			Top = Q1.front();
			Q1.pop();	
			
			for (i = 0; i < 4; i++) {
				nextX = Top.x + dir[i][0];
				nextY = Top.y + dir[i][1];
				if (!isInside(nextX, nextY)) {
					continue;
				}
				
				//下一个状态
				Next.x = nextX;
				Next.y = nextY;
				for (j = 0; j < 9; j ++) {
					Next.state[j] = Top.state[j];
				}
				a = Next.x * 3 + Next.y;
				b = Top.x * 3 + Top.y;
				temp = Next.state[a];
				Next.state[a] =  Next.state[b];
				Next.state[b] = temp;	
				Next.cantorValue = Cantor(Next.state, 9);
				if (!searched[Next.cantorValue]) {
					searched[Next.cantorValue] = 2;
					parent[Next.cantorValue] = Top.cantorValue;
					opration[Next.cantorValue] = i;
					Q1.push(Next);
				} else if (searched[Next.cantorValue] == 1) {
					int len = 0;
					
					int cur = Next.cantorValue;
					while (parent[cur] != -1) {
						step[len ++] = opration[cur];
						cur = parent[cur];
					}
					for (j = len - 1; j >= 0; j--) {
						switch (step[j])
						{
						case 0:
							printf("d");
							break;
						case 1:
							printf("u");
							break;
						case 2:
							printf("r");
							break;
						case 3:
							printf("l");
							break;
						}
					}

					//top到next的这一步
					switch (i)
					{
						case 0:
							printf("u");
							break;
						case 1:
							printf("d");
							break;
						case 2:
							printf("l");
							break;
						case 3:
							printf("r");
							break;
					}

					cur = Top.cantorValue;
					while (parent[cur] != -1) {
						switch (opration[cur])
						{
							case 0:
								printf("u");
								break;
							case 1:
								printf("d");
								break;
							case 2:
								printf("l");
								break;
							case 3:
								printf("r");
								break;
						}
						cur = parent[cur];
					}
					printf("\n");
					return true;
				}
			}
		}	
	}

	return false;
}

int main() {
	int i;
	char init[10];

	while (cin >> init[0]) {
		memset(searched, 0, sizeof(searched));
		node start, end;
		
		if (init[0] == 'x') {
			init[0] = '0' + 9;
			start.x = 0;
			start.y = 0;
		}
		for (i = 1; i < 9; i++) {
			cin >> init[i];
			if (init[i] == 'x') {
				init[i] = '0' + 9;
				start.x = i / 3;
				start.y = i % 3;
			}
		}	
		for (i = 0; i < 9; i++) {
			start.state[i] = init[i];
		}
		start.cantorValue = Cantor(init, 9);

		searched[start.cantorValue] = 1;
		parent[start.cantorValue] = -1;
		opration[start.cantorValue] = -1;
		
		for (i = 0; i < 9; i++) {
			end.state[i] = '0' + i + 1;
		}
		end.x = 2;
		end.y = 2;
		end.cantorValue = 0;
		
		searched[end.cantorValue] = 2;
		parent[end.cantorValue] = -1;
		opration[end.cantorValue] = -1;
		
		if(!BFS(start, end)) {
			printf("unsolvable\n");
		}

	}

	return 0;
}


 

内容概要:该论文研究了一种基于行波理论的输电线路故障诊断方法。当输电线路发生故障时,故障点会产生向两侧传播的电流和电压行波。通过相模变换对三相电流行波解耦,利用解耦后独立模量间的关系确定故障类型和相别,再采用小波变换模极大值法标定行波波头,从而计算故障点距离。仿真结果表明,该方法能准确识别故障类型和相别,并对故障点定位具有高精度。研究使用MATLAB进行仿真验证,为输电线路故障诊断提供了有效解决方案。文中详细介绍了三相电流信号生成、相模变换(Clarke变换)、小波变换波头检测、故障诊断主流程以及结果可视化等步骤,并通过多个实例验证了方法的有效性和准确性。 适合人群:具备一定电力系统基础知识和编程能力的专业人士,特别是从事电力系统保护与控制领域的工程师和技术人员。 使用场景及目标:①适用于电力系统的故障检测与诊断;②能够快速准确地识别输电线路的故障类型、相别及故障点位置;③为电力系统的安全稳定运行提供技术支持,减少停电时间和损失。 其他说明:该方法不仅在理论上进行了深入探讨,还提供了完整的Python代码实现,便于读者理解和实践。此外,文中还讨论了行波理论的核心公式、三相线路行波解耦、行波测距实现等关键技术点,并针对工程应用给出了注意事项,如波速校准、采样率要求、噪声处理等。这使得该方法不仅具有学术价值,也具有很强的实际应用前景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值