绘制兰顿蚂蚁的路径。
平面上的正方形有不同的颜色,有黑色也有白色。我们任意确定一个正方形为“蚂蚁”。蚂蚁每走一步都可以沿着四个主要方向中的任何一个方向行进。蚂蚁按照以下规则移动:
_#@。更新:case的N= 450输出是错误的。
N=0
_________________
_________________
_________________
_________________
_________________
_________________
_________________
_________________
________@________
_________________
_________________
_________________
_________________
_________________
_________________
_________________
_________________N=1
_________________
_________________
_________________
_________________
_________________
_________________
_________________
_________________
________#________
________@________
_________________
_________________
_________________
_________________
_________________
_________________
_________________N= 450
_________________
_________________
___________##____
____##______##___
___#__##___##_#__
__###_#@#__#__#__
__#_#_#_#__#_#___
_____###___#_____
_____#___________
_____#__###______
___#_#_#__#_#_#__
__#__#_#____###__
__#_##__##___#___
___##______##____
____##___________
_________________
_________________发布于 2011-03-19 05:46:13
~17.'_'*n+*\153:|;{|/()[124^.2/6+:6.1&17*)\2&(*|+:|;]@++}*|/();'@'@hallvabo的Python解决方案最类似于此,因此我只描述主要的区别。
板存储为字符串而不是数组。这样我们就可以用更少的字符(因为字符串总是扁平的)在板上更新一个值,因此使它达到所需的输出格式是很容易的。
蚂蚁的位置由公式((d&1)*17+1)*((d&2)-1) (即.1&17*)\2&(*)递增,其中d是方向。我们使用变量6,这样就可以跳过初始化。
发布于 2011-03-14 02:15:44
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]}}通过函数参数输入。
mr*r>1而不是r.abs>1String#scan生成输出。将==更改为>String#tr发布于 2011-03-14 11:25:06
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.缩进和注释代码如下所示:
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.输入:
450产出:
_________________
_________________
___________##____
____##______##___
___#__##___##_#__
__###_#@#__#__#__
__#_#_#_#__#_#___
_____###___#_____
_____#___________
_____#__###______
___#_#_#__#_#_#__
__#__#_#____###__
__#_##__##___#___
___##______##____
____##___________
_________________
_________________https://codegolf.stackexchange.com/questions/1600
复制相似问题