Description Consider the following 5 picture frames placed on an 9 x 8 array. ........ ........ ........ ........ .CCC.... EEEEEE.. ........ ........ ..BBBB.. .C.C.... E....E.. DDDDDD.. ........ ..B..B.. .C.C.... E....E.. D....D.. ........ ..B..B.. .CCC.... E....E.. D....D.. ....AAAA ..B..B.. ........ E....E.. D....D.. ....A..A ..BBBB.. ........ E....E.. DDDDDD.. ....A..A ........ ........ E....E.. ........ ....AAAA ........ ........ EEEEEE.. ........ ........ ........ ........ 1 2 3 4 5 Now place them on top of one another starting with 1 at the bottom and ending up with 5 on top. If any part of a frame covers another it hides that part of the frame below. Viewing the stack of 5 frames we see the following. .CCC.... ECBCBB.. DCBCDB.. DCCC.B.. D.B.ABAA D.BBBB.A DDDDAD.A E...AAAA EEEEEE.. In what order are the frames stacked from bottom to top? The answer is EDABC. Your problem is to determine the order in which the frames are stacked from bottom to top given a picture of the stacked frames. Here are the rules: 1. The width of the frame is always exactly 1 character and the sides are never shorter than 3 characters. 2. It is possible to see at least one part of each of the four sides of a frame. A corner shows two sides. 3. The frames will be lettered with capital letters, and no two frames will be assigned the same letter. Input Each input block contains the height, h (h<=30) on the first line and the width w (w<=30) on the second. A picture of the stacked frames is then given as h strings with w characters each. Your input may contain multiple blocks of the format described above, without any blank lines in between. All blocks in the input must be processed sequentially. Output Write the solution to the standard output. Give the letters of the frames in the order they were stacked from bottom to top. If there are multiple possibilities for an ordering, list all such possibilities in alphabetical order, each one on a separate line. There will always be at least one legal ordering for each input block. List the output for all blocks in the input sequentially, without any blank lines (not even between blocks). Sample Input 9 8 .CCC.... ECBCBB.. DCBCDB.. DCCC.B.. D.B.ABAA D.BBBB.A DDDDAD.A E...AAAA EEEEEE.. Sample Output EDABC
时间: 2024-02-14 07:09:54 浏览: 19
这是一道经典的拓扑排序问题。我们可以将每个图形看作一个节点,如果一个图形可以覆盖另一个图形,那就从覆盖的图形指向被覆盖的图形。最后得到的图是一个 DAG,我们可以对其进行拓扑排序。拓扑排序的结果就是图形的叠放顺序。
代码实现可以使用邻接表存储 DAG,然后使用 Kahn 算法进行拓扑排序。对于存在多个拓扑序列的情况,我们可以使用字典序最小的方式输出所有的序列。
以下是 Python 代码实现:
相关问题
In xtfrm.data.frame(x) : cannot xtfrm data frames
这个警告通常是在对数据框进行排序操作时出现的,警告的意思是无法对数据框进行 `xtfrm()` 操作。
`xtfrm()` 是一个用于排序的内部函数,它是在 `sort()` 函数中被调用的,它将数据转换为一种可排序的格式。在对数据框进行排序时,数据框中的每一列都需要转换为可排序的格式,但是数据框本身并不能转换为可排序的格式,因此会出现该警告。
如果你确实需要对数据框进行排序,可以使用 `order()` 函数来排序,而不是使用 `sort()` 函数。`order()` 函数可以按照指定列的顺序排序数据框,并且不会出现上述警告。示例代码如下:
```r
# 假设数据框为df,按照x列升序排列
df <- df[order(df$x), ]
```
如果你不需要对数据框进行排序,而仅是在其他操作中出现了上述警告,可以忽略该警告,因为它不会影响代码的执行和结果。
in xtfrm.data.frame(x) : cannot xtfrm data frames
### 回答1:
这个错误提示是因为在使用xtfrm函数时,不能对数据框进行转换。xtfrm函数是用于对数据进行排序和比较的,但是数据框不支持这种操作。如果需要对数据框进行排序或比较,可以使用sort函数或者order函数。
### 回答2:
在R编程中,当我们使用xtfrm函数对数据进行排序时,可能会遇到“in xtfrm.data.frame(x) : cannot xtfrm data frames”的错误提示。
这个错误提示的意思是xtfrm函数无法对数据框进行排序,因为它们包含多种数据类型,如字符、数值和逻辑等。xtfrm函数只能对具有单一数据类型的向量进行排序。
为解决这个问题,我们可以将数据框转换为单一数据类型的向量,例如将每列的数据合并到一个向量中,然后使用该向量进行排序。这种方法可以使用unlist函数。
另一种解决方法是使用dplyr或tidyr包中的函数进行数据转换和排序。这些包提供了丰富的数据处理功能,可以轻松地处理各种数据类型,避免了xtfrm函数对数据框的限制。
最后,我们需要注意,在使用xtfrm函数对数据框进行排序时,如果数据框中的列具有不同的数据类型,则结果可能会不正确。因此,在进行排序时,应确保数据框中的所有列具有相同的数据类型。
### 回答3:
xtfrm.data.frame(x) : cannot xtfrm data frames这个错误信息是在处理R语言的数据分析时常见的错误。一般情况下,如果在对一个数据框进行排序时出现这个错误信息,那么原因可能是数据框中存在非数字类型的变量,如字符型、逻辑型或因子型变量。
在进行排序操作时,R语言需要对数据框的每一列进行比较,以决定如何排序。然而,非数字型变量无法比较,因此会出现错误信息。
要解决这个问题,可以用以下两种方式:
1. 删除非数字型变量:在进行排序操作前,将非数字型变量删除,只对数字型变量进行排序。可以使用函数select()来选择需要的变量。
例如:
library(dplyr)
data %>% select_if(is.numeric) %>% arrange(desc(x))
这个例子中,用select_if函数选择了数值型变量,并按照变量x倒序排序。
2. 将非数字型变量转换为数字型变量:将非数字型变量转换为数字型变量后,可以继续对数据框进行排序操作。可以使用函数as.numeric()将字符型变量转换为数字型变量,使用函数as.integer()将逻辑型或因子型变量转换为整数型。
例如:
data$y <- as.numeric(data$y)
data %>% arrange(desc(x), y)
这个例子中,将变量y由字符型转换为数字型后,对x和y进行降序排列。
总之,在进行数据框的排序操作前,一定要注意数据框中的变量类型,以免出现xtfrm data frames的错误信息。