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. mysql出现错误“ Every derived table must have its own alias”

    Every derived table must have its own alias 这句话的意思是说每个派生出来的表都必须有一个自己的别名 一般在多表查询时,会出现此错误. 因为,进行嵌套查询的时 ...

  2. SSH转发机制

    第一部分 概述 当你在咖啡馆享受免费 WiFi 的时候,有没有想到可能有人正在窃取你的密码及隐私信息?当你发现实验室的防火墙阻止了你的网络应用端口,是不是有苦难言?来看看 SSH 的端口转发功能能给我 ...

  3. zabbix 添加自定义key

    vim /etc/zabbix/zabbix_agentd.conf UserParameter=zjzc.login,/bin/sh /usr/sbin/get_login.sh UserParam ...

  4. startkde出现$DISPLAY is not set or cannot connect to the X server

    #startkde $DISPLAY is not set or cannot connect to the X server 解决: xinit /usr/bin/startkde --displa ...

  5. linux 下的对拍

    搞了一上午终于弄好了一个对拍,估计以后调试会方便很多. #!/bin/bash while true; do ./makedate>tmp.in ./XXXXX<tmp.in>tmp ...

  6. H5页面音频自动播放问题

        最近有这么一个需求,需要在手机加载一个页面的时候,自动播放音乐资源.一般情况下,这个问题也就解决了,但是要保证各种手机上表现一致,那就相当困难了,至少要费点儿周折.       下面有三种常规 ...

  7. javascript笔记4之运算符

    /* var box = 100; --box; //前置递增,box = box +1 alert(box);//99 var box = 100; var age = ++box; //先box累 ...

  8. LoadRunner测试下载功能点脚本(方法二)

    在上一篇<LoadRunner下载功能点脚本(方法一)>中,实现的脚本仅是录制下载功能点的脚本,现在性能需求的场景更改如下: 性能需求:对系统某页面中,点击下载并将下载文件保存到本地电脑的 ...

  9. nagios和zabbix自定义监控脚本

    一. 自定义nagios监控脚本1. 在客户端上创建脚本/usr/local/nagios/libexec/check_disk.shvim  /usr/local/nagios/libexec/ch ...

  10. 查看linux版本和内核信息

    一.查看Linux内核版本命令(两种方法): 1.cat /proc/version [root@localhost ~]# cat /proc/versionLinux version 2.6.32 ...