IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> 【python】CodingGames Shadows of the Knight - Episode 1 -> 正文阅读

[Python知识库]【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 -= 1
    elif bomb_dir == "UR":
        x0 += 1
        y0 -= 1
    elif bomb_dir == "R":
        x0 += 1
    elif bomb_dir == "DR":
        x0 += 1
        y0 += 1
    elif bomb_dir == "D":
        y0 += 1
    elif bomb_dir == "DL":
        x0 -= 1
        y0 += 1
    elif bomb_dir == "L":
        x0 -= 1
    else:
        x0 -= 1
        y0 -= 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 - 1
        y0 = (y_min + y_max) // 2
    elif bomb_dir == "UR":
        x_min = x0 + 1
        y_max = y0 - 1
        x0 = (x_min + x_max) // 2
        y0 = (y_min + y_max) // 2
    elif bomb_dir == "R":
        x_min = x0 + 1
        x0 = (x_min + x_max) // 2
    elif bomb_dir == "DR":
        x_min = x0 + 1
        y_min = y0 + 1
        x0 = (x_min + x_max) // 2
        y0 = (y_min + y_max) // 2
    elif bomb_dir == "D":
        y_min = y0 + 1
        y0 = (y_min + y_max) // 2
    elif bomb_dir == "DL":
        x_max = x0 - 1
        y_min = y0 + 1
        x0 = (x_min + x_max) // 2
        y0 = (y_min + y_max) // 2
    elif bomb_dir == "L":
        x_max = x0 - 1
        x0 = (x_min + x_max) // 2
    else:
        x_max = x0 - 1
        y_max = y0 + 1
        x0 = (x_min + x_max) // 2
        y0 = (y_min + y_max) // 2

    # the location of the next window Batman should jump to.
    print("%d %d" %(x0, y0))

在Tower中老爷反复横跳的样子让我幻视SGD……

总结

本质上是个二分问题。

  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2022-06-29 19:00:05  更:2022-06-29 19:02:19 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年4日历 -2024/4/19 23:42:47-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码