package game;
import java.util.*;
public class Linkup {
public static void main(String[] args) {
Linkup test = new Linkup(6);
if(test.judge(new int[] {1,1}, new int[] {1,6})) System.out.println("T");;
}
int[][] map;
int size;
private Map pic;
public Linkup(int n) {
size = n;
map = new int[n + 2][n + 2];
init();
}
private void init() {
int cnt = 1;
for (int i = 1; i < size + 1; i++) {
if (i < size / 2 + 1) {
map[i][i] = cnt;
map[size / 2 + i][size / 2 + i] = cnt;
cnt++;
}
for (int j = i + 1; j < size + 1; j++) {
map[i][j] = cnt;
map[j][i] = cnt;
cnt++;
}
}
print(map);
System.out.println();
mixup();
print(map);
}
private void print(int[][] m) {
for (int i = 1; i < size + 1; i++) {
for (int j = 1; j < size + 1; j++) {
System.out.print(m[i][j] + "\t");
}
System.out.println();
}
}
// 打乱
private void mixup() {
for (int i = 1; i < size + 1; i++) {
for (int j = 1; j < size + 1; j++) {
int tmp = 0;
int x = 1, y = 1;
while (tmp == 0) {
x = (int) (Math.random() * size) + 1;
y = (int) (Math.random() * size) + 1;
tmp = map[x][y];
}
map[x][y] = map[i][j];
map[i][j] = tmp;
}
}
}
public boolean judge(int[] a, int[] b) {
if (map[a[0]][a[1]] != map[b[0]][b[1]]) {
return false;
}
Integer[][] distance = new Integer[size + 2][size + 2];
Deque<int[]> dq = new ArrayDeque<>();
int cnt = 0;
int len = 1;
int tmp = 0;
dq.push(a);
while (cnt < 3) {
int[] p = dq.pop();
distance[p[0]][p[1]] = cnt;
int i;
for (i = a[0]; i < size + 1; i++) {
if (map[i][a[1]] == 0 && distance[i][a[1]] == null) {
dq.push(new int[] { i, a[1] });
tmp++;
} else {
break;
}
}
for (i = a[0]; i > 0; i--) {
if (map[i][a[1]] == 0 && distance[i][a[1]] == null) {
dq.push(new int[] { i, a[1] });
} else {
break;
}
}
for (i = a[1]; i < size + 1; i++) {
if (map[a[0]][i] == 0 && distance[a[0]][i] == null) {
dq.push(new int[] { a[0], i });
} else {
break;
}
}
for (i = a[1]; i > 0; i--) {
if (map[a[0]][i] == 0 && distance[a[0]][i] == null) {
dq.push(new int[] { a[0], i });
} else {
break;
}
}
len--;
if (len == 0) {
len = tmp;
tmp = 0;
cnt++;
if (dq.contains(b)) {
return true;
}
}
}
return false;
}
}