2017-01-19 3 views
2

私は出席したインタビューの1つでこの質問に遭遇しました。私は困惑しています。指定された文字列から座標をプリントするには?

次のような入力文字列を指定すると、文字列の最後の文字で取得された座標を出力する必要があります。

道順:

L-Left 
U-Up 
R-Right 
D-Down 
X-Delete the previous move. 

仮定:

start with the coordinates (0,0) 
ここ

出力を計算する方法です。

与えられた入力は:

3L5UR2DDX2LR 

私たちは一歩一歩やってみましょう。

3L - Move 3 points to the left of (0,0) i.e (-3,0) 

5U- Move 5 points upper to (-3,0) i.e (-3,5) 

R - Move 1 point to the right of (-3,5) i.e (-2,5) 

2D - Move 2 points down to (-2,5) i.e (-2,3) 

D - Move 1 point further down i.e (-2,2) 

x - Delete the previous move.(current value is (-2,3)) 

2L -Move 2 left to (-2,3) i.e(-4,3) 

R- Move 1 Right to (-4,3) i.e (-3,3) 

最終出力は(-3,3)

である私は、しかし、コードに入れしようとしています、私はthis.Any助けを打破する方法についての出発点が高く評価されるだろう得ていないのです。

答えて

1

Iは任意因子(どのように多くの工程)と必須方向をそれぞれ有する移動命令の認識などの問題を見ることになります。因子を省略すると、実際には1の値を意味します。

したがって、これらは、正規表現内のパターンのように表すことができる。これが行われると

String regex = "(?<factor>\\d*)" 
      + "(?<dir>[LURDX])"; 

、我々だけ座標で対応する 変更dxdyに方向をマッピングする必要があります)、正規表現マッチのwhileループで移動命令を処理するときに変更を適用します(係数の値を掛けた値)。Xは常に lastXlastYとして最後の位置を覚えているが扱える特殊なケースであること

注意。

次は私の実装です:このプログラムの

import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

public class Walk { 
    enum Move { 
     L (-1, 0) 
     , U (0, 1) 
     , R (1, 0) 
     , D (0, -1) 
     , X (0, 0) 
     ; 
     private int dx; 
     private int dy; 
     private Move(int dx, int dy) { 
      this.dx = dx; 
      this.dy = dy; 
     } 
     public int getDx() { 
      return dx; 
     } 
     public int getDy() { 
      return dy; 
     } 

    } 

    public static void main(String[] args) { 
     String input = "3L5UR2DDX2LR"; 
     String regex = "(?<factor>\\d*)" 
       + "(?<dir>[LURDX])"; 
     Pattern p = Pattern.compile(regex); 
     Matcher m = p.matcher(input); 
     int x = 0; 
     int y = 0; 
     int lastX = 0; 
     int lastY = 0; 
     while (m.find()) { 
      String factorString = m.group("factor"); 
      int factor; 
      if (factorString.length()==0) { 
       factor=1; 
      } else { 
       factor=Integer.parseInt(factorString); 
      } 
      String dirString = m.group("dir"); 
      Move move = Move.valueOf(dirString); 
      System.out.format("(%d,%d) last was (%d, %d) %d %s -> " 
        , x, y 
        , lastX, lastY 
        , factor, move.name()); 
      if (move==Move.X) { 
       x = lastX; 
       y = lastY; 
      } else { 
       lastX = x; 
       lastY = y; 
       x += factor * move.getDx(); 
       y += factor * move.getDy(); 
      }   
      System.out.format("(%d,%d)%n", x, y); 
     } 
     System.out.format("finally arrive at (%d,%d)%n", x, y); 

    } 

} 

出力はこれです:

(0,0) last was (0, 0) 3 L -> (-3,0) 
(-3,0) last was (0, 0) 5 U -> (-3,5) 
(-3,5) last was (-3, 0) 1 R -> (-2,5) 
(-2,5) last was (-3, 5) 2 D -> (-2,3) 
(-2,3) last was (-2, 5) 1 D -> (-2,2) 
(-2,2) last was (-2, 3) 1 X -> (-2,3) 
(-2,3) last was (-2, 3) 2 L -> (-4,3) 
(-4,3) last was (-2, 3) 1 R -> (-3,3) 
finally arrive at (-3,3) 
2

以下が可能です。これは私が使用するアルゴリズムです。

  1. 入力文字列を取ります。
  2. トークンの配列を返すパーサメソッドに渡します。各 トークンは数字で始まり文字で終わります。 2つの文字「 」が互いに続く場合、それらは2つの異なるトークンと見なされます。 Xは の独立したトークンです。
  3. 最後に 座標を返すcalculateメソッドにトークンの配列を渡します。計算メソッドでは、 ポイント2で返された配列を読み取ります。各トークンを読み取った後、座標に必要な操作を行います。

のようにCoordinateクラスを設計します。これはあなたの問題を簡単に解決するのに役立ちます。

public class Point { 

    private int x; 
    private int y; 

    public int getX() { 
     return x; 
    } 
    public void setX(int x) { 
     this.x = x; 
    } 
    public int getY() { 
     return y; 
    } 
    public void setY(int y) { 
     this.y = y; 
    } 

    public int modifyX(int xDiff){ 
     return (getX()+xDiff); 
    } 

    public int modifyY(int yDiff){ 
     return (getY()+yDiff); 
    } 

} 
関連する問題