您的位置:首页 > 编程语言

编程求解机械迷城第11关中类青蛙过河的问题

2017-07-02 10:52 218 查看
在玩机械迷城游戏的时候,第11关遇到一个类似青蛙过河的游戏http://jingyan.baidu.com/article/e73e26c014903024adb6a7a4.html,下图是三张地图的攻略,可以略现其玩法。绿点为起点,红点为障碍,灰点是可走的路径。起点可以是任意一个非红点的位置。游戏开始后,点击任意灰点(想象成青蛙踏在上面),其会变成绿点,同时绿点周围的四个格子如果不是红色的,则有黄色的上下左右的箭头显示。然后,点击其中一个箭头(表示青蛙往箭头方向前进),于是,箭头方向的连续的灰格会被标记成黄色(青蛙一旦选择了方向,不遇到边界或者红格,将一直跳下去,不会转向),遇到红色格子或者边界后,会提示下一个可以移动方向(青蛙转向)。如果没有可以移动的方向,那么游戏终止,判别游戏失败与否:若地图上有任意一个灰格存在,那么,判定失败,否则,判定成功。



这不是一个地图遍历的问题么?!于是想通过程序的形式实现求解。下面直接贴代码啦:

#-------------------------------------------------------------------------------

# Name:        fill_map.py

# Purpose:     This program is used to help solve a map in the game Machinarium

#

#

# Author:      stonecome11

#

# Created:     24/06/2017

# Copyright:   (c)  2017

# Licence:     <your licence>

#-------------------------------------------------------------------------------

# Input:a map

# Output: the start point and directions which can fill the map

# Define the map and the marks

ROW_MAX=7

DARK=5 # Mark the broard

GREY=0 # Mark the position that can be stepped on

GREEN=1 # Mark the start position

RED=3  # Mark the position that can not be stepped on

YELLOW=4 # Mark the position that is visited before

the_map=[[5 for i in range(ROW_MAX)] for i in range(ROW_MAX)]

def setMap(a_map):

    for i in range(1, ROW_MAX-1):

        for j in range(1, ROW_MAX-1):

            a_map[i][j]=GREY

#    a_map[1][5]=RED

#    a_map[3][4]=RED

#    a_map[4][1]=RED

#    a_map[5][3]=RED

    a_map[1][3]=RED

    a_map[1][4]=RED

    a_map[1][5]=RED

    a_map[5][1]=RED

    a_map[5][2]=RED

#   print a_map # Debug

# Define directions

UP=0

DOWN=1

RIGHT=2

LEFT=3

dirs_list=[]

def isDead(a_map, start_pt):

    #If start_pt[0]=0

    row=start_pt[0]

    col=start_pt[1]

    if a_map[row+1][col] != GREY and a_map[row-1][col] != GREY and \

    a_map[row][col+1] != GREY and a_map[row][col-1] != GREY:

        return True

    return False

def work(a_map, start_pt):

    # If the place around the start_pt is not dark, exit with false

    if isDead(a_map, start_pt):

        for i in range(1,6):

            for j in range(1,6):

                if a_map[i][j] == GREY:

                    return False

        print a_map

        return True

    # Mark as start point

    if a_map[start_pt[0]][start_pt[1]] != RED:

        a_map[start_pt[0]][start_pt[1]] = GREEN

    # Right

    if a_map[start_pt[0]][start_pt[1]+1] == GREY:

        new_map = [[0 for i in range(ROW_MAX)] for i in range(ROW_MAX)]

        for i in range(ROW_MAX):

            for j in range(ROW_MAX):

                new_map[i][j] = a_map[i][j]

        # If Right is OK

        row = start_pt[0]

        for j in range(start_pt[1]+1, 7):

            col = j

            if a_map[row][col] != GREY :

                if work(new_map, [row,col-1]):

                    dirs_list.insert(0, "right")

                    return True

                else:

                    break

            else:

                new_map[row][col]=YELLOW

    # Left

    if a_map[start_pt[0]][start_pt[1]-1] == GREY:

        new_map=[[0 for i in range(ROW_MAX)] for i in range(ROW_MAX)]

        for i in range(ROW_MAX):

            for j in range(ROW_MAX):

                new_map[i][j]=a_map[i][j]

        # If Right is OK

        row = start_pt[0]

        for j in range(1, start_pt[1]+1):

            col = start_pt[1] - j

            if a_map[row][col] != GREY :

                if work(new_map, [row,col+1]):

                    dirs_list.insert(0, "left")

                    return True

                else:

                    break

            else:

                new_map[row][col]=YELLOW

    # Up

    if a_map[start_pt[0]-1][start_pt[1]] == GREY:

        new_map=[[0 for i in range(ROW_MAX)] for i in range(ROW_MAX)]

        for i in range(ROW_MAX):

            for j in range(ROW_MAX):

                new_map[i][j]=a_map[i][j]

        # If up is OK

        col = start_pt[1]

        for i in range(1, start_pt[0]+1):

            row = start_pt[0] - i

            if a_map[row][col] == GREY :

                new_map[row][col]=YELLOW

            else:

                if work(new_map, [row+1,col]):

                    dirs_list.insert(0, "up")

                    return True

                else:

                    break

    # Down

    if a_map[start_pt[0]+1][start_pt[1]] == GREY:

        new_map=[[0 for i in range(ROW_MAX)] for i in range(ROW_MAX)]

        for i in range(ROW_MAX):

            for j in range(ROW_MAX):

                new_map[i][j]=a_map[i][j]

        # If down is OK

        col = start_pt[1]

        for i in range(start_pt[0]+1, 7):

            row = i

            if a_map[row][col] != GREY :

                if work(new_map, [row-1,col]):

                    dirs_list.insert(0, "down")

                    return True

                else:

                    break

            else:

                new_map[row][col]=YELLOW

    return False

def solver(a_map):

    for i in range(1,6):

        for j in range(1,6):

            setMap(a_map)

                # If the state of the point is RED, exit with false

            if a_map[i][j] == RED:

                continue

            if work(a_map, [i,j])==True:

               print [i, j], dirs_list

               del dirs_list[:]

def main():

    solver(the_map)

    pass

if __name__ == '__main__':

    main()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息