public class FifteenPuzzle {
//---------------------------------------------------
public static boolean solvable15Puzzle(int[][] b) {
int B = 0, L = 0;
int a[] = new int[15];
int x = 0;
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[0].length; j++) {
if (b[i][j] != 0) {
a[x] = b[i][j];
x++;
}
if (b[i][j] == 0)
B = i + 1;
}
}
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[i] > a[j]) L++;
}
}
if ((L + B) % 2 == 0)
return true;
else return false;
}
//---------------------------------------------------
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=4, CM=13, ST=2, KY=413, 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 ;
|