赞
踩
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
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
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
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)
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
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
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
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
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
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
欢迎各位同学加群讨论,一起学习,共同成长!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。