题解 UVA12666 【疯狂的谜题 Killer Puzzle】

· · 题解

2025/9/12:修复了新代码的错误,在 UVa 上测试后跑了 610ms,稳固了最快解的地位。

2024/10/10:代码重构,题解改动。

你需要找出通过 Lisp 编写的谜题的答案。多组数据,其中大多数数据为小数据,而大数据中最多有 10 个问题,每个问题有 5 个答案。

可以通过穷举 5^{10}=9765625 种可能答案,并对每个答案进行正确性检验。检验伪代码如下:

forall(answer_list):
    bad = False
    for testing_question in [1,2,3,4,5,6,7,8,9,10]:
        for testing_option in ["a","b","c","d","e"]:
            # 你的答案应该是正确的 
            if testing_option == answer_list[testing_question] 
                    and check(testing_question, testing_option) == False: 
                bad = True
            # 其他选项都是错误的
            if testing_option != answer_list[testing_question] 
                    and check(testing_question, testing_option) == True: 
                bad = True
    if not bad:
        print answer_list
``` check(testing_question, testing_option): 1. set-up the function (option-value) so that it returns the evaluation result of testing_option of testing_question 2. evaluate the lisp expression of testing_question (e.g. the expression (equal (answer 3) (answer (option-value))) in the example above) 3. if an unhandled exception is raised during the evaluation, returns False 4. if the result of step 2 is boolean, return it; otherwise return False ``` ## 题目分析 在判断正确性之前,我们需要一份答案,这个时候可以利用 DFS 或者枚举获取,这里不再赘述。 然后就是判断正确性。根据上面的伪代码,我们要判断我们选中的答案是否正确,并且其他答案是否完全错误。因此,我们只需要对每个答案对应的语句,运行 $\operatorname{check}(\text{testing\_question}, \text{testing\_option})$,通过正确性判断即可。 接下来就是重头戏:如何计算一个表达式的值。 首先,根据 $\operatorname{check}$ 的描述,我们需要处理一些函数出现的异常。因此,$\operatorname{check}$ 函数的返回值需要有两个参数:异常编码,以及表达式的值。 我们随后需要分析这个表达式。由于 Lisp 语言下的表达式的函数写法为 `(function_name [function_options])`,并且题目提供的全部是参数一定的函数和谓词,因此可以直容易对表达式进行解析和构建 AST(对应的,AST 版本的代码中的 `get_expr` 部分十分简洁)。 接下来有两种实现思路。一是递归法,即通过传入字符串的左右端点,得到答案就回溯的方式求解;二是表达式树(就是 AST)法,在表达式树上通过 DFS 的方法求值。后者相对来说多了构造 AST 的步骤,但是可以很容易获取参数的根位置进行计算,并且在这道题中可以适当减少函数的谓词的区分度,减少代码复杂程度。比如在计算谓词时需要传入下一个树,AST 上可以直接返回某个已经计算好的值,而递归法需要进行一次表达式括号匹配。 然后这道题的分析就基本上结束了。欢迎来到思维少但是代码多的部分:分类讨论。我们可以通过获取字符串最左端的函数/谓词名或者在 AST 中查找根信息的方式获取当前需要计算的字符串。然后,分成两部分:函数计算和谓词计算。最后就是根据名字模拟了。 在这一道题中,方便起见,我们需要在计算谓词的时候中传入下一个数的值,这样可以在到达基础谓词(e.g. `primp-p`)时计算答案,在谓词生成器(e.g. `make-not`)时合并答案。 当然,我们也可以选择新增若干个函数种类,分别对应谓词生成器生成的谓词种类(比如新增一个 `prepared-not` 代表 `make-not` 的返回值)。这个方法在面向对象编程和多态上可以更好的处理内存回收等问题。 --- - 2019 年 9 月实现的递归代码:[Link](https://www.luogu.com.cn/paste/37jdywde); - 2024 年 10 月实现的 AST 代码:[Link](https://www.luogu.com.cn/paste/r0w1z8l8),在自己实现 `shared_ptr` 后运行时间为 800ms,在本题解重写时为 rk1。 另外,下面的输入对应为题目一开始给出的问题: ```lisp 10 5 (equal (option-value) (first-question (make-answer-equal "b"))) 2 3 4 5 6 (equal (option-value) (only-question (make-answer-diff-next-equal 0))) 2 3 4 5 6 (equal (answer 3) (answer (option-value))) 1 2 4 7 6 (equal (option-value) (count-question (make-answer-equal "a"))) 0 1 2 3 4 (equal (answer 5) (answer (option-value))) 10 9 8 7 6 (equal (count-question (make-answer-equal "a")) (count-question (make-answer-equal (option-value)))) "b" "c" "d" "e" "none-of-above" (equal (option-value) (diff-answer 7 8)) 4 3 2 1 0 (equal (option-value) (count-question (make-answer-is vowel-p))) 2 3 4 5 6 ((option-value) (count-question (make-answer-is consonant-p))) prime-p factorial-p square-p cubic-p (make-is-multiple 5) (equal (option-value) (answer 10)) "a" "b" "c" "d" "e" ```