/
code_game_robot_dev_v2.3.2.py
908 lines (814 loc) · 38.5 KB
/
code_game_robot_dev_v2.3.2.py
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
32
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
141
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
#coding=utf-8
import sys
import timing
import time
from selenium import webdriver
import traceback
import datetime
import multiprocessing
import copy
g_version = 'v2.3.2'
'''
气眼检测 v1.0
波纹探测墙 v1.1
波纹探测岛 v2.0
探索顺序3,2,4,1 v2.1
初级残影检测 v2.2
改成进程类实现单机多进程 v2.3
效果拔群... v2.3.2
'''
global driver
class ProcRobot(multiprocessing.Process):
def __init__(self, dict_args):
multiprocessing.Process.__init__(self)
self.globalArgs = dict()
self.arg = dict_args['arg']
self.level = 0
self.line = 0
self.row = 0
self.pos = 0
self.house = 0
self.house_list = 0
self.history = 0
self.num_map = 0
self.shadow = 0
self.sum_second = 0
self.sum_walk = 0
self.mission_type = int(dict_args['mission_type'])
self.reverse = int(dict_args['reverse'])
self.queue = dict_args['queue']
self.data_lock = dict_args['data_lock']
self.start_time = ''
self.end_time = ''
def run(self):
print self.name+' start '
try:
self.init(self.arg)
ans = self.robotStart(self.mission_type, self.reverse)
if ans is not None:
print '(%d, %d) is ok' % (self.mission_type, self.reverse)
self.data_lock.acquire()
self.queue.put(ans)
self.data_lock.release()
self.end_time = datetime.datetime.now()
print 'lv:%s %s -> %s' % (self.level, self.start_time.strftime("%Y%m%d-%H%M%S.%f"), self.end_time.strftime("%Y%m%d-%H%M%S.%f"))
print 'lv:%s (%d, %d) %s: %.2f mins, %d wa_lks, avg: %.2f seconds, %.2f mins' % (self.level, self.mission_type, self.reverse, g_version, self.sum_second/float(60), self.sum_walk, self.sum_second*1.0/self.sum_walk, (self.end_time-self.start_time).seconds/60.0)
except Exception, e:
print 'error', e
time.sleep(1)
print traceback.format_exc()
print self.name+' stop '
def init(self, question):
self.arg = question.replace('\n', '')
self.level = int(self.arg.split('&')[0].split('=')[1])
self.line = int(self.arg.split('&')[1].split('=')[1])
self.row = int(self.arg.split('&')[2].split('=')[1])
self.pos = self.arg.split('&')[3].split('=')[1]
self.house = [[0 for i in range(self.row)] for j in range(self.line)]
self.house_list = []
self.history = ''
sys.setrecursionlimit(1000*self.line*self.row)
self.num_map = {'num_-1': 0, 'num_0': 0, 'num_1': 0, 'num_2': 0, 'num_3': 0, 'num_4': 0}
# each shadow position is (1, 1, 1, 1), means can start walking (up, down, left, right)
self.shadow = [[[1 for k in range(4)] for i in range(self.row)] for j in range(self.line)]
self.sum_second = 0
self.sum_walk = 0
#init each position
for each in range(len(self.pos)):
j = each%self.row
i = (each-j)/self.row
if self.pos[each] == '1':
self.house[i][j] = -1
self.num_map['num_-1'] += 1
else:
self.house[i][j] = 0
#count position value
for i in range(self.line):
for j in range(self.row):
if self.house[i][j] >= 0:
self.posCount(self.house, i, j, self.num_map, True)
self.start_time = datetime.datetime.now()
if self.mission_type == 3 and self.reverse == 0:
print 'lv:%s start time: %s' % (self.level, self.start_time.strftime("%Y%m%d-%H%M%S.%f"))
print self.arg
print self.line, self.row
self.print_house(self.house)
def print_house(self, temp_house):
print ' ',
for j in range(len(temp_house[0])):
print '%3d ' % j,
print ' '
for i in range(len(temp_house)):
each = temp_house[i]
print '%3d' % i,
for each2 in each:
print '%3d ' % each2,
print ' '
#count one position value
def posCount(self, temp_house, i, j, temp_num_map, firstInit=False):
n = 0
if i-1 >= 0 and temp_house[i-1][j] >= 0:
n += 1
if i+1 < self.line and temp_house[i+1][j] >= 0:
n += 1
if j-1 >= 0 and temp_house[i][j-1] >= 0:
n += 1
if j+1 < self.row and temp_house[i][j+1] >= 0:
n += 1
if not firstInit:
temp_num_map['num_'+str(temp_house[i][j])] -= 1
temp_num_map['num_'+str(n)] += 1
temp_house[i][j] = n
def multiArea(self, temp_house, block_line, block_row):
#wave all blocks, and check if A-B==0 or A-B==2
#wide is highest when travelling
block_now = copy.deepcopy(temp_house)
selected_list = []
selected_list.append((block_line, block_row, 1))#(self.line, self.row, depth)
search_wall = {'state': 0, 'cross': 0}
#select all linked blocks
#print 'block', block_line, block_row
#print_house(block_now)
try:
block_now[block_line][block_row] = -2
if block_line == 0 or block_line == self.line-1 or block_row == 0 or block_row == self.row-1:
search_wall['wall-2'] = 1
result = self.travelBlock(block_now, selected_list, search_wall)
except RuntimeError, e:
print 'error', e
result = False
#analyse wall to wall
if not result:
result = self.analyzeMulti(block_now)
if result:
#print 'analyse block true'
#print_house(block_now)
pass
else:
#print 'travel block true'
#print_house(block_now)
pass
#print result
#print_house(block_now)
return result
def travelBlock(self, block_now, selected_list, search_wall):
if len(selected_list) == 0:
return False
i, j, depth = selected_list.pop(0)
#print_house(block_now)
#select all blocks nearby
peer_block = 0
if i > 0:
if block_now[i-1][j] == -1:
#up
block_now[i-1][j] = -1 - (depth+1)
selected_list.append((i-1, j, depth+1))
#elif block_now[i-1][j] == block_now[i][j]:
# peer_block += 1
if i+1 < self.line:
if block_now[i+1][j] == -1:
#down
block_now[i+1][j] = -1 - (depth+1)
selected_list.append((i+1, j, depth+1))
#elif block_now[i+1][j] == block_now[i][j]:
# peer_block += 1
if j > 0:
if block_now[i][j-1] == -1:
#left
block_now[i][j-1] = -1 - (depth+1)
selected_list.append((i, j-1, depth+1))
#elif block_now[i][j-1] == block_now[i][j]:
# peer_block += 1
if j+1 < self.row:
if block_now[i][j+1] == -1:
#right
block_now[i][j+1] = -1 - (depth+1)
selected_list.append((i, j+1, depth+1))
#elif block_now[i][j+1] == block_now[i][j]:
# peer_block += 1
if peer_block > 2:
search_wall['cross'+str(-1 - (depth))] = 1
if search_wall.__contains__('cross'+str(-1 - (depth-1))):
search_wall['cross'] += 1
if search_wall['cross'] > 1:
return True
elif search_wall['cross'] == 1 and search_wall['state'] > 0:
return True
if i > 0 and j > 0 and block_now[i-1][j-1] == -1:
#up,left
block_now[i-1][j-1] = -1 - (depth+1)
selected_list.append((i-1, j-1, depth+1))
if i+1 < self.line and j > 0 and block_now[i+1][j-1] == -1:
#down,left
block_now[i+1][j-1] = -1 - (depth+1)
selected_list.append((i+1, j-1, depth+1))
if i > 0 and j+1 < self.row and block_now[i-1][j+1] == -1:
#up,right
block_now[i-1][j+1] = -1 - (depth+1)
selected_list.append((i-1, j+1, depth+1))
if i+1 < self.line and j+1 < self.row and block_now[i+1][j+1] == -1:
#down,right
block_now[i+1][j+1] = -1 - (depth+1)
selected_list.append((i+1, j+1, depth+1))
#mark this wave if wall
if i == 0 or i == self.line-1 or j == 0 or j == self.row-1:
search_wall['wall'+str(-1 - (depth))] = 1
#print 'walled', (-1 - (depth))
if search_wall['state'] == 0:
#searching wall1
if search_wall.__contains__('wall'+str(-1 - (depth-1))):
if search_wall['cross'] > 1:
return True
#print (-1 - (depth)), '0->1'
search_wall['state'] = 1
elif search_wall['state'] == 1:
#in wall1
if search_wall['cross'] > 1:
return True
if search_wall.__contains__('wall'+str(-1 - (depth-1))):
pass
else:
#print (-1 - (depth)), '1->2'
search_wall['state'] = 2
elif search_wall['state'] == 2:
#searching wall2
if search_wall['cross'] > 1:
return True
if search_wall.__contains__('wall'+str(-1 - (depth-1))):
#print (-1 - (depth)), '2->Ture'
return True
if len(selected_list) == 0:
if search_wall['state'] == 2:
#searching wall2
if search_wall.__contains__('wall'+str(-1 - (depth))):
#print (-1 - depth), '2->Ture'
return True
return self.travelBlock(block_now, selected_list, search_wall)
def analyzeMulti(self, block_now):
target = 0
searching = True
if block_now[0][0] < -1:
#selected
for j in range(self.row):
if searching:
if block_now[0][j] >= -1:
target += 1
if target == 2:
return True
searching = False
else:
if block_now[0][j] < -1:
searching = True
for i in range(self.line):
if searching:
if block_now[i][self.row-1] >= -1:
target += 1
if target == 2:
return True
searching = False
else:
if block_now[i][self.row-1] < -1:
searching = True
for j in range(self.row-1, -1, -1):
if searching:
if block_now[self.line-1][j] >= -1:
target += 1
if target == 2:
return True
searching = False
else:
if block_now[self.line-1][j] < -1:
searching = True
for i in range(self.line-1, -1, -1):
if searching:
if block_now[i][0] >= -1:
target += 1
if target == 2:
return True
searching = False
else:
if block_now[i][0] < -1:
searching = True
else:
#not selected
for j in range(self.row):
if searching:
if block_now[0][j] < -1:
target += 1
if target == 2:
return True
searching = False
else:
if block_now[0][j] >= -1:
searching = True
for i in range(self.line):
if searching:
if block_now[i][self.row-1] < -1:
target += 1
if target == 2:
return True
searching = False
else:
if block_now[i][self.row-1] >= -1:
searching = True
for j in range(self.row-1, -1, -1):
if searching:
if block_now[self.line-1][j] < -1:
target += 1
if target == 2:
return True
searching = False
else:
if block_now[self.line-1][j] >= -1:
searching = True
for i in range(self.line-1, -1, -1):
if searching:
if block_now[i][0] < -1:
target += 1
if target == 2:
return True
searching = False
else:
if block_now[i][0] >= -1:
searching = True
return False
def areYouOK(self, temp_house, block_line, block_row, temp_num_map, depth):
num_1, num0, num1, num2, num3, num4 = (temp_num_map['num_-1'], temp_num_map['num_0'], temp_num_map['num_1'], temp_num_map['num_2'], temp_num_map['num_3'], temp_num_map['num_4'])
#print num_1, num0, num1, num2, num3, num4
#sure
if num0 == 1 and num1 == 0 and num2 == 0 and num3 == 0 and num4 == 0:
return True
if num0 > 0:
return False
if num1 > 2:
return False
if num0 == 0 and num3 == 0 and num4 == 0:
return None
#if num_1*4>self.line*self.row and num_1*2<self.line*self.row and block_line!=None and block_row!=None and multiArea(temp_house, block_line, block_row):
# return False
if depth % 16 == 0:
if self.multiArea(temp_house, block_line, block_row):
return False
self.data_lock.acquire()
queue_size = self.queue.qsize()
self.data_lock.release()
if queue_size == 1:
return False
#possibly
return None
def robotStart(self, mission_type, reverse):
#origin_house at self.house_list[0]
#print travel(2, 2, 1)
'''
self.house_list.append((copy.deepcopy(self.house), copy.deepcopy(self.num_map)))
self.history = ''
i = 30
j = 6
result = travel_first(i, j, 1)
if result[0]:
url = 'http://www.qlcoder.com/train/crcheck?x='+str(i+1)+'&y='+str(j+1)+'&path='+self.history
print url
'''
# print 'search '+str(mission_type)+' '+str(reverse)
timing.start()
self.house_list.append((copy.deepcopy(self.house), copy.deepcopy(self.num_map)))
i_range = range(self.line)
j_range = range(self.row)
if reverse == 1:
i_range.reverse()
j_range.reverse()
for i in i_range:
for j in j_range:
self.data_lock.acquire()
queue_size = self.queue.qsize()
self.data_lock.release()
if self.house[i][j] == int(mission_type) and queue_size == 0:
self.sum_walk += 1
# print '%d,%d\r' % (i, j),
print '(%d, %d) walk %d %d' % (self.mission_type, self.reverse, i, j)
self.history = ''
result = self.travel_first(i, j, 1)
if result[0]:
url = 'http://www.qlcoder.com/train/crcheck?x='+str(i+1)+'&y='+str(j+1)+'&path='+self.history
print url
self.sum_second += timing.stop()
return url
self.sum_second += timing.stop()
return None
#move from (pos_line, pos_row)
#return (result, (ul, ur, dl, dr))
def travel_first(self, pos_line, pos_row, depth):
result = (False, ((0, 0), (0, 0)))
a_house, a_num_map = self.house_list[depth-1]
house_now = copy.deepcopy(a_house)
num_map_now = copy.deepcopy(a_num_map)
this_shadow_line = [0, 0, 0, 0]#for up, down, left, right count
#print pos_line, pos_row
#print_house(house_now)
#move up
#if self.shadow[pos_line][pos_row][0] == 0:
# print 'shadow (%d, %d) %d' % (pos_line, pos_row, 0)
if self.shadow[pos_line][pos_row][0] == 1 and pos_line-1 >= 0 and house_now[pos_line-1][pos_row] >= 0:
no_shadow = False#flag for not use shadow because eye analyse
house_up = copy.deepcopy(house_now)
num_map_up = copy.deepcopy(num_map_now)
inc = 1
while pos_line-inc >= 0 and house_up[pos_line-inc][pos_row] >= 0:
num_map_up['num_'+str(house_up[pos_line-(inc-1)][pos_row])] -= 1
num_map_up['num_'+str(-1)] += 1
house_up[pos_line-(inc-1)][pos_row] = -1
this_shadow_line[0] += 1
inc+=1
#recount value
for i in range(pos_line-inc if pos_line-inc>=0 else 0, (pos_line+2) if pos_line+1<self.line else self.line):
for j in [pos_row-1, pos_row, pos_row+1] if pos_row-1>=0 and pos_row+1<self.row else [pos_row-1, pos_row] if pos_row-1>0 else [pos_row, pos_row+1] if pos_row+1<self.row else [pos_row,]:
if house_up[i][j] >= 0:
self.posCount(house_up, i, j, num_map_up)
if house_up[i][j] == 0 or house_up[i][j] == 1:
no_shadow = True
self.history = self.history[:depth-1]+'u'
if depth % 100 == 0:
#print self.history
pass
#print_house(house_up)
self.house_list = self.house_list[:depth]+[(house_up,num_map_up),]
result = self.travel(pos_line-(inc-1), pos_row, depth+1)
shadow_line_excluded = self.excludeShadowUp(((pos_line-this_shadow_line[0], pos_row), (pos_line, pos_row)), result[1])
#print result[1], shadow_line_excluded
if no_shadow is False and shadow_line_excluded is not None:
for step in range(shadow_line_excluded[0][0], shadow_line_excluded[1][0]+1):
self.shadow[step][pos_row][0] = 0
if result[0]:
return result
#move down
#if self.shadow[pos_line][pos_row][1] == 0:
# print 'shadow (%d, %d) %d' % (pos_line, pos_row, 1)
if self.shadow[pos_line][pos_row][1] == 1 and pos_line+1 < self.line and house_now[pos_line+1][pos_row] >= 0:
no_shadow = False#flag for not use shadow because eye analyse
house_down = copy.deepcopy(house_now)
num_map_down = copy.deepcopy(num_map_now)
inc = 1
while pos_line+inc < self.line and house_down[pos_line+inc][pos_row] >= 0:
num_map_down['num_'+str(house_down[pos_line+(inc-1)][pos_row])] -= 1
num_map_down['num_'+str(-1)] += 1
house_down[pos_line+(inc-1)][pos_row] = -1
this_shadow_line[1] += 1
inc+=1
#recount value
for i in range(pos_line-1 if pos_line-1>=0 else 0, (pos_line+inc) if pos_line+inc<self.line else self.line):
for j in [pos_row-1, pos_row, pos_row+1] if pos_row-1>=0 and pos_row+1<self.row else [pos_row-1, pos_row] if pos_row-1>0 else [pos_row, pos_row+1] if pos_row+1<self.row else [pos_row,]:
if house_down[i][j] >= 0:
self.posCount(house_down, i, j, num_map_down)
if house_down[i][j] == 0 or house_down[i][j] == 1:
no_shadow = True
self.history = self.history[:depth-1]+'d'
if depth % 100 == 0:
#print self.history
pass
#print_house(house_down)
self.house_list = self.house_list[:depth]+[(house_down,num_map_down),]
result = self.travel(pos_line+(inc-1), pos_row, depth+1)
shadow_line_excluded = self.excludeShadowDown(((pos_line, pos_row), (pos_line+this_shadow_line[1], pos_row)), result[1])
#print result[1], shadow_line_excluded
if no_shadow is False and shadow_line_excluded is not None:
for step in range(shadow_line_excluded[0][0], shadow_line_excluded[1][0]+1):
#if step == '31' and pos_row == 6:
#print 'close', pos_line, pos_row, step, pos_row, this_shadow_line, shadow_line_excluded
self.shadow[step][pos_row][1] = 0
if result[0]:
return result
#move left
#if self.shadow[pos_line][pos_row][2] == 0:
# print 'shadow (%d, %d) %d' % (pos_line, pos_row, 2)
if self.shadow[pos_line][pos_row][2] == 1 and pos_row-1 >= 0 and house_now[pos_line][pos_row-1] >= 0:
no_shadow = False#flag for not use shadow because eye analyse
house_left = copy.deepcopy(house_now)
num_map_left = copy.deepcopy(num_map_now)
inc = 1
while pos_row-inc >= 0 and house_left[pos_line][pos_row-inc] >= 0:
num_map_left['num_'+str(house_left[pos_line][pos_row-(inc-1)])] -= 1
num_map_left['num_'+str(-1)] += 1
house_left[pos_line][pos_row-(inc-1)] = -1
this_shadow_line[2] += 1
inc+=1
#recount value
for i in [pos_line-1, pos_line, pos_line+1] if pos_line-1>=0 and pos_line+1<self.line else [pos_line-1, pos_line] if pos_line-1>0 else [pos_line, pos_line+1] if pos_line+1<self.line else [pos_line,]:
for j in range(pos_row-inc if pos_row-inc>=0 else 0, (pos_row+2) if pos_row+1<self.row else self.row):
if house_left[i][j] >= 0:
self.posCount(house_left, i, j, num_map_left)
if house_left[i][j] == 0 or house_left[i][j] == 1:
no_shadow = True
self.history = self.history[:depth-1]+'l'
if depth % 100 == 0:
#print self.history
pass
#print_house(house_left)
self.house_list = self.house_list[:depth]+[(house_left,num_map_left),]
result = self.travel(pos_line, pos_row-(inc-1), depth+1)
shadow_line_excluded = self.excludeShadowLeft(((pos_line, pos_row-this_shadow_line[2]), (pos_line, pos_row)), result[1])
#print result[1], shadow_line_excluded
if no_shadow is False and shadow_line_excluded is not None:
for step in range(shadow_line_excluded[0][1], shadow_line_excluded[1][1]+1):
self.shadow[pos_line][step][2] = 0
if result[0]:
return result
#move right
#if self.shadow[pos_line][pos_row][3] == 0:
# print 'shadow (%d, %d) %d' % (pos_line, pos_row, 3)
if self.shadow[pos_line][pos_row][3] == 1 and pos_row+1 < self.row and house_now[pos_line][pos_row+1] >= 0:
no_shadow = False#flag for not use shadow because eye analyse
house_right = copy.deepcopy(house_now)
num_map_right = copy.deepcopy(num_map_now)
inc = 1
while pos_row+inc < self.row and house_right[pos_line][pos_row+inc] >= 0:
num_map_right['num_'+str(house_right[pos_line][pos_row+(inc-1)])] -= 1
num_map_right['num_'+str(-1)] += 1
house_right[pos_line][pos_row+(inc-1)] = -1
this_shadow_line[3] += 1
inc+=1
#recount value
for i in [pos_line-1, pos_line, pos_line+1] if pos_line-1>=0 and pos_line+1<self.line else [pos_line-1, pos_line] if pos_line-1>0 else [pos_line, pos_line+1] if pos_line+1<self.line else [pos_line,]:
for j in range(pos_row-1 if pos_row-1>0 else 0, (pos_row+inc) if pos_row+inc<self.row else self.row):
if house_right[i][j] >= 0:
self.posCount(house_right, i, j, num_map_right)
if house_right[i][j] == 0 or house_right[i][j] == 1:
no_shadow = True
self.history = self.history[:depth-1]+'r'
if depth % 100 == 0:
#print self.history
pass
#print_house(house_right)
self.house_list = self.house_list[:depth]+[(house_right,num_map_right),]
result = self.travel(pos_line, pos_row+(inc-1), depth+1)
shadow_line_excluded = self.excludeShadowRight(((pos_line, pos_row), (pos_line, pos_row+this_shadow_line[3])), result[1])
#print result[1], shadow_line_excluded
if no_shadow is False and shadow_line_excluded is not None:
for step in range(shadow_line_excluded[0][1], shadow_line_excluded[1][1]+1):
self.shadow[pos_line][step][3] = 0
if result[0]:
return result
return result
#move from (pos_line, pos_row)
#return (result, (ul, dr))
def travel(self, pos_line, pos_row, depth):
result = (False, ((0, 0), (0, 0)))
this_shadow = ((pos_line, pos_row), (pos_line, pos_row))#ul, dr
#this_shadow_line = {'ul': {'line': pos_line, 'row': pos_row}, 'dr': {'line': pos_line, 'row': pos_row}}#ul, dr
this_shadow_line = [0, 0, 0, 0]#for up, down, left, right count
a_house, a_num_map = self.house_list[depth-1]
house_now = copy.deepcopy(a_house)
num_map_now = copy.deepcopy(a_num_map)
ok = self.areYouOK(house_now, pos_line, pos_row, num_map_now, depth)
if ok==False:
result = (False, ((pos_line, pos_row), (pos_line, pos_row)))
return result
elif ok==True:
result = (True, ((pos_line, pos_row), (pos_line, pos_row)))
return result
#return None
#move up
if pos_line-1 >= 0 and house_now[pos_line-1][pos_row] >= 0:
house_up = copy.deepcopy(house_now)
num_map_up = copy.deepcopy(num_map_now)
inc = 1
while pos_line-inc >= 0 and house_up[pos_line-inc][pos_row] >= 0:
num_map_up['num_'+str(house_up[pos_line-(inc-1)][pos_row])] -= 1
num_map_up['num_'+str(-1)] += 1
house_up[pos_line-(inc-1)][pos_row] = -1
this_shadow_line[0] += 1
inc+=1
#recount value
for i in range(pos_line-inc if pos_line-inc>=0 else 0, (pos_line+2) if pos_line+1<self.line else self.line):
for j in [pos_row-1, pos_row, pos_row+1] if pos_row-1>=0 and pos_row+1<self.row else [pos_row-1, pos_row] if pos_row-1>0 else [pos_row, pos_row+1] if pos_row+1<self.row else [pos_row,]:
if house_up[i][j] >= 0:
self.posCount(house_up, i, j, num_map_up)
self.history = self.history[:depth-1]+'u'
if depth % 100 == 0:
#print self.history
pass
#print_house(house_up)
self.house_list = self.house_list[:depth]+[(house_up,num_map_up),]
result = self.travel(pos_line-(inc-1), pos_row, depth+1)
this_shadow = self.mergeShadow(self.mergeShadow(((pos_line - this_shadow_line[0], pos_row), (pos_line, pos_row)), result[1]), this_shadow)
#if depth == 2:
# print 'lgbu', pos_line, pos_row, this_shadow
if result[0]:
result = (True, this_shadow)
return result
#move down
if pos_line+1 < self.line and house_now[pos_line+1][pos_row] >= 0:
house_down = copy.deepcopy(house_now)
num_map_down = copy.deepcopy(num_map_now)
inc = 1
while pos_line+inc < self.line and house_down[pos_line+inc][pos_row] >= 0:
num_map_down['num_'+str(house_down[pos_line+(inc-1)][pos_row])] -= 1
num_map_down['num_'+str(-1)] += 1
house_down[pos_line+(inc-1)][pos_row] = -1
this_shadow_line[1] += 1
inc+=1
#recount value
for i in range(pos_line-1 if pos_line-1>=0 else 0, (pos_line+inc) if pos_line+inc<self.line else self.line):
for j in [pos_row-1, pos_row, pos_row+1] if pos_row-1>=0 and pos_row+1<self.row else [pos_row-1, pos_row] if pos_row-1>0 else [pos_row, pos_row+1] if pos_row+1<self.row else [pos_row,]:
if house_down[i][j] >= 0:
self.posCount(house_down, i, j, num_map_down)
self.history = self.history[:depth-1]+'d'
if depth % 100 == 0:
#print self.history
pass
#print_house(house_down)
self.house_list = self.house_list[:depth]+[(house_down,num_map_down),]
result = self.travel(pos_line+(inc-1), pos_row, depth+1)
this_shadow = self.mergeShadow(self.mergeShadow(((pos_line, pos_row), (pos_line + this_shadow_line[1], pos_row)), result[1]), this_shadow)
#if depth == 2:
# print 'lgbd', pos_line, pos_row, this_shadow
if result[0]:
result = (True, this_shadow)
return result
#move left
if pos_row-1 >= 0 and house_now[pos_line][pos_row-1] >= 0:
house_left = copy.deepcopy(house_now)
num_map_left = copy.deepcopy(num_map_now)
inc = 1
while pos_row-inc >= 0 and house_left[pos_line][pos_row-inc] >= 0:
num_map_left['num_'+str(house_left[pos_line][pos_row-(inc-1)])] -= 1
num_map_left['num_'+str(-1)] += 1
house_left[pos_line][pos_row-(inc-1)] = -1
this_shadow_line[2] += 1
inc+=1
#recount value
for i in [pos_line-1, pos_line, pos_line+1] if pos_line-1>=0 and pos_line+1<self.line else [pos_line-1, pos_line] if pos_line-1>0 else [pos_line, pos_line+1] if pos_line+1<self.line else [pos_line,]:
for j in range(pos_row-inc if pos_row-inc>=0 else 0, (pos_row+2) if pos_row+1<self.row else self.row):
if house_left[i][j] >= 0:
self.posCount(house_left, i, j, num_map_left)
self.history = self.history[:depth-1]+'l'
if depth % 100 == 0:
#print self.history
pass
#print_house(house_left)
self.house_list = self.house_list[:depth]+[(house_left,num_map_left),]
result = self.travel(pos_line, pos_row-(inc-1), depth+1)
this_shadow = self.mergeShadow(self.mergeShadow(((pos_line, pos_row - this_shadow_line[2]), (pos_line, pos_row)), result[1]), this_shadow)
#if depth == 2:
# print 'lgbl', pos_line, pos_row, depth, this_shadow, result[1]
if result[0]:
result = (True, this_shadow)
return result
#move right
if pos_row+1 < self.row and house_now[pos_line][pos_row+1] >= 0:
house_right = copy.deepcopy(house_now)
num_map_right = copy.deepcopy(num_map_now)
inc = 1
while pos_row+inc < self.row and house_right[pos_line][pos_row+inc] >= 0:
num_map_right['num_'+str(house_right[pos_line][pos_row+(inc-1)])] -= 1
num_map_right['num_'+str(-1)] += 1
house_right[pos_line][pos_row+(inc-1)] = -1
this_shadow_line[3] += 1
inc+=1
#recount value
for i in [pos_line-1, pos_line, pos_line+1] if pos_line-1>=0 and pos_line+1<self.line else [pos_line-1, pos_line] if pos_line-1>0 else [pos_line, pos_line+1] if pos_line+1<self.line else [pos_line,]:
for j in range(pos_row-1 if pos_row-1>0 else 0, (pos_row+inc) if pos_row+inc<self.row else self.row):
if house_right[i][j] >= 0:
self.posCount(house_right, i, j, num_map_right)
self.history = self.history[:depth-1]+'r'
if depth % 100 == 0:
#print self.history
pass
#print_house(house_right)
self.house_list = self.house_list[:depth]+[(house_right,num_map_right),]
result = self.travel(pos_line, pos_row+(inc-1), depth+1)
this_shadow = self.mergeShadow(self.mergeShadow(((pos_line, pos_row), (pos_line, pos_row - this_shadow_line[3])), result[1]), this_shadow)
#if depth == 2:
# print 'lgbr', pos_line, pos_row, depth, this_shadow, result[1]
if result[0]:
result = (True, this_shadow)
return result
result = (False, this_shadow)
return result
def mergeShadow(self, sa, sb):
sr = ((sa[0][0] if sa[0][0]<sb[0][0] else sb[0][0], sa[0][1] if sa[0][1]<sb[0][1] else sb[0][1]), (sa[1][0] if sa[1][0]>sb[1][0] else sb[1][0], sa[1][1] if sa[1][1]>sb[1][1] else sb[1][1]))
return sr
def excludeShadowUp(self, l, s):
if l[1][0] > s[1][0]:
return ((s[1][0]+1, l[1][1]), (l[1][0], l[1][1]))
return None
def excludeShadowDown(self, l, s):
if l[0][0] < s[0][0]:
return ((l[0][0], l[0][1]), (s[0][0]-1, l[0][1]))
return None
def excludeShadowLeft(self, l, s):
if l[1][1] > s[1][1]:
return ((l[1][0], s[1][1]+1), (l[1][0], l[1][1]))
return None
def excludeShadowRight(self, l, s):
if l[0][1] < s[0][1]:
return ((l[0][0], l[0][1]), (l[0][0], s[0][1]-1))
return None
def driver_init():
global driver
driver = webdriver.Chrome()
def driver_quit():
global driver
driver.quit()
def driver_login():
global driver
driver.get('http://www.qlcoder.com/auth/login')
time.sleep(1)
driver.maximize_window()
time.sleep(1)
driver.find_element_by_xpath("//input[@id='email']").send_keys('mrujowfi@163.com')
time.sleep(1)
driver.find_element_by_xpath("//input[@id='password']").send_keys('kzchmwdi10')
time.sleep(1)
driver.find_element_by_xpath("//input[@id='login-submit']").click()
time.sleep(3)
def driver_getQuestion():
global driver
driver.get('http://www.qlcoder.com/train/autocr')
time.sleep(3)
content = ''
while content == '':
time.sleep(1)
print 'wait for page'
content = driver.find_element_by_xpath("//body").text
if content.__contains__(u'请先登陆'):
driver_login()
return driver_getQuestion()
else:
question = content.split('\n')[7]
return question
def driver_sendAnswer(ans):
global driver
driver.get(ans)
time.sleep(3)
content = driver.find_element_by_xpath("//body").text
if content.__contains__(u'请先登陆'):
driver_login()
driver_sendAnswer(ans)
if __name__ == '__main__':
dev = True
if dev is False:
driver_init()
try:
if dev:
dict_args = {}
dict_args['queue'] = multiprocessing.Queue(maxsize=5)
dict_args['data_lock'] = multiprocessing.RLock()
dict_args['arg'] = '''
level=106&x=38&y=39&map=000000100110011000010000000000000000000000010000100011001000000100010001000010100000010001001101101000001000100010000101101011111100001100000001111111000101001001000000000010000000000000000000101011000110000010000100001000000000100101000010010001000101101100010011000100100001000011000000100100000111000001000110000110000010000110000010000010001000100000110000111111110011000110011100011001111111100001110010000010000111111010011000000001001110000100000110011111010000000011001000010100000100111000111000110001000010011000100010001000110000000000001001000111001111011001010000011100011111000000011111110011000000111001001100100010000010000000110010100000001000100001100100000000011110000001000000010000011001001101111000010001000011111000010011000011100111111010000000111000001000000000111110000100010000110011011101001001000100100000100110100001000000101000111001100001000010010001000000100001000111101101100011001011000010000001000100100001100000111100000011110100000010000000011000010111000100000000111110011000010001000000000011100110001110010000000010100000001110011110000100010000101111000100111100000011111000000000100001000111100100000001000100000010001001000010000100101000100010100100000100011110010010000000011111010000100100000001100010000111010000000010010010000010100001010100001000000001110001000011010010000000001000000010001110100100011000010000101010011111110111100110001001000001000001010000111110000100010000100000100001000000000111110010000010010000110000100000
'''
#create processes
processed = []
for i in [3, 4, 2, 1]:
dict_args['mission_type'] = i
dict_args['reverse'] = 0
processed.append(ProcRobot(dict_args))
dict_args['reverse'] = 1
processed.append(ProcRobot(dict_args))
#start processes
for i in range(len(processed)):
processed[i].start()
time.sleep(1)
#join processes
for i in range(len(processed)):
processed[i].join()
dict_args['data_lock'].acquire()
if dict_args['queue'].qsize() == 1:
ans = dict_args['queue'].get()
print 'driver_sendAnswer(ans)'
else:
print 'all failed'
dict_args['data_lock'].release()
else:
while True:
dict_args = {}
dict_args['queue'] = multiprocessing.Queue(maxsize=5)
dict_args['data_lock'] = multiprocessing.RLock()
dict_args['arg'] = driver_getQuestion()
#create processes
processed = []
for i in [3, 4, 2, 1]:
dict_args['mission_type'] = i
dict_args['reverse'] = 0
processed.append(ProcRobot(dict_args))
dict_args['reverse'] = 1
processed.append(ProcRobot(dict_args))
#start processes
for i in range(len(processed)):
processed[i].start()
time.sleep(1)
#join processes
for i in range(len(processed)):
processed[i].join()
dict_args['data_lock'].acquire()
if dict_args['queue'].qsize() == 1:
ans = dict_args['queue'].get()
driver_sendAnswer(ans)
else:
print 'all failed'
dict_args['data_lock'].release()
except Exception, e:
print 'error', e
time.sleep(1)
print traceback.format_exc()
if dev is False:
driver_quit()