CodingGame 前两题 -- The Descent -- Dungeons and Maps

https://www.codingame.com/ide/puzzle/the-descent

大概就是飞机飞一次就下降1的距离,每次飞行都要击毁一座最高的山来保驾护航,知道飞机落地。

这题有个坑就是 飞机发射不一定会将一座山的高度夷平,可能只会消除一部分,因此不能说一次while循环就输出整个输入的结果,不过这样也让代码变得更简单,之前想的太复杂了

每次while循环都要讲最大高度 和 攻击山头的下标置0,无缝衔接下一次攻击

import sys
import math

# The while loop represents the game.
# Each iteration represents a turn of the game
# where you are given inputs (the heights of the mountains)
# and where you have to print an output (the index of the mountain to fire on)
# The inputs you are given are automatically updated according to your last actions.

# game loop
while True:
    max_height = 0
    fire_index = 0
    for i in range(8):
        mountain_h = int(input())  # represents the height of one mountain.
        if mountain_h >= max_height:
            max_height = mountain_h
            fire_index = i

    # Write an action using print
    # To debug: print("Debug messages...", file=sys.stderr, flush=True)

    # The index of the mountain to fire on.
    print(fire_index)

 

 

https://www.codingame.com/ide/puzzle/dungeons-and-maps

这题考二维数组和递归调用,题目大概意思是:给几个二维数组的地图,跟着箭头走相应的位置,最终找到宝藏T,看那个地图需要走的路径最短就输出该地图下标。

第一行输入是二维数组的宽和高

第二行输入是数组的起始点位置

第三行输入是地图个数

接着就是地图的具体二维数组了

. - Empty square
# - Wall
^ - Move UP
v - Move DOWN
< - Move LEFT
> - Move RIGHT
T - The treasure square

有最短路径时就输出地图下标,无法走到T位置输出TRAP

好久没写递归,写个递归都有点费劲,还需要考虑一些边界问题,越界的话当作TRAP处理;

还要考虑真的TRAP情况,就是一直在走死循环,这里我用递归深度来判断有没有死循环,如果递归深度大于二维数组元素个数就说明死循环了

剩下判断去了哪个地方就再进一步递归

输出的时候判断一下是否需要打印TRAP

import sys
import math


# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.
def calculate_length(matrix, start_row, start_col, deep_count):
    # out of range
    if start_row < 0 or start_row > len(matrix)-1 or \
        start_col < 0 or start_col > len(matrix[0]):
        return len(matrix) * len(matrix[0]) + 1

    # end
    if matrix[start_col][start_row] == 'T':
        return 1

    # trap
    if deep_count > len(matrix) * len(matrix[0]):
        return len(matrix) * len(matrix[0]) + 1

    if matrix[start_col][start_row] == '>':
        return 1 + calculate_length(matrix, start_row+1, start_col, deep_count+1)
    elif matrix[start_col][start_row] == '<':
        return 1 + calculate_length(matrix, start_row-1, start_col, deep_count+1)
    elif matrix[start_col][start_row] == 'v':
        return 1 + calculate_length(matrix, start_row, start_col+1, deep_count+1)
    elif matrix[start_col][start_row] == '^':
        return 1 + calculate_length(matrix, start_row, start_col-1, deep_count+1)
    # cannot go
    else:
        return len(matrix) * len(matrix[0]) + 1


w, h = [int(i) for i in input().split()]
start_row, start_col = [int(i) for i in input().split()]
matrix_list = []
n = int(input())

for i in range(n):
    matrix = []
    for j in range(h):
        map_row = input()
        list_temp = []
        for row in map_row:
            list_temp.append(row)
        matrix.append(list_temp)
    matrix_list.append(matrix)

#################
# print(start_row, start_col)
# for i in range(len(matrix_list[2])):
#     print(matrix_list[2][i])
#
################

index = 0
is_trap = False
min_lenght = w * h + 1
for i in range(len(matrix_list)):
    length = calculate_length(matrix_list[i], start_col, start_row, 1)
    if length > w * h:
        is_trap = True
    elif length < min_lenght:
        min_lenght = length
        index = i
# Write an answer using print
# To debug: print("Debug messages...", file=sys.stderr, flush=True)
if index == 0 and is_trap:
    print("TRAP")
else:
    print(index)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值