public BAmplitud() { origin = new matrixState(); goal = new matrixState(); steps = new ArrayList(); solved = false; posible = true; }
/// <summary> /// Steps: /// 0 Up /// 1 Down /// 2 Left /// 3 Right /// </summary> ArrayList available_movements(matrixState ma, int last) { ArrayList alternatives = new ArrayList(); for (int fila = 0; fila < 3; fila++) { for (int columna = 0; columna < 3; columna++) { if (ma.currentNumberStates[fila, columna] == 0) { // up if (fila != 0 && last != 1) { alternatives.Add(0); } //Down if (fila != 2 && last != 0) { alternatives.Add(1); } // Left if (columna != 0 && last != 3) { alternatives.Add(2); } // Right if (columna != 2 && last != 2) { alternatives.Add(3); } } } } return(alternatives); }
public bool comparison(matrixState other) { for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) if (currentNumberStates[i, j] != other.currentNumberStates[i, j]) return false; return true; }
public void setValues(matrixState external) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { external.currentNumberStates[i, j] = currentNumberStates[i, j]; } } }
public bool comparison(matrixState other) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (currentNumberStates[i, j] != other.currentNumberStates[i, j]) { return(false); } } } return(true); }
public void goTroughtMovements(matrixState ma, ArrayList moves, bool print) { if (print) { Console.WriteLine("\n"); ma.printMatrix(); } for (int i = 0; i < moves.Count; i++) { makeMovement(ma, (int)moves[i]); if (print) { Console.WriteLine("\n"); ma.printMatrix(); } } }
void makeMovement(matrixState ma, int movement) { int raw = 0, column = 0; for (int fila = 0; fila < 3; fila++) { for (int columna = 0; columna < 3; columna++) { if (ma.currentNumberStates[fila, columna] == 0) { raw = fila; column = columna; } } } switch (movement) { case 0: // Up ma.currentNumberStates[raw, column] = ma.currentNumberStates[raw - 1, column]; ma.currentNumberStates[raw - 1, column] = 0; break; case 1: // Down ma.currentNumberStates[raw, column] = ma.currentNumberStates[raw + 1, column]; ma.currentNumberStates[raw + 1, column] = 0; break; case 2: // Left ma.currentNumberStates[raw, column] = ma.currentNumberStates[raw, column - 1]; ma.currentNumberStates[raw, column - 1] = 0; break; case 3: // Right ma.currentNumberStates[raw, column] = ma.currentNumberStates[raw, column + 1]; ma.currentNumberStates[raw, column + 1] = 0; break; default: // imposible break; } }
public bool calculate_steps(int maxDepth) { Queue posibilidades_cola = new Queue(); ArrayList calculatedSteps = new ArrayList(); matrixState current = new matrixState(); posibilidades_cola.Enqueue(calculatedSteps); while (!solved) { origin.setValues(current); ArrayList currentMovements = (ArrayList)posibilidades_cola.Dequeue(); goTroughtMovements(current, currentMovements, false); if (current.comparison(goal)) { solved = true; posible = true; steps = currentMovements; } else { int last = 10; if (currentMovements.Count != 0) { last = (int)currentMovements[currentMovements.Count - 1]; } foreach (int movement in available_movements(current, last)) { ArrayList currentMovesPlus1 = new ArrayList(); copyArrays(currentMovements, currentMovesPlus1); currentMovesPlus1.Add(movement); posibilidades_cola.Enqueue(currentMovesPlus1); } } if (currentMovements.Count > maxDepth) { posible = false; solved = true; } } return(posible); }
public bool calculate_steps(int maxDepth) { Queue posibilidades_cola = new Queue(); ArrayList calculatedSteps = new ArrayList(); matrixState current = new matrixState(); posibilidades_cola.Enqueue(calculatedSteps); while(!solved) { origin.setValues(current); ArrayList currentMovements = (ArrayList) posibilidades_cola.Dequeue(); goTroughtMovements(current, currentMovements, false); if (current.comparison(goal)) { solved = true; posible = true; steps = currentMovements; } else { int last = 10; if (currentMovements.Count != 0) last = (int)currentMovements[currentMovements.Count - 1]; foreach (int movement in available_movements(current, last)) { ArrayList currentMovesPlus1 = new ArrayList(); copyArrays(currentMovements, currentMovesPlus1); currentMovesPlus1.Add(movement); posibilidades_cola.Enqueue(currentMovesPlus1); } } if (currentMovements.Count > maxDepth) { posible = false; solved = true; } } return posible; }
// Reference the local goal to the destiny location in memory public void setDestiny(matrixState destiny) { destiny.setValues(goal); solved = false; posible = true; }
// Reference the local origin to the original location in memory public void setOrigin(matrixState origin) { this.origin = origin; solved = false; posible = true; }
void makeMovement(matrixState ma, int movement) { int raw = 0, column = 0; for (int fila = 0; fila < 3; fila++) for (int columna = 0; columna < 3; columna++) if (ma.currentNumberStates[fila, columna] == 0) { raw = fila; column = columna; } switch (movement) { case 0: // Up ma.currentNumberStates[raw, column] = ma.currentNumberStates[raw-1, column]; ma.currentNumberStates[raw-1, column] = 0; break; case 1: // Down ma.currentNumberStates[raw, column] = ma.currentNumberStates[raw+1, column]; ma.currentNumberStates[raw+1, column] = 0; break; case 2: // Left ma.currentNumberStates[raw, column] = ma.currentNumberStates[raw, column-1]; ma.currentNumberStates[raw, column-1] = 0; break; case 3: // Right ma.currentNumberStates[raw, column] = ma.currentNumberStates[raw, column+1]; ma.currentNumberStates[raw, column+1] = 0; break; default: // imposible break; } }
/// <summary> /// Steps: /// 0 Up /// 1 Down /// 2 Left /// 3 Right /// </summary> ArrayList available_movements(matrixState ma, int last) { ArrayList alternatives = new ArrayList(); for (int fila = 0; fila < 3; fila++) for (int columna = 0; columna < 3; columna++) if (ma.currentNumberStates[fila, columna] == 0) { // up if (fila != 0 && last != 1) alternatives.Add(0); //Down if (fila != 2 && last != 0) alternatives.Add(1); // Left if (columna != 0 && last != 3) alternatives.Add(2); // Right if (columna != 2 && last != 2) alternatives.Add(3); } return alternatives; }
static void Main(string[] args) { int option; bool origen = false, destino = false; BAmplitud busqueda_amplitud = new BAmplitud(); matrixState matrix_origin = new matrixState(), matrix_goal = new matrixState(); do { option = menu(); switch (option) { case 1: // Ingresa el original matrix_origin = new matrixState(0, 1, 2, 3, 4, 5, 6, 7, 8); busqueda_amplitud.setOrigin(matrix_origin); origen = true; break; case 2: // Ingresa meta matrix_goal = new matrixState(1, 2, 3, 4, 5, 6, 7, 8, 0); busqueda_amplitud.setDestiny(matrix_goal); destino = true; break; case 3: // Mostrar origen y destino if (origen) { Console.WriteLine("\n\tOrigen:"); matrix_origin.printMatrix(); } if (destino) { Console.WriteLine("\n\tDestino:"); matrix_goal.printMatrix(); } Console.ReadLine(); break; case 4: // Solucion if (origen && destino) { Console.WriteLine("\n\n\t Busqueda por amplitud"); if (busqueda_amplitud.calculate_steps(35)) { matrixState auxiliar = new matrixState(); matrix_origin.setValues(auxiliar); busqueda_amplitud.goTroughtMovements(auxiliar, busqueda_amplitud.solucion(), true); Console.WriteLine("\n\n\tCantidad optima de movimientos: " + busqueda_amplitud.solucion().Count); Console.WriteLine("\n"); } else { Console.WriteLine("\n\n\t No se encontro solucion"); } } else { if (!origen) { Console.WriteLine("\n No se ha establecido origen"); } if (!destino) { Console.WriteLine("\n No se ha establecido destino"); } } Console.ReadLine(); break; case 7: //Salir Console.WriteLine("\n\n\t ... Haz elegido salir ..."); Console.ReadLine(); break; default: Console.WriteLine("Opcion invalida"); Console.ReadLine(); break; } } while (option != 7); }
public void setValues(matrixState external) { for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) external.currentNumberStates[i, j] = currentNumberStates[i, j]; }
static void Main(string[] args) { int option; bool origen = false, destino = false; BAmplitud busqueda_amplitud = new BAmplitud(); matrixState matrix_origin = new matrixState(), matrix_goal = new matrixState(); do { option = menu(); switch (option) { case 1: // Ingresa el original matrix_origin = new matrixState(0, 1, 2, 3, 4, 5, 6, 7, 8); busqueda_amplitud.setOrigin(matrix_origin); origen = true; break; case 2: // Ingresa meta matrix_goal = new matrixState(1, 2, 3, 4, 5, 6, 7, 8, 0); busqueda_amplitud.setDestiny(matrix_goal); destino = true; break; case 3: // Mostrar origen y destino if (origen) { Console.WriteLine("\n\tOrigen:"); matrix_origin.printMatrix(); } if (destino) { Console.WriteLine("\n\tDestino:"); matrix_goal.printMatrix(); } Console.ReadLine(); break; case 4: // Solucion if (origen && destino) { Console.WriteLine("\n\n\t Busqueda por amplitud"); if (busqueda_amplitud.calculate_steps(35)) { matrixState auxiliar = new matrixState(); matrix_origin.setValues(auxiliar); busqueda_amplitud.goTroughtMovements(auxiliar, busqueda_amplitud.solucion(), true); Console.WriteLine("\n\n\tCantidad optima de movimientos: " + busqueda_amplitud.solucion().Count); Console.WriteLine("\n"); } else { Console.WriteLine("\n\n\t No se encontro solucion"); } } else { if (!origen) Console.WriteLine("\n No se ha establecido origen"); if (!destino) Console.WriteLine("\n No se ha establecido destino"); } Console.ReadLine(); break; case 7: //Salir Console.WriteLine("\n\n\t ... Haz elegido salir ..."); Console.ReadLine(); break; default: Console.WriteLine("Opcion invalida"); Console.ReadLine(); break; } } while (option != 7); }