• 重庆大学第八届编程大赛初赛1、2题目


    1

    A. balls game

    import math
    # 1. 从左到有遍历列表元素,发现数据组,第一个元素为正,第二个元素为负
    # 判断是否会吃掉还是消除
    def judge(one_list):
        if len(one_list) < 2:
            # 长度等于1的列表不需要传进来,需要提前判断
            # print('error')
            return
        flag = False
        new_list=[]
        record_idx = []
        for idx in range(len(one_list) - 1):
            # print(idx,one_list[idx])
            if one_list[idx] > 0 and one_list[idx + 1] < 0:
                flag = True
                if math.fabs(one_list[idx]) == math.fabs(one_list[idx + 1]):
                    record_idx.append(idx)
                    record_idx.append(idx+1)
                elif math.fabs(one_list[idx]) > math.fabs(one_list[idx + 1]):
                    record_idx.append(idx+1)
                elif math.fabs(one_list[idx]) < math.fabs(one_list[idx + 1]):
                    record_idx.append(idx)
        for idx in range(len(one_list)):
            if idx not in record_idx:
                new_list.append(one_list[idx])
        return flag, new_list
    case_num = int(input())
    data_array = []
    for i in range(case_num):
        data_array.append([float(ele.strip()) for ele in input().split(',')])
    for one_list in data_array:
        flag = True
        while flag:
            if len(one_list) == 1 or len(one_list) == 0:
                flag = False
                break
            flag, one_list = judge(one_list)
        one_list.sort()
        print(' '.join(str(int(ele)) for ele in one_list), end='
    ')

    2.

    B. Survive

    if __name__ == "__main__":
        def my_sum(arr):
            s =0
            for i in range(len(arr)):
                s += sum(arr[i])
            return s
    
        def set_0(arr,idx,jdx):
            # print(idx, jdx)
            row = len(arr)
            col = len(arr[0])
            arr[idx][jdx] =0
    
            if idx == 0 :
                if jdx == 0:
                    if arr[idx][jdx+1]==1:
                        set_0(arr,idx,jdx+1)
                    if arr[idx+1][jdx]==1:
                        set_0(arr,idx+1,jdx)
                    pass
                elif jdx==col-1:
                    if arr[idx][jdx-1]==1:
                        set_0(arr,idx,jdx-1)
                    if arr[idx+1][jdx]==1:
                        set_0(arr,idx+1,jdx)
                    pass
                elif jdx>0 or jdx<col-1:
                    if arr[idx][jdx-1]==1:
                        set_0(arr,idx,jdx-1)
                    if arr[idx][jdx + 1] == 1:
                        set_0(arr, idx, jdx + 1)
                    # print('ooo=', idx, jdx)
                    if arr[idx+1][jdx] == 1:
                        # print('dd=', idx,jdx)
                        set_0(arr, idx+1, jdx + 1)
    
            elif idx==row-1:
                if jdx==0:
                    if arr[idx][jdx + 1] == 1:
                        set_0(arr, idx, jdx + 1)
                    if arr[idx-1][jdx]==1:
                        set_0(arr,idx-1,jdx)
                    pass
                if jdx==col-1:
                    if arr[idx-1][jdx] == 1:
                        set_0(arr, idx-1, jdx)
                    if arr[idx][jdx-1]==1:
                        set_0(arr,idx,jdx-1)
                    pass
                elif jdx > 0 or jdx < col - 1:
                    if arr[idx-1][jdx] == 1:
                        set_0(arr, idx-1, jdx)
                    if arr[idx][jdx - 1] == 1:
                        set_0(arr, idx, jdx - 1)
                    if arr[idx][jdx+1]==1:
                        set_0(arr,idx,jdx+1)
            elif idx>0 or idx<row-1:
                if jdx==0:
                    if arr[idx][jdx + 1] == 1:
                        set_0(arr,idx,jdx+1)
                    if arr[idx-1][jdx ] == 1:
                        set_0(arr,idx-1,jdx)
                    if arr[idx+1][jdx ] == 1:
                        set_0(arr,idx+1,jdx)
                    pass
                elif jdx==col-1:
                    if arr[idx][jdx - 1] == 1:
                        set_0(arr,idx,jdx-1)
                    if arr[idx-1][jdx ] == 1:
                        set_0(arr,idx-1,jdx)
                    if arr[idx+1][jdx ] == 1:
                        set_0(arr,idx+1,jdx)
                    pass
                elif jdx > 0 or jdx < col - 1:
                    if arr[idx][jdx - 1] == 1:
                        set_0(arr,idx,jdx-1)
                    elif arr[idx][jdx + 1] == 1:
                        set_0(arr,idx,jdx+1)
                    elif arr[idx - 1][jdx] == 1:
                        set_0(arr,idx-1,jdx)
                    elif arr[idx + 1][jdx] == 1:
                        set_0(arr,idx+1,jdx)
            return arr
        def judge(arr,arr_size):
            '''
            :param data_array: 0-1 array
            :return: num, type:int
            '''
            # 找到了1个连通区域之后,把该区域的1变为0,计算是否还有1
            arr_sum = my_sum(arr)
            cnt = 0
            while(arr_sum>0):
                for idx in range(arr_size[0]):
                    for jdx in range(arr_size[1]):
                        if arr[idx][jdx]==1:
                            cnt += 1
                            set_0(arr,idx,jdx)
                arr_sum = my_sum(arr)
            result.append(cnt)
            print(cnt)
            pass
    
        case_num = int(input())
        result = []
        for i in range(case_num):
            data_i_size = [int(ele) for ele in input().split(' ')]
            # print(data_i_size)
            data_i=[]
            for j in range(data_i_size[0]):
                data_i.append([int(ele) for ele in input().split(',')])
                # print(data_i)
        #     todo 判斷有幾個聯通區域是1
            judge(data_i, data_i_size)
    def dfs(board, used, i, j):
        n = len(board)
        m = len(board[0])
        if board[i][j] == '0' or used[i][j]: return
        used[i][j] = True
        dir = [-1, 0, 1, 0, -1]
        for di in range(4):
            if 0 <= dir[di]+i <= n-1 and 0 <= dir[di+1]+j <= m-1:
                dfs(board, used, dir[di]+i, dir[di+1]+j)
    def f():
        rown = int(input())
        for _ in range(rown):
            n, m = map(int, input().split())
            board = []
            for i in range(n):
                board.append(input().strip().split(','))
            res = 0
            used = [[False for _ in range(m)] for _ in range(n)]
            for i in range(n):
                for j in range(m):
                    if board[i][j] == '1' and not used[i][j]:
                        res += 1
                        dfs(board, used, i, j)
            print(res)
    if __name__ == '__main__':
        f()
  • 相关阅读:
    Oracle 删除重复数据的几种方法
    12.25模拟赛T3
    java实现第五届蓝桥杯圆周率
    java实现第五届蓝桥杯武功秘籍
    Oracle 审计初步使用
    [CERC2017]Intrinsic Interval——扫描线+转化思想+线段树
    ORA-12012 Error on auto execute of job "SYS"."ORA$AT_OS_OPT_SY_<NN> in 12.2.0 Database
    12.25模拟赛T2
    java实现第五届蓝桥杯写日志
    java实现第五届蓝桥杯李白打酒
  • 原文地址:https://www.cnblogs.com/sunupo/p/12993870.html
Copyright © 2020-2023  润新知