将有一个9x9
网格,在那里将放置12
岩石。
当你看到一块岩石时,你有两种选择:一开始从岩石开始,然后沿着起始方向行走(北、南、西或东):
*不是向west
,你向左转,就好像你在走,你走自己的左边。
因此,路径的流始终是counter-clockwise
。下面是一个说明流程的示例:
注意:在本例中,起始点是黄色的(X)
,起始方向是south
。
为了将网格和岩石放置永久化,以防图像因未知原因而死亡,我将把它写成纯文本:
100100000
100100010
000000000
000000000
000000000
010010000
000000000
010100000
100010010
//rocks = 1, empty = 0
发布于 2012-03-01 22:30:16
S=7
D=-1j
R=[0,4,7,1+1j,3+1j,1+3j,4+3j,7j,3+7j,7+7j,8j,3+8j]
Z={}
for i in range(4096):
d=D;p=R[S];L=[]
while len(L)<12:
p+=d;P=[(abs(r-p),r)for r in R if r-p==abs(r-p)*d]
if not P:break
p=min(P)[1];d*=1j**(i>>R.index(p)&1);L+=[p]
if p==R[S]:Z[tuple(L)]=1;break
print len(Z)
我正在计算的代码从R=[...
开始,前两行是“输入”,我没有计算。
R
是一组岩石坐标,使用复数坐标列出。S
是起点。D
是开始方向,同样使用复数(1=right、-1=left,1j=up、-1j=down)。
该代码尝试了所有2^12=4096的可能性:对每个岩石直走/左转,然后从起始岩石走到返回起始岩石,或者路径上没有更多的岩石,或者路径太长(它循环)。如果成功,它将在Z
中保存路径。
发布于 2012-03-01 17:37:18
要运行,提供岩石标识和方向。
program c1
其中字母是岩石(左上角= a,右下角= l),数字是方向:向上= 3,左= 2,下= 1,右=0。
mov bp,1000h
mov di,bp
mov cx,bp
xor ax,ax
rep stosw
mov bl,b[82h]
sub bl,'a'-1
mov bh,ch
add bx,bx
mov cl,b[83h]
sub cl,48
shl cl,2
mov di,bx
call l2
mov dx,bp
add b[bp],48
mov b[bp+1],'
mov ah,9
int 21h
ret
l1:inc b[bp]
cmp di,bx
je ret
dec b[bp]
cmp b[di+bp],0
jne ret
call l2
sub cl,4
l2:mov dx,[di+l3-2]
ror dx,cl
and dx,15
jz ret
mov b[di+bp],1
pusha
mov di,dx
add di,di
call l1
popa
mov b[di+bp],0
ret
l3:xor al,b[bx+si]
inc ax
add w[si+09510],sp
and ax,ax
add al,087
add b[bx+si+0906],dh
pusha
add b[bx+si+0b],cl
xor b[si],cl
jpe l4
l4:pop bx
发布于 2012-03-02 22:27:32
11
。(网格被压缩为5x5)前两行是输入的,不包括在字符计数中,我还添加了一些中断行来包装代码。
D=3;//starting direction
C=21;//starting rock
function a(a){return a+="",(a[0]-1)*5+(a[1]-1)}
function b(a){var b=!1,c=C+"";a==4?(h=c[0]+(c[1]-1))[1]>0&&(b=!0):a==3?(h=-(-c[0]-1)+c[1]+"")[0]<6&&(b=!0):a==2?(h=c[0]+ -(-c[1]-1))[1]<6&&(b=!0):(h=c[0]-1+c[1])[0]>0&&(b=!0);if(b)return C=h*1,!0}
function c(){var c=C;while(b(D)&&F[a(C)]==0);if(C!=c&&F[a(C)]==1)return!0}
function d(){if(c()){F[C]++;var a=D,b=C;C!=B?(d(),C=b,D=a,D==1?D=4:D--,d()):(A++,F=E)}}
A=0,B=C,E=F="1010010101010100110010011".split(""),d(),alert(A)
未压缩和解释的代码:
dir=3;
start=21;
//transforms row-column notation to array index
function toAV(a){
return a+="",(a[0]-1)*5+(a[1]-1);
}
//advance one square on the current direction
//returns true when it succeed
function walk(num){
var walkbol=false,str=xy+"";
if(num==4){
if((wal_rec=str[0]+(str[1]-1))[1]>0){
walkbol=true;
}
}else if(num==3){
if((wal_rec=-(-str[0]-1)+str[1]+"")[0]<6){
walkbol=true;
}
}else if(num==2){
if((wal_rec=str[0]+-(-str[1]-1))[1]<6){
walkbol=true;
}
}else{
if((wal_rec=(str[0]-1)+str[1])[0]>0){
walkbol=true;
}
}
if(walkbol){
xy=wal_rec*1;
return true;
}
}
//start walking on the current dir until a)didn't succeed walking or b)step on a rock
//returns true when it moved at least once and it ended above a rock
function nextRock(){
var str1=xy;
while(walk(dir)&&map[toAV(xy)]==0){}
if(xy!=str1&&map[toAV(xy)]==1){
return true;
}
}
//if it finds a rock, it calls itself twice
//one with the current direction and one with the direction to its left
function splitways(){
if(nextRock()){
map[xy]++;
var cc=dir,dd=xy;
if(xy!=start){
splitways();
//--
xy=dd;
dir=cc;
dir==1?dir=4:dir--;
splitways();
}else{
count++;
map=xmap;
}
}
}
//globals
count=0,xy=start,xmap=map="1010010101010100110010011".split("");
//start
splitways();
alert(count);
https://codegolf.stackexchange.com/questions/5017
复制相似问题