public class FifteenPuzzle {
//---------------------------------------------------
public static boolean solvable15Puzzle(int[][] b) {
int l = 0, zero = 0, k = 0;
int a[] = new int[15];
boolean d = true;
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[0].length; j++) {
if (b[i][j] == 0)
zero = i + 1;
else {
a[k] = b[i][j];
k++;
}
}
}
if ((count(a)+ zero) % 2 == 1)
d = false ;
return d;
}
//---------------------------------------------------
public static void main(String[] a) {
int[][] ok = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 12, 15 },
{ 13, 14, 11, 0 } };
int[][] nok = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 15, 14, 0 } };
System.out.println(solvable15Puzzle(ok));
System.out.println(solvable15Puzzle(nok));
}
public static int count (int[] b) {
int l = 0;
for (int i = 0; i < b.length; i++) {
for (int j = i; j < b.length; j++) {
if (b[i] > b[j])
l++;
}
}
return l;
}
}
|
SM=1, CM=57, ST=75, KY=1156, TR=60:00
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
ERR = 60:00 ;
|