首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有多少招?

有多少招?
EN

Code Golf用户
提问于 2018-11-26 06:29:50
回答 3查看 1.3K关注 0票数 17

给定棋盘上的两个不同位置和棋子类型,输出棋盘从一个位置到另一个位置所需的最小移动次数。

规则

给出的作品可以是国王,王后,鲁克,骑士和主教。(此输入可视为任意5个唯一字符)

这两个职位可以采取任何方便的形式,

代码语言:javascript
复制
Example:
a8 b8 c8 d8 ... h8
a7 b7 c7 d7 ... h7
...
...
a1 b1 c1 d1 ... h1

如果该块无法到达,则输出正整数以外的任何内容。

示例

代码语言:javascript
复制
i/p ---- o/p
King
a1,a4    3
a1,h6    7
b3,h5    6

Queen
a1,a4    1
a1,h6    2
b3,f7    1

Rook
a1,a4    1
a1,h6    2
h2,c7    2

Knight
a1,a4    3
a1,h6    4
b2,d3    1
b2,c3    2
b3,c3    3
a1,b2    4

Bishop
a1,a4    -1
a1,h6    2
b2,d3    -1
e1,h4    1
EN

回答 3

Code Golf用户

发布于 2018-11-26 16:27:14

Java (JDK),229个字节

代码语言:javascript
复制
(p,a,b,c,d)->{c^=a/4*7;a^=a/4*7;d^=b/4*7;b^=b/4*7;int x=c<a?a-c:c-a,y=d<b?b-d:d-b,z=(x^=y^(y=y<x?y:x))-y;return p<1?x:p<2?z*y<1?1:2:p<3?2-z%2:p<4?x+y<2?3:(a<c?a+b:c+d)+x<2|x==2&z<1?4:z+2*Math.ceil((y-z)/(y>z?3:4.)):z<1?1:~z*2&2;}

在网上试试!

解释

  • 董事会是一个0为基础的董事会。
  • 返回的值是一个整数,表示为双值.永远不会有十进制部分。

代码:

代码语言:javascript
复制
(p,a,b,c,d)->{                          // double-returning lambda.
                                        // p is the piece-type (0: king, 1: queen, 2: rook, 3: knight, 4: bishop)
                                        // a is the origin-X
                                        // b is the origin-Y
                                        // c is the destination-X
                                        // d is the destination-Y
 c^=a/4*7;a^=a/4*7;                     // Mirror board if origin is in the top part of the board
 d^=b/4*7;b^=b/4*7;                     // Mirror board if origin is in the left part of the board
 int x=c<a?a-c:c-a,                     // x is the X-distance between a and c
     y=d<b?b-d:d-b,                     // y is the Y-distance between b and d
     z=(x^=y^(y=y<x?y:x))-y;            // z is the delta between x and y
                                        // also, swap x and y if necessary so that x is the greater value.
               //    At this point,
               //     x      cannot be 0 (because the two positions are different)
               //     z<1    means the origin and destination are on the same diagonal
               //     y<1    means the origin and destination are on the same horizontal/vertical line
 return
  p<1?x:                                //  For a king, just take the max distance.
  p<2?z*y<1?1:2:                        //  For a queen, just move once if in direct line, or twice.
  p<3?2-z%2:                            //  For a rook, just move once if on the same horizontal or vertical line, or twice
  p<4?                                  //  For a knight, 
   x+y<2?3:                             //   Hardcode 3 if moving to the next horizontal/vertical square
   (a<c?a+b:c+d)+x<2|x==2&z<1?4:        //   Hardcode 4 if moving 2 cases in diagonal or one case in diagonal in a corner.
   z+2*Math.ceil((y-z)/(y>z?3:4.)):     //   Compute the number of moves necessary for the usual cases
  z<1?1:                                //  For a bishop, hardcode 1 if they are on the same diagonal
   ~z*2&2;                              //   Return 2 if they have the same parity else 0.
}

Credits

  • -2字节,感谢阿纳尔德,以及让我意识到我的所有角落-案例有一个问题。
票数 2
EN

Code Golf用户

发布于 2018-11-28 16:34:58

杰夫特,67字节

代码语言:javascript
复制
®ra
g[_rw}_â è}@=ã ü;@pUÌïVõ á ÈíaY})Ìde[TT]}a Ä}_è}_ra v *Zâ l}]gV

