# 生成牙签序列

10

## 测试用例

``````1
\
\

5
\
/\
/\
/ /\
\/\/\ \ \
\ \ \/\/\
\/ /
\/
\/
\
``````

6

## CJam，99 93字节

``````"\ "_W%]{{Sf+W%z}4*2ew{2fewz{_sa"\//\\"4S**4/^_,3={:.e>W%2/\}&;}%z{\)a@.e>+}:Ff*}%{F}*}q~(*N*
``````

``````            \
/\
/\
/ /\
\/\/\ \ \
\ \/\/\/\
/\/\/
/ /\ \
\    /\/\ \     \
/\  /  \/\/\    /\
/\  /\  /\/  \ \/\
/ /\/ /\/ /\  /\ \/\
\/\/\/\/\/\/\ \/\ \/\ \ \
\ \ \/\ \/\ \/\/\/\/\/\/\
\/\ \/  \/ /\/ /\/ /
\/\ \  /\/  \/  \/
\/    \/\/\  /  \/
\     \ \/\/    \
\ \/ /
/\/\/
\/\/\/\ \
\ \ \/\/\
\/ /
\/
\/
\
``````

``````"\ "_W%]{{Sf+W%z}4*2few2ew::.{+_a"\//\\"4S**4/^_,3={:.e>W%\}&;2/}:z{\)a@.e>+}ff*{\)a@..e>+}*}ri(*N*
``````
``````"\ "_W%]{{Sf+W%z}4*2ew{2fewz{_sa"\//\\"4S**4/^_,3={:.e>W%2/\}&;}%{.{\)a@.e>+}}*}%{\)a@.e>+}*}q~(*N*
``````

1

# JavaScript（ES6），263个字节

``````n=>(o=(o=[..." ".repeat(n*2)]).map(_=>o.map(_=>s=c=" ")),(g=a=>s++<n&&g(q=[],a.map(p=>o[p[4]][p[3]]==c&&(o[y=p[1]][x=p[0]]=o[y-1][(b=+p[2])?x-1:x+1]="/\\"[b],q.push([x,++y,!b,b?x+1:x-1,y],[b?x-=2:x+=2,y-2,!b,x,y-3])))))([[n,n,1,n,n]]),o.map(r=>r.join``).join`
`)``````

## 说明

``````n=>(                           // n = desired stage

o=                           // o = output grid
//     [ [ "\\", " " ], [ " ", "\\" ], etc... ]
(o=[..." ".repeat(n*2)])   // create an array the size of the grid
.map(_=>o.map(_=>          // loop over it and return the output grid
s=                       // s = current stage (" " acts the same as 0)
c=                     // c = blank character
" "                  // initialise each element to " "
)),

(g=                          // g = compute stage function
a=>                        // a = positions to place toothpicks
//     [ x, y, isBackslash, checkX, checkY ]
s++<n&&                  // do nothing if we have reached the desired stage
g(q=[],                  // q = positions for the next stage's toothpicks
a.map(p=>              // p = current potential toothpick position
o[p[4]][p[3]]==c&&(  // check the position to make sure it is clear

o[y=p[1]][x=p[0]]= // place bottom toothpick, x/y = position x/y
o[y-1][            // place top toothpick
(b=+p[2])        // b = isBackslash
?x-1:x+1         // top toothpick x depends on direction
]="/\\"[b],        // set the location to the appropriate character

// Add the next toothpick positions
q.push([x,++y,!b,b?x+1:x-1,y],
[b?x-=2:x+=2,y-2,!b,x,y-3])
)
)
)
)([[n,n,1,n,n]]),            // place the initial toothpicks
o.map(r=>r.join``).join`
` // return the grid converted to a string
)``````

## 测试

``````Stages: <input type="number" oninput='result.innerHTML=(

n=>(o=(o=[..." ".repeat(n*2)]).map(_=>o.map(_=>s=c=" ")),(g=a=>s++<n&&g(q=[],a.map(p=>o[p[4]][p[3]]==c&&(o[y=p[1]][x=p[0]]=o[y-1][(b=+p[2])?x-1:x+1]="/\\"[b],q.push([x,++y,!b,b?x+1:x-1,y],[b?x-=2:x+=2,y-2,!b,x,y-3])))))([[n,n,1,n,n]]),o.map(r=>r.join``).join`
`)

)(+this.value)' /><pre id="result"></pre>``````

1

# Ruby，151个字节

``````->n{m=n*2
s=(' '*m+\$/)*m
l=m*m+m
s[l/2+n]=s[l/2-n-2]=?\\
(n*l-l).times{|j|(s[i=j%l]+s[i-m-2+2*k=j/l%2]).sum==124-k*45&&s[i-m-1]=s[i-1+2*k]="/\\"[k]}
s}
``````

``````f=->n{
m=n*2                                       #calculate grid height / width
s=(' '*m+\$/)*m                              #fill grid with spaces, separated by newlines
l=m*m+m                                     #calculate length of string s
s[l/2+n]=s[l/2-n-2]=?\\                     #draw the first toothpick
(n-1).times{|j|                             #iterate n-1 times
l.times{|i|                               #for each character in the string
(s[i]+s[i-m-2+2*k=j%2]).sum==124-k*45&& #if checksum of current character + character diagonally above indicates the end of a toothpick
s[i-m-1]=s[i-1+2*k]="/\\"[k]         #draw another toothpick at the end
}
}
s}                                            #return value = s

puts f[gets.to_i]
``````