public class FifteenPuzzle {
//---------------------------------------------------
public static boolean solvable15Puzzle(int[][] b) {
int num[] = new int[16]; int k = 0;
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[i].length; j++) {
num[k] = b[i][j];
k++;
}
}
int countL = 0;
int countB = 0;
for (int i = 0; i < num.length; i++) {
for (int j = i + 1; j < num.length; j++) {
if(num[j] !=0){
if (num[j] < num[i]) countL = countL + 1;
} else if (num[j] == 0 && (i+1) % 4 == 0) countB = (i+1) / 4;
else if (num[j] == 0 && (i+1) % 4 != 0) countB = ((i+1) / 4) + 1;
}
}
System.out.println(countB);
if ((countL + countB) % 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=0, CM=21, ST=35, KY=796, 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 ;
|