Python applet----Solution Maze Picture

Source: Internet
Author: User
Tags abs range

A total of three species, the first two are BFS and Astar, the third is not a maze, is connected to the area of the mark, with a check set.

The effect is as follows (Csdn looks like you can't display a GIF animated diagram, so you just cut a few pictures.)

I think the GIF is still very good-looking, GitHub: Https://github.com/scauPYygzx/picture_maze ^-^):

BFs

Astar

and check the collection

From the picture can be seen Astar than BFS search a lot of points, but because of the use of the priorityqueue reason, in this picture on the time efficiency than BFS.

PS: Making gif with the Convert command is really convenient, recommended.

Code

?

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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 The 96 97 98 99 100 101 102 103 104 105 106 107 108 109 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140-1 41 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170-171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201-2 204 205 206 207 203 209 210 211 #-*-coding:utf-8-*-  from PIL import Image import Queue import time   #只是玩玩就硬编码了. ext = png imagename = p.%s% ext savename = {0:05d}. {1}   #用PIL库载入图片 im = Image.open (imagename) im = Im.convert (RGB) print Im.format, im.size, im.mode pix = im.load () si Ze = im.size   half = 256 >> 1 white = (255, 255, 255) Black = (0, 0, 0) red = (255, 0, 0) green = (0, 255, 0) Blue = (0, 0, 255) Yellow = (255, 255, 0) FillColor = red LineColor = Yellow colors = [red, green, blue, yellow, (0, 223, 248), (223, 8, 248), (255, 2, 146)] CLen = len (colors)   #起点, end point startpoint = [984] EndPoint = [O, 398]   F A = [0] * size[0] * size[1]     def changeimage ():      '      convert picture to two-value graph      '     x, y = size     for i in xrange (x):          for J in Xrange (y):            &nbsP; r, G, B = Pix[i, j]             if r > Half and G & Gt Half and B > half:                  pix[i, j] = white             else:                  pix[i, j] = black   #===== ===========================================================# #BFS def Getneighbor (point):      X, y = Point     return [[x, Y + 1], [x + 1, y], [x, Y-1], [X-1, Y]]   def inbounds (Point, Si Ze):     x, y = Point     sx, sy = size     if x < 0 or Y & Lt 0 or x >= sx or y >= sy:         return False     retur N True   def iswhite (point):   &nbsP; x, y = point     if pix[x, y] = = white:          return True     return False   def BFS (start, end):     count = no = 0      q = [[[start]]     while len (q):          path = Q.pop (0);         p = path[-1]         if p[ 0] = = end[0] and p[1] = = End[1]:             im.save ( Savename.format (No, ext))             no + 1              for pa in Path:                  pix[pa[0], pa[1]] = pix[pa[0], pa[1]-1] = Pix[pa[0 ], Pa[1]-2] = LineColor      &NBsp;          count + 1                  if not count%:                      im.save ( Savename.format (No, ext))                      no + 1              no + + 1             im.save (SaveName.format) (No, EXT))             return path          for NE in Getneighbor (p):              if inbounds (NE, size) and Iswhite (NE):                  newpath = List (path)                  newpath.append (NE)                  q.append (NewPath)                  pix[ne[0], ne[1]] = FillColor          if not count% 10000:             im.save ( Savename.format (No, ext))             no + 1          count + 1     return None   #=================== =============================================# #Astar def getValue (Point, end):      '       inspiration function      '     return ABS (Point[0]-end[0]) + ABS (Point[1]-end[1])   def aStar (start, end):     fillcolor = Green      LineColor = Black     count = no = 0     q = queue.priorityqueue ()   &n Bsp; q.put ([0,[start]])     while (Q.qsize ()):          q = Q.get ()         path = q[-1]          p = path[-1]         if p[0] = = End[0] and p[1] = = End[1]: &nbs P;           im.save (SaveName.format (No, ext))              no + 1              for pa in Path:                  pix[pa[0], pa[1]] = pix[pa[0], pa[1]-1] = Pix[pa[0], Pa[1]-2] = LineColor                  count + 1                  If not count%:                      im.save (Savename.format (No, ext))                      no + 1              im.save (Savename.format (No, ext))              return Path         for NE in Getneighbor (P):             if inBounds (NE, size) and Iswhite (NE):                &nbsP;newpath = List (path)                  newpath.append (NE)                  newlist = [GetValue (p, end) + len (NewPath), NewPath]                  q.put (newlist)                  pix[ne[0], ne[1]] = FillColor          if not count% 10000:             im.save ( Savename.format (No, ext))             no + 1          count + 1     return None     #============= ===================================================# #并查集 def getID (x, y):    &nbsP;return x * size[0] + y   def ufinit (size):     for i in range (Size[0]):    &nbsp ;     for j in Range (Size[1]):              fa[getid (I, j)] = GetID (i, j)   Def Uffind (v):     if v!= fa[v]:    & NBSP;    FA[V] = Uffind (Fa[v])         return Fa[v]     return Fa[v]   def ufunion (x, y):     x = Uffind (x)      y = Uffind (y)     if x = = Y:return     fa[x] = y   def connectivity ():     fillcolor = Yellow     no = Count = Now = 0     d = {} &N Bsp;   x_range = (5, 792)     y_range = (986)        Ufinit (size)       for x in RANge (X_range[0], x_range[1]):         for y in range (y_range[0], y_range[1]):             if pix[x, y]!= black:                  if pix[x-1, y] = = black and pix[x, Y-1] = Black:                      pass                  elif pix[x-1, y] = = white and pix[x, y-1] = Black:                      ufunion (GetID (x-1, y), GetID (x, y))                  elif pix[x-1, y] = = black and Pix[x, y-1] = = White:         &nbSp;           ufunion (GetID (x, Y-1), GetID (x, y))                  else:                       Ufunion (GetID (x-1, y), GetID (x, y-1))                      ufunion (GetID (x, y), GetID (x-1, y))        For x in range (X_range[0], x_range[1]):         for y in range (y_range[0), Y_ RANGE[1]):             count + 1              if pix[x, y]!= black:                  father = Uffind (GetID (x,y))  &nbsP;              if father not in D:                       d[father] = colors[now% cLen]                      now + 1                  pix[x, Y] = D[father]              if not count% 15000:                  im.save (Savename.format (No, ext))                  no + 1     im.save (SaveName.format (No, ext))     #================================================================# t1 = time.time ()   ChangEimage () #bfs (StartPoint, EndPoint) #aStar (StartPoint, EndPoint) connectivity ()   t2 = Time.time () print t2-t1
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.