Java - 2632
While I admire the technical purity of Claudiu's answer, I decided to try my hand at making slightly more difficult puzzles ;)
Basic steps (pretty simple):
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
I also mark each spot as 'nogo' as I'm sliding. If I end up on a nogo spot(or right in front of one which would mean a rock was going there), it's an invalid step.
So, basically the idea is to randomly generate a lot of maps, and keep the first one that is valid. I plan to make this smarter(backtracking, etc), but it works fine right now. It might also cut down on some redundant code, we'll see.
As it is, it generates small maps(15x10) almost instantly, medium(30x20) maps in a couple seconds, and large(40x30) in some random amount of time between 20 seconds and 20 minutes, depending on seed. It tests between 300k-500k maps/second on my machine, depending on size.
Side note: Sometimes the maps aren't too hard, simply because there are only as many rocks as steps, and unless the step takes you to a wall, most times there's only one option if you want to hit an actual rock. I'll fix that later by placing "random" rocks in safe spots after all the steps are drawn. Since nogo spots are already marked, that should be pretty simple. For now, just enjoy these examples:
Output showing different sizes/seeds:
$ 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
Max size 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
Golfed:
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);}}
With line breaks:
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);
}
}
2I cannot understand the objective: "you must travel from one place to another by sliding all the way in one direction until you either hit a wall or a boulder." Is it good to hit walls or boulders? Where do you aim to go from the start? If you hit a boulder does the game end? What happens when you hit a wall? Is it just me or are the directions unclear? – DavidC – 2014-03-06T21:21:13.767
it means that you cant change direction until you are stopped by either a wall or a boulder – masterX244 – 2014-03-06T21:24:12.583
You slide until you hit a boulder, and then turn left and right and do the same thing until you reach the place you want to go, basically. – Joe Z. – 2014-03-06T21:35:07.847
followed by a number representing the position (from the left or top) on that side to be entered from.
0-index or 1-index? Does it matter? – Shelvacu – 2014-03-06T21:53:56.273The shortest solution must have at least min(M,N) steps? Or the outputted solution? You could arbitrarily increase any solution by repeating the final steps until you meet this minimum. So you could generate the simplest puzzles and just bounce around in free space to make a solution. But it might also be very difficult to show that the best solution is long enough. – intx13 – 2014-03-06T21:58:05.637
@intx13: The outputted solution must be the shortest solution to the puzzle. I'll update the description to reflect that. – Joe Z. – 2014-03-06T22:00:42.370
@shelvacu: It's 1-index in the example, but if 0-index is easier for you, then go for it. – Joe Z. – 2014-03-06T22:01:21.953
3Oh, old memories of Pokémon Gold and Silver here. Find the exit, get HM07 and go to Blackthorn City. – Victor Stafusa – 2014-03-07T00:44:24.240
Indeed, the Ice Path inspired this puzzle. – Joe Z. – 2014-03-07T00:48:35.847
3
This reminds me of the ice levels in Chip's Challenge.
– luser droog – 2014-03-07T05:57:46.910Can we choose the input format? e.g. can I take input from stdin as the string
[12, 18, 314]
? – Claudiu – 2014-03-10T19:32:18.4201Why not use
>
and<
(or any character) for the enter and the exit? The puzzles will be easier to read. – A.L – 2014-03-11T01:06:50.470@n.1: the entrance & exit are actually off the grid. that being said there'll never be a rock right in front of it so it could be counted as a blank spot, and can use
<>v^
to point the direction in case of ambiguity (i.e. entrance on a corner). – Claudiu – 2014-03-11T01:54:36.4171Actually your sample output is invalid - the shortest path is
LDLDRULD
which is only 8 steps long – Claudiu – 2014-03-11T03:31:33.927Oh, really? Thanks for telling me, I hand-crafted that puzzle and yet didn't notice. – Joe Z. – 2014-03-12T04:05:37.870