有一种儿童游戏,通常被称为“哈克巴克尔豆茎”,是与两个玩家一起玩的。以下是对游戏进行方式的简要描述:
你的孩子想让你和他们玩这个游戏,但是你却忙着在codegolf.SE上回答问题。所以,你决定写一个程序和他们一起玩游戏。然而,您希望使用尽可能少的时间键入,所以您试图使程序尽可能少的字符。
我们可以将游戏所处的房间定义为一个二维的圆环方场。坐标0,0是左下角,坐标99,99是右上角。宝藏放置在n,m的某个位置,其中n和m都是0到99包含的正整数。
您的程序将使用其内置的用户输入功能(例如prompt()、raw_input()等)从播放器获得输入。如果您选择的语言没有用户输入函数,则从STDIN获取输入。游戏的工作方式如下:
n,m。x y形式出现,其中x和y是正整数。x,y等于宝藏的位置n,m并终止,则程序输出“正确”。否则:a b形式出现,其中a和b是可能为负数的整数。这表示导引头移动的方向矢量(a是x方向,b是y方向)。“搬走”和“走向”这两个词可能有些矛盾。对于这一挑战,如果探索者移动后的位置比他们移动前的位置更接近宝藏,那么他们正在向宝藏移动。否则,他们就要搬走了。(是的,这确实意味着如果得到的位置和先前的位置距离相同,程序应该输出“冷却器”)。
这是密码高尔夫,所以最短的代码获胜。如果规格不明确,请提出问题。
发布于 2014-09-06 09:46:50
代码:
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")未高尔夫球:
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")样本运行:
$ 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我不会说我寻找宝藏的策略是特别快速.
发布于 2014-09-06 23:54:47
h←?2⍴s←100⋄1{h≡n←s|s+⍵+⎕:⎕←"correct"⋄⍺:0∇n⋄⎕←(</+/⊃×⍨n⍵-¨⊂h)⌷"cooler" "hotter"⋄0∇n}0 0距离计算不是环绕,而是移动。
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⎕:
22 33
⎕:
2 6
hotter
⎕:
0 1
cooler
⎕:
0 ¯3
correct发布于 2014-09-07 03:59:08
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的答案进行位置更新。
我们只需要比较的距离:我们在确切的位置吗?我们比以前更亲近了吗?对于这两种情况,我们得到的结果和比较距离的平方是一样的。要得到实际距离所需的平方根计算是不必要的。
未高尔夫球:
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'样本运行:
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
correcthttps://codegolf.stackexchange.com/questions/37380
复制相似问题