首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >兰顿的蚂蚁艺术。

兰顿的蚂蚁艺术。
EN

Code Golf用户
提问于 2011-03-13 23:35:22
回答 5查看 2.6K关注 0票数 25

绘制兰顿蚂蚁的路径。

描述

平面上的正方形有不同的颜色,有黑色也有白色。我们任意确定一个正方形为“蚂蚁”。蚂蚁每走一步都可以沿着四个主要方向中的任何一个方向行进。蚂蚁按照以下规则移动:

  • 在白色方块处,向右转90度,翻转方格的颜色,向前移动一个单位。
  • 在黑色方块处,向左转90度,翻转方格的颜色,向前移动一个单位。

Specifications

  • 输入:介于0到725之间的整数N(含)。
  • 输出:一个17乘17的网格表示蚂蚁在步骤N时的“路径”。

规则

  • 蚂蚁开始向右(3点钟)。
  • 蚂蚁从网格的中心开始。
  • 分别对白色方块、黑色方块和蚂蚁使用_#@
  • 网格最初是完全白色的。
  • 您可以在解释语言上制作完整的程序或函数。
  • 由stdin或论点输入。

示例

更新:case的N= 450输出是错误的。

N=0

代码语言:javascript
运行
复制
_________________
_________________
_________________
_________________
_________________
_________________
_________________
_________________
________@________
_________________
_________________
_________________
_________________
_________________
_________________
_________________
_________________

N=1

代码语言:javascript
运行
复制
_________________
_________________
_________________
_________________
_________________
_________________
_________________
_________________
________#________
________@________
_________________
_________________
_________________
_________________
_________________
_________________
_________________

N= 450

代码语言:javascript
运行
复制
_________________
_________________
___________##____
____##______##___
___#__##___##_#__
__###_#@#__#__#__
__#_#_#_#__#_#___
_____###___#_____
_____#___________
_____#__###______
___#_#_#__#_#_#__
__#__#_#____###__
__#_##__##___#___
___##______##____
____##___________
_________________
_________________
EN

回答 5

Code Golf用户

回答已采纳

发布于 2011-03-19 05:46:13

GolfScript - 67 chars

代码语言:javascript
运行
复制
~17.'_'*n+*\153:|;{|/()[124^.2/6+:6.1&17*)\2&(*|+:|;]@++}*|/();'@'@

hallvabo的Python解决方案最类似于此,因此我只描述主要的区别。

板存储为字符串而不是数组。这样我们就可以用更少的字符(因为字符串总是扁平的)在板上更新一个值,因此使它达到所需的输出格式是很容易的。

蚂蚁的位置由公式((d&1)*17+1)*((d&2)-1) (即.1&17*)\2&(*)递增,其中d是方向。我们使用变量6,这样就可以跳过初始化。

票数 10
EN

Code Golf用户

发布于 2011-03-14 02:15:44

Ruby1.9,104个字符

代码语言:javascript
运行
复制
f=->z{l=[*[r=1]*17,2]*17;c=152;z.times{c+=r=(r*r>1?r/18:-r*18)*l[c]*=-1};l[c]=0;l.map{|a|putc"@_
#"[a]}}

通过函数参数输入。

  • (146个-> 142)内联m
  • (142 -> 140)检查r*r>1而不是r.abs>1
  • (142 -> 128)使用String#scan生成输出。将==更改为>
  • (128个-> 125)删除过时变量
  • (125 -> 122)用条件替换String#tr
  • (122 -> 122)现在生成与更新的示例相同的输出
  • (122 -> 111)在生成蚂蚁路径时使用ant代替字符。
  • (111 -> 109)重新排序一些表达式以保存括号
  • (109 -> 108)代码现在是一个函数
  • (108 -> 104)逐个打印每个字符
票数 9
EN

Code Golf用户

发布于 2011-03-14 11:25:06

Delphi,217

代码语言:javascript
运行
复制
var g,a:PByte;i,d,Word;begin g:=AllocMem(306);a:=g+153;Read(i);for n:=1to i do begin a^:=2-a^;d:=d-1+a^;a:=a+(1-2and d)*(1+17*(1and d))end;a^:=1;for n:=1to 306do if n mod 18=0then WriteLn else Write('_@#'[1+g[n]])end.

缩进和注释代码如下所示:

代码语言:javascript
运行
复制
var
  g,a:PByte;
  i,d,n:Int32;
begin
  g:=AllocMem(306); // g: The grid is initially completely white. (size=18*17=306)
  // Assume 0=d: Ant start 'd'irection faces right (=0, see below)
  a:=g+153; // a: Ant position starts at the center of the grid (=8*18+9=153)
  Read(i);
  for n:=1to i do
  begin
    // Flip the color of the square;
    a^:=2-a^;
    // Turn 90° right if at an '_' space, 90° left otherwise;
    d:=d-1+a^;
    // Move one unit forward;
    //   For this, determine the step size, using the two least significant bits of d.
    //   This gives the following relation :
    //     00 = 0 =  90° = right =   1
    //     01 = 1 = 180° = down  =  18
    //     10 = 2 = 270° = left  = - 1
    //     11 = 3 =   0° = up    = -18
    //   (d and 2) gives 0 or 2, translate that to 1 or -1
    //   (d and 1) gives 0 or 1, translate that to 1 or 18
    //   Multiply the two to get an offset 1, 18, -1 or -18 :
    a:=a+(1-2and d)*(1+17*(1and d))
  end;
  // Place the ant and print the grid :
  a^:=1; // 0 > '_', 1='@', 2 > '#'
  for i:=1to 306do
    if i mod 18=0then // we insert & abuse column 0 for newlines only (saves a begin+end pair)
      WriteLn
    else
      Write('_@#'[1+g[i]])
end.

输入:

代码语言:javascript
运行
复制
450

产出:

代码语言:javascript
运行
复制
_________________
_________________
___________##____
____##______##___
___#__##___##_#__
__###_#@#__#__#__
__#_#_#_#__#_#___
_____###___#_____
_____#___________
_____#__###______
___#_#_#__#_#_#__
__#__#_#____###__
__#_##__##___#___
___##______##____
____##___________
_________________
_________________
票数 4
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

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

复制
相关文章

相似问题

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