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/

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

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

 #recursive search
#in fact recursive_depth is the max number of enemies can capture
max_recursive_depth = 0 def search(cur_pos, captured_enemies, remain_enemies):
global max_recursive_depth for enemy in remain_enemies:
if cur_pos[0] == enemy[0] or cur_pos[1] == enemy[1]: #this enemy can be captured not_jump = True if cur_pos[1] == enemy[1]: upper, lower = cur_pos[0], enemy[0] if enemy[0] > cur_pos[0]:
upper, lower = lower, upper while not_jump:
next = chr(ord(lower) + 1) if next == upper:
break if (next + cur_pos[1]) in remain_enemies:
not_jump = False lower = next elif cur_pos[0] == enemy[0]: upper, lower = int(cur_pos[1]), int(enemy[1]) if enemy[1] > cur_pos[1]:
upper, lower = lower, upper for next in range(lower + 1, upper):
if (cur_pos[0] + str(next)) in remain_enemies:
not_jump = False
break if not_jump:
remain_enemies.discard(enemy)
captured_enemies.add(enemy) rel = search(enemy, captured_enemies, remain_enemies) if rel > max_recursive_depth:
max_recursive_depth = rel remain_enemies.add(enemy)
captured_enemies.discard(enemy) return len(captured_enemies) def berserk_rook(berserker, enemies):
global max_recursive_depth
max_recursive_depth = 0
search(berserker, set(), enemies)
return max_recursive_depth

Berserk Rook的更多相关文章

  1. codeforces A. Rook, Bishop and King 解题报告

    题目链接:http://codeforces.com/problemset/problem/370/A 题目意思:根据rook(每次可以移动垂直或水平的任意步数(>=1)),bishop(每次可 ...

  2. CF Rook, Bishop and King

    http://codeforces.com/contest/370/problem/A 题意:车是走直线的,可以走任意多个格子,象是走对角线的,也可以走任意多个格子,而国王可以走直线也可以走对角线,但 ...

  3. bfs UESTC 381 Knight and Rook

    http://acm.uestc.edu.cn/#/problem/show/381 题目大意:给你两个棋子:车.马,再给你一个n*m的网格,从s出发到t,你可以选择车或者选择马开始走,图中有一些障碍 ...

  4. [Swift]LeetCode999. 车的可用捕获量 | Available Captures for Rook

    在一个 8 x 8 的棋盘上,有一个白色车(rook).也可能有空方块,白色的象(bishop)和黑色的卒(pawn).它们分别以字符 “R”,“.”,“B” 和 “p” 给出.大写字符表示白棋,小写 ...

  5. [rook] rook的控制流

    以下是rook为一个pod准备可用块存储的过程: 1. rook operator运行,并且在k8s每台机器上运行一个rook agent的pod: 2. 用户创建一个pvc,并指定storagecl ...

  6. Available Captures for Rook LT999

    On an 8 x 8 chessboard, there is one white rook.  There also may be empty squares, white bishops, an ...

  7. rook 排错记录 + Orphaned pod found kube-controller-manager的日志输出

    1.查看rook-agent(重要)和mysql-wordpress 的日志,如下: MountVolume.SetUp failed for volume "pvc-f002e1fe-46 ...

  8. kubespray 容器存储设备 -- rook ceph

    1./root/kubespray/roles/docker/docker-storage/defaults/main.yml  #在用kubespray部署集群是制定docker用什么设备 dock ...

  9. rook 入门理解

    参考:https://my.oschina.net/u/2306127/blog/1830356?from=timeline 1.Rook通过一个操作器(operator)完成后续操作,只需要定义需要 ...

随机推荐

  1. Linux企业级项目实践之网络爬虫(13)——处理user-agent

    User Agent即用户代理,是Http协议中的一部分,属于头域的组成部分,User Agent也简称UA.它是一个特殊字符串头,是一种向访问网站提供你所使用的浏览器类型及版本.操作系统及版本.浏览 ...

  2. Android ExpandableListActivity的简单介绍及小例子

    Android中常常要用到ListView,但也经常要用到ExpandableListView,ListView是显示列表,而ExpandableListView显示的是分类的列表: 下面用一个例子来 ...

  3. 在OCX初始化时获取其在网页中的DOM对象

    OCX初始化的时候会调用SetClientSite,会传入IOleClientSite对象. CComQIPtr<IOleControlSite, &IID_IOleControlSit ...

  4. Putty server refused our key的解决方法

    在使用putty工具使用密钥远程登陆CentOS系统时,出现Putty server refused our key提示,解决办法: 1.查看是否关掉SELINUX. 相关命令:getenforce, ...

  5. [iOS] 创建第一个应用程序项目

    开发环境:MacBook Pro XCode 5.0.1 1. 创建新的空的工程 2. 手动添加Controller 3. 将Controller添加到AppDelegate 4. 编辑.xib 5. ...

  6. Python 中文报错 SyntaxError: Non-ASCII character解决办法

    只需要在最顶部的位置加上 #-*- coding:utf-8 -*- 就行了. 如果还是没有好,打开设置并搜索file encoding并作如图修改便好.

  7. Javascript: 截取字符串多出来并用省略号[...]显示

    /背景知识/ substring 方法用于提取字符串中介于两个指定下标之间的字符 substring(start,end) 开始和结束的位置,从零开始的索引 参数描述 start 必需.一个非负的整数 ...

  8. HDU 3622 Bomb Game(2-sat)

    HDU 3622 Bomb Game 题目链接 题意:求一个最大半径,使得每一个二元组的点任选一个,能够得到全部圆两两不相交 思路:显然的二分半径,然后2-sat去判定就可以 代码: #include ...

  9. XMPP通讯开发-服务器好友获取以及监听状态变化

    在 XMPP通讯开发-好友获取界面设计   我们设计了放QQ的列表功能,这里我们获取我们服务器上的 数据. 这一部分知识我们可以查看smack_3_3_0/smack_3_3_0/documentat ...

  10. VMware vSphere 5.5的12个更新亮点(1)

    [IT专家网虚拟化]在VMworld 2013大会上发布的VMware vSphere 5.5版本提供的增强和改进,横跨从hypervisor到管理整个堆栈,提升了VMware的性能.可伸缩性和可用性 ...