在网上试试!

这是一次很棒的经历。我从优秀的APL应答中学到了很多灵感。我怀疑有很多高尔夫仍然有可能,特别是在骑士的代码。

这些位置是[[x1,x2],[y1,y2]]格式的第一个输入。它在[[y1,y2],[x1,x2]]上也应该能很好地工作。作品选择是第二个输入,有0=king,1=queen,2=knight,3=rook,4=bishop。请注意,与APL答案相比,Knight和Rook是交换的。

解释:

代码语言:javascript
复制
®ra         :Turn absolute positions into relative movement and store in U
®           : For each of X and Y
 ra         : Get the absolute difference between the start position and the end position

g[...]gV    :Apply the appropriate function
 [...]      : A list of functions
      gV    : Get the one indicated by the second input
g           : Apply it to U

_rw}        :King function
 rw         : Get the maximum of X and Y

_â è}       :Queen function
 â          : Get unique elements
   è        : Count non-zero elements

@=ã ü;@pUÌï2õ á ÈíaY})Ìde[TT]}a Ä}  :Knight function
 =ã ü;                              : Wrap U twice (U -> [[U]])
      @                      }a Ä   : Repeat until True; return number of tries:
        UÌ                          :  Get the previous positions
          ï                         :  Cartesian product with:
           2õ                       :   The range [1,2]
              á                     :   All permutations, i.e. [[1,2],[2,1]]
                ÈíaY})              :  Apply each move to each position
       p                            :  Store the new positions
                      Ìde[TT]       :  True if any are at the destination

_è}         :Rook function
 è          : Count non-zero elements

_ra v *Zâ l}    :Bishop function
 ra             : Absolute difference between X and Y
    v           : Is divisible by 2? (returns 1 or 0)
      *         : Times:
       Zâ       :  Get the unique elements
          l     :  Count them
票数 2
EN

Code Golf用户

发布于 2018-11-27 19:12:51

木炭,108个字节

代码语言:javascript
复制
F…β⁸F⁸⊞υ⁺ι⊕κ≔⟦⟦η⟧⟧δW¬№§δ±¹ζ⊞δΦυΦ§δ±¹⁼⁵ΣEμX⁻℅ξ℅§κπ²≔Eη↔⁻℅ι℅§ζκε≡θKI⌈εQI∨∨¬⌊ε⁼⊟ε⊟ε²RI∨¬⌊ε²BI∧¬﹪Σε²∨⁼⊟ε⊟ε²NI⊖Lδ

在网上试试!链接是详细的代码版本。解释:

代码语言:javascript
复制
F…β⁸F⁸⊞υ⁺ι⊕κ

将板的所有64方格列出到预定义的空列表变量中。

代码语言:javascript
复制
≔⟦⟦η⟧⟧δ

列出第一个条目是包含开始位置的列表。

代码语言:javascript
复制
W¬№§δ±¹ζ

重复,直到列表的最后一个条目包含结束位置。

代码语言:javascript
复制
⊞δΦυΦ§δ±¹⁼⁵ΣEμX⁻℅ξ℅§κπ²

过滤所有的董事会立场,是一个骑士的移动,远离任何条目,在名单的最后一个条目,并将该名单的列表。这包括之前访问过的职位,但我们对它们并不感兴趣,所以我们最终首先搜索了董事会的最终位置。

代码语言:javascript
复制
≔Eη↔⁻℅ι℅§ζκε

计算起始位置和结束位置之间的绝对坐标差异。

代码语言:javascript
复制
≡θ

根据输入部分选择。

代码语言:javascript
复制
KI⌈ε

如果是国王,则打印最大绝对坐标差。

代码语言:javascript
复制
QI∨∨¬⌊ε⁼⊟ε⊟ε²

如果是皇后,那就打印2,除非这两个差值相等,或者1是零。

代码语言:javascript
复制
RI∨¬⌊ε²

如果它是一辆车,那就打印2,除非其中一个差是零。

代码语言:javascript
复制
BI∧¬﹪Σε²∨⁼⊟ε⊟ε²

如果它是一个主教,那么打印0,如果方格是相反的平价,否则打印2,除非两个差异是相等的。

代码语言:javascript
复制
NI⊖Lδ

如果是骑士,那就打印出找到末端位置所需的循环数。

票数 1
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

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

复制
相关文章

相似问题

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