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/
题目大意: 最多可以吃掉几枚敌方棋子
思路: 递归搜索, 模拟手工过程, 注意不要跳过棋子
#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的更多相关文章
- codeforces A. Rook, Bishop and King 解题报告
题目链接:http://codeforces.com/problemset/problem/370/A 题目意思:根据rook(每次可以移动垂直或水平的任意步数(>=1)),bishop(每次可 ...
- CF Rook, Bishop and King
http://codeforces.com/contest/370/problem/A 题意:车是走直线的,可以走任意多个格子,象是走对角线的,也可以走任意多个格子,而国王可以走直线也可以走对角线,但 ...
- bfs UESTC 381 Knight and Rook
http://acm.uestc.edu.cn/#/problem/show/381 题目大意:给你两个棋子:车.马,再给你一个n*m的网格,从s出发到t,你可以选择车或者选择马开始走,图中有一些障碍 ...
- [Swift]LeetCode999. 车的可用捕获量 | Available Captures for Rook
在一个 8 x 8 的棋盘上,有一个白色车(rook).也可能有空方块,白色的象(bishop)和黑色的卒(pawn).它们分别以字符 “R”,“.”,“B” 和 “p” 给出.大写字符表示白棋,小写 ...
- [rook] rook的控制流
以下是rook为一个pod准备可用块存储的过程: 1. rook operator运行,并且在k8s每台机器上运行一个rook agent的pod: 2. 用户创建一个pvc,并指定storagecl ...
- 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 ...
- rook 排错记录 + Orphaned pod found kube-controller-manager的日志输出
1.查看rook-agent(重要)和mysql-wordpress 的日志,如下: MountVolume.SetUp failed for volume "pvc-f002e1fe-46 ...
- kubespray 容器存储设备 -- rook ceph
1./root/kubespray/roles/docker/docker-storage/defaults/main.yml #在用kubespray部署集群是制定docker用什么设备 dock ...
- rook 入门理解
参考:https://my.oschina.net/u/2306127/blog/1830356?from=timeline 1.Rook通过一个操作器(operator)完成后续操作,只需要定义需要 ...
随机推荐
- universal image loader在listview/gridview中滚动时重复加载图片的问题及解决方法
在listview/gridview中使用UIL来display每个item的图片,当图片数量较多需要滑动滚动时会出现卡顿,而且加载过的图片再次上翻后依然会重复加载(显示设置好的加载中图片) 最近在使 ...
- mysql出现错误“ Every derived table must have its own alias”
Every derived table must have its own alias 这句话的意思是说每个派生出来的表都必须有一个自己的别名 一般在多表查询时,会出现此错误. 因为,进行嵌套查询的时 ...
- list排序成员函数对string对象与char*对象排序的差别
对list容器中的对象排序,不能使用sort()算法,只能采用其自身的排序函数sort().因为,算法sort()只支持随机存取的容器的排序,如vector等. 对基本数据对象list排序:成员函数s ...
- 【转】Win7+Ubuntu12.04.1硬盘安装错误及解决方案----不错
原文网址:http://blog.csdn.net/ys_073/article/details/8310115 前言: 说起来了,为了在Win7上进行硬盘安装Ubuntu浪费了整整一个晚上的时间.装 ...
- 【No system images installed for this target】的解决方式
打开eclipse,新建安卓SDK模拟器时,选择完Target之后,再选择CPU/ABI时,默认为No system images installed for this target. 且无法编辑: ...
- 利用Connect By构造数列
,) yymm ;
- cf442C Artem and Array
C. Artem and Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- programData
以.结束语句输入;
- 【hoj】1604 cable master
简单,二分法,可是在oj上交的时候会有精度的限制,所以仅仅能把数扩得大一点,并且在扩的时候为防止尾数会自己主动生成一些非零数,所以还是自己吧扩到的位置设置为0,还有输出时由于%.2lf会自己有4设5入 ...
- ngui点击与场景点击判断
注:NGUI 组件上加上 BoxCollider 并设置区域大小 public void OnMouseDown() { if (UICamera.hoveredObject == null) ...