2016-12-17 10 views
3

私はこのような迷路ファイルを持っています。移動方向を記録する方法はSASですか?

1111111 
1001111 
1101101 
1101001 
1100011 
1111111 

方向

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

を示すフォーマット$方向次に、Iは、開始と終了点を示すデータセットを持っています。

Row Column 
start 2  2 
end 3  6 

このように開始から終了までの移動方向はどのように記録できますか?

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

私が使用アレー正しい移動順序で

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

しかし、それだけではないだけを持っています。

ご協力いただきありがとうございます。

+0

アレイで何をしようとしているかを詳しく説明し、配列ステートメントのドキュメントをご覧ください。 – user667489

+0

は、与えられた入力セットから期待される出力をどのように表示するかの例を示します。それはあなたが探しているものを提供したものから明らかではない – DCR

+0

"迷路ファイル"は1が壁で、0は可動スペースである迷路ですか? (そうであれば、あなたは斜めに動かない限りあなたの例は解決できません)。出力として何をしたいですか?開始から終了までのパスのリスト?希望の出力データセットを表示してください。 – Quentin

答えて

1

これはこれを行う方法の1つです。 SASデータステップロジックを使用してより一般化された迷路解決アルゴリズムを書くことは、読者の練習として残されていますが、これはラビリンスにとっては有効です。

/* 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; 
+0

私の質問にお答えいただきありがとうございます。タスク(開始点と終了点)をハッシュに格納し、開始点と終了点を設定し、現在の点が終了点になるまでループすることは可能ですか? – Dan

+0

はい、しかし、ちょうど2つのポイントを持っていて、変数から直接得ることができる場合は、それは残酷なようです。 – user667489

+0

ああ、それは本当です。いったん迷路やタスクが変わると、ハードコードを防止したいだけです。 – Dan

関連する問題