Documentos de Académico
Documentos de Profesional
Documentos de Cultura
h
const int INF = 1000000;
int cn; //cantidad de nodos
vector< vector<int> > ady; //matriz de adyacencia
int padre = 0;
int hijo = 0;
while(markedLines.size() + 1 < cn){
padre = hijo;
// Marco la fila y elimino la columna del nodo padre.
markedLines.push_back(padre);
for(int i = 0; i < cn; i++)
adyacencia[i][padre] = INF;
arbol[padre][hijo] = min;
arbol[hijo][padre] = min;
}
return arbol;
}
Código en JAVA
public class Algorithms
{
public static Graph PrimsAlgorithm (Graph g, int
s)
{
int n = g.getNumberOfVertices ();
Entry[] table = new Entry [n];
for (int v = 0; v < n; ++v)
table [v] = new Entry ();
table [s].distance = 0;
PriorityQueue queue =
new BinaryHeap (g.getNumberOfEdges());
queue.enqueue (
new Association (new Int (0), g.getVertex
(s)));
while (!queue.isEmpty ())
{
Association assoc = (Association)
queue.dequeueMin();
Vertex v0 = (Vertex) assoc.getValue ();
int n0 = v0.getNumber ();
if (!table [n0].known)
{
table [n0].known = true;
Enumeration p = v0.getEmanatingEdges
();
while (p.hasMoreElements ())
{
Edge edge = (Edge) p.nextElement ();
Vertex v1 = edge.getMate (v0);
int n1 = v1.getNumber ();
Int wt = (Int) edge.getWeight ();
int d = wt.intValue ();
if (!table[n1].known &&
table[n1].distance>d)
{ table [n1].distance = d;
table [n1].predecessor = n0;
queue.enqueue (
new Association (new Int (d),
v1));
}
}
}
}
Graph result = new GraphAsLists (n);
for (int v = 0; v < n; ++v)
result.addVertex (v);
for (int v = 0; v < n; ++v)
if (v != s)
result.addEdge (v, table
[v].predecessor);
return result;
}
}
1 Historia
2 Planteamiento del Problema
o 2.1 Establecimiento del Algoritmo
o 2.2 Descripción del Algoritmo
3 El problema de las n Reinas
4 Soluciones al Problema de las Ocho Reinas
5 Referencias
6 Véase también
7 Enlaces externos
o 7.1 Enlaces a Soluciones
[editar]Historia
U es k-prometedor
V es (k + 1)-prometedor
Ui = Vi para todo
procedimiento
// es k-prometedor//
// //
//
//
//
//
si entonces //un vector 8-
prometedor es una solución//
escribir
si no //explorar las extensiones (k + 1)-
prometedoras de sol//
para hasta hacer
si y
y entonces
// es (k + 1)-
prometedor//
Codigo en java
tablero = new Array(
new Array(),
new Array(),
new Array(),
new Array(),
new Array(),
new Array(),
new Array(),
new Array()
);
Other code
import java.util.*;
public static void algoritmoHanoi(int n, String from, String temp, String to) {
if (n == 0) return;
algoritmoHanoi(n-1, from, to, temp);
System.out.println("Mover disco " + n + " de " + from + " a " + to);
algoritmoHanoi(n-1, temp, from, to);
}
import java.awt.*;
import java.applet.*;
boolean first=true;
int origen,destino;
int movimientos;
void Parametros()
String param;
param = getParameter("TORRES");
param = getParameter("COLS");
int i;
setBackground(cfondo);
resize(XDOTS,YDOTS);
if (first)
Parametros();
Herramientas();
first=false;
}
else B[i].disable();
Centorr[i]=(XDOTS/(numcols+1))*(i+1);
h[0]=numtorres;
h[i]=0;
Ficha[0][i]=numtorres-i;
movimientos=0;
origen=destino=NULL;
setLayout(new BorderLayout());
p.setBackground(cfondo);
p.add(B[i]=new Button(Integer.toString(i+1));
p.add(new Button("Cancelar"));
p.add(new Button("Resolver"));
p.add(new Button("Nueva"));
add("South",p);
g.setBackground(cfondo);
g.add(new Label("Columnas"));
g.add(TFTorres=new TextField(Integer.toString(numtorres));
g.add(new Label("Movimientos"));
TFMovimientos=new TextField("0",8);
TFMovimientos.disable();
g.add(TFMovimientos);
add("North",g);
int l,k;
int anchomin=(Centorr[1]-Centorr[0])/numtorres;
int altotorre=(YDOTS-85)/numtorres;
g.setColor(cbarra);
g.setColor(coltorr);
Long=anchomin*Ficha[k][l];
x=(Centorr[k]-(Long/2));
y=YDOTS-60-l*altotorre;
g.fillRect(x,y,Long,altotorre-altotorre/3);
{
Dibujar_Torres(g, ctorre);
TFMovimientos.setText(Integer.toString(movimientos));
if (h[i]==numtorres) Final();
Dibujar_Torres(getGraphics(), cfin);
return true;
Mover(numtorres,0,1,numcols-1);
h[origen]--;
Ficha[destino][h[destino]]=Ficha[origen][h[origen]];
h[destino]++;
movimientos++;
int varaux;
Mover(i-1,Comienzo,Final,Auxiliar);
Desplazar(Comienzo,Final);
update(getGraphics());
varaux=Comienzo;
Comienzo=Auxiliar;
Auxiliar=varaux;
int i;
if ((Integer.toString(i+1)).equals(e.arg))
if (origen==NULL)
origen=i;
B[origen].disable();
else
destino=i;
break;
numtorres=Integer.parseInt(TFTorres.getText());
numcols =Integer.parseInt(TFCols.getText());
TFTorres.setText(Integer.toString(numtorres));
TFCols.setText(Integer.toString(numcols));
TFMovimientos.setText("0");
init();
if( "Cancelar".equals(e.arg))
origen=destino=NULL;
B[i].enable();
if (valido(origen,destino))
Desplazar(origen,destino);
origen=destino=NULL;
}
repaint();
return true;
return false;
"e-mail : mbarrera@inf.uach.cl.\r\n";