Berserk Rook

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

转载于:https://www.cnblogs.com/hzhesi/p/3919514.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值