Advertisement
tiberiup

Nave - nivel

Dec 10th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. import copy
  2.  
  3. class Nod:
  4. def __init__(self, nrStare, stare):
  5. self.nrStare=nrStare
  6. self.stare=stare
  7. self.valoare=self.calculeazaValoare()
  8.  
  9. def calculeazaValoare(self):
  10. valoare=50
  11.  
  12. if len(self.stare)>4:
  13. if self.stare[4]==2:
  14. valoare+=4
  15. if len(self.stare)>2:
  16. if self.stare[2]==1:
  17. valoare+=5
  18. for i in range(0,len(self.stare)-1):
  19. if self.stare[i]%2-(self.stare[i+1]%2)==0:
  20. valoare-=8
  21. return valoare
  22.  
  23. def navaDejaRepartizata(self, nava):
  24. if nava in self.stare:
  25. return True
  26. return False
  27.  
  28. def afisareSolutie(nod):
  29. print ("Repartizarea cea mai buna este (", end="")
  30. for echipaj in nod.stare:
  31. print ("{} ".format(echipaj), end="")
  32. print (") cu valoarea {}.".format(nod.valoare))
  33.  
  34. def Cautare():
  35.  
  36. frontiera=[]
  37. teritoriu=[]
  38. optim=Nod(1,[])
  39. frontiera.append(optim)
  40.  
  41. while len(frontiera)!=0:
  42. nodCurent=frontiera.pop(0)
  43. teritoriu.append(nodCurent)
  44. for i in range(1,6):
  45. if nodCurent.navaDejaRepartizata(i)==False:
  46. stare=copy.deepcopy(nodCurent.stare)
  47. stare.append(i)
  48. nod=Nod(len(frontiera)+len(teritoriu)+1,stare)
  49. if len(stare)==5 and nod.valoare>optim.valoare:
  50. optim=nod
  51. frontiera.append(nod)
  52.  
  53. afisareSolutie(optim)
  54.  
  55. print ("Au fost expandate {} noduri.".format(len(teritoriu)))
  56.  
  57. Cautare()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement