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. smarty 从配置文件读取变量

    smarty变量分3种: Variables [变量] Variables assigned from PHP [从PHP分配的变量] Variables loaded from config fil ...

  2. BZOJ3713: [PA2014]Iloczyn

    3713: [PA2014]Iloczyn Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 206  Solved: 112[Submit][Status ...

  3. 2015第24周一Spring事务

    1. Spring事务管理简介 (1)Spring为多种不同类型的事务管理机制提供统一编程模型,这些事务管理模型包括JTA.JDBC.Hibernate.JPA和JDO. (2)Spring支持声明式 ...

  4. 关于as3实现对任何对象进行深刻复制的思考

    无意中看到关于as3深度复制思考的文章,觉得不错,于是转来记录以后用到可以参考. 转载来源:http://xmchcly.iteye.com/blog/1307425,下面是转文: 通过 ByteAr ...

  5. cf437D The Child and Zoo

    D. The Child and Zoo time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  6. window.open和window.close的使用详解

    有时候,我们想通过JS实现一个<a>的新开标签的效果,此时我们想到了window.open方法实现.那么window.open到底应该怎么使用呢?   我们知道window.open可以新 ...

  7. 关于bootstrap--列表(ol、ul)

    1.list-unstyled : 在<ol>(有序列表)</ol><ul>(无序列表)</ul>中加入class="list-styled& ...

  8. 【转】AAC ADTS格式分析

    1.ADTS是个啥 ADTS全称是(Audio Data Transport Stream),是AAC的一种十分常见的传输格式. 记得第一次做demux的时候,把AAC音频的ES流从FLV封装格式中抽 ...

  9. Javascript 文件操作(整理版)

    Javascript 文件操作 一.功能实现核心:FileSystemObject 对象 其实,要在Javascript中实现文件操作功能,主要就是依靠FileSystemobject对象.在详细介绍 ...

  10. [Javascript] lodash: memoize() to improve the profermence

    Link: https://lodash.com/docs#memoize Example: .service('UserPresenter', function(UserConstants){ va ...