立方体的一个遍历的问题求解,怎么采用C语言的程序的设计的技术的编写的程序怎么做

Problem Description
On a small planet named Bandai, a landing party of the starship Tadamigawa discovered colorful cubes traveling on flat areas of the planet surface, which the landing party named beds. A cube appears at a certain position on a bed, travels on the bed for a while, and then disappears. After a longtime observation, a science officer Lt. Alyssa Ogawa of Tadamigawa found the rule how a cube travels on a bed.

A bed is a rectangular area tiled with squares of the same size.

One of the squares is colored red,

one colored green,

one colored blue,

one colored cyan,

one colored magenta,

one colored yellow,

one or more colored white, and

all others, if any, colored black.

Initially, a cube appears on one of the white squares. The cube’s faces are colored as follows.
top red

bottom cyan

north green

south magenta

east blue

west yellow

The cube can roll around a side of the current square at a step and thus rolls on to an adjacent square. When the cube rolls on to a chromatically colored (red, green, blue, cyan, magenta or yellow) square, the top face of the cube after the roll should be colored the same. When the cube rolls on to a white square, there is no such restriction. The cube should never roll on to a black square.

Throughout the travel, the cube can visit each of the chromatically colored squares only once, and any of the white squares arbitrarily many times. As already mentioned, the cube can never visit any of the black squares. On visit to the final chromatically colored square, the cube disappears. Somehow the order of visits to the chromatically colored squares is known to us before the travel starts. Your mission is to find the least number of steps for the cube to visit all the chromatically colored squares in the given order.

Input
The input is a sequence of datasets. A dataset is formatted as follows:

w d

c11 · · · cw1

...

...

c1d · · · cwd

v1v2v3v4v5v6

The first line is a pair of positive integers w and d separated by a space. The next d lines are w-character-long strings c11 · · · cw1,. . . , c1d · · · cwd with no spaces. Each character cij is one of the letters r, g, b, c, m, y, w and k, which stands for red, green, blue, cyan, magenta, yellow, white and black respectively, or a sign #. Each of r, g, b, c, m, y and # occurs once and only once in a dataset. The last line is a six-character-long string v1v2v3v4v5v6 which is a permutation of “rgbcmy”.

The integers w and d denote the width (the length from the east end to the west end) and the depth (the length from the north end to the south end) of a bed. The unit is the length of a side of a square. You can assume that neither w nor d is greater than 30.

Each character cij shows the color of a square in the bed. The characters c11, cw1, c1d and cwd correspond to the north-west corner, the north-east corner, the south-west corner and the southeast corner of the bed respectively. If cij is a letter, it indicates the color of the corresponding square. If cij is a #, the corresponding square is colored white and is the initial position of the cube. The string v1v2v3v4v5v6 shows the order of colors of squares to visit. The cube should visit the squares colored v1, v2, v3, v4, v5 and v6 in this order.

The end of the input is indicated by a line containing two zeros separated by a space.

Output
For each input dataset, output the least number of steps if there is a solution, or “unreachable” if there is no solution. In either case, print it in one line for each input dataset.

Sample Input
10 5
kkkkkwwwww
w#wwwrwwww
wwwwbgwwww
kwwmcwwwkk
kkwywwwkkk
rgbcmy
10 5
kkkkkkkkkk
k#kkkkkkkk
kwkkkkkwwk
kcmyrgbwwk
kwwwwwwwwk
cmyrgb
10 5
kkkkkkkkkk
k#kkkkkkkk
kwkkkkkwkk
kcmyrgbwwk
kwwwwwwwwk
cmyrgb
0 0

Sample Output
9
49
unreachable

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

相似问题

0
求解,举例解释一下数据仓库的完全立方体和冰山立方体
3
C语言实现,给定一段连续的整数,求出他们中所有偶数的平方和以及所有奇数的立方和
1
立方体的表面的数字编号的和isuande问题,堆叠的方块怎么计算?采用C语言
1
Java,知道一个不规则的立方体八个顶点,来确定一个点是否在立方体内
0
立方体多面体叠加,数字编号的排列的问题,计算每一个的编号,C语言的实现
0
一个有关于三维立方体的路径连接的问题,运用C语言
1
要求输出所有在m和n范围内的水仙花数。这个用C语言怎么解答的?
1
分别表示该段连续的整数中所有偶数的平方和以及所有奇数的立方和,这个算法问题的C语言的解法,谢谢
1
给定一段连续的整数,求出他们中所有偶数的平方和以及所有奇数的立方和,具体运用C语言的实现
0
三维立方体在平面上投影距离的计算,怎么使用C程序的语言代码编写程序的方式来实现呢?
1
要输出所有在m和n范围内的水仙花数,采用C程序的语言代码编写程序的手段如何实现的?
0
由字符串构成的一个立方体的图案的生成,怎么利用C程序语言代码编写的方式来进行的?
0
超立方体的的构造命题的算法,怎么采用C程序的语言代码编写的技术去实现?
0
正立方体的匹配问题的计算,怎么采用C程序的语言的代码编写的技术实现的?
0
输出所有在给定范围内的水仙花数,怎么采用C语言的程序编写代码的技术方式去实现这个程序编写的?
1
Python/matplotlib:已知空间上一点坐标和长宽高值,如何画出立方体?
1
判断给定的范围内不存在水仙花数,如果有输出水仙花数否则输出no,怎么用 C语言的程序设计的
0
超立方体的一个最小的连接花费的问题计算,怎么使用C语言的程序的编写的过程的步骤来实现呢
0
输出所有在m和n范围内的水仙花数,请问怎么使用C语言的代码的编写做法实现的程序的编写的过程
0
立方体的整齐的排列的算法,怎么采用C语言的程序的设计的格式来实现的