旋转魔方的脸

时间:2019-03-14 14:59:06

标签: java multidimensional-array transpose

我有一个Face类,它为rubiks多维数据集的每一面采用3x3整数数组。我正在尝试创建一个rotateRight()方法,

010203                 070401
040506     becomes     080502
070809                 090603

但是,我不确定如何使我的方法RotateRight()返回一个Face类型(这是我在运行代码时面临的当前错误)。我可以帮忙吗?以下是我目前拥有的代码:

public class Face{
  private int[][] grid;

  public Face(int[][] grid){
    grid = new int[3][3];
  }


  public Face rotateRight(){
    int rows = 3;
    int cols = 3;
    int[][] transposedArray = new int[3][3];

    for (int i = 0; i<rows; i++){
      for (int j = 0; j<cols; j++){
        transposedArray[j][i]=grid[i][j];
      }
    }
  }
}

2 个答案:

答案 0 :(得分:1)

如果我的问题正确,那么您将失败,因为您在方法末尾没有返回Face类型。如果正确,您只需在Driver.Instance.FindElement(By.XPath("//p[@class='k-reset' and normalize-space()='Unit: QA Room: 1']")).Click(); 处创建一个新的# the IP(s) on which your node server is running. I chose port 3000. upstream rentfromowner { server 159.65.88.218:3000; keepalive 8; } # the nginx server instance server { listen 0.0.0.0:80; server_name rentfromowner.co.uk www.rentfromowner.co.uk; return 301 https:/rentfromowner.co.uk$request_uri; } server { listen 0.0.0.0:443 default_server ssl; server_name rentfromowner.co.uk www.rentfromowner.co.uk; access_log /var/log/nginx/rentfromowner.log; ssl on; ssl_certificate /etc/ssl/certs/ssl-bundle.crt; ssl_certificate_key /etc/ssl/certs/rentfromowner.co.uk.key; ssl_prefer_server_ciphers on; # pass the request to the node.js server with the correct headers # and much more can be added, see nginx config options location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-NginX-Proxy true; proxy_pass http://www.rentfromowner.co.uk:3000; proxy_redirect off; return 301 http://www.rentfromowner.co.uk; } } 实例:

Face

答案 1 :(得分:1)

您必须创建一个新的Face实例才能返回,但是还需要在循环中调整数组分配。否则,您将无法分配正确的位置。

public Face rotateRight(){
    int rows = 3;
    int cols = 3;
    int[][] transposedArray = new int[3][3];

    for (int i = 0; i<rows; i++){
        for (int j = 0; j<cols; j++){
            transposedArray[j][2-i]=grid[i][j];
        }
    }
    return new Face(transposedArray);
}

我已经采取了稍微修改代码的自由,以允许在3x3、4x4、5x5,..., 并在两个方向上

这是一个带有辅助方法的类来演示用法:

public class Face{
    private int[][] grid;

    public Face(int[][] grid){
        this.grid = grid;
    }

    enum DIRECTION {
        RIGHT,
        LEFT
    }

    public Face rotateFront(DIRECTION direction){
        int rows = grid.length;
        int[][] transposedArray = new int[rows][rows];

        for (int i = 0; i<rows; i++){
            for (int j = 0; j<rows; j++){
                if (direction == DIRECTION.RIGHT) {
                    transposedArray[j][(rows-1)-i]=grid[i][j];
                } else {
                    transposedArray[(rows-1)-j][i]=grid[i][j];
                }
            }
        }

        return new Face(transposedArray);
    }

    public String toString() {
        int rows = grid.length;
        String output = "";
        for (int i = 0; i<rows; i++){
            for (int j = 0; j<rows; j++){
                output += "["+ grid[i][j]+"] ";
            }
            output += "\n";
        }
        return output;
    }

    public static void main(String[] args) {
        int[][] originalArray = new int[][]{ { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }};

        System.out.println("Create our inital Face:");
        Face startFace = new Face(originalArray);
        System.out.println(startFace);

        System.out.println("Rotate our inital Face to the right:");
        Face rotatedFace = startFace.rotateFront(DIRECTION.RIGHT);
        System.out.println(rotatedFace);

        System.out.println("Rotate our rotated Face to the left:");
        Face rotatedFace2 = rotatedFace.rotateFront(DIRECTION.LEFT);
        System.out.println(rotatedFace2);
    }
}

这将输出:

  

创建我们的初始面孔:
  [1] [2] [3]
  [4] [5] [6]
  [7] [8] [9]

     

将我们的初始面孔向右旋转:
  [7] [4] [1]
  [8] [5] [2]
  [9] [6] [3]

     

将旋转的脸旋转到左侧:
  [1] [2] [3]
  [4] [5] [6]
  [7] [8] [9]

我希望这可以帮助您继续开发Rubiks多维数据集。