-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNewClass2.java
More file actions
162 lines (141 loc) · 5.51 KB
/
NewClass2.java
File metadata and controls
162 lines (141 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package numberpuzzle;
import java.util.ArrayList;
import java.util.Arrays;
import javax.swing.JOptionPane;
public class NumberPuzzle {
static int profundidade;
public static void executa(String[] args) throws CloneNotSupportedException {
profundidade = 1;
int[][] meta = {{1, 2, 3}, {8, 0, 4}, {7, 6, 5}};
int[][] facil = {{1, 3, 4}, {8, 6, 2}, {7, 0, 5}};
int[][] medio = {{2, 8, 1}, {0, 4, 3}, {7, 6, 5}};
int[][] dificil = {{2, 8, 1}, {4, 6, 3}, {0, 7, 5}};
int[][] hell = {{5, 6, 7}, {4, 0, 8}, {3, 2, 1}};
options.largura = false;
State raiz = null;
if (options.Iten.equals("Dificil")) {
raiz = new State(dificil);
} else if (options.Iten.equals("Facil")) {
raiz = new State(facil);
} else if (options.Iten.equals("Medio")) {
raiz = new State(medio);
} else if (options.Iten.equals("HELL")) {
raiz = new State(hell);
}
State resultado = new State(meta);
State atual = raiz;
State[] nivel = aprofunda(atual, resultado);
boolean res = false;
ArrayList<State> filhos = new ArrayList<>();
ArrayList<State[]> niveis = null;
ArrayList<State> solucao = new ArrayList<>();
options.cont_ant_exist = 0;
int nos = 0;
if (options.largura) {
niveis = new ArrayList<>();
}
do {
if (options.largura) {
niveis.add(nivel);
}
System.out.println("Nível " + profundidade);
System.out.println("\t" + nivel.length + " Filhos no nivel " + profundidade);
for (State state : nivel) {
nos++;
if (state.compareTo(resultado) == 0) {
System.out.println("Solução encontrada");
res = true;
while (state.pai != null) {
solucao.add(state);
state = state.pai;
}
solucao.add(state);
}
}
if (res) {
break;
} else {
State[] aux = null;
for (State state : nivel) {
aux = aprofunda(state, resultado);
filhos.addAll(Arrays.asList(aux));
}
nivel = filhos.toArray(nivel);
filhos.removeAll(Arrays.asList(aux));
aux = null;
}
profundidade++;
System.gc();
} while (true);
System.out.println("" + nivel.length + " Filhos no nivel " + profundidade);
System.out.println("Um total de "+nos+" nós na árvore.");
System.out.println("Existia um total de "+options.cont_ant_exist+" antecessores repetidos.");
State[] result = new State[solucao.size()];
int c = solucao.size() - 1;
for (State state : solucao) {
result[c] = state;
c--;
}
options.Resultado_Completo = result;
options.resultado = options.Resultado_Completo[0];
JOptionPane.showMessageDialog(null, "Resultado Encontrado!");
NewJFrame.preenche();
}
public static State[] aprofunda(State atual, State resultado) throws CloneNotSupportedException {
State[] aux = new State[atual.qtd_mov()];
State[] finalAux = new State[atual.qtd_mov()];
int cont = 0;
boolean found = false;
State stt_aux;
if (atual.mov_baixo(atual.getVazio())) {
stt_aux = (State) atual.clone();
stt_aux.move(0);
aux[cont] = stt_aux;
cont++;
}
if (atual.mov_esq(atual.getVazio())) {
stt_aux = (State) atual.clone();
stt_aux.move(1);
aux[cont] = stt_aux;
cont++;
}
if (atual.mov_dir(atual.getVazio())) {
stt_aux = (State) atual.clone();
stt_aux.move(2);
aux[cont] = stt_aux;
cont++;
}
if (atual.mov_cima(atual.getVazio())) {
stt_aux = (State) atual.clone();
stt_aux.move(3);
aux[cont] = stt_aux;
cont++;
}
///Verifica se o novo tabuleiro é igual a algum já existente
for(int x = 0; x < atual.qtd_mov(); x++){
State auxpai = aux[x].pai;
while(auxpai != null){
if(aux[x].compareTo(auxpai)==0){
options.cont_ant_exist++;
///Hora de excluir este estado
for(int k = x; k < aux.length-1; k++){
aux[k] = aux[k+1];
}
finalAux = Arrays.copyOf(aux, aux.length-1);
found = true;
break;
}
auxpai = auxpai.pai;
}
auxpai = null;
}
///Se encontrar algum antecessor repetido logo, retorna-se o vetor menor sem a repeticao
if(found){
aux = null;
return finalAux;
}else{
finalAux = null;
return aux;
}
}
}