读一读填字游戏!


13

与此问题类似,但这是填字游戏的变体!

而不是每个网格正方形只有一个字母,您可以拥有一个或两个。

输入:

  • 2D数组,或任何以您的语言工作的数组。
  • 您可以假设输入有效
  • 任何数组大小都必须有效

输出:

  • 所有单词的数组
    • 上下
    • 所有单词都必须连接在一起,即以不间断的单词链链接(如果不返回false)
    • 单词必须至少是两个网格正方形,而不是字母

例:

[["",  "wo", "r",  "k"],
[ "",   "r",  "",   ""],
[ "he", "l",  "lo", ""],
[ "",   "d",  "ad", ""]]

返回值:

["work", "world", "hello", "load", "dad"]

例:

[["he", "ll", "o"],
[ "",   "",   ""],
[ "wo", "r",  "ld"]]

返回值:

false

这是,因此我将在Windows 7上以2.5ghz和16gb的ram运行它。如果您的代码确实很深奥,请提供指向编译器的链接,以便我可以实际运行它。


9
欢迎来到PPCG!
— FlipTack

2
您应该将两个空格部分替换为两个网格正方形。
— 加博尔·费克特

1
将使用什么尺寸的输入尺寸来测量速度?
— 马丁·恩德

@MartinEnder示例
— epicbob57 '16

@ epicbob57似乎很难测量可靠的时间。您通常会测量I / O和其他开销。
— 马丁·恩德

Answers:


1

Python 3

import numpy
from scipy.ndimage import measurements

def crosswords(arr):
    M=numpy.asarray(arr)
    # check connectivity
    if measurements.label(numpy.where(M!='',1,0))[-1] != 1:
        return 'false'

    words = []
    def get_words(mat):
        for r in mat:
            word,counter='',0
            for c in r:
                if c=='':
                    if counter>1:
                        words.append(word)
                    word, counter = '', 0
                else:
                    word, counter = word+c, counter+1
            if counter > 1:
                words.append(word)
    get_words(M)
    # transpose M
    get_words(M.T)
    return words

用法:

函数将字符串数组的数组作为输入:

crosswords( [["", "wo", "r", "k"], [ "", "r", "", ""], [ "he", "l", "lo", ""], [ "", "d", "ad", ""]])

false连接性返回多个标签时,返回字符串。否则返回有效单词的数组。

我使用timeit,time.time()并使用console命令对它进行了计时time,但是我不知道要使用哪一个或要在此处发布哪一个。


我意识到我没有Python 3 ...无论如何,我将使用time.time()对其进行测试
— epicbob57

我似乎无法使用pip安装scipy ...
— epicbob57 '20

你用过pip3吗?
— 加博尔·费克特

pip 9.0.1(python 3.5)
— epicbob57 '16

哦,你是在Windows上,以管理员特权时尝试
— 的Gabor菲克特
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.