码迷,mamicode.com
首页 > 其他好文 > 详细

Berserk Rook

时间:2014-08-18 15:45:02      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   strong   for   

Berserk Rook

As you may know, chess is an ancient game for which almost everyone has at least a basic understanding of the rules. Chess is a two-player strategy game played on a checkered game board laid out in eight rows (called ranks and denoted with numbers 1 to 8) and eight columns (called files and denoted with letters a to h) of squares. Each square of the chessboard is identified by a unique coordinate pair — a letter and a number (ex, "a1", "h8", "d6").

For this mission let‘s look at rooks, a dangerous shock unit which can be used for a swift thrust or for a dense defence. The rook moves horizontally or vertically, through any number of unoccupied squares, but may not leap over other pieces. As with capturing using any other unit, the rook captures by occupying the square on which the target enemy piece sits.

For this mission you have one berserk rook facing off against an army of enemy rooks. The berserk rook can only move if it will capture an enemy unit. So on each turn it will move and capture until there are no enemy targets left where it will take one "empty" step and stop.

You are given the position of your berserk rook and the positions of the enemy rooks. Your goal is capture as many units as possible without stopping. Each move in your sequence should capture an enemy unit. The result will be the maximum possible number of enemy rooks captured.

Input: Two arguments. Berserk rook position as a string and enemy rook positions as a set of strings.

Output: The maximum possible number of captured enemy rooks as an integer.

原题链接: http://www.checkio.org/mission/berserk-rook/

题目大意: 最多可以吃掉几枚敌方棋子

思路: 递归搜索, 模拟手工过程, 注意不要跳过棋子

 1 #recursive search
 2 #in fact recursive_depth is the max number of enemies can capture
 3 max_recursive_depth = 0
 4 
 5 def search(cur_pos, captured_enemies, remain_enemies):
 6     global max_recursive_depth
 7 
 8     for enemy in remain_enemies:
 9         if cur_pos[0] == enemy[0] or cur_pos[1] == enemy[1]: #this enemy can be captured
10 
11             not_jump = True
12 
13             if cur_pos[1] == enemy[1]:
14 
15                 upper, lower = cur_pos[0], enemy[0]
16 
17                 if enemy[0] > cur_pos[0]:
18                     upper, lower = lower, upper
19 
20                 while not_jump:
21                     next = chr(ord(lower) + 1)
22 
23                     if next == upper:
24                         break
25 
26                     if (next + cur_pos[1]) in remain_enemies:
27                         not_jump = False
28 
29                     lower = next
30 
31             elif cur_pos[0] == enemy[0]:
32 
33                 upper, lower = int(cur_pos[1]), int(enemy[1])
34 
35                 if enemy[1] > cur_pos[1]:
36                     upper, lower = lower, upper
37 
38                 for next in range(lower + 1, upper):
39                     if (cur_pos[0] + str(next)) in remain_enemies:
40                         not_jump = False
41                         break
42 
43             if not_jump:
44                 remain_enemies.discard(enemy)
45                 captured_enemies.add(enemy)
46     
47                 rel = search(enemy, captured_enemies, remain_enemies)
48     
49                 if rel > max_recursive_depth:
50                     max_recursive_depth = rel
51     
52                 remain_enemies.add(enemy)
53                 captured_enemies.discard(enemy)
54 
55     return len(captured_enemies)
56 
57 def berserk_rook(berserker, enemies):
58     global max_recursive_depth
59     max_recursive_depth = 0
60     search(berserker, set(), enemies)
61     return max_recursive_depth

Berserk Rook,布布扣,bubuko.com

Berserk Rook

标签:style   blog   http   color   os   io   strong   for   

原文地址:http://www.cnblogs.com/hzhesi/p/3919514.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!