public class FifteenPuzzle {
//---------------------------------------------------
public static boolean solvable15Puzzle(int[][] b) {
int c[] = new int[15];
int d = 0;
int count = 0;
int k = 0;
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[0].length; j++) {
if (b[i][j] != 0) {
c[d] = b[i][j];
d++;
} else k = i + 1;
}
}
for (int i = 0; i < c.length - 1; i++) {
for (int j = i + 1; j < c.length; j++) {
if (c[i] > c[j] && c[i] != 0 && c[j] != 0) {
count++;
}
}
}
return ((count + k) % 2 == 0);
}
//---------------------------------------------------
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));
}
}
|
SM=1, CM=55, ST=106, KY=1796, 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 ;
|