uva 10085 The most distant state(BFS + HASH)

16 篇文章 0 订阅
1 篇文章 0 订阅

                              uva 10085 The most distant state



The 8-puzzle is a square tray in which eight square tiles are placed. The remaining ninth square is uncovered. Each tile has a number on it. A tile that is adjacent to the blank space can be slid into that space. A game consists of a starting state and a specified goal state. The starting state can be transformed into the goal state by sliding (moving) the tiles around. The 8-puzzle problem asks you to do the transformation in minimum number of moves.

 

2

8

3

 

 

 

1

2

3

1

6

4

=>

8

 

4

7

 

5

 

 

 

7

6

5

Start

 

 

 

Goal

 

However, our current problem is a bit different. In this problem, given an initial state of the puzzle your are asked to discover a goal state which is the most distant (in terms of number of moves) of all the states reachable from the given state.


Input

The first line of the input file contains an integer representing the number of test cases to follow. A blank line follows this line.


Each test case consists of 3 lines of 3 integers each representing the initial state of the puzzle. The blank space is represented by a 0 (zero). A blank line follows each test case.

 

Output

For each test case first output the puzzle number. The next 3 lines will contain 3 integers each representing one of the most distant states reachable from the given state. The next line will contain the shortest sequence of moves that will transform the given state to that state. The move is actually the movement of the blank space represented by four directions : U (Up), L (Left), D (Down) and R (Right). After each test case output an empty line.

 

Sample Input

1

2 6 4
1 3 7
0 5 8

Sample Output

Puzzle #1
8 1 5
7 3 6
4 0 2

UURDDRULLURRDLLDRRULULDDRUULDDR



题目大意:八数码问题,不同的是,这个不是求一个八数码初始状态到达目标状态的最少步数, 而是要随便走,找出走的步数最多的那个状态,不能重复。

解题思路:不重复,用全排列哈希判重实现,读取数据时,要将二维数组存在一维数组中方便后面的操作,为了输出最后的步骤,在BFS时要同时记录上一个状况的编号(rec->head),以及方向(dir->i)。



#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
using namespace std;
struct knight{
	int g[9], x, y, rec, dir;
};
knight k[400000];
int move[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; //上下左右
int exp[8] = {40320, 5040, 720, 120, 24, 6, 2, 1}; //阶乘
int vis[400000], head, tail, output[400000];
int hash(int x) { //哈希判重
	int sum = 0;
	for (int i = 0; i < 9; i++) {
		int cnt = 0;
		for (int j = i + 1; j < 9; j++) {
			if (k[x].g[i] > k[x].g[j]) cnt++; //统计逆序数
		}
		sum += cnt*exp[i];
	}
	return sum;
}
void BFS() {
	int px, py, temp, h, p1, p2;
	while (head <= tail) {
		for (int i = 0; i < 4; i++) {
			px = k[head].x + move[i][0];
			py = k[head].y + move[i][1];
			if (px < 0 || py < 0 || px >= 3 || py >= 3) continue;
			p1 = k[head].x * 3 + k[head].y; 
			p2 = px * 3 + py;
			tail++;
			for (int j = 0; j < 9; j++) {
				k[tail].g[j] = k[head].g[j];
			}
			temp = k[tail].g[p1];
			k[tail].g[p1] =  k[tail].g[p2];
			k[tail].g[p2] = temp;	
			h = hash(tail);
			if (vis[h])	tail--;
			else {
				vis[h] = 1;
				k[tail].rec = head;
				k[tail].dir = i;
				k[tail].x = px;
				k[tail].y = py;
			}
		}
		head++;
	}
}
int main() {
	int T, Case = 1;
	scanf("%d", &T);
	while (T--) {
		memset(vis, 0, sizeof(vis));
		head = tail = 0;
		for (int i = 0; i < 9; i++) {
			scanf("%d", &k[head].g[i]);
			if (k[head].g[i] == 0) { //将二维数据一维存储
				k[head].x = i / 3;
				k[head].y = i % 3;
			}
		}
		vis[hash(head)] = 1;

		BFS();

		printf("Puzzle #%d\n", Case++);
		head--;
		for (int i = 0; i < 9; i++) {
			printf("%d", k[head].g[i]);
			if ((i + 1) % 3 == 0) printf("\n");
			else printf(" ");
		} 
		int cnt = 0;
		while (head) {
			output[cnt++] = k[head].dir; //dir记录空格移动方向
			head = k[head].rec; //rec记录当前状况的原始状况
		}
		for (int i = cnt - 1; i >= 0; i--) { //逆序输出
			if (output[i] == 0) printf("U");
			else if (output[i] == 1) printf("D");
			else if (output[i] == 2) printf("L");
			else if (output[i] == 3) printf("R");
		}
		printf("\n\n");
	}
	return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值