当前位置:   article > 正文

Python练习题答案: 简单有趣#155:吃豆人【难度:3级】--景越Python编程实例训练营,1000道上机题等你来挑战_python吃豆人代码

python吃豆人代码

简单有趣#155:吃豆人【难度:3级】:

答案1:

def pac_man(size, pacman, enemies):
    px, py = pacman
    mx, my, Mx, My = -1, -1, size, size
    for x, y in enemies:
        if x < px and x > mx: mx = x
        if y < py and y > my: my = y
        if x > px and x < Mx: Mx = x
        if y > py and y < My: My = y
    return (Mx - mx - 1) * (My - my - 1) - 1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

答案2:

def pac_man(N, PM, enemies):
    dims = [[0,N], [0,N]]   # [[minX, maxX], [minY, maxY]]
    for pos in enemies:
        for i,x in enumerate(pos):
            if PM[i] > x: dims[i][0] = max(dims[i][0], x+1)
            else:         dims[i][1] = min(dims[i][1], x)
    return (dims[0][1]-dims[0][0]) * (dims[1][1]-dims[1][0]) - 1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

答案3:

def pac_man(n, pm, enemies):
    # Build initial grid
    # O open, P Pacman, E enemy, # Enemy line of sight
    row = lambda c="O": [c] * n
    grid = [row() for _ in range(n)]
    grid[pm[0]][pm[1]] = "P"
    for er, ec in enemies:
        grid[er] = row("#")
        grid = [["#" if c == ec else v for c, v in enumerate(r)] for r in grid]
    for er, ec in enemies:
        grid[er][ec] = "E"
    
    # Perform a flood fill detecting O and converting to . starting from PM
    edges = [pm]
    count = 0
    while edges:
        new_edges = []
        for er, ec in edges:
            for dr, dc in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
                r, c = er + dr, ec + dc
                if 0 <= r < n and 0 <= c < n:
                    if grid[r][c] == "O":
                        count += 1
                        grid[r][c] = "."
                        new_edges.append((r, c))
        edges = new_edges
    return count​

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

答案4:

def pac_man(N, PM, enemies):
  borders=[[-1, 0], [0, -1], [-1, N-1], [N-1, -1], [N, 0], [N, N-1], [0, N], [N-1, N]]
  for n in range(1, N-1):
      borders.append([-1, n])
      borders.append([n, -1])
      borders.append([n, N])
      borders.append([N, n])
  ghosts=[]
  for i in enemies:
      x=i[0]
      y=i[1]
      for n in range(N):
          ghosts.append([x, n])
          ghosts.append([n, y])
  possibilities=[]
  moved=[PM]
  while moved != []:
      moved2=moved
      moved=[]
      for i in moved2:
          x=i[0]
          y=i[1]
          mon=[[x, y+1], [x, y-1], [x+1, y], [x-1, y]]
          for m in mon:
              if m not in borders and m not in ghosts:
                  if m not in possibilities and m not in moved and m != PM:
                      possibilities.append(m)
                      moved.append(m)
  return len(possibilities)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

答案5:

from bisect import bisect

def pac_man(n,p,es):
    if not es: return n*n-1
    l=len(es)
    y,x=map(sorted,zip(*es))
    i=bisect(y,p[0])
    hy=y[i] if i<l else n
    ly=y[i-1] if i>0 else -1
    i=bisect(x,p[1])
    hx=x[i] if i<l else n
    lx=x[i-1] if i>0 else -1
    return (hy-ly-1)*(hx-lx-1)-1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

答案6:

def pac_man(N, PM, enemies):
    mnI, mxI, mnJ, mxJ = 0, N-1, 0, N-1
    for i,j in enemies:
        if i > PM[0]: mxI = min(mxI, i-1)
        else: mnI = max(mnI, i+1)
        if j > PM[1]: mxJ = min(mxJ, j-1)
        else: mnJ = max(mnJ, j+1)
    return (mxI - mnI + 1) * (mxJ - mnJ + 1) - 1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

答案7:

def pac_man(N, PM, enemies):
  x_e, y_e, x_e_less, y_e_less = [], [], [], []
  x_e_l, x_e_u, y_e_l, y_e_u = 0, 0, 0, 0
  if enemies == []:
      answer =  N * N - 1
  else:
      for i in enemies:
          x_e.append(i[0] + 1)
      for j in enemies:
          y_e.append(j[1] + 1)
      print(x_e, y_e)

      x_e_l = [x for x in x_e if x < PM[0] + 1]
      if not x_e_l:
        x_e_l = 0
      elif len(x_e_l) == 1:
        x_e_l = x_e_l[0]
      else:
        x_e_l = max(x_e_l)

      x_e_u = [x for x in x_e if x > PM[0] + 1]
      if not x_e_u:
        x_e_u = N + 1
      elif len(x_e_u) == 1:
        x_e_u = x_e_u[0]
      else:
        x_e_u = min(x_e_u)          

      y_e_l = [y for y in y_e if y < PM[1] + 1]
      if not y_e_l:
        y_e_l = 0
      elif len(y_e_l) == 1:
        y_e_l = y_e_l[0]
      else:
        y_e_l = max(y_e_l)

      y_e_u = [y for y in y_e if y > PM[1] + 1]
      if not y_e_u:
        y_e_u = N + 1
      elif len(y_e_u) == 1:
        y_e_u = y_e_u[0] 
      else:
        y_e_u = min(y_e_u) 

      print("l,u", x_e_l, x_e_u, y_e_l, y_e_u)
      x_dist = (x_e_u - x_e_l - 1)
      y_dist = (y_e_u - y_e_l - 1)
      print(x_dist, y_dist)
      answer = x_dist * y_dist - 1 
  print(answer)      
  return answer​

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

答案8:

def pac_man(N, PM, e):
    if not e: return N*N -1
    d = {'l': 0, 'u': 0, 'r': N, 'd': N}
    for o in e:
        if d['u'] <= o[0] < PM[0]: d['u'] = o[0]+1
        elif PM[0] < o[0] < d['d']: d['d'] = o[0]
        if d['l'] <= o[1] < PM[1]: d['l'] = o[1]+1
        elif PM[1] < o[1] < d['r']: d['r'] = o[1]
    return (d['r'] - d['l']) * (d['d'] - d['u']) -1 if (d['r'] - d['l']) * (d['d'] - d['u']) > 0 else 0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

答案9:

def pac_man(N, PM, enemies):
    (up, down), (left, right), (pi, pj) = (0, N - 1), (0, N - 1), PM
    
    for i, j in enemies: 
        if i < pi and i + 1 > up: up = i + 1
        if i > pi and i - 1 < down: down = i - 1
        if j < pj and j + 1 > left: left = j + 1
        if j > pj and j - 1 < right: right = j - 1
    
    return (down - up + 1) * (right - left + 1) - 1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

答案10:

def pac_man(N, PM, enemies):
    left = -1
    right = N
    high = -1
    low = N
    for enemy in enemies:
        if left < enemy[0] < PM[0]:
            left = enemy[0]
        if right > enemy[0] > PM[0]:
            right = enemy[0]
        if high < enemy[1] < PM[1]:
            high = enemy[1]
        if low > enemy[1] > PM[1]:
            low = enemy[1]
    
    width = right -(left + 1)
    height = low - (high + 1)

    
    d = ([[1 for y in range(height)] for x in range(width)])
    return sum(sum(a) for a in d)-1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22



Python基础训练营景越Python基础训练营QQ群

在这里插入图片描述
欢迎各位同学加群讨论,一起学习,共同成长!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/122807
推荐阅读
相关标签
  

闽ICP备14008679号