【问题标题】:How to print the coordinates from the given string?如何从给定的字符串打印坐标?
【发布时间】:2017-01-19 04:40:27
【问题描述】:

我在参加过的一次采访中遇到了这个问题,我被难住了。

给定如下输入字符串,我们需要打印在字符串最后一个字符处获得的坐标。

方向:

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)

我正在尝试将其放入代码中但是,我没有得到如何破解它的起点。任何帮助将不胜感激。

【问题讨论】:

    标签: java arrays graph 2d coordinates


    【解决方案1】:

    我认为问题在于识别移动指令,每个指令都有一个可选的因素(多少步)和一个强制的方向。省略因子时,实际上是1的值。

    所以这些可以表示为正则表达式中的模式:

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

    完成后,我们只需要将方向映射到对应的 更改坐标(dxdy),然后在我们在正则表达式匹配的while循环中处理移动指令时应用更改(乘以因子的值)。

    注意X是个特例,可以通过always remember来处理 最后一个位置为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. 将它传递给返回标记数组的解析器方法。每个 token 以数字开头,以字母结尾。如果两个字母 互相跟随,它们被认为是两个不同的令牌。 X 是一个 独立令牌。
      3. 将令牌数组传递给返回最终结果的计算方法 坐标。在calculate方法中,读取返回的数组 point 2. 读取每个token后,对坐标进行必要的操作。

      像这样设计坐标类。这将帮助您轻松解决问题。

      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);
          }
      
      }
      

      【讨论】:

        【解决方案3】:

        输入字符串:“UUUDULR”,输出为:{0,3}

        输入字符串:“ULLLDUDUURLRLR”,输出为:{-2,2}

        下面是上述问题的简单C++实现:

        void findCoordinate(string s)
        {
            int dx,dy;
            int x=0, y=0;
            
            for(int i=0; s[i]!='\0'; i++)
            {
                if(s[i]=='U')
                {
                    dx=0;
                    dy=1;
                }
                else if(s[i]=='D')
                {
                    dx=0;
                    dy=-1;
                }
                else if(s[i]=='L')
                {
                    dx=-1;
                    dy=0;
                }
                else if(s[i]=='R')
                {
                    dx=1;
                    dy=0;
                }
            
                x+=dx;
                y+=dy;
                
            }
            
            cout<<"Coordinates are:"<<x<<" "<<y;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-11-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-12-12
          • 1970-01-01
          相关资源
          最近更新 更多