PDA

Ver la versión completa : Algoritmo de la n damas en un tablero de ajedrez



jessyn
18/10/2006, 13:14
Necesito un algoritmo que coloque las n damas en un tablero pero con la heuristica de tabu serach, o solo el algoritmo simple.
link que explica el problema

http://es.wikipedia.org/wiki/Problema_de_las_n_damas
porfis ayudenme..

ranefi
15/11/2006, 12:41
Necesito un algoritmo que coloque las n damas en un tablero pero con la heuristica de tabu serach, o solo el algoritmo simple.
link que explica el problema

http://es.wikipedia.org/wiki/Problema_de_las_n_damas
porfis ayudenme..

Buen día jessyn. El tema ya es un poco viejo, pero aquí va algo de lo que necesitas.

Notación algebraica



1.- Dd1
2.- Df2
3.- Dh3
4.- Db4
5.- Dg5
6.- Da6
7.- Dc7
8.- De8



Código en C++



using System;

namespace Las_Damas
{
class Class1
{
static int n;
static int cont;
static int []d;
static void Main()
{
Console.WriteLine("Dame n");
n=Convert.ToInt32(Console.ReadLine());
d= new int[n];

int i=0;
while(!(i==0 && d[i]==n/2))
{
while(i<=(n-1) && PonerD(i))
i++;

if(i==n) ImpDoble();

i--;
}
if(n%2==1)
{
i=1;
d[0]=(n+1)/2;
for(int t=1; t<n; t++)
d[t]=0;
while(!(i==1 && d[i]==n))
{
while(i<=(n-1) && PonerD(i))
i++;

if(i==n) Imp();

i--;
}
}

Console.ReadLine();

}
static bool PonerD(int i)
{
do d[i]++;
while(d[i]<=n && VerifConTodosAnt(i));

if(d[i]==(n+1))
{
d[i]=0;
return false;
}
else return true;
}
static bool VerifConTodosAnt(int i)
{
bool v=false;
for(int k=0; k<i; k++)
v=v || Verif(k,i);

return v;
}
static bool Verif(int k, int i)
{
return ( (d[k]==d[i]) || ( Math.Abs(d[k]-d[i])==Math.Abs(i-k) ) );
}
static void ImpDoble()
{
cont++;
Console.Write(cont+".- ");
for(int i=0; i<n; i++)
{

Console.Write(""+(i+1)+","+d[i]+" ");
}
Console.WriteLine();

cont++;
Console.Write(cont+".- ");
for(int i=0; i<n; i++)
{

Console.Write(""+(i+1)+","+(n-d[i]+1)+" ");
}
Console.WriteLine();
}
static void Imp()
{
cont++;
Console.Write(cont+".- ");
for(int i=0; i<n; i++)
{

Console.Write(""+(i+1)+","+d[i]+" ");
}
Console.WriteLine();
}
}
}



Espero te sirva. Nos vemos.