ヘビ配列
2893 ワード
package com;
public class T {
public int x;
public int y;
public int[][] arr;
public int WIDTH = 10;
public static int num;
public static int width;
public static T t;
public T() {
arr = new int[WIDTH][WIDTH];
num = 0;
width = 0;
x = 0;
y = 0;
}
public void test() {
for (int i = 0; i < WIDTH; i++) {
for (int j = 0; j < WIDTH; j++) {
{
if (x == WIDTH - 1) {
y = WIDTH - 1 - x;
x++;
} else {
x = WIDTH - 1 - y;
y++;
}
arr[x][y] = num;
num++;
}
}
width++;
}
}
public void test2() {
for (int j = 0; j < WIDTH; j++) {
for (int i = 0; i < WIDTH; i++) {
width++;
for (int t = 0; t < width; t++) {
width = i;
x = i;
y = width - x;
num++;
arr[x][y] = num;
}
}
}
}
public void test3() {
int i = 0;
while (i < WIDTH) {
int t = 0;
while (t < width) {
x = width - y;
arr[x][y] = 0;
x++;
}
width++;
}
}
public void test4() {
int width = 0;
int post = 0;
for (int i = 0; i < WIDTH * WIDTH; i++) {
while (post <= width) {
x = width - y;
arr[x][y] = num;
num++;
post++;
}
width++;
}
}
public void test5() {
boolean left = false;
width = 1;
for (int i = 0; i < WIDTH; i++) {
for (int j = 0; j < width; j++) {
if (left) {
x = width - 1 - y;
arr[x][y] = num++;
if (x != 0) {
x--;
y++;
}
if(y == 0)
left = !left;
} else {
y = width - 1 - x;
arr[x][y] = num++;
if (y != 0) {
y--;
x++;
}
if(x == 0)
left = !left;
}
}
width++;
left = !left;
System.out.println(t.toString());
}
}
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[0].length; j++) {
if(arr[i][j] < 10)
sb.append("0");
sb.append(arr[i][j]);
sb.append(' ');
}
sb.append("
");
}
return sb.toString();
}
public static void main(String[] args) {
t= new T();
t.test5();
System.out.println(t);
}
}