八数码问题

作者: 来个芒果 | 来源:发表于2017-04-01 14:57 被阅读0次
八数码

Note:实现代码之前可以先写出伪代码,可以节省很多时间。编程语言只是一种实现工具。

def judge_position(init_m,target_m):#返回当前位置正确的数字的个数,以及0所在位置
    current_right_nums=0 #store numbers of right position currently.
    zero_r=0
    zero_c=0
    count=0  #当前状态中,各数字移动到正确位置的步数
    for row in range(3):
        for col in range(3):
            if init_m[row][col]==0:
                zero_r=row
                zero_c=col
            if init_m[row][col]==target_m[row][col]:
                current_right_nums+=1
            else:
                for j in range(3):
                    for k in range(3): 
                        if init_m[row][col]==target_m[j][k] and init_m[row][col]!=0:
                            count+=abs(row-j)+abs(col-k)
    return (current_right_nums,zero_r,zero_c,count)
    
#----------------------------

def change_position(init_m,zero_r,zero_c):
    #判断当前状态是否等于结束状态
    current_right_nums,zero_r,zero_c,count=judge_position(init_m,target_matrix)
    print("----------")
    m1=[[i for i in row] for row in init_m]
    m2=[[i for i in row] for row in init_m]
    m3=[[i for i in row] for row in init_m]
    m4=[[i for i in row] for row in init_m]
    next_matrixes=[]
    #交换位置:(zero_r+1,zero_c+1)(zero_r,zero_c-1)(zero_r,zero_c+1)(zero_r-1,zero_c)
    if  zero_r+1<3:
        temp=m1[zero_r+1][zero_c]
        m1[zero_r+1][zero_c]=m1[zero_r][zero_c]
        m1[zero_r][zero_c]=temp
        next_matrixes.append(m1)
    
    if zero_c-1>-1:   #zero_r,zero_c-1
        temp=m2[zero_r][zero_c-1]
        m2[zero_r][zero_c-1]=m2[zero_r][zero_c]
        m2[zero_r][zero_c]=temp
        next_matrixes.append(m2)
    
    if zero_c+1<3:    #(zero_r,zero_c+1)
        temp=m3[zero_r][zero_c+1]
        m3[zero_r][zero_c+1]=m3[zero_r][zero_c]
        m3[zero_r][zero_c]=temp
        next_matrixes.append(m3)
    
    if zero_r-1>-1: #(zero_r-1,zero_c)
        temp=m4[zero_r-1][zero_c]
        m4[zero_r-1][zero_c]=m4[zero_r][zero_c]
        m4[zero_r][zero_c]=temp
        next_matrixes.append(m4)
    #判断每一状态中,各数字移动到正确位置的补步数,选取最小的
    matrixes_info=[]
    for martrix in next_matrixes:
        matrixes_info.append(list(judge_position(martrix,target_matrix)))
    to_target_counts=[i[3] for i in matrixes_info]
    best_matrix=next_matrixes[to_target_counts.index(min(to_target_counts))]
    if matrixes_info[next_matrixes.index(best_matrix)][3]==0:
        print("\nSuccess!Next step is target matrix:")
        for row in best_matrix:
            print(row)
        return
    print("next_step:")
    for row in best_matrix:
            print(row)
    print("Current_right_numbers:",matrixes_info[next_matrixes.index(best_matrix)][0])
    print("To target need steps:",matrixes_info[next_matrixes.index(best_matrix)][3])
    zero_r=matrixes_info[next_matrixes.index(best_matrix)][1]
    zero_c=matrixes_info[next_matrixes.index(best_matrix)][2]
    change_position(best_matrix,zero_r,zero_c)
    
def regular_matrix(init_m,target_m):
    current_right_nums,zero_r,zero_c,count=judge_position(init_matrix,target_matrix)
    change_position(init_m,zero_r,zero_c)
    
if __name__ == "__main__":
    
    init_matrix=[[2,8,3],[1,0,4],[7,6,5]]
    target_matrix=[[1,2,3],[8,0,4],[7,6,5]]
    current_right_nums,zero_r,zero_c,count=judge_position(init_matrix,target_matrix)    
    regular_matrix(init_matrix,target_matrix)

    

相关文章

  • 八数码问题

    Note:实现代码之前可以先写出伪代码,可以节省很多时间。编程语言只是一种实现工具。

  • 八数码问题解析

    PPT展示

  • python解决八数码问题

    八数码问题也称为九宫问题。在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的某一数字,不同棋子上标的数字不相同。...

  • 数字能量学之《八星数组作用规律》

    八星数码: 生气数码,延年数码, 天医数码,㐲位数码, 绝命数码,五鬼数码, 六煞数码,祸害数码, 一、能量大小的...

  • 八数码问题的A*算法求解

    A*算法是启发式搜素算法中较为出名和高效的算法之一,其关键是对于启发式函数的实际,启发式函数h(x)需要尽可能的接...

  • 广度优先搜索:八数码问题

    问题描述 问题分析 状态空间 广度优先搜索 用队列保存待扩展的节点从队首队取出节点, 扩展出的新节点放入队尾,直到...

  • 搜索 | 八数码问题(续一)

    写在前面: 中秋假期,孤单一人,码码字,学学习,也挺好的。 本篇文章是对于 八数码是否有解的问题及其推广 进行一个...

  • 八数码

    八数码,BFS模板题,不做人生不完整

  • 八数码

    这道题是Acwing上面845题. 八数码是一道bfs的扩展应用。这道题主要是你能够把整个二维数组抽象成一个str...

  • 经典搜索问题合集

    题目链接:算24点 题目链接:经典n连环问题 题目链接:标准数独问题 题目链接:经典八数码问题

网友评论

    本文标题:八数码问题

    本文链接:https://www.haomeiwen.com/subject/mdmiottx.html