# 代码（迷你）高尔夫

50

``````      ____       ____ _
__/    \     /    U \
__/        \   /        \_
\_/
``````

• `_` 降低速度 `1`
• `/` 降低速度 `5`
• `\` 通过增加速度 `4`

## 测试用例

``````Input: 27
____       ____ _
__/    \     /    U \
__/        \   /        \_
\_/
Output: true

----------

Input: 26
____       ____ _
__/    \     /    U \
__/        \   /        \_
\_/
Output: false

----------

Input: 1

U
Output: true

----------

Input: 1
_
U
Output: false

----------

Input: 22

/U
/
/
/
\/
Output: true

----------

Input: 999
_       _
\     /
\   /
\ /
U
Output: true

----------

Input: 5
/
/U
Output: false

----------

Input: 9

/\/\/\/\/U
Output: false

----------

Input: 16

_/\                                         _
\      __       /\/\/\                  /
\    /  \     /      \                /
\__/    \   /        \____________ _/
\_/                      U

Output: true
``````

1

Cyoce

1

Zach Gates

24

ezrast

2

Leif Willerts '16

SnoringFrog

17

# Pyth，27个字节

``````.Am<sXsd"_\ /"[1_4Z5)Q._C.z
``````

``````.Am<sXsd"_\ /"[1_4Z5)Q._C.z
Implicit: Z = 0, Q = eval(input())
Q is the initial power.
.z    Take all input, as a list of lines.
C      Transpose, giving all columns.
._       Form all prefixes.
m                            Map over the prefixes.
sd                       Concatenate the prefix.
X  "_\ /"[1_4Z5)          Change '_' to 1, '\' to -4, ' ' to 0, and '/' to 5.
In particular, 'U' is left unchanged.
If all elements were numbers,
this results in the total change in power.
If there was a 'U', it results in a string.
<                 Q         If the previous result was a number, this compares
it with the initial input to see if the ball is
still rolling.
If the previous result was a string, this slices off
the first Q characters, which always has a truthy
result.
.A                             Test whether all of the prefixes mapped to a thruthy
result.
``````

flindeberg '16

@flindeberg那不是它的工作原理。该`< ... Q`工程作为一个数字比较起来，直到孔，而不是一个切片。在漏洞之后，重要的是结果是真实的。
isaacg '16

14

``````import Data.List
g"_"=1
g"/"=5
g _= -4
f n=all(>0).scanl(-)n.map g.fst.span(/="U").(>>=words).transpose.lines
``````

``````*Main> f 27 "      ____       ____ _   \n   __/    \\     /    U \\  \n__/        \\   /        \\_\n            \\_/           "
True
*Main> f 26 "      ____       ____ _   \n   __/    \\     /    U \\  \n__/        \\   /        \\_\n            \\_/           "
False
``````

``````                            lines  -- split into list of lines at nl
transpose   -- transpose
(>>=words)       -- turn each line into words (i.e. remove spaces)
fst.span(/="U")        -- take all words up to but excluding "U"
map g                     -- turn each word into the speed modifier
scanl(-)n                      -- build list of partial sums starting with n
--   note: speed modifiers are negative so we
--   use (-) with scanl to build sums
all(>0)                            -- return true if all sums are greater than 0
``````

7

# Ruby，104个 87个字符

``````->s,t{t.lines.map(&:bytes).transpose.map{|o|(c=o.max)==85||s<0?break: s+=c*3%14-6}
s>0}``````

``````2.1.5 :001 > track = '      ____       ____ _
2.1.5 :002'>    __/    \     /    U \
2.1.5 :003'> __/        \   /        \_
2.1.5 :004'>             \_/
2.1.5 :005'> '
=> "      ____       ____ _   \n   __/    \\     /    U \\  \n__/        \\   /        \\_\n            \\_/           \n"

2.1.5 :006 > ->s,t{t.lines.map(&:bytes).transpose.map{|o|(c=o.max)==85||s<0?break: s+=c*3%14-6};s>0}[27, track]
=> true

2.1.5 :007 > ->s,t{t.lines.map(&:bytes).transpose.map{|o|(c=o.max)==85||s<0?break: s+=c*3%14-6};s>0}[26, track]
=> false
``````

6

# Japt，38个字节

``````Vz r"%s|U[^]*" ¬e@UµX¥'_?1:X¥'/?5:-4 ¬
``````

Try it here!

# 说明

isaacg

Cyoce

Mama Fun Roll

J Atkin

ETHproductions 2016年

6

## CJam，40 39字节

``````liqN/:.e>'U/0="\_/"[4W-5]er{1\$+}/]:e<0>
``````

### 说明

``````li    e# Read power and convert to integer.
qN/   e# Read course and split into lines.
:.e>  e# Flatten course by folding maximum over columns.
'U/   e# Split around the hole.
0=    e# Keep the first chunk.
"\_/"[4W-5]er
e# Replace \, _, / with 4, -1, 5, respectively.
{     e# For each of those costs...
1\$+ e#   Copy the previous power and add the cost.
}/    e# This leaves all partial sums on the stack.
]     e# Wrap them in an array.
:e<   e# Find the minimum.
0>    e# Check whether it's positive.
``````

5

## Retina，82 81 77 74 68 67 68字节

``````+`(?<=(.)*) (?=.*¶(?<-1>.)*(.))
\$2
\\
>>>>
+`>_|>{5}/|>¶

^>*U
``````

• 输入以一进制为单位表示，例如n `>`s，例如4 is `>>>>\n`。（这合法吗？）
• `+`(?<=(.)*) (?=.*¶(?<-1>.)*(.))` `\$2` -弄平路线-用下方的字符替换空格。

在此阶段之后，数据将如下所示：

``````>>>>>>>>>>>>>>>>>>>>>>>>>>
__/__/____\\\_///____U_\\_
__/__/    \\\_///    U \\_
__/        \\_//        \_
\_/
``````

我们只能在第一个之后忽略所有内容`U`，无论如何我们都不会到达那里。

• `>` 代表我们被允许迈出的一步或剩余的能量。
• `\`用四个替换每个`>`-倾斜会给我们带来额外的能量。
• 循环：有争议地删除`>_``>>>>>/`直到没有剩余。`_`s和`/`s 消耗能量。
• 最后，尝试匹配`^>*U`-检查我们是否可以达到`U`正能量（或没有能量）。
这将输出`0``1`

``````+`(?<=¶(.)*) (?=.*¶(?<-1>.)*(.))
\$2
^(>)+\n(?<-1>_|/(?<-1>){4}|\\(?<1>){5})+U
``````

1

Martin Ender

1

。–马丁·恩德

Kobi

3

## ES6，117个字节

``````(c,p)=>c.split`
`.map(s=>[...s.slice(0,c.match(/^.*U/m)[0].length-1)].map(c=>p+=c=='/'?-5:'    \\'.indexOf(c)))&&p>0
``````

``````function hole(course, power) {
width = course.match(/^.*U/m)[0].length - 1; // calculate width to hole
lines = course.split("\n");
for (i = 0; i < lines.length; i++) {
line = lines[i].slice(0, width); // ignore extraneous parts of the course
for (j = 0; j < line.length; j++) {
switch (line[j]) { // accumulate remaining power
case '/': power -= 5; break;
case '\\': power += 4; break;
case ' ': break;
default: power--; break;
}
}
}
return power > 0;
}
``````

@ՊՓԼՃՐՊՃՈԲՍԼ谢谢，我一直在努力优化速度...
Neil

3

# 的JavaScript（ES6），108个107 106字节

``````(p,c)=>[...(l=c.split`
`)[w=0]].map((_,i)=>l.map(t=>(g=t[i])-1|p<=0?0:p-=g>"]"?1:g>"U"?-4:g>"/"?w=1:5))&&w``````

## 说明

``````(p,c)=>
[...(l=c.split`
`)                          // l = array of lines
[w=0]]                    // w = true if the ball has entered the hole
.map((_,i)=>                // for each index i
l.map(t=>                 // for each line t
(g=t[i])                // g = the character at the current index
-1|p<=0?0:              // do nothing if g is a space or the ball has no speed left
p-=
g>"]"?1               // case _: subtract 1 from p
:g>"U"?-4             // case \: add 4 to p
:g>"/"?w=1            // case U: set w to true (it doesn't matter what happens to p)
:5                    // case /: subtract 5 from p
)
)
&&w                         // return w``````

## 测试

``````var solution = (p,c)=>[...(l=c.split`
`)[w=0]].map((_,i)=>l.map(t=>(g=t[i])-1|p<=0?0:p-=g>"]"?1:g>"U"?-4:g>"/"?w=1:5))&&w``````
``````Power = <input type="number" id="power" value="16" /><br />
<textarea id="course" rows="6" cols="50">_/\                                         _
\      __       /\/\/\                  /
\    /  \     /      \                /
\__/    \   /        \____________ _/
\_/                      U     </textarea><br />
<button onclick="result.textContent=solution(+power.value,course.value)">Go</button>
<pre id="result"></pre>``````

3

## Python（3.5）169160字节

``````def f(c,p):c=c.splitlines();l=len(c);f=lambda x,h,v:v if'U'==c[h][x]or v<1 else f(x+(h==l-1),(h+1)%l,v+{"_":-1,"\\":4,"/":-5," ":0}[c[h][x]]);return f(0,0,p)>0
``````

## 不打高尔夫球

c为路线，p为功率，v为速度，h为高度

``````def f(c,p):
c=c.splitlines()
l=len(c)
tmp = {"_":-1,"\\":4,"/":-5," ":0}
f=lambda x,h,v:v if'U'==c[h][x]or v<1 else f(x+(h==l-1),(h+1)%l,v+tmp[c[h][x]])
return f(0,0,p)>0
``````

## 用法

``````f(16,"_/\                                         _\n   \      __       /\/\/\                  / \n    \    /  \     /      \                /  \n     \__/    \   /        \____________ _/   \n              \_/                      U     ")
f(9,"/\/\/\/\/U")
``````

2

## Pyth，35个字节

``````VC.z=-Q@(1_4 5)x"_\\/"JrN6IqJ\U>Q_5
``````

``````                                    - Autoassign Q = eval(input())
- Autoassign .z = rest of input
VC.z                                - For N in zip(*.z)
=-Q                             - Q -= ...
JrN6          - Autoassign J to N.strip() (get rid of spaces)
@(1_4 5)x"_\\/"              - {"_":1, "\\": -4, "/": 5, "U":5}[J] ("U" isn't defined but that's what it is according to how str.index works)
IqJ\U     - If J == "U"
>Q_5 - print Q > -5 ()
``````

1

# Ruby，85个字符

``->i,s{s.lines.map(&:bytes).transpose.any?{|o|(c=o.max)==85||i<0||!(i+=c*3%14-6)};i>0}``

1

JavaScript中，266个 263 244字节

``````(s,a)=>{var f=(e,x)=>{for(var i=1;D=e[i][x],i<e.length;i++)if(D!=" ")return D},o=a.split(`
`),l=o.reduce((a,b)=>Math.max(a.length||a,b.length)),b="";for(i=0;i<l;i)b+=f(o,i++);for(i=0;b[i]!="U"&&s>0;i++)s-=b[i]=="_"?1:b[i]=="/"?5:-4;return s>0}
``````

``````(s,a)=>{
var f=(e,x)=>{
for(var i=1;D=e[i][x],i<e.length;i++)
if(D!=" ")
return D
},
o=a.split(`
`),
l=o.reduce((a,b)=>Math.max(a.length||a,b.length)),
b="";
for(i=0;i<l;)
b+=f(o,i++);
for(i=0;b[i]!="U"&&s>0;i++)
s-=b[i]=="_"?1:b[i]=="/"?5:-4;
return s>0
}
``````

``````var o = (s,a)=>{var f=(e,x)=>{for(var i=1;D=e[i][x],i<e.length;i++)if(D!=" ")return D},o=a.split(`
`),l=o.reduce((a,b)=>Math.max(a.length||a,b.length)),b="";for(i=0;i<l;)b+=f(o,i++);for(i=0;b[i]!="U"&&s>0;i++)s-=b[i]=="_"?1:b[i]=="/"?5:-4;return s>0}

o(27, `
____       ____ _
__/    \\     /    U \\
__/        \\   /        \\_
\\_/           `); // will return true
``````

user49328 '16

1

# Java，219字节

``boolean p(int v,String c){int z=c.length(),f[]=new int[z],e,i,k;for(String r:c.split("\n"))for(i=-1;++i<r.length();)if((e=r.charAt(i))>32)f[i]=e;for(i=-1,e=0;++i<z&v>0;)v-=(k=f[i])>94?1:k>91?-4:k>84?(e=1):5;return 0<e;}``
• 平整过程，因为y坐标无关紧要，不幸的是Java没有垂直修剪。它也没有字符串转置。

• 遍历平坦的路线并跟踪球速。

1

# 八度，111110字节

``````function g(v,s) A([95,47,92])=[1,5,-4];all(v>cumsum(A(m=max(cat(1,strsplit(s,'\n'){:}),[],1)))(1:find(m==85)))
``````

### 说明：

• 在换行符上拆分输入，然后将烦人的单元格数组转换为矩阵
• 通过找到`max`每一列的来展平矩阵
• 地图中的字符`'_/\'``[1, 5, -4]`（所有其他字符小于`'_'`映射到`0`
• 计算映射数组的所有元素的累积总和
• 输出`True`如果从过程到杯的开始所有累计总和小于起始速度（`False`否则）。

``````s9 =
/\
/  \
_/    \
\
\
U

g(11,s9) %False
ans = 0
g(17,s9) %True
ans =  1
``````

``````s10 =
_
/ U\
/    \
\
\
\
\
\_

>> g(11,s10)
ans = 0
>> g(12,s10)
ans =  1
``````

Erwan

@Erwan但是我确实删除了`U`。之后的字符。那是做什么的`(1:find(m==85))`；它需要从第一个索引到的位置的子数组`U`。我将以几个初始速度来检查您的测试用例，然后再与您联系。

Erwan

@Erwan我已经添加了您建议的两个测试用例。请看一下结果是否符合您的期望。如果您在MATLAB中尝试此操作，那么您将无法运行它，因为它使用的某些索引仅在Octave中有效。您将必须将结果分配给`cumsum`中间变量，然后将其用于最终比较`all(v>tmp(1:find(m==85)))`

Erwan 2016年

0

## C，629字节

``````#include <string.h>
#include <stdlib.h>
#include <string.h>

bool swing(char *c, unsigned int p)
{
char *olc = calloc(strlen(c), 1);
int x = 0;
char *n = c;

while(1) {
if(*n == '\0')  break;
else if(*n == ' ') x += 1;
else if(*n == '\n') x = 0;
else {
olc[x] = *n;
x += 1;
}
n++;
}

int hd = 0;
for(char *i = olc; i != strchr(olc, 'U'); i++) {
if(*i == '_') hd += 1;
else if(*i == '/') hd += 5;
else hd -= 4;
}

free(olc);
if(hd < p) return 1;
return 0;
}
``````

``````bool swing(char *course, unsigned int power)
{
const size_t course_len = strlen(course);
char *one_line_course = calloc(course_len, sizeof(char));
assert(one_line_course);
int x_pos = 0;
char *next = course;

//Convert to one line representation
while(1) {
if(*next == '\0') {
break;
}
else if(*next == ' ') {
x_pos += 1;
}
else if((*next == '\n') || (*next == '\r')) {
x_pos = 0;
}
else {
one_line_course[x_pos] = *next;
x_pos += 1;
}
next++;
}

//Calculate power vs distance
const char *hole_location = strchr(one_line_course, 'U');
int hole_distance = 0;
for(char *i = one_line_course; i != hole_location; i++) {
if(*i == '_') {
hole_distance += 1;
}
else if(*i == '/') {
hole_distance += 5;
}
else {
hole_distance -= 4;
}
}

free(one_line_course);
if(hole_distance < power) {
return true;
}
else {
return false;
}
}
``````

Toby Speight

0

# Python中，212个201 188 143字节

``````def g(c,p):
o=[''.join(x).split()[0] for x in zip(*c.split('\n'))]
t={"_":1,"/":5,"\\":-4}
for v in o:
if v=="U" or p<1:return p>0
p-=t[v]
``````

``````def g(course,power):
course=course.split('\n') # split into lines
course=zip(*course)

#transpose and flatten course, then remove spaces
one_line_course=[''.join(x).split[0] for x in zip(*course)]

terrain_values={"_":1,"/":5,"\\":-4}
for char in one_line_course:
if char=="U" or power<1:
return power>0 # true when power remains, false otherwise
power-=terrain_values[char]
``````

Erwan 2016年

Erwan

@Erwan如果行的长度不一样（短行的尾部空格匹配长行），则第一个技巧不起作用。由于帖子中说“课程可以有选择地用空格填充”，所以我不确定我们可以假设这是真的。我已经在评论中问过这个问题。
SnoringFrog