Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Collections;
- import java.util.LinkedList;
- class sostojba implements Comparable {
- int[][] tabla;
- int hevristika;
- boolean neupotrebliv;
- sostojba(int[][] sos) {
- tabla = new int[3][];
- for (int i = 0; i < 3; i++) {
- tabla[i] = sos[i].clone();
- }
- neupotrebliv = false;
- presmetajHevristika();
- }
- private void presmetajHevristika() {
- int hev = 0;
- if (tabla[0][0] == 1)
- hev++;
- if (tabla[0][1] == 2)
- hev++;
- if (tabla[0][2] == 3)
- hev++;
- if (tabla[1][0] == 8)
- hev++;
- if (tabla[1][1] == 0)
- hev++;
- if (tabla[1][2] == 4)
- hev++;
- if (tabla[2][0] == 7)
- hev++;
- if (tabla[2][1] == 6)
- hev++;
- if (tabla[2][2] == 5)
- hev++;
- hevristika = hev;
- }
- public int compareTo(sostojba arg0) {
- if (this.hevristika > arg0.hevristika)
- return -1;
- else if (this.hevristika < arg0.hevristika)
- return 1;
- else
- return 0;
- }
- @Override
- public int compareTo(Object o) {
- return this.compareTo((sostojba) o);
- }
- public String toString() {
- String sos1 = "";
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++) {
- sos1 += tabla[i][j];
- }
- sos1 += "\n";
- }
- return sos1;
- }
- public boolean equals(Object ob){
- if(this.equals((sostojba) ob))
- return true;
- return false;
- }
- public boolean equals(sostojba sos) {
- String sos1 = "";
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++) {
- sos1 += tabla[i][j];
- }
- }
- String sos2 = "";
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++) {
- sos2 += sos.tabla[i][j];
- }
- }
- return sos1.equals(sos2);
- }
- }
- class slagalica {
- // 0 e praznoto mesto
- int[][] tabla;
- slagalica() {
- tabla = new int[3][3];
- tabla[0][0] = 2;
- tabla[0][1] = 8;
- tabla[0][2] = 3;
- tabla[1][0] = 1;
- tabla[1][1] = 6;
- tabla[1][2] = 4;
- tabla[2][0] = 0;
- tabla[2][1] = 7;
- tabla[2][2] = 5;
- }
- public LinkedList<sostojba> moznisostojbi() {
- LinkedList<sostojba> sostojbi = new LinkedList<sostojba>();
- int in = -1, jn = -1;
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++) {
- if (tabla[i][j] == 0) {
- in = i;
- jn = j;
- }
- }
- }
- int[][] temp = new int[3][3];
- // proveri gore
- if (in > 0) {
- int tmp = tabla[in][jn];
- tabla[in][jn] = tabla[in - 1][jn];
- tabla[in - 1][jn] = tmp;
- sostojbi.add(new sostojba(tabla));
- tmp = tabla[in][jn];
- tabla[in][jn] = tabla[in - 1][jn];
- tabla[in - 1][jn] = tmp;
- }
- // proveri dole
- temp = new int[3][3];
- if (in < 2) {
- int tmp = tabla[in][jn];
- tabla[in][jn] = tabla[in + 1][jn];
- tabla[in + 1][jn] = tmp;
- sostojbi.add(new sostojba(tabla));
- tmp = tabla[in][jn];
- tabla[in][jn] = tabla[in + 1][jn];
- tabla[in + 1][jn] = tmp;
- }
- // proveri levo
- temp = new int[3][3];
- if (jn > 0) {
- int tmp = tabla[in][jn];
- tabla[in][jn] = tabla[in][jn - 1];
- tabla[in][jn - 1] = tmp;
- sostojbi.add(new sostojba(tabla));
- tmp = tabla[in][jn];
- tabla[in][jn] = tabla[in][jn - 1];
- tabla[in][jn - 1] = tmp;
- }
- // proveri desno
- temp = new int[3][3];
- if (jn < 2) {
- int tmp = tabla[in][jn];
- tabla[in][jn] = tabla[in][jn + 1];
- tabla[in][jn + 1] = tmp;
- sostojbi.add(new sostojba(tabla));
- tmp = tabla[in][jn];
- tabla[in][jn] = tabla[in][jn + 1];
- tabla[in][jn + 1] = tmp;
- }
- // Debug
- // for(int i=0;i<sostojbi.size();i++)
- // System.out.println(sostojbi.get(i) + "" +
- // sostojbi.get(i).hevristika);
- return sostojbi;
- }
- }
- public class lab {
- @SuppressWarnings("unchecked")
- public static void main(String[] args) {
- LinkedList<sostojba> izminati = new LinkedList<sostojba>();
- slagalica s1 = new slagalica();
- sostojba pocetok = new sostojba(s1.tabla);
- izminati.add(pocetok);
- int[][] kraj = new int[3][3];
- kraj[0][0] = 1;
- kraj[0][1] = 2;
- kraj[0][2] = 3;
- kraj[1][0] = 8;
- kraj[1][1] = 0;
- kraj[1][2] = 4;
- kraj[2][0] = 7;
- kraj[2][1] = 6;
- kraj[2][2] = 5;
- sostojba krajna = new sostojba(kraj);
- System.out.println(pocetok);
- int brojac = 0;
- while (!izminati.contains(krajna)) {
- boolean promena = false;
- brojac++;
- LinkedList<sostojba> tmplist = s1.moznisostojbi();
- for (int i = 0; i < tmplist.size(); i++) {
- if (!izminati.contains(tmplist.get(i))) {
- izminati.add(tmplist.get(i));
- promena = true;
- //ERR
- }
- }
- if (!promena) {
- izminati.get(0).neupotrebliv = true;
- } else {
- Collections.sort(izminati);
- }
- sostojba najdobra = null;
- for (int i = 0; i < izminati.size(); i++) {
- if (!izminati.get(i).neupotrebliv) {
- najdobra = izminati.get(i);
- break;
- }
- }
- if (najdobra == null) {
- System.out.println("ERROR");
- break;
- } else {
- System.out.println(najdobra);
- s1.tabla = najdobra.tabla;
- }
- }
- System.out.println(brojac);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement