Java - 2632
Mientras admiro la pureza técnica de la respuesta de Claudiu , decidí intentar hacer rompecabezas un poco más difíciles;)
Pasos básicos (bastante simples):
Randomize entry location
Step forward
For min(m,n)-1 steps:
Rotate left or right
Slide until I hit something or go a random distance
Place a rock in front of stopping location
If I can slide straight to any wall:
Slide to exit
Else
Create another step and try again
If at any step I get trapped, start over
If BFS finds shorter path, start over
También marco cada lugar como 'nogo' mientras me deslizo. Si termino en un lugar nogo (o justo en frente de uno que significaría que una roca iría allí), es un paso no válido.
Entonces, básicamente la idea es generar aleatoriamente muchos mapas y mantener el primero que sea válido. Planeo hacer esto más inteligente (retroceso, etc.), pero funciona bien en este momento. También podría reducir el código redundante, ya veremos.
Tal como está, genera mapas pequeños (15x10) casi instantáneamente, mapas medianos (30x20) en un par de segundos y grandes (40x30) en una cantidad de tiempo aleatorio entre 20 segundos y 20 minutos, dependiendo de la semilla. Prueba entre 300k-500k mapas / segundo en mi máquina, dependiendo del tamaño.
Nota al margen: a veces los mapas no son demasiado difíciles, simplemente porque solo hay tantas rocas como escalones, y a menos que el escalón te lleve a una pared, la mayoría de las veces solo hay una opción si quieres golpear una roca real. Lo arreglaré más tarde colocando rocas "aleatorias" en lugares seguros después de que se hayan dibujado todos los pasos. Como los puntos nogo ya están marcados, eso debería ser bastante simple. Por ahora, solo disfruta de estos ejemplos:
Salida que muestra diferentes tamaños / semillas:
$ java I 30 20 6851 $ java I 15 10 1 $ java I 15 10 65513
............................O. .......O....... ....O..........
.............................. ............... ...............
.............................. .........O..... .........O.....
..........O......O............ .............O. ..............O
...............O...........O.. ............... ...............
.............................. .......O....... .....O.O.......
.............................. O.............. ...............
........................O..... ............... ..........O....
.............................. ............... O..............
...O.......................O.. ......O........ ...............
O...............O.OO..........
..............O..........O....
...........O.................. T 14 R 6
....O......................... T 7 T 14
.............................. DLDLULURU LULDLDRURU
..............................
..............................
.................O............
.O............................
..............................
B 28
R 9
ULURDLDLDRURDLDRURUR
Tamaño máximo 40x30:
$ java I 40 30 2
........................................
........................................
........................................
........................................
................O.......................
..........O.............................
........................................
.......O................................
.....................O..........O.......
......................O.................
.................................O......
......................................O.
........................................
........................................
..............................O.........
...........O............................
........................................
.......................................O
.........O...................O..........
....................O...................
...............................O........
............O..O......................O.
......O...........O.....................
..................O....O................
..................................O.....
........................................
..............................O.........
.....................................O..
...........O............................
...................O....................
B 19
B 11
URURDLULULDRDRDLULDLDLULURDLD
Golfizado:
import java.util.*;import java.awt.*;class I{int m,n,p,g,a[][],b[][];Random r;Point s,e,c;ArrayList<Integer>z;void Q(String q,int l){if(l>0)System.out.println(q);else System.out.print(q);}void G(String[]y){m=Integer.valueOf(y[0]);n=Integer.valueOf(y[1]);p=Integer.valueOf(y[2]);r=new Random(p);Q("",1);int o=0,i,j,u=0;char t,f[]={85,76,68,82};while(o<3){if(++u%20000==0)Q("\r#"+u,0);a=new int[m+2][n+2];b=new int[m+2][n+2];for(i=0;i<m+2;i++)for(j=0;j<n+2;j++)if(i==0||i==m+1||j==0||j==n+1)a[i][j]=2;s=new Point();int e=r.nextInt(m*2+n*2);if(e<m*2){s.x=e%m+1;s.y=e<m?0:n+1;}else{s.y=(e-m*2)%n+1;s.x=(e-m*2)<n?0:m+1;}if(s.x<1)g=3;else if(s.x>m)g=1;else if(s.y<1)g=2;else if(s.y>n)g=0;a[s.x][s.y]=0;c=new Point(s);z=new ArrayList<Integer>();z.add(g);for(i=0;i++<Math.min(m,n)-1;)if(N()<1&&N()<1)break;o=((z.size()>=Math.min(m,n)-1)?1:0)+F()+((V()==z.size())?1:0);}Q("\r",0);for(j=1;j<n+1;j++){for(i=1;i<m+1;i++)Q(String.valueOf(a[i][j]>0?'O':'.'),0);Q("",1);}Q("\n\n",0);if(s.x<1||s.x>m){t=s.x<1?'L':'R';u=s.y;}else{t=s.y<1?'T':'B';u=s.x;}Q(t+" "+u,1);if(e.x<1||e.x>m){t=e.x<1?'L':'R';u=e.y;}else{t=e.y<1?'T':'B';u=e.x;}Q(t+" "+u,1);for(i=0;i<z.size();)Q(String.valueOf(f[z.get(i++)]),0);Q("",1);}public static void main(String[]a){new I().G(a);}int F(){int c=0;while(C()<1&&c++<10)if(N()<1)return 0;return e==null?0:1;}int C(){int d=g<2?-1:1;if(g%2<1){int y=c.y;while(y>0&&y<n+1){y+=d;if(a[c.x][y]==1)return 0;}e=new Point(c.x,y);}else{int x=c.x;while(x>0&&x<m+1){x+=d;if(a[x][c.y]==1)return 0;}e=new Point(x,c.y);}a[e.x][e.y]=0;return 1;}int V(){if((s.x-e.x)+(s.y-e.y)<2)return 0;Queue<Point>q=new ArrayDeque<Point>();Queue<Integer>d=new ArrayDeque<Integer>();a[s.x][s.y]=-2;q.add(s);d.add(0);while(q.size()>0){Point t=q.poll();int h=d.poll(),i=0;if(t.equals(e))return h;for(;i<4;i++){Point n=S(a,t,i<2?0:1,i%2<1?-1:1,99,1);if(a[n.x][n.y]==-2)continue;a[n.x][n.y]=-2;q.add(n);d.add(h+1);}}return 0;}int N(){Point q;int d=g<2?-1:1,x,y;System.arraycopy(a,0,b,0,a.length);q=S(b,c,g,d,r.nextInt((g%2<1?n:m)/2)+2,0);if(q.x<1||q.y<1||q.x>m||q.y>n||q.equals(c)||b[q.x][q.y]!=0)return 0;x=q.x;y=q.y;if(g%2<1)y+=d;else x+=d;if(b[x][y]<0)return 0;b[q.x][q.y]=-1;b[x][y]=1;int f=r.nextInt(2)<1?-1:1;g=g%2<1?(f<0?1:3):(g=f<0?0:2);c=q;System.arraycopy(b,0,a,0,a.length);z.add(g);return 1;}Point S(int[][]u,Point f,int w,int d,int q,int s){int i=1,x=f.x,y=f.y;for(;i<=q;i++){if(w%2<1)y=f.y+i*d;else x=f.x+i*d;if(e!=null&&e.x==x&&e.y==y)return e;if(y<0||y>n+1||x<0||x>m+1)return f;if(s<1&&u[x][y]<1)u[x][y]=-1;if(u[x][y]>0){if(w%2<1)y-=d;else x-=d;return new Point(x,y);}}if(w%2<1)return new Point(f.x,f.y+i*d);else return new Point(f.x+i*d,f.y);}}
Con saltos de línea:
import java.util.*;
import java.awt.*;
class I{
int m,n,p,g,a[][],b[][];
Random r;
Point s,e,c;
ArrayList<Integer>z;
void Q(String q,int l){if(l>0)System.out.println(q);else System.out.print(q);}
void G(String[]y){
m=Integer.valueOf(y[0]);
n=Integer.valueOf(y[1]);
p=Integer.valueOf(y[2]);
r=new Random(p);
Q("",1);
int o=0,i,j,u=0;
char t,f[]={85,76,68,82};
while(o<3){
if(++u%20000==0)
Q("\r#"+u,0);
a=new int[m+2][n+2];
b=new int[m+2][n+2];
for(i=0;i<m+2;i++)
for(j=0;j<n+2;j++)
if(i==0||i==m+1||j==0||j==n+1)
a[i][j]=2;
s=new Point();
int e=r.nextInt(m*2+n*2);
if(e<m*2){
s.x=e%m+1;
s.y=e<m?0:n+1;
}else{
s.y=(e-m*2)%n+1;
s.x=(e-m*2)<n?0:m+1;
}
if(s.x<1)g=3;
else if(s.x>m)g=1;
else if(s.y<1)g=2;
else if(s.y>n)g=0;
a[s.x][s.y]=0;
c=new Point(s);
z=new ArrayList<Integer>();
z.add(g);
for(i=0;i++<Math.min(m,n)-1;)
if(N()<1&&N()<1)
break;
o=((z.size()>=Math.min(m,n)-1)?1:0)+F()+((V()==z.size())?1:0);
}
Q("\r",0);
for(j=1;j<n+1;j++){
for(i=1;i<m+1;i++)
Q(String.valueOf(a[i][j]>0?'O':'.'),0);
Q("",1);
}
Q("\n\n",0);
if(s.x<1||s.x>m){
t=s.x<1?'L':'R';
u=s.y;
}else{
t=s.y<1?'T':'B';
u=s.x;
}
Q(t+" "+u,1);
if(e.x<1||e.x>m){
t=e.x<1?'L':'R';
u=e.y;
} else {
t=e.y<1?'T':'B';
u=e.x;
}
Q(t+" "+u,1);
for(i=0;i<z.size();)
Q(String.valueOf(f[z.get(i++)]),0);
Q("",1);
}
public static void main(String[]a){
new I().G(a);
}
int F(){
int c=0;
while(C()<1&&c++<10)
if(N()<1)
return 0;
return e==null?0:1;
}
int C(){
int d=g<2?-1:1;
if(g%2<1){
int y=c.y;
while(y>0&&y<n+1){
y+=d;
if(a[c.x][y]==1)
return 0;
}
e=new Point(c.x,y);
}else{
int x=c.x;
while(x>0&&x<m+1){
x+=d;
if(a[x][c.y]==1)
return 0;
}
e=new Point(x,c.y);
}
a[e.x][e.y]=0;
return 1;
}
int V(){
if((s.x-e.x)+(s.y-e.y)<2)
return 0;
Queue<Point>q=new ArrayDeque<Point>();
Queue<Integer>d=new ArrayDeque<Integer>();
a[s.x][s.y]=-2;
q.add(s);
d.add(0);
while(q.size()>0){
Point t=q.poll();
int h=d.poll(),i=0;
if(t.equals(e))
return h;
for(;i<4;i++){
Point n=S(a,t,i<2?0:1,i%2<1?-1:1,99,1);
if(a[n.x][n.y]==-2)
continue;
a[n.x][n.y]=-2;
q.add(n);d.add(h+1);
}
}
return 0;
}
int N(){
Point q;
int d=g<2?-1:1,x,y;
System.arraycopy(a,0,b,0,a.length);
q=S(b,c,g,d,r.nextInt((g%2<1?n:m)/2)+2,0);
if(q.x<1||q.y<1||q.x>m||q.y>n||q.equals(c)||b[q.x][q.y]!=0)
return 0;
x=q.x;
y=q.y;
if(g%2<1)
y+=d;
else
x+=d;
if(b[x][y]<0)
return 0;
b[q.x][q.y]=-1;
b[x][y]=1;
int f=r.nextInt(2)<1?-1:1;
g=g%2<1?(f<0?1:3):(g=f<0?0:2);
c=q;
System.arraycopy(b,0,a,0,a.length);
z.add(g);
return 1;
}
Point S(int[][]u,Point f,int w,int d,int q,int s){
int i=1,x=f.x,y=f.y;
for(;i<=q;i++){
if(w%2<1)
y=f.y+i*d;
else
x=f.x+i*d;
if(e!=null&&e.x==x&&e.y==y)
return e;
if(y<0||y>n+1||x<0||x>m+1)
return f;
if(s<1&&u[x][y]<1)
u[x][y]=-1;
if(u[x][y]>0){
if(w%2<1)
y-=d;
else
x-=d;
return new Point(x,y);
}
}
if(w%2<1)
return new Point(f.x,f.y+i*d);
else
return new Point(f.x+i*d,f.y);
}
}