POJ 3083 - Children of the Candy Corn + Python

一 原题及参考资料
# 英文:https://www.cnblogs.com/lovychen/p/3879044.html
# 中文:https://exp-blog.com/algorithm/poj/poj3083-children-of-the-candy-corn/
# https://blog.csdn.net/lyy289065406/article/details/6647668
# https://blog.csdn.net/weixin_44689154/article/details/99731192

特别注意:左转、右转时,face的更新方式不同。

二 代码

# 万圣节——玉米地迷宫
# 英文:https://www.cnblogs.com/lovychen/p/3879044.html
# 中文:https://exp-blog.com/algorithm/poj/poj3083-children-of-the-candy-corn/
# https://blog.csdn.net/lyy289065406/article/details/6647668
# https://blog.csdn.net/weixin_44689154/article/details/99731192

import collections
## 索引转坐标
def sub2cor(h,w,data):
    data_dict = collections.defaultdict(list)
    temp = []
    count = 0
    for i in range(h):
        for j in range(w):
            temp.append(data[i][j])
            data_dict[str(i)+";"+str(j)] = [count,data[i][j]]
            count += 1
            # 查找初始节点和目标节点
            if data[i][j] == "S":
                s = str(i)+";"+str(j);
            if data[i][j] == "E":
                target = str(i)+";"+str(j);
    # print(data_dict)
    return temp,data_dict,s,target
##
class Solution:
    def DFS(self,s,target,way):
        self.keys = data_dict.keys()

        self.steps = 0
        self.dfs(s,0,way,1)
        return self.steps

    def dfs(self,s,face,way,step):
        # 判断是否到达目标点
        if s == target:
            #
            self.steps = step
            # print(step)
            # return self.steps
            return
        # 由当前方向确定下一个方向
        # 分左右进行判断
        if way == 'left':
            nextFace = (face+3)%4
        elif way == 'right':
            nextFace = (face+1)%4
        for i in range(4):
            hh,ww = map(int,s.split(';'))
            new_hh = hh + dir[nextFace][0]
            new_ww = ww + dir[nextFace][1]
            if (str(new_hh)+";"+str(new_ww)) not in self.keys or data_dict[str(new_hh)+";"+str(new_ww)][1]=='#':
                # 转移方向

                if way == 'left':
                    face += 1 #千万注意啊,走不通时,往前走
                    nextFace = (face + 3) % 4
                elif way == 'right':
                    face -= 1 #千万注意啊,走不通时,往后退
                    nextFace = (face + 1) % 4
                continue
            if (str(new_hh)+";"+str(new_ww)) in self.keys and data_dict[str(new_hh)+";"+str(new_ww)][1] !='#':
            # if data_dict[str(new_hh) + ";" + str(new_ww)][1] != '#':
                face = nextFace;
                self.dfs(str(new_hh)+";"+str(new_ww),face,way,step+1)
                break
        return
## 最短路径bfs
def numOFpath(target,parent):
    V = target
    step = 0
    while V!=None:
        V = parent[V]
        step += 1
    return step-1

def bfs(s, target):
    if s == target:
        return 0
    # 将起始节点加入到列表中
    queue = []
    queue.append(s)
    # 设置已访问的节点
    visited = set()
    visited.add(s)
    # 设置父节点
    parent = {s:None}
    # 键值节点
    keys = data_dict.keys()
    # 循环处理
    while len(queue) >0:
        # 弹出节点
        vertex = queue.pop(0)
        visited.add(vertex)
        # 查找邻接点
        nodes = []
        hh, ww = map(int, vertex.split(';'))
        str1 = str(hh+1) + ";" + str(ww)
        str2 = str(hh-1) + ";" + str(ww)
        str3 = str(hh) + ";" + str(ww+1)
        str4 = str(hh) + ";" + str(ww-1)
        nodes.append(str1)
        nodes.append(str2)
        nodes.append(str3)
        nodes.append(str4)
        for i in nodes:
            if i in keys:
                if i not in visited and data_dict[i][1] !='#':
                    queue.append(i)
                    # visited.add(i)
                    parent[i] = vertex
                    if i == target:
                        return numOFpath(target,parent)+1
    return -1



## 关于输入
n = int(input().strip()) #共n个迷宫
maze_size = [] #存储迷宫大小
maze_data = []
for i in range(n):
    # 第i组迷宫
    w,h = map(int,input().strip().split(' ')) #获取迷宫的宽度和高度
    maze_size.append([w,h])
    # 接下来,存储这些数据
    temp = []
    for j in range(h):
        temp.append(input().strip())
    maze_data.append(temp)
print(maze_size)
print(maze_data)

## 定义旋转方向
dir = {0:[0,-1], 1:[-1,0], 2:[0,1], 3:[1,0]}
# 将索引值转化为坐标值
for i in range(n):
    # 迷宫size
    h,w = maze_size[i][1],maze_size[i][0]
    data = maze_data[i]
    # 利用字典存储坐标:【索引,元素】
    data,data_dict,start,target = sub2cor(h,w,data)
    # 执行相应的搜索程序
    test = Solution()
    left = test.DFS(start,target,'left')
    # print(left)
    test = Solution()
    right = test.DFS(start, target, 'right')
    # print(right)
    ans = bfs(start,target)
    print(left,right,ans)
    

输入:

2
8 8
########
#......#
#.####.#
#.####.#
#.####.#
#.####.#
#...#..#
#S#E####
9 5
#########
#.#.#.#.#
S.......E
#.#.#.#.#
#########

输出:

37 5 5
17 17 9

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值