首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更热还是更冷:寻宝

更热还是更冷:寻宝
EN

Code Golf用户
提问于 2014-09-05 22:58:12
回答 6查看 1.2K关注 0票数 9

有一种儿童游戏,通常被称为“哈克巴克尔豆茎”,是与两个玩家一起玩的。以下是对游戏进行方式的简要描述:

  1. 一个玩家被指定为“隐藏者”,另一个被指定为“探索者”。
  2. 探索者走出房间,而隐藏者隐藏一个小的,预选的物品,“宝藏”。
  3. 然后,探索者试图寻找目标,而探索者给出有用的提示:。
    • 如果探索者正在接近宝藏,藏匿者会喊出“温暖!”
    • 如果探索者离开了宝藏,藏匿者会喊出“更酷!”

  4. 一旦探索者找到宝藏,他们就会宣布他们已经找到了。

你的孩子想让你和他们玩这个游戏,但是你却忙着在codegolf.SE上回答问题。所以,你决定写一个程序和他们一起玩游戏。然而,您希望使用尽可能少的时间键入,所以您试图使程序尽可能少的字符。

我们可以将游戏所处的房间定义为一个二维的圆环方场。坐标0,0是左下角,坐标99,99是右上角。宝藏放置在n,m的某个位置,其中nm都是0到99包含的正整数。

您的程序将使用其内置的用户输入功能(例如prompt()raw_input()等)从播放器获得输入。如果您选择的语言没有用户输入函数,则从STDIN获取输入。游戏的工作方式如下:

  1. 程序“隐藏”宝藏在一个位置n,m
  2. 程序提示探索者输入初始搜索位置。输入将以x y形式出现,其中xy是正整数。
  3. 如果初始搜索位置x,y等于宝藏的位置n,m并终止,则程序输出“正确”。否则:
  4. 这个计划会促使探索者移动。输入以a b形式出现,其中ab是可能为负数的整数。这表示导引头移动的方向矢量(a是x方向,b是y方向)。
  5. 如果探索者的最终位置在宝藏处,程序输出“正确”并终止。否则:
  6. 程序输出“冷却器”,如果探索者正在远离宝藏,或“更热”,如果他们正在向宝藏移动。
  7. 进入第四步。

“搬走”和“走向”这两个词可能有些矛盾。对于这一挑战,如果探索者移动后的位置比他们移动前的位置更接近宝藏,那么他们正在向宝藏移动。否则,他们就要搬走了。(是的,这确实意味着如果得到的位置和先前的位置距离相同,程序应该输出“冷却器”)。

这是密码高尔夫,所以最短的代码获胜。如果规格不明确,请提出问题。

EN

回答 6

Code Golf用户

发布于 2014-09-06 09:46:50

Python 3- 238字节

代码:

代码语言:javascript
复制
from random import*
r=randint;a=100;X=r(0,a);Y=r(0,a);d=0;F=lambda:((X-x%a)**2+(Y-y%a)**2)**0.5;i=lambda:map(int,input().split());x,y=i()
while F():
 if d:print(["cooler","hotter"][d<D])
 D=F();Z,K=i();x+=Z+a;y+=K+a;d=F()
print("correct")

未高尔夫球:

代码语言:javascript
复制
from random import*

treasure_x = random.randint(0,100)
treasure_y = random.randint(0,100)
distance = lambda:((treasure_x - x % 100) ** 2 + (treasure_y - y % 100) ** 2) ** 0.5
x, y = map(int, input("Coordinates in the form x y: ").split())
new_distance = 0

while distance():
    if new_distance:
        if new_distance < prev_distance:
            print("hotter")
        else:
            print("cooler")
    prev_distance = distance()
    dx, dy = map(int, input("Move in the form dx dy: ").split())
    x = (dx + x) % 100
    y = (dy + y) % 100
    new_distance = distance()

print("correct")

样本运行:

代码语言:javascript
复制
$ python hotter_colder.py 
50 50
10 0
cooler
-10 0
hotter
-10 0
hotter
-10 0
hotter
-10 0
hotter
-10 0
cooler
5 0
hotter
1 0
hotter
1 0
hotter
1 0
hotter
1 0
hotter
1 0
cooler
-1 0
hotter
0 10
cooler
0 -10
hotter
0 -10
hotter
0 -10
cooler
0 5
hotter
0 1
hotter
0 1
correct

我不会说我寻找宝藏的策略是特别快速.

票数 2
EN

Code Golf用户

发布于 2014-09-06 23:54:47

APL,86 chars

代码语言:javascript
复制
h←?2⍴s←100⋄1{h≡n←s|s+⍵+⎕:⎕←"correct"⋄⍺:0∇n⋄⎕←(</+/⊃×⍨n⍵-¨⊂h)⌷"cooler" "hotter"⋄0∇n}0 0

距离计算不是环绕,而是移动。

Ungolfed:

代码语言:javascript
复制
h←?2⍴s←100                  ⍝ generate random starting point
1{                          ⍝ loop starting with ⍺←1 (1 if first loop) and ⍵←0 0 (position)
    n←s|s+⍵+⎕               ⍝ n←new position, ⍵ plus the move read from input, modulo 100
    n≡h: ⎕←"correct"        ⍝ check for end condition
    ⍺: 0∇n                  ⍝ if first loop, skip the rest and ask for a move again
    t←</+/⊃×⍨n⍵-¨⊂h         ⍝ t←1 if n is closer to h than ⍵ (squared distance)
    ⎕←t⌷"cooler" "hotter"   ⍝ output thermal gradient label
    0∇n                     ⍝ loop with new position
}0 0

示例:

代码语言:javascript
复制
⎕:
      22 33
⎕:
      2 6
hotter
⎕:
      0 1
cooler
⎕:
      0 ¯3
correct
票数 2
EN

Code Golf用户

发布于 2014-09-07 03:59:08

Python2.7,227

代码语言:javascript
复制
from random import*
r=randint
h=100
n=r(0,h)
m=r(0,h)
i=lambda:map(int,raw_input().split())
x,y=i()
d=lambda:(x%h-n)**2+(y%h-m)**2
e=d()
while e:
 p=e;a,b=i();x+=a;y+=b;e=d()
 if e:print e<p and'hotter'or'cooler'
print'correct'

我得到了输入函数和在距离计算中应用模块的想法,而不是根据matsjoyce的答案进行位置更新。

我们只需要比较的距离:我们在确切的位置吗?我们比以前更亲近了吗?对于这两种情况,我们得到的结果和比较距离的平方是一样的。要得到实际距离所需的平方根计算是不必要的。

未高尔夫球:

代码语言:javascript
复制
import random

h = 100 # height (and width) of the square grid

# location of item
n = random.randint(0, h)
m = random.randint(0, h)

def input_pair():
    return map(int, raw_input().split())

x,y = input_pair()

def distance_squared():
    return (x % h - n)**2 + (y % h - m)**2

er = distance_squared()
while er:
    previous_er = er
    a,b = input_pair()
    x += a
    y += b
    er = distance_squared()
    if er:
        print 'hotter' if er < previous_er else 'cooler'
print 'correct'

样本运行:

代码语言:javascript
复制
50 50
20 0
hotter
20 0
cooler
-20 0
hotter
10 0
cooler
-10 0
hotter
-1 0
hotter
-1 0
hotter
-5 0
cooler
5 0
hotter
-1 0
cooler
1 0
hotter
1 0
cooler
-1 0
hotter
0 10
hotter
0 10
hotter
0 10
cooler
0 -5
hotter
0 -1
hotter
0 -1
hotter
0 -1
correct
票数 2
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codegolf.stackexchange.com/questions/37380

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档