uva 10085 The most distant state(八数码)

737 篇文章 0 订阅

The Most Distant State

Input: standard input

Output: standard output

 

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搜索到最后一个装态就可以了,移动记录只要开个字符串储存就可以了。

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;

typedef int State[20];
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};
const int MAXN = 1000005;
const char sign[5] = "UDLR";

int dis[MAXN], goal;
int front, rear;
State st[MAXN];
char str[MAXN][100];
int head[MAXN], next[MAXN];

int hash(State& s) {
    int v = 0;
    for (int i = 0; i < 9; i++)
	v = v * 10 + s[i];
    return v % MAXN;
}

bool tryInsert(int s) {
    int h = hash(st[s]);
    int u = head[h];

    while (u) {
	if (memcmp(st[u], st[s], sizeof(st[s])) == 0)
	    return false;
	u = next[u];
    }
    next[s] = head[h];
    head[h] = s;
    return true;
}

int bfs() {
    dis[1] = 0;
    while (front < rear) {
	State& now = st[front];
	int z;
	for (z = 0; z < 9; z++)
	    if (!now[z])	break;
	int x = z / 3, y = z % 3;
	for (int i = 0; i < 4; i++) {
	    int newx = x + dx[i];
	    int newy = y + dy[i];
	    int newz = newx * 3 + newy;
	    if (newx >= 0 && newx < 3 && newy >= 0 && newy < 3) {
		State& net = st[rear];

		for (int j = 0; j < 9; j++)
		    net[j] = now[j];
		net[newz] = now[z];
		net[z] = now[newz];

		for (int j = 0; j < dis[front]; j++)
		    str[rear][j] = str[front][j];
		str[rear][dis[front]] = sign[i];
		dis[rear] = dis[front] + 1;
		if (tryInsert(rear))	rear++;
	    }
	}
	front++;
    }
}

int main() {

    int order[9], cas, t = 1;
    scanf("%d", &cas);
    while (cas--) {
	for (int i = 0; i < 9; i++)
	    scanf("%d", &order[i]);

	front = 1;
	rear = 2;
	memset(st, 0, sizeof(st));
	memset(dis, 0, sizeof(dis));
	memset(str, 0, sizeof(str));
	memset(head, 0, sizeof(head));
	memset(next, 0, sizeof(next));
	memcpy(st[front], order, sizeof(order));
	bfs();
	rear--;

	printf("Puzzle #%d\n", t++ );
	for (int i = 0; i < 3; i++) {
	    for (int j = 0; j < 2; j++)
		printf("%d ", st[rear][i * 3 + j]);
	    printf("%d\n", st[rear][i * 3 + 2]);
	}
	str[rear][dis[rear]] = '\0';
	puts(str[rear]);
	printf("\n");
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值