码迷,mamicode.com
首页 > 其他好文 > 详细

UVA The most distant state

时间:2014-09-07 13:35:55      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:acm   uva   implicit graph trave   bfs   

题目如下:

Problem A

The Most Distant State

Input: standard input

Output: standard output

 

The 8-puzzle is a square tray inwhich 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 beslid 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 inminimum 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 abit different. In this problem, given an initial state of the puzzle your areasked to discover a goal state which is the most distant (in terms of number ofmoves) of all the states reachable from the given state.


Input

The first line of the input filecontains an integer representing the number of test cases to follow. A blankline follows this line.


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

 

Output

For each test case first outputthe puzzle number. The next 3 lines will contain 3 integers each representingone of the most distant states reachable from the given state. The next linewill contain the shortest sequence of moves that will transform the given stateto that state. The move is actually the movement of the blank space representedby four directions : U (Up), L (Left), D (Down) and R (Right). After each testcase 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解决,最后一个状态就是最远的状态,用哈希判重。

AC的代码如下:


UVA The most distant state

标签:acm   uva   implicit graph trave   bfs   

原文地址:http://blog.csdn.net/u013840081/article/details/39118563

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!