3

I have a maze file which is like this.

1111111
1001111
1101101
1101001
1100011
1111111

a format $direction indicating the direction

start end label
D     D    down
L     L    left
R     R    right
U     U    up

Then, I have a dataset indicating the start and end point.

Row   Column
start  2      2
end    3      6

How can I record the moving direction from the start to the end like this?

direction  row column
           2    2
right      2    3
down       3    3      
down       4    3
down       5    3

i have use array

array m(i,j)
if  m(i,j) = 0 then
row=i;
column=j;
output;

however, it simply just not in the correct moving order.

Thanks if you can help.

Dan
  • 31
  • 2
  • Please describe in more detail what you are trying to do with the array, and have a look at the documentation for the array statement. – user667489 Dec 17 '16 at 18:40
  • give an example of what your expected output should look like from a given set of inputs. It's not clear from what you provided what you are looking for – DCR Dec 17 '16 at 19:25
  • Is that "maze file" a maze where the 1's are walls and the 0's are moveable spaces? (If so, your example is unsolvable unless you can move diagonally). What would you want as output? A list of the path from start to end? Please show the desired output dataset. – Quentin Dec 17 '16 at 22:16
  • Sorry for typo in the maze file. Yes, 1 refers to walls and 0 are movable spaces. – Dan Dec 18 '16 at 04:01

1 Answers1

1

Here's one way of doing this. Writing a more generalised maze-solving algorithm using SAS data step logic is left as an exercise for the reader, but this should work for labyrinths.

/* Define the format */

proc format;
 value $direction
 'D' = 'down'
 'L' = 'left'
 'R' = 'right'
 'U' = 'up'
;
run;


data want;

/*Read in the maze and start/end points in (y,x) orientation*/
array maze(6,7) (
1,1,1,1,1,1,1,
1,0,0,1,1,1,1,
1,1,0,1,1,0,1,
1,1,0,1,0,0,1,
1,1,0,0,0,1,1,
1,1,1,1,1,1,1
);
array endpoints (2,2) (
2,2
3,6
);
/*Load the start point and output a row*/
x = endpoints(1,2);
y = endpoints(1,1);
output;

/*
 Navigate through the maze.
 Assume for the sake of simplicity that it is really more of a labyrinth,
 i.e. there is only ever one valid direction in which to move, 
 other than the direction you just came from,
 and that the end point is reachable
*/
do _n_ = 1 by 1 until(x = endpoints(2,2) and y = endpoints(2,1));
    if maze(y-1,x) = 0 and direction ne 'D' then do;
        direction = 'U';
        y + -1;
    end;
    else if maze(y+1,x) = 0 and direction ne 'U' then do;
        direction = 'D';
        y + 1;
    end;
    else if maze(y,x-1) = 0 and direction ne 'R' then do;
        direction = 'L';
        x + -1;
    end;    
    else if maze(y,x+1) = 0 and direction ne 'L' then do;
        direction = 'R';
        x + 1;
    end;            
    output;
    if _n_ > 15 then stop; /*Set a step limit in case something goes wrong*/
end;
format direction $direction.;
drop maze: endpoints:;
run;
user667489
  • 9,501
  • 2
  • 24
  • 35
  • Thanks for answering my question. Is it possible to store the task (starting point and end point ) in hash and set the starting point and end point and then loop until the current point equal to end point? – Dan Dec 19 '16 at 07:26
  • Yes, but that seems like overkill when you just have 2 point s and you can get them directly from variables. – user667489 Dec 19 '16 at 08:38
  • Oh, that's true. I just want to prevent the hardcodes once the maze or the task change. – Dan Dec 19 '16 at 08:56
  • Have a go at doing that yourself, and post another question if you get stuck. – user667489 Dec 19 '16 at 08:58