zoukankan      html  css  js  c++  java
  • 练习:亲子游戏(矩阵)--python实现

    dir0=[[0,1],[1,0],[0,-1],[-1,0]]
    step = 999999999
    tempStep = 0
    tempValue = 0
    def grid_input(N):
        grid = [[]for i in range(N)]
        for i in range(N):
            line = input().split('')
            for j in range(len(line)):
                grid[i].append(int[line[j]])
        return grid
    
    def M_find_C(grid,row,col,e_row,e_col):
        global step
        global candy
        global tempValue
        global tempStep
        global dir0
        tempStepValue = 0
        if row<0 or col<0 or row>len(grid) or col>len(grid) or grid[row][col]==-1:
            return None
        if row == e_row and col == e_col:
            if tempStep+1 < step:
                step = tempStep+1
                candy = tempValue
            elif tempStep+1 == step and tempValue > candy:
                candy = tempValue
            return None
        if grid[row][col] >= 0:
            tempStepValue = grid[row][col]
            tempValue += tempStepValue
            tempStep+=1
            grid[row][col] = -1
        for i in range(0,4):
            M_find_C(grid,dir0[i][0]+row,dir0[i][1]+col,e_row,e_col)
            if grid[row][col]>=0:
                tempValue -=tempStepValue
                tempStep -=1
                grid[row][col] = tempStepValue
    
    def M_C_games(N,grid):
        s_row = 0
        s_col = 0
        e_row = 0
        e_col = 0
        for i in range(0,N):
            for j in range(0,N):
                if grid [i][j] == -3:
                    s_row = i
                    s_col = j
                elif grid[i][j] == -2:
                    e_row = i
                    e_col = j
        M_find_C(grid,s_row,s_col,e_row,e_col)
        return candy
    
    if __name__ == "__main__":
        N = int(input())
        M_C_games(N,grid_input(N))
        print(candy)
    

      

    2021-1-2,笔记

  • 相关阅读:
    windows下jmeter安装配置
    golang读取json配置文件
    磁盘分区、扇区等概念理解
    linux 命令笔记
    jvm静态分派和动态分派
    java实例初始化顺序
    计算机网络
    文件对比工具Meld
    IaaS、PaaS、SaaS
    USB 驱动常见名词解释
  • 原文地址:https://www.cnblogs.com/yuntimer/p/14224040.html
Copyright © 2011-2022 走看看