diff --git a/Test_case/level_2/14.json b/Test_case/level_2/14.json new file mode 100644 index 0000000000000000000000000000000000000000..f95e9c9f873172f97b6c92aab53cd1e4bd6256f7 --- /dev/null +++ b/Test_case/level_2/14.json @@ -0,0 +1,43 @@ +{ + "upper": [ + [ + "r", + 1, + -1 + ] + ], + "lower": [ + [ + "s", + -3, + 1 + ] + ], + "block": [ + [ + "", + -1, + -1 + ], + [ + "", + -1, + 0 + ], + [ + "", + -1, + 1 + ], + [ + "", + 0, + -2 + ], + [ + "", + 0, + 0 + ] + ] +} \ No newline at end of file diff --git a/Test_case/level_3/13.json b/Test_case/level_3/13.json new file mode 100644 index 0000000000000000000000000000000000000000..40787b5b958dc076278ddaa704243f3786f3c2fd --- /dev/null +++ b/Test_case/level_3/13.json @@ -0,0 +1,108 @@ +{ + "upper": [ + [ + "p", + 0, + -3 + ], + [ + "s", + 0, + 0 + ] + ], + "lower": [ + [ + "r", + 0, + 1 + ], + [ + "p", + 0, + 2 + ], + [ + "r", + 2, + 0 + ] + ], + "block": [ + [ + "", + -1, + -3 + ], + [ + "", + -1, + -2 + ], + [ + "", + -1, + -1 + ], + [ + "", + -1, + 0 + ], + [ + "", + -1, + 1 + ], + [ + "", + -1, + 2 + ], + [ + "", + -1, + 3 + ], + [ + "", + 0, + 3 + ], + [ + "", + 1, + -4 + ], + [ + "", + 1, + -3 + ], + [ + "", + 1, + -2 + ], + [ + "", + 1, + -1 + ], + [ + "", + 1, + 0 + ], + [ + "", + 1, + 1 + ], + [ + "", + 1, + 2 + ] + ] +} \ No newline at end of file diff --git a/search/__pycache__/main.cpython-38.pyc b/search/__pycache__/main.cpython-38.pyc index 1ac4f3a1eb06acd9bcb68a9797aeb04de23944bd..306b913ed92d9ff2c02f03382457c5518ae3a453 100644 Binary files a/search/__pycache__/main.cpython-38.pyc and b/search/__pycache__/main.cpython-38.pyc differ diff --git a/search/__pycache__/search_algo.cpython-38.pyc b/search/__pycache__/search_algo.cpython-38.pyc index 9c6190551c3a2812e7abaca1b7123fa02b9eb34a..7f185dc34de8bb722305f1df201ef8f424c48ac6 100644 Binary files a/search/__pycache__/search_algo.cpython-38.pyc and b/search/__pycache__/search_algo.cpython-38.pyc differ diff --git a/search/main.py b/search/main.py index d463cb96a92a52797c817800fd5ee67096ef59ee..adf21701aac675e73148276757a0817e3ff9a8a3 100644 --- a/search/main.py +++ b/search/main.py @@ -76,6 +76,7 @@ def parse_input(data): nums = nums + 1 keyWrite = "S" + str(nums) upperDictPieces[keyWrite] = (piece[ROW], piece[COLUMN]) + visit_record[keyWrite] = {} # parse the Lower player's token nump, numr, nums = 0, 0, 0 @@ -146,3 +147,46 @@ def parse_input(data): # '-._.-'-._.-'-._.-'-._.-'-._.-'-._.-' # | | | | | | # '-._.-'-._.-'-._.-'-._.-'-._.-' + # Situation 2.B (B are blocks) + # how to tell if there are no way to reach the target + # + # .-'-._.-'-._.-'-._.-'-._.-'-. + # | | | | | | + # .-'-._.-'-._.-'-._.-'-._.-'-._.-'-. + # | | | | | | | + # .-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-. + # | | | | | r | | | + # .-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-. + # | B | B | B | B | B | B | B | | + # .-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-. + # | B | P | | | S | r | p | B | | + # '-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-' + # | B | B | B | B | B | B | B | | + # '-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-' + # | | | | | | | | + # '-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-' + # | | | | | | | + # '-._.-'-._.-'-._.-'-._.-'-._.-'-._.-' + # | | | | | | + # '-._.-'-._.-'-._.-'-._.-'-._.-' + # Scenario 3: + # can cause a infinite loops. + # .-'-._.-'-._.-'-._.-'-._.-'-. + # | | | | | | + # .-'-._.-'-._.-'-._.-'-._.-'-._.-'-. + # | | | | | | | + # .-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-. + # | | | | | | | | + # .-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-. + # | | | R1 | | | | | | + # .-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-. + # | | | B | | B | | | | | + # '-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-' + # | | | B | B | B | | | | + # '-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-' + # | | | | | | | | + # '-._.-'-._.-'-._.-'-._.-'-._.-'-._.-'-._.-' + # | | | s1 | | | | + # '-._.-'-._.-'-._.-'-._.-'-._.-'-._.-' + # | | | | | | + # '-._.-'-._.-'-._.-'-._.-'-._.-' diff --git a/search/search_algo.py b/search/search_algo.py index 5b9dfef56458f4720f00fb1d3bae0cb3e926e628..9c6415f327a64201991d4c373d65db42dc245bb9 100644 --- a/search/search_algo.py +++ b/search/search_algo.py @@ -19,6 +19,14 @@ targetDict = {} targeted_l_piece = set() setBlocks = set() +# to handle the issue of hit dead end by just choose the minimal action. +# { piece_key: { +# (row, column) : #visit +# ... +# } +# } +# each time a token visit a tile: mark it , the more time it visit a tile, the less appeal it is . +visit_record = {} def make_board(lowerPieces, upperPieces, setBlocks): """ @@ -128,10 +136,10 @@ def make_priority_list_of_action(upperDict, piece, targetDict, board): # add all the swing action to queue position_list.extend(add_swing_action(upperDict, piece, board)) - + print(visit_record) # sort the list base on the how close it is to target if piece in targetDict.keys(): - position_list.sort(key=(lambda x: distance_between(x, targetDict[piece]))) + position_list.sort(key=(lambda x: rank_by_appeal(x, targetDict,piece))) return position_list @@ -199,6 +207,8 @@ def choose_optimal_combination(possible_action, upperPieces, board, targetDict): board[possible_action[piece][index]] = piece moved = True + + decrease_appeal(possible_action[piece][index], piece) else: # check if no collision happen: if piece_collision(board[possible_action[piece][index]], piece) == DRAW: @@ -208,11 +218,22 @@ def choose_optimal_combination(possible_action, upperPieces, board, targetDict): board[possible_action[piece][index]] = piece moved = True + + decrease_appeal(possible_action[piece][index], piece) index += 1 return upperPieces +def decrease_appeal(position, piece): + global visit_record + + if position not in visit_record[piece].keys(): + visit_record[piece][position] = 1 + else: + visit_record[piece][position] += 1 + return visit_record + def check_if_piece_hit_target(upperPieces, lowerPieces, targetDict): """ @@ -229,6 +250,8 @@ def check_if_piece_hit_target(upperPieces, lowerPieces, targetDict): removed = removed_piece del targetDict[piece] del lowerPieces[removed] + # reset the visited node since piece start a new journey + visit_record[piece] = {} removed = True # once the piece hit it target, it is free to find a new target. # if there are more lower piece then which already targeted {some lower piece is not target} @@ -383,3 +406,17 @@ def find_target(piece_key): targetDict[piece_key] = lowerDictPieces[targetPiece] targeted_l_piece.add(targetPiece) + +def rank_by_appeal(x, targetDict, piece): + # distance_between(x, targetDict[piece]) + # make_priority_list_of_action(upperDict, piece, targetDict, board) + # the reduce would base on how close it is to its target and how many time we visit this. + base = distance_between(x, targetDict[piece]) + reduce_factor = 0 + + if x in visit_record[piece].keys(): + reduce_factor = visit_record[piece][x] + + adjust_score = base + reduce_factor + print(str(x) + ": " + str(adjust_score)) + return adjust_score