数字矩阵里数字的移动的问题,输出移动行的数字,采用C语言如何实现?

Problem 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, several descriptions of configuration of the 8 puzzle. One 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. Do not print a blank line between cases.

Sample Input
2 3 4 1 5 x 7 6 8

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问

相似问题

1
c语言或者C++旋转矩阵问题求解
5
matlab矩阵提取,如何用矩阵B信息从矩阵A里提取子矩阵
4
matlab和单片机串口通信矩阵传输
4
为什么求对卷积核导数时要把输入矩阵或者被卷积矩阵旋转180
2
求助matlab如何生成8行8列、8行16列、8行24列矩阵,满足如下要求
5
简单的js问题,回答就有c币了:请问怎么在js里面实现求一个矩阵的数学期望值?
3
matlab中怎样将一个多行4列的矩阵变成4个多行1列的矩阵?
1
MATLAB里如何将存在二维数组/矩阵中的坐标点画出来
3
在ACM上看到的题目:求矩阵元素之和
1
一个C语言方面有关分数矩阵的问题
1
python的矩阵运算的相关问题
0
动态规划问题:输入一个只有0和1的矩阵,消去1使得每行每列至多只有一个1,求消去1后矩阵中1的最大个数
1
用C 语言编程,实现矩阵(3行3列)的转置(即行列互换)
2
输入一个整数n再输入一个n行n列的矩阵,找出该矩阵中绝对值最大的元素以及它的行号列号
1
输入一个3行4列的整数矩阵,计算并输出对角线上的元素之和
1
如何用C语言实现求给定矩阵的逆矩阵?
1
多个矩阵相乘程序的编写,我是新手,急,大家帮帮忙
0
数字矩阵构成图像的问题,类似ascii画图,用C语言思想实现的办法
0
二维数组的数字移动问题,结果还是矩阵,要求用C语言的办法来实现,谢谢
1
矩阵求和变量取最大输出,这个问题采用C语言的解决的思路