Advertisement
cmiN

max area cut

Jan 21st, 2013
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.69 KB | None | 0 0
  1. #! /usr/bin/env python
  2.  
  3.  
  4. import sys
  5.  
  6.  
  7. def area(topLeft, botRight):
  8.     return (botRight[0] - topLeft[0] + 1) *\
  9.            (botRight[1] - topLeft[1] + 1)
  10.  
  11.  
  12. def process(topLeft, botRight, holes):
  13.     """Functie ce cauta si gaseste coordonatele suprafetei
  14.    de arie maxima.
  15.    
  16.    Cauta recursiv in jumatatea de sus/jos apoi in cea din
  17.    stanga si din dreapta si in momentul in care nu se mai
  18.    gasesc gauri in interiorul ariei curente, atunci
  19.    avem un posibil candidat pentru aria maxima.
  20.    """
  21.  
  22.     noHoles = True    # nu sunt gauri
  23.     maxRet = (0, None, None)
  24.  
  25.     # testam daca este posibila suprafata curenta
  26.     if topLeft[0] > botRight[0] or\
  27.        topLeft[1] > botRight[1]:
  28.            return maxRet
  29.  
  30.     for hole in holes:
  31.         # gaura se afla in interior
  32.         if hole[0] >= topLeft[0] and hole[0] <= botRight[0] and\
  33.            hole[1] >= topLeft[1] and hole[1] <= botRight[1]:
  34.                noHoles = False
  35.                # mergem in N
  36.                ret = process(topLeft,
  37.                              (botRight[0], hole[1] - 1),
  38.                              holes)
  39.                if ret[0] > maxRet[0]:
  40.                    maxRet = ret
  41.                # mergem in S
  42.                ret = process((topLeft[0], hole[1] + 1),
  43.                              botRight,
  44.                              holes)
  45.                if ret[0] > maxRet[0]:
  46.                    maxRet = ret
  47.                # mergem in W
  48.                ret = process(topLeft,
  49.                              (hole[0] - 1, botRight[1]),
  50.                              holes)
  51.                if ret[0] > maxRet[0]:
  52.                    maxRet = ret
  53.                # mergem in E
  54.                ret = process((hole[0] + 1, topLeft[1]),
  55.                              botRight,
  56.                              holes)
  57.                if ret[0] > maxRet[0]:
  58.                    maxRet = ret
  59.  
  60.     if noHoles:
  61.         # nu avem nicio gaura, returnam aria curenta
  62.         maxRet = (area(topLeft, botRight), topLeft, botRight)
  63.     return maxRet
  64.  
  65.  
  66. def main(argc, argv):
  67.     if argc < 3:
  68.         print "Usage: %s LENGTH WIDTH [X Y]..." % argv[0]
  69.         return 0
  70.  
  71.     length = int(argv[1])
  72.     width = int(argv[2])
  73.     holes = list()
  74.     for i in xrange(3, argc, 2):
  75.         holes.append((int(argv[i]), int(argv[i + 1])))
  76.  
  77.     (topLeft, botRight) = ((0, 0), (length - 1, width - 1))
  78.     maxRet = process(topLeft, botRight, holes)
  79.     print "Coordonate colt stanga sus: %d %d" % (maxRet[1][0], maxRet[1][1])
  80.     print "Coordonate colt dreapta jos: %d %d" % (maxRet[2][0], maxRet[2][1])
  81.     print "Arie: %d" % maxRet[0]
  82.  
  83.     return 0
  84.  
  85.  
  86. if __name__ == "__main__":
  87.      sys.exit(main(len(sys.argv), sys.argv))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement