1.热血格斗场
该题需要用有序队列实现,用c++中的map,set都可以轻松实现。但是由于python的dict是无序表,因此采用对list容器进行二分查找的方法解决该题。
from math import * def minsert(newone): global mydict p = 0 q = len(mydict) - 1 m = floor((p+q)/2) while(p != q): if mydict[m][0] < newone[0]: p = m + 1 else: q = m m = floor((p+q)/2) mydict.insert(p, newone) return p n = int(input()) mydict = [[1e9, 1]] for i in range(n): newone = list(map(int, input().split())) newone.reverse() j = minsert(newone) if j == 0: print(newone[1], mydict[j + 1][1]) elif j == len(mydict) - 1: print(newone[1], mydict[j - 1][1]) else: if (mydict[j + 1][0] + mydict[j - 1][0] < 2 * newone[0]): print(newone[1], mydict[j + 1][1]) else: print(newone[1], mydict[j - 1][1])
2.拯救行动
一道深度搜索跑迷宫的题目
def inmap(i, j): global m global n global mydict if i >= 0 and i <= m - 1 and j >= 0 and j <= n - 1 and ( mydict[i][j] == '@' or mydict[i][j] == 'x' or mydict[i][j] == 'a'): return True else: return False S = int(input()) for d in range(S): m, n = map(int, input().split()) #print(m, n) mydict = [] myqueue = [[], []] myso = {} for i in range(m): mydict.append(input()) # print(mydict) for i in range(m): for j in range(n): if mydict[i][j] == 'r': warrior = (i, j) elif mydict[i][j] == 'a': princess = (i, j) elif mydict[i][j] == 'x': myso[(i, j)] = -1 myqueue[0].append(warrior) cnt = 0 flag = False while len(myqueue[0]): cnt = cnt + 1 #print(cnt) for v in myqueue[0]: #print(v) if v == princess: print(cnt - 1) flag = True break #print(v in myso.keys()) if (v in myso.keys()): # print(v) if myso[v] == -1: myso[v] = cnt myqueue[1].append(v) continue elif myso[v] == 0 or myso[v] == cnt: continue myso[v] = 0 if inmap(v[0] - 1, v[1]): myqueue[1].append((v[0] - 1, v[1])) if inmap(v[0] + 1, v[1]): myqueue[1].append((v[0] + 1, v[1])) if inmap(v[0], v[1] - 1): myqueue[1].append((v[0], v[1] - 1)) if inmap(v[0], v[1] + 1): myqueue[1].append((v[0], v[1] + 1)) if(flag): break del myqueue[0] myqueue.append([]) if flag == False: print('Impossible')
三.拨钟问题
实际上是解一个矩阵方程的问题。
直接使用枚举法超时了,于是只遍历操作1,2,3,其他的均可有1,2,3确定。
from copy import deepcopy def move_clock(k, n = 1): global temp global move global op for i in range(9): temp[i] = (temp[i] + n * move[k][i]) % 4 def find(depth): global temp global min_num global min_op global op if depth == 9: if sum(temp) == 0 and sum(op) < min_num: min_num = sum(op) min_op = deepcopy(op) return elif depth < 3: for i in range(4): move_clock(depth) op[depth] = (op[depth] + 1) % 4 find(depth + 1) return elif depth < 7: op[depth] = (4 - temp[depth - 3]) % 4 elif depth == 7: op[depth] = (4 - temp[6]) % 4 else: op[depth] = (4 - temp[4]) % 4 move_clock(depth, op[depth]) find(depth + 1) move_clock(depth, -op[depth]) op[depth] = 0 return op = [0 for i in range(9)] temp = [] #temporary result min_num = 28 min_op = [0 for i in range(9)] move = [[0 for i in range(9)] for j in range(9)] move[0][0] = move[0][1] = move[0][3] = move[0][4] = 1 move[1][0] = move[1][1] = move[1][2] = 1 move[2][1] = move[2][2] = move[2][4] = move[2][5] = 1 move[3][0] = move[3][3] = move[3][6] = 1 move[4][1] = move[4][3] = move[4][4] = move[4][5] = move[4][7] = 1 move[5][2] = move[5][5] = move[5][8] = 1 move[6][3] = move[6][4] = move[6][6] = move[6][7] = 1 move[7][6] = move[7][7] = move[7][8] = 1 move[8][4] = move[8][5] = move[8][7] = move[8][8] = 1 for i in range(3): temp += input().split() temp = list(map(int, temp)) find(0) for i in range(9): for j in range(min_op[i]): print(i + 1,end = ' ')