轩_雨

青山不厌三杯酒,长日惟消一局棋
原神_如果稻妻方块是一道编程题

题目

在提瓦特大陆,有个叫稻妻的城邦,藏着能带来巨大财富的宝藏。旅行者需要破解关于方块的谜题,才能得到宝藏。

稻妻的土地上分布着许多组方块(每个方块是能水平旋转的正方体,并且只能在水平面上保持相隔90度的四个朝向)。每组方块各具特色,但他们都有一个共同点,那就是被打的时候会触发可能包括自己在内的数个方块顺时针旋转90度(也有可能是0个方块,也就是打了一点反应也没有)。

旅行者的最终目的就是让这些方块转到同一个朝向上,就可以触发机关,获得宝藏。

但是由于旅行者的数学是体育老师教的,多番尝试无果已经陷入极其愤怒的状态,就要把派蒙当成应急食品吃了。所以派蒙向我们求助,希望我们能够帮旅行者破解谜题。

输入描述

由于题主不想用c和c++来写,所以输入是一个python的字典数组,每个字典代表一个方块,一个字典有两个字段: value 字段代表当前方块朝向,可以理解为 0--东 1--南 2--西 3--北catch 字段代表攻击当前方块会触发顺时针旋转90度变化的方块组,是一个整形数组,每个整数是方块的数组下标。

输出描述

返回一个数组,代表旅行者攻击的方块,按从小到大排序。存在多种解法时返回攻击次数最少的解法。

输入

[{'value':0,'catch':[0,1]},{'value':1,'catch':[0,1,2]},{'value':1,'catch':[1,2]}]

给出一套C/C++的输入,就不解释了,懂的都懂🐶

3
0 1 1
2 0 1
3 0 1 2
2 1 2

输出

[2]

题解

class Solution:
    record = {}
    solvePath = []
    limit = 20
    def isInvailSolvePath(self):
        count = {}
        # print(self.solvePath)
        for a in self.solvePath:
            if a in count:
                count[a] += 1
            else:
                count[a] = 1
            if count[a] >= 4:
                return True
        return False
    def calc_dfs(self, cubeStatus):
        if json.dumps(cubeStatus) in self.record:
            return False
        if self.isInvailSolvePath():
            return False
        if self.check(cubeStatus):
            print("解法:")
            self.log(self.solvePath)
            return
        if len(self.solvePath)>self.limit:
            # print("超过限制")
            return False
        self.record[json.dumps(cubeStatus)] = True
        for (i,item) in enumerate(cubeStatus):
            tempCubeStatus = json.loads(json.dumps(cubeStatus))
            self.solvePath.append(i)
            # print(tempCubeStatus[i]["catch"])
            for c in tempCubeStatus[i]["catch"]:
                tempCubeStatus[c]["value"] = (tempCubeStatus[c]["value"]+1) % 4
            self.calc_dfs(tempCubeStatus)
            self.solvePath.remove(i)
    
    def calc_bfs(self, cubeStatus):
        record = {}
        limit = 20
        nodes = []
        if self.check(cubeStatus):
            print(solvePath)
            return True
        for (i,item) in enumerate(cubeStatus):
            nodes = [[i]] + nodes
        while len(nodes)>0:
            a = nodes.pop()
            # print(a)
            # solvePath.append(a)
            for d in a:
                for c in cubeStatus[d]["catch"]:
                    cubeStatus[c]["value"] = (cubeStatus[c]["value"]+1) % 4
            if self.check(cubeStatus):
                print("解法:")
                self.log(a)
                break
            for d in a:
                for c in cubeStatus[d]["catch"]:
                    cubeStatus[c]["value"] = (cubeStatus[c]["value"]+3) % 4
            for (i,item) in enumerate(cubeStatus):
                nodes = [[i]+a]+nodes
                # solvePath.remove(i)
    
    def check(self, cubeStatus):
        return len(set(map(lambda x:x["value"],cubeStatus))) == 1

    def log(self, path):
        for p in path:
            print("派蒙:A第%d个方块" % (p+1))

s = Solution()
s.calc_dfs([
    {'value':0,'catch':[0,2]},
    {'value':2,'catch':[0,1,2]},
    {'value':1,'catch':[0,2,4]},
    {'value':2,'catch':[2,3,4]},
    {'value':0,'catch':[2,4]}
])
s.calc_bfs([
    {'value':0,'catch':[0,2]},
    {'value':2,'catch':[0,1,2]},
    {'value':1,'catch':[0,2,4]},
    {'value':2,'catch':[2,3,4]},
    {'value':0,'catch':[2,4]}
])

因为自己写着玩,有很多格式化输出的东西,大家看个乐呵就好。
本来想附上实际运用的视频,结果最近跑图都没有见到,估计是被我刷完了。

posted on 2021-08-16 20:05  轩_雨  阅读(244)  评论(0编辑  收藏  举报