fork(2) download
  1. from queue import PriorityQueue
  2. from collections import deque
  3.  
  4.  
  5. def plan(T):
  6. Plamy,Places=swap(T)
  7. paliwo=Plamy[0]
  8.  
  9.  
  10.  
  11. i=1
  12. flag=1
  13. sum1=0
  14. n=len(T[0])
  15. Q=PriorityQueue()
  16. while paliwo<n-1:
  17.  
  18. while i<len(Places) and Places[i]<=paliwo:
  19. Q.put(-Plamy[Places[i]])
  20. sum1+=(Plamy[Places[i]])
  21. i+=1
  22. quantity=Q.get()
  23.  
  24. paliwo-=quantity
  25. flag+=1
  26.  
  27.  
  28.  
  29.  
  30.  
  31. return flag
  32.  
  33. def swap(T):
  34. m=len(T)
  35. n=len(T[0])
  36. Plamy=[0 for _ in range(n)]
  37. Places=[]
  38.  
  39. for i in range(n):
  40.  
  41. if T[0][i]!=0 :
  42. sum=T[0][i]
  43. T[0][i]=0
  44. Q=deque()
  45. Q.append((0,i))
  46. while len(Q)!=0:
  47. x,y=Q.popleft()
  48.  
  49. if x<m-1:
  50. flag=x+1
  51. if T[flag][y]!=0:
  52. Q.append((flag,y))
  53. sum+=T[flag][y]
  54. T[flag][y]=0
  55. if y<n-1:
  56. flag=y+1
  57. if T[x][flag]!=0:
  58. Q.append((x,flag))
  59. sum+=T[x][flag]
  60. T[x][flag]=0
  61. if x>0:
  62. flag=x-1
  63. if T[flag][y]!=0:
  64. Q.append((flag,y))
  65. sum+=T[flag][y]
  66. T[flag][y]=0
  67. if y>0:
  68. flag=y=1
  69. if T[x][flag]!=0:
  70. Q.append((x,flag))
  71. sum+=T[x][flag]
  72. T[x][flag]=0
  73.  
  74.  
  75.  
  76.  
  77.  
  78. Plamy[i]=sum
  79. Places.append(i)
  80.  
  81. return Plamy,Places
Success #stdin #stdout 0.03s 9748KB
stdin
Standard input is empty
stdout
Standard output is empty