| |

VerySource

 Forgot password?
 Register
Search
View: 1268|Reply: 3

Find the Rubik's Cube Algorithm

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2021-3-10 14:30:02
| Show all posts |Read mode
1. Input: the order of the Rubik's Cube Output: the complete Rubik's Cube


2 In addition, I want to ask a question. The questions are as follows:
  Given n cubes of different sizes, the area of ​​each of these cubes is between 1-n, stack these cubes into two towers, and ask what is the minimum height difference between the two towers

Attachment: The magic square matrix refers to the N×N square matrix with natural numbers 1, 2,..., N2, and the value of each element is not equal, and the sum of N elements in each row, column, and main and sub-diagonal lines Are all equal.

For odd-order magic squares, the Dole Rob algorithm can be used to generate them. The process is: start from 1, and insert each natural number until N2. The principle of choosing the insertion position is:

a. The first position is in the middle of the first line;

b. The new position should be at the upper right of the most recent insertion position, but if the upper right position has exceeded the upper boundary of the square matrix, the new position shall be the lowest position of the selected column; if it exceeds the right boundary, the new boundary shall be the selected row The leftmost position;

c. If the last inserted element is an integer multiple of N, select the position in the same column in the next row as the new position.
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2021-3-10 16:00:02
| Show all posts
The algorithm is not accurate. I don't know if this code can be realized with VB. It seems a bit dizzy, staged problems...
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 Asia/Pacific Region

Post time: 2021-3-10 16:30:01
| Show all posts
Is there matlab? Look at the code of the magic function.
Generally speaking, the right (left) oblique upward algorithm is used for odd-numbered orders, and the arm swap method is used for even-numbered orders. The latter is a bit more complicated.

There is an introduction of Tier 4 (from the Nine Chapters of Arithmetic) in the shot carving or the god carving. . . If the host is smart enough, he can infer how the even order is realized.
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2021-3-10 16:45:01
| Show all posts
Given the odd-order algorithm, the even-order algorithm is still being studied!

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MagicNum {
   
    private int x;
    private int y;
    /**
     * @return returns x.
     */
    public int getX() {
        return x;
    }

    /**
     * @param x The x to be set.
     */
    public void setX(int x) {
        this.x = x;
    }

    /**
     * @return returns y.
     */
    public int getY() {
        return y;
    }

    /**
     * @param y The y to be set.
     */
    public void setY(int y) {
        this.y = y;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        
        MagicNum mn=new MagicNum();
        printTxt(mn.createMagic(9));
    }
   
    private int[][] createMagic(int n){
        if(n>2){
            MagicNum mn=new MagicNum();
            int[][] magic =new int [n][n];
            
            //Even-order magic square algorithm
            if(n%2==0){
               
               
            }else{//Odd-order magic square algorithm
                mn.setX((n-1)/2);
                mn.setY(0);
                magic[(n-1)/2][0]=1;
                for(int i=2;i<=n*n;i++){
                    mn.getnextXandY(magic,mn,i);
                }
            }
            return magic;
        }else{
            System.out.print("Please enter a natural number greater than 2");
            return null;
        }
    }
   
    /**
     * Merzirac method to generate odd magic square
     * @param magic
     * @param mn
     * @param n
     * @return
     */
    private MagicNum getnextXandY(int[][] magic,MagicNum mn,int n){
        int x =mn.getX();
        int y=mn.getY();
        
        //Move up left
        if(x+1<magic.length){
            x=x+1;
        }else{
            x=0;
        }
        
        if(y<1){
           y = magic.length-1;
        }else{
            y=y-1;
        }
        
        //Determine whether there is a number
        if(magic[x][y]==0){
            magic[x][y] =n;
        }else{
            //Move down one space to the original number
            x=mn.getX();
            y=mn.getY();
            
            if(y<magic.length-1){
                y=y+1;
            }else{
                y=0;
            }
            magic[x][y] =n;
        }
        
        mn.setX(x);
        mn.setY(y);
        
        return mn;
    }

    private static void printTxt(int[][] magic) {
        if (magic != null) {
            FileOutputStream file;
            try {
                StringBuffer s = new StringBuffer(100);
                String ret = "\r\n";
                file = new FileOutputStream("c:/magic.txt");
                for (int i = 0; i <magic.length; i++) {
                    for (int j = 0; j <magic.length; j++) {
                        s.append(magic[i][j]);
                        s.append(" ");
                        if (j == magic.length-1) {
                            s.append(ret);
                        }
                    }
                }
                byte[] bt = s.toString().getBytes();
                file.write(bt);
                file.close();
            } catch (FileNotFoundException e) {
                System.out.print("FileNotFoundException: "+ e.getMessage());
            } catch (IOException e) {
                System.out.print("IOException: "+ e.getMessage());
            } catch (Exception e) {
                System.out.print("Exception: "+ e.getMessage());
            }

        }
    }
}
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list