1- from PIL import Image , ImageSequence
1+ from PIL import Image
2+ import logging
23
34
45def main ():
6+ logging .basicConfig (filename = "maze/24.log" , filemode = "w" , level = logging .DEBUG ,
7+ format = '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s' )
58 file_path = "maze/maze.png"
69 im = first_step (file_path )
710 im_size = im .size
811 width = im_size [0 ] # 641
912 height = im_size [1 ] # 641
10- second_step (im , height )
13+ # second_step(im, height)
1114 start = (639 , 0 )
1215 third_step (im , start , width , height )
1316
@@ -28,30 +31,43 @@ def second_step(im, height):
2831def third_step (im , start , width , height ):
2932 x = start [0 ]
3033 y = start [1 ]
34+ way = "0"
35+ max_key = int (way )
3136 ok_points = [(x , y )]
32- one_way (im , start , x , y , ok_points )
37+ ok_points_dic = {way : ok_points }
38+ one_way (im , x , y , ok_points_dic , max_key )
3339
3440
35- def one_way (im , start , x , y , ok_points ):
41+ def one_way (im , x , y , ok_points_dic , max_key ):
3642 while True :
37- print ("ok_points={}" .format (ok_points ))
38- next_points = find_next_point (im , x , y , ok_points )
39- print ("point=({}, {}),next_point={}" .format (x , y , next_points ))
40- if next_points is None :
41- print ("break code 1" )
42- break
43- else :
44- if len (next_points ) == 1 :
45- x = next_points [0 ][0 ]
46- y = next_points [0 ][1 ]
47- ok_points .append ((x , y ))
48- else :
49- print ("len(next_points)={}" .format (len (next_points )))
50- print ("break code 2" )
43+ logging .debug ("ok_points_dic={}" .format (ok_points_dic ))
44+ keys = ok_points_dic .keys ()
45+ to_be_delete = set ()
46+ to_be_add_dict = dict ()
47+ for way in keys :
48+ way_ok_points = ok_points_dic .get (way )
49+ next_points = find_next_point (im , x , y , way_ok_points , way )
50+ if next_points is None :
51+ logging .debug ("break code 1" )
52+ to_be_delete .add (way )
53+ logging .debug ("add way={} to be delete, because next_points is None" .format (way ))
5154 break
55+ else :
56+ for point in next_points :
57+ max_key += 1
58+ to_be_delete .add (way )
59+ logging .debug ("add way={}to to be delete, because create a new one={}" .format (way , max_key ))
60+ to_be_add_dict [str (max_key )] = way_ok_points
61+ x = point [0 ]
62+ y = point [1 ]
63+ way_ok_points .append ((x , y ))
64+ for element in to_be_delete :
65+ del ok_points_dic [element ]
66+ for key in to_be_add_dict .keys ():
67+ ok_points_dic [key ] = to_be_add_dict [key ]
5268
5369
54- def find_next_point (im , x , y , ok_points ):
70+ def find_next_point (im , x , y , ok_points , way ):
5571 right_x = x + 1
5672 left_x = x - 1
5773 up_y = y - 1
@@ -89,13 +105,14 @@ def find_next_point(im, x, y, ok_points):
89105 if down_y_pixel is not None and down_y_pixel != (255 , 255 , 255 , 255 ):
90106 print ("x={}, down_y={}, down_y_pixel={}" .format (x , down_y , down_y_pixel ))
91107 points .append (down_point )
92- print ( " right_x_pixel={}, left_x_pixel={}, up_y_pixel={}, down_y_pixel={}" .format (right_x_pixel , left_x_pixel ,
108+ logging . debug ( "way={}, x={}, y={}, right_x_pixel={}, left_x_pixel={}, up_y_pixel={}, down_y_pixel={}" .format (way , x , y , right_x_pixel , left_x_pixel ,
93109 up_y_pixel , down_y_pixel ))
94110 for point in points :
95111 if point in ok_points :
112+ logging .debug ("way={}, remove point={}" .format (way , point ))
96113 points .remove (point )
97114 if len (points ) < 1 :
98- print ("points为None,x={}, y={}" .format (x , y ))
115+ # print("points为None,x={}, y={}".format(x, y))
99116 return None
100117 return points
101118
0 commit comments