【python】CodingGames Shadows of the Knight - Episode 1
CodingGames 之 蝙蝠侠救人
描述:
你是蝙蝠侠,你将通过使用你的抓斗枪从一个窗口跳到另一个窗口来寻找给定建筑物上的人质。您的目标是跳到人质所在的窗口以解除炸弹的武装。不幸的是,在炸弹爆炸之前你的跳跃次数是有限的……在每次跳跃之前,热信号装置会根据您当前的位置为您提供炸弹的方向:(八个)
初始化输入:
第 1 行: 2 个整数W H. 这 (W,H) 对将建筑物的宽度和高度表示为多个窗户。
第 2 行: 1 个整数n,它代表你在炸弹爆炸之前可以跳的次数。
第 3 行: 2 个整数X0 Y0,代表你的起始位置。
这题简单做法是直接按照提供方向一步一步前进,但是显而易见效率很低。
做法一(能完成75%):步步紧逼
import sys
import math# w: width of the building.
# h: height of the building.
w, h = [int(i) for i in input().split()]
n = int(input()) # maximum number of turns before game over.
x0, y0 = [int(i) for i in input().split()]# game loop
while True:bomb_dir = input() # the direction of the bombs from batman's current location (U, UR, R, DR, D, DL, L or UL)if bomb_dir == "U":y0 -= 1elif bomb_dir == "UR":x0 += 1y0 -= 1elif bomb_dir == "R":x0 += 1elif bomb_dir == "DR":x0 += 1y0 += 1elif bomb_dir == "D":y0 += 1elif bomb_dir == "DL":x0 -= 1y0 += 1elif bomb_dir == "L":x0 -= 1else:x0 -= 1y0 -= 1# the location of the next window Batman should jump to.print("%d %d" %(x0, y0))
更好的办法是按边界范围收缩,因为老爷无所不能!哪都能跳,我们只要能保证最后范围收缩的中心点是目标点就好!
做法二: 反复横跳
设置
Xmin,Ymin,Xmax,Ymax
,分别表示(0,0)
点 和(W-1, H-1)
两个端点按照系统提供的方向更新
(Xmin,Ymin), (Xmax, Ymax)
的位置,让范围向目标靠拢比如人质在老爷右上方,就可以把xmin 和 ymax 边界设置在老爷的右上角
老爷每次跳在范围的中心点
import sys
import math# w: width of the building.
# h: height of the building.
w, h = [int(i) for i in input().split()]
n = int(input()) # maximum number of turns before game over.
x0, y0 = [int(i) for i in input().split()]
x_min, y_min = 0, 0
x_max, y_max = w - 1, h - 1# game loop
while True:bomb_dir = input() # the direction of the bombs from batman's current location (U, UR, R, DR, D, DL, L or UL)if bomb_dir == "U":y_max = y0 - 1y0 = (y_min + y_max) // 2elif bomb_dir == "UR":x_min = x0 + 1y_max = y0 - 1x0 = (x_min + x_max) // 2y0 = (y_min + y_max) // 2elif bomb_dir == "R":x_min = x0 + 1x0 = (x_min + x_max) // 2elif bomb_dir == "DR":x_min = x0 + 1y_min = y0 + 1x0 = (x_min + x_max) // 2y0 = (y_min + y_max) // 2elif bomb_dir == "D":y_min = y0 + 1y0 = (y_min + y_max) // 2elif bomb_dir == "DL":x_max = x0 - 1y_min = y0 + 1x0 = (x_min + x_max) // 2y0 = (y_min + y_max) // 2elif bomb_dir == "L":x_max = x0 - 1x0 = (x_min + x_max) // 2else:x_max = x0 - 1y_max = y0 + 1x0 = (x_min + x_max) // 2y0 = (y_min + y_max) // 2# the location of the next window Batman should jump to.print("%d %d" %(x0, y0))
在Tower中老爷反复横跳的样子让我幻视SGD……
总结
本质上是个二分问题。
【python】CodingGames Shadows of the Knight - Episode 1相关推荐
- 【python】图像映射:单应性变换与图像扭曲
[python]图像映射:单应性变换与图像扭曲 单应性变换(Homography) 图像扭曲(仿射变换) 图中图 分段仿射扭曲 单应性变换(Homography) 单应性变换(Homography)即 ...
- 【Python】函数图像绘制:二维图像、三维图像、散点图、心形图
[Python]函数图像绘制:二维图像.三维图像.散点图.心形图 所有需要用的包 二维图像 三维图像 散点图绘制 心形图绘制 所有需要用的包 from mpl_toolkits.mplot3d imp ...
- 【python】 OSError:sift not found 问题解决
[python] OSError: XXXX.sift not found 问题解决 在python环境下通过sift描述子对图像进行特征匹配时出现错误(心累):mpire.sift not foun ...
- 【Python】Python中令人头疼的变量作用域问题,终于弄清楚了
[Python]Python中令人头疼的变量作用域问题,终于弄清楚了_fengdu78的博客-CSDN博客 [Python]Python中令人头疼的变量作用域问题,终于弄清楚了_fengdu78的博客 ...
- 【Python】将字典(dict)转化为Dataframe
[Python]将字典(dict)转化为Dataframe_张欣的博客-CSDN博客_python字典转dataframe dictory = {'a':1,'b':2} df = pd.DataFr ...
- 【Python】Numpy扩充数组函数之repeat和tile用法
2019独角兽企业重金招聘Python工程师标准>>> [Python]Numpy扩充数组函数之repeat和tile用法,有需要的朋友可以参考下. 用repeat和tile扩充数组 ...
- 11210怎么等于24_【Python】鸡兔同笼怎么“妙解”?
上一节: <<[python] 学了编程.我写的第二个游戏...>> 鸡兔同笼,是中国古代著名典型趣题之一,记载于<孙子算经>之中.鸡兔同笼问题,是小学奥数的常见题 ...
- 【Python】解决Django Admin管理界面样式表(CSS Style)丢失问题
[Python]解决Django Admin管理界面样式表(CSS Style)丢失问题 参考文章: (1)[Python]解决Django Admin管理界面样式表(CSS Style)丢失问题 ( ...
- 【python】-- try except (异常捕获)、断言
[python]-- try except (异常捕获).断言 参考文章: (1)[python]-- try except (异常捕获).断言 (2)https://www.cnblogs.com/ ...
最新文章
- Redeclared ‘list_b‘ defined above without usage
- 斯皮尔曼相关系数范围_数据的相关系数
- python能做软件开发吗-学习Python软件开发能做什么?
- [Spring cloud 一步步实现广告系统] 13. 索引服务编码实现
- K均值算法matlab代码实现
- linux自动归档,Linux之归档、压缩
- 游戏中基于物理的渲染(一)
- LeetCode 39. 组合总和(回溯+剪枝)
- [网络安全自学篇] 五十六.i春秋老师分享小白渗透之路及Web渗透技术总结
- jsp实现文件下载,out = pageContext.pushBody();out.close();不用写到jsp中
- 创业19年的湖南竞网如何拥抱数字化转型,按下成长加速键?
- 软件测试中英文术语对照表
- 《可以量化的管理学》目录
- gps+wifi+mobile 5的手机参数对比
- 记离职同事给我们的建议之一:关于人员培养方面的思考
- Perl Regular Expression Syntax Perl的正则表达式语法
- 2.5寸硬盘和3.5寸硬盘的区别和适用场景
- 加入2b2t服务器显示过期,我的世界:2B2T服务器罪魁祸首是他,为一己之私毁掉了整个服务器...
- LAMP+DISCUZ论坛
- 亚马逊云购买和配置苹果MacOs系统的云主机
热门文章
- 太空建站、探测火星、发射次数“50+”——2021中国航天这些大事值得铭记
- 电影、电视和游戏中的帧率
- 有关学习参与度的计算
- 关于自媒体运营变现经验分享
- 把吃出来的病吃回去 张悟本_吃荔枝会得“荔枝病”?医生说吃它真的有讲究!...
- Springboot单元测试mysql_Springboot Mybatis-Plus数据库单元测试实战(三种方式)
- GPIO口,上下拉电阻与推挽输出
- 做好flash手绘基本功,简单的手绘人物头像和眨眼动画
- Android中复制到剪切板
- 玩转华为ENSP模拟器系列 | 配置多段拼接场景下的伪线BFD示例