照亮镜面房间


13

目标

在本次比赛中,您将获得一个随机的房间,里面放着一支蜡烛。目的是编写最短的程序(这是高尔夫球),通过用代替暗点来确定房间的哪些部分被蜡烛照亮@。该程序应占用STDIN的空间,并将输出打印到STDOUT。

输入/房间示例

+------+
|  C   |
|      +--+
|  \      |
+---------+

蜡烛被表示用C,墙壁/反射镜被用来表示|-/,或\。墙壁本身就是镜子。房间的角落用表示+

房间永远不会有对角墙,光线也永远不会逸出房间。

同样,一行上的第一个字符将始终是房间墙壁的一部分。每行上的绝对最后一个字符将是房间的另一面墙。这两个之间的任何字符都不会在房间外面。

光线与反射

蜡烛在八个基本方向上分别发出八束(类似激光的)光束:N,S,E,W,NE,SE,SW和NW。这些光线从镜子反射出来,如下所述:

Old Direction of Travel | Mirror | New Direction
N S E W NE SE SW NW       /        E W N S -- -- -- --
N S E W NE SE SW NW       \        W E S N -- -- -- --
N S E W NE SE SW NW       |        - - - - NW SW NE SW
N S E W NE SE SW NW       -        - - - - SE NE SW NE

A -代表被吸收的光。光总是被C或+吸收。重要的是要注意,只有当镜子占用与镜子相同的空间时,光线才会从镜子反射回来。当您在纸上绘制反射时,这些规则更容易理解。

示例输出

作为输出,程序应打印照明房间的图像,暗点写为@,亮点留为空白,镜面不受影响。对于上面的示例,输出为:

+------+
|  C   |
|@   @ +--+
| @\      |
+---------+

这意味着,如果您抽出光束,它们将永远不会到达标记为的空间@

更多例子

Input:
+-----+
|     |
|     |
|  C  |
|     |
|     |
+-----+
Output:
+-----+
| @ @ |
|@   @|
|  C  |
|@   @|
| @ @ |
+-----+

Input:
+-----+
|  \  |
|/ C \+-+
|       |
|  \ - ++
+------+
Output:
+-----+
|  \ @|
|/ C \+-+
|      @|
| @\ -@++
+------+

在您的示例中,左下角@也不应该吗?
彼得·泰勒

1
@Peter Taylor:SW光束击中了那个位置。
Briguy37,2011年

3
在堆栈溢出方面受到好评的Lasers挑战有一些相似之处。足够多的相似之处使其中使用的方法变得有趣而又有足够的差异,需要对如何应用它们进行一些深思熟虑。
dmckee ---前主持人小猫,

可以使用更多的验证案例。
dmckee ---前主持人小猫,

@dmckee我又添加了两个示例。
PhiNotPi 2011年

Answers:


2

Python,292个字符

import sys
R=''
for x in sys.stdin:R+='%-97s\n'%x[:-1].replace(' ','@')
M={'/':'-98/d','\\':'98/d'}
for d in(-98,-1,1,98,99,97,-97,-99):
 if d>98:M={'|':'d^2','-':'-d^2'}
 p=R.find('C')
 while 1:
  p+=d
  if R[p]in' @':R=R[:p]+' '+R[p+1:]
  elif R[p]in M:d=eval(M[R[p]])
  else:break
print R,

在房间里读书,使其成矩形,然后从各个方向从蜡烛中走出来。M包含活动的镜像字符及其效果(/\对于基本方向,|-对于其他方向)

最多可以处理97个字符的房间。


2

c-504

依赖于K&R默认函数调用语义。非常简单的实现,除了带有弹跳光线的小提琴手。

#define N M[x+y*97]
#define Y abs(y)
#define O M[c]==
#define E else break;
int j[]={-98,-97,-96,-1,1,96,97,98},c,x,y,p,s,M[9409];main(){for(;
(c=getchar())!=-1;){if(c==10)x=0,++y;else{if(c==67)p=x+y*97;if(c==32)
c=64;N=c;++x;}}for(x=0;x<8;++x){y=j[x];c=p;do{c+=y;if(O'@')M[c]=32;s=y/Y;
if(O 92)if(y%2){y=s*(98-Y);}E if(O'/')if(y%2){y=s*-(98-Y);}E if(O'|')
if(~y%2){y=s*(97+(97-Y));}E if(O'-')if(~y%2){y=s*-(97+(97-Y));}E}while
(!(O'+')&&!(O'C'));}for(y=0;x=0,N!=0;++y){for(;N!=0;++x)putchar(N);
putchar(10);}}

不打高尔夫球

//#include <stdio.h>
int j[]={ -98, -97, -96, /* Increments to move around the array */
           -1,       1,
           96,  97,  98},
  c, x, y, p, s, /* take advantage of static initialization to zero */
  M[9409]; /* treat as 97*97 */

main(){
  /* read the map */
  while((c=getchar())!=-1/*Assume the deffinition of EOF*/){
    /* putchar(c);  */
    if (c=='\n')
      x=0,++y;
    else {
      if (c=='C') p=x+y*97; /* set start position */
      if (c==' ') c='@'; /* The room starts dark */
      M[x+y*97]=c; ++x;
    }
  }
  /* printf("Start position is %d (%d, %d)\n",p,p%97,p/97); */
  /* Now loop through all the direction clearing '@' cells as we
   * encounter them 
   */
  for(x=0;x<8;++x){
    y=j[x];c=p; /* y the increment, c the position */
    /* printf("\tposition %d (%d, %d) '%c'\n",c,c%97,c/97,M[c]); */
    /* printf("\tdirection = %d (%d, %d)\n",y,-(abs(y)-97),(y+98)/97-1); */
    do {
      c+=y;
      /* printf("\t\tposition %d (%d, %d) '%c'\n",c,c%97,c/97,M[c]); */
      /* We ought to do bounds checking here, but we rely on  *
       * the guarantee that the room will be bounded instead. */
      if(M[c]=='@') M[c]=' ';
      /* The reflections are handles
       *   + Stop or not stop based on the even/oddness of the increment
       *   + New direction is a little fiddly, look for yourself
       */
      s=y/abs(y); /* sign of y (need for some reflections) */
      if (M[c]=='\\') if (y%2){ y=s* (98-abs(y));     }else break; 
      if (M[c]=='/')  if (y%2){ y=s*-(98-abs(y));     }else break; 
      if (M[c]=='|')  if (~y%2){y=s* (97+(97-abs(y)));}else break; 
      if (M[c]=='-')  if (~y%2){y=s*-(97+(97-abs(y)));}else break;  
      /* printf("\t\t\tdirection = %d (%d, %d)\n",y,97-abs(y),(y+98)/97-1); */
    } while (!(M[c]=='+')&&!(M[c]=='C'));
    /* printf("\t...hit a %c. Done\n",M[c]); */
  }
  /* print the result */
  for(y=0;x=0,M[x+y*97]!=0;++y){
    for(;M[x+y*97]!=0;++x)
      putchar(M[x+y*97]);
    putchar('\n');
  }
}

验证方式

$ gcc -g -o candle candle_golfed.c
$ for f in candle_room*; do (./candle < $f) ; done
+------+
|  C   |
|@   @ +--+
| @\      |
+---------+
+------+
|  C   |
|@   @ +--+
|  /@ @ @ |
+---------+
+------+
| @/   |
|@   @ +--+
|  C      |
+---------+
+------+
|  \@ @|
|@   @ +--+
|  C      |
+---------+
+-----+
| @ @ |
|@   @|
|  C  |
|@   @|
| @ @ |
+-----+
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.