Documentos de Académico
Documentos de Profesional
Documentos de Cultura
h>
#include <string.h>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include<conio.h>
#define
#define
#define
#define
#define
DOWN 0
UP 1
LEFT 2
RIGHT 3
H2
class Arbol_Elementos
{
public:
int Tabla[9];
char* str;
int CostoRuta;
int Valido;
int CostoTotal;
Arbol_Elementos* next;
};
int heur(int Tabla[]);
void Anteponer(Arbol_Elementos* NuevoNodo, Arbol_Elementos* AntiguoNodo, int
operator1);
int Meta(int* Tabla);
int notonqueue(int Tabla[]);
Arbol_Elementos* MejorNodoDeLaCola();
void print_Tabla(int* Tabla);
int apply (int* NuevoEstado, int* AntiguoEstado, int op);
Arbol_Elementos* NuevoElemento();
int op(char);
char to_char(int i);
char rep[] = "dulr";
int NoValidoo1[4] = {
int NoValidoo2[4] = {
int NoValidoo3[4] = {
int applyparam[4] = {
int Meta_Tabla[9] = {
int maxdepth;
Arbol_Elementos* top;
6, 0, 0, 2 };
7, 1, 3, 5 };
8, 2, 6, 8 };
+3, -3, -1, +1 };
0, 1, 2, 3, 4, 5, 6, 7, 8};
int main(){
system("cls");
int Tabla[9];
printf("\n8 Puzzle!\n");
printf("\=========\n");
printf("\nIntroduzca el estado inicial, similar a ' 1 2 3 4 x 6 7 8 5 ' (con
espacios):\n-----> ");
int i = 0;
while(i<9){
char chr;
chr = fgetc(stdin);
if (chr==32) continue;
if (chr=='x') Tabla[i] = 8;
else if (chr >= '1' && chr <= '9') Tabla[i] = chr - '1';
i++;
}
printf("\n Problema: \n");
printf("=========");
print_Tabla(Tabla);
printf("\n");
fgetc(stdin);
printf("\n Meta: \n");
printf("=====");
printf("\n");
printf ("\n-------------");
printf ("\n| %c | %c | %c |", '1', '2', '3');
printf ("\n-------------");
printf ("\n| %c | %c | %c |", '4', '5', '6');
printf ("\n-------------");
printf ("\n| %c | %c | %c |", '7', '8', 'x');
printf ("\n-------------");
getch();
char chr = '1 2 3 4 5 6 7 8 x';
i = 0;
/**while(i<9){
char chr;
chr = fgetc(stdin);
if (chr==32) continue;
if (chr=='x') Meta_Tabla[i] = 8;
else if (chr >= '1' && chr <= '9') Meta_Tabla[i] = chr - '1';
else { printf("Entrada Invlida. Ejemplo de entrada vlida ... 2 1 3 4 7 5 6
8 x :",(int) chr); return 1; }
i++;
}*/
printf("\nIntroduzca la profundidad max que desea buscar Menor a 25 se resue
lve rapidamente): ");
scanf("%d", &maxdepth);
printf("\nTrabajando...");
top = NuevoElemento();
for(i=0; i<9; i++)
top->Tabla[i] = Tabla[i];
top->CostoTotal = heur(Tabla);
Arbol_Elementos* NuevoNodo = NuevoElemento();
while (1){
Arbol_Elementos* Nodo = MejorNodoDeLaCola();
if (Nodo == NULL) {
printf("hecho!\n");
printf("No hay una solucion a este menor que %d profundidad.\n", max
depth);
printf("Intente aumentar la profundidad de 5.\n");
printf("Si no hay solucion dentro de 35-40 profundidad , el patron e
s generalmente insoluble.\n\n");
break;
}
else if (Meta(Nodo->Tabla)) {
char chr[15];
#endif
}
void Anteponer(Arbol_Elementos* NuevoNodo, Arbol_Elementos* AntiguoNodo, int op)
{
NuevoNodo->next = top;
top = NuevoNodo;
strcpy(NuevoNodo->str, AntiguoNodo->str);
NuevoNodo->str[AntiguoNodo->CostoRuta] = rep[op];
NuevoNodo->str[AntiguoNodo->CostoRuta+1] = 0;
NuevoNodo->CostoRuta = AntiguoNodo->CostoRuta+1;
NuevoNodo->CostoTotal = NuevoNodo->CostoRuta + heur(NuevoNodo->Tabla);
if (NuevoNodo->CostoTotal < AntiguoNodo->CostoTotal) NuevoNodo->CostoTotal =
AntiguoNodo->CostoTotal;
}
int Meta(int* Tabla){
int* g_Tabla = Meta_Tabla;
for(int i=0; i<9; i++)
if ((*(Tabla++))!=(*(g_Tabla++)))
return 0;
return 1;
}
int notonqueue(int* Tabla){
int i,j;
Arbol_Elementos* t = top;
while (t!=NULL){
for(i=0; i<9; i++)
if (t->Tabla[i] != Tabla[i]) break;
if (i==9) return 0;
t = t->next;
}
return 1;
}
Arbol_Elementos* MejorNodoDeLaCola(){
Arbol_Elementos* t = top;
int min_CostoTotaloRuta = 1000;
int CostoTotaloRuta;
Arbol_Elementos* to_return = NULL;
while (t != NULL){
if (t->Valido==1 && t->CostoTotal < min_CostoTotaloRuta){
min_CostoTotaloRuta = t->CostoTotal;
to_return = t;
}
t = t->next;
}
if (to_return != NULL) to_return->Valido = 0;
return to_return;
}
int apply (int* NuevoEstado, int* AntiguoEstado, int op){
int j;
int blank;
for (j=0; j<9; j++)