java俄罗斯方块旋转算法,求解

2025-03-24 06:41:48
推荐回答(3个)
回答1:

可以给每一个小方块设置为一个坐标,变为一个三阶行列式,3*3矩阵,转变为二元数组旋转。观察一下左旋:

11 12 13                       31 21 11

21 22 23           →→      32  22  12

31 32 33                        33  23  13

坐标变换如下:(1,1)变为(1,3),(1,2)变为(2,3),(1,3)变为(3,3)

(2,1)变为(1,2),(2,2)变为(2,2),(2,3)变为(3,2)

(3,1)变为(1,1),(3,2)变为(2,1),(3,3)变为(3,1)

规律就是(i,j)变为(j,3-i+1):

如果是2*2的方格,就可以变为二阶行列式,也就是2*2的二元数组,这里给出3*3九宫格改变的示意,我的代码如下:


import java.util.Random;

public class T{

public static void main(String[] args){

int[][] a=new int[3][3];
System.out.println("now begin to form a new integer array");
Random r=new Random();
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
a[i][j]=r.nextInt(10);
}
}
System.out.println("the array is shown as follows:");
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(a[i][j]+" ");
}
System.out.println();
}
System.out.println("左转九十度");
for(int i=0;ifor(int j=0;jSystem.out.print(a[a[i].length-1-j][i]+" ");
}
System.out.println();
}
}
}

回答2:

这个问题我也遇到过。
我曾经写过一个俄罗斯方块程序。
也是去找中心点。然后旋转的方式去实现。
现在想想这种思路不好。
应该是用矩阵变换的思路。

1 1
0 1
0 1

顺时针应该变成

0 0 1
1 1 1

回答3:

我这里有成功的,是你的数组问题,你要么?