Skip to content

Instantly share code, notes, and snippets.

@delatorremario
Created March 29, 2016 12:57
Show Gist options
  • Save delatorremario/541efede49b22ee431fe to your computer and use it in GitHub Desktop.
Save delatorremario/541efede49b22ee431fe to your computer and use it in GitHub Desktop.
Dentalink Programación
/**
* Created by mario on 28/03/16.
*/
var contador=0;
var N;
var tablero;
function problemaCaballoAjedrez(n,x,y) {
//n*n tamaño del tablero
N=n;
// x y , posicion inicial de la pieza, iniciando en fila y columna 1...n
tablero = creartablero(n, n, 0)
resolverproblema(x-1,y-1,1);
mostrartablero();
console.log("Cantidad de veces que procesa:",contador);
}
function resolverproblema(f,c,num){
contador++;
tablero[f][c]=num;
if(num===N*N) return true;
var posibles = buscarposibles(f,c);
posibles=ordenar(posibles);
for(var i= 0;i<posibles.length;i++){
if(resolverproblema(posibles[i][0],posibles[i][1],num+1)) return true;
}
tablero[f][c]=0;
return true;
}
function buscarposibles(f,c){
var resp=creartablero(8,2,null);
var pos = 0;
if(esvalido(f-2,c-1)){ resp[pos][0]=f-2; resp[pos++][1]=c-1; }
if(esvalido(f-2,c+1)){ resp[pos][0]=f-2; resp[pos++][1]=c+1; }
if(esvalido(f-1,c-2)){ resp[pos][0]=f-1; resp[pos++][1]=c-2; }
if(esvalido(f-1,c+2)){ resp[pos][0]=f-1; resp[pos++][1]=c+2; }
if(esvalido(f+2,c-1)){ resp[pos][0]=f+2; resp[pos++][1]=c-1; }
if(esvalido(f+2,c+1)){ resp[pos][0]=f+2; resp[pos++][1]=c+1; }
if(esvalido(f+1,c-2)){ resp[pos][0]=f+1; resp[pos++][1]=c-2; }
if(esvalido(f+1,c+2)){ resp[pos][0]=f+1; resp[pos++][1]=c+2; }
var tmp = creartablero(pos,2,null);
for(var i=0; i<pos; i++) {
tmp[i][0] = resp[i][0]; tmp[i][1]=resp[i][1];
}
return tmp;
}
function ordenar(posibles) {
for(var i=0; i<posibles.length; i++) {
for(var j=i+1; j<posibles.length; j++) {
var cantposiblesI = buscarposibles(posibles[i][0], posibles[i][1]).length;
var cantposiblesJ = buscarposibles(posibles[j][0], posibles[j][1]).length;
if(cantposiblesJ<cantposiblesI) {
var tmp0 = posibles[i][0];
posibles[i][0] = posibles[j][0];
posibles[j][0] = tmp0;
var tmp1 = posibles[i][1];
posibles[i][1] = posibles[j][1];
posibles[j][1] = tmp1;
}
}
}
return posibles;
}
function esvalido(f,c){
if(f<0 || f>N-1 || c<0 || c>N-1) return false;
if(tablero[f][c]!=0) return false;
return true;
}
function creartablero( rows, cols, defaultValue){
var arr = [];
for(var i=0; i < rows; i++){
arr.push([]);
arr[i].push( new Array(cols));
for(var j=0; j < cols; j++){
arr[i][j] = defaultValue;
}
}
return arr;
}
function mostrartablero(){
tablero.forEach(function (valor) {
console.log( valor);
})
console.log('------------------------')
}
/**
* Created by mario on 28/03/16.
*/
function test(number){
console.log('El resultado de '+number+'! es ' + factorial(number));
console.log('y contiene ' + endzeros(number) + ' ceros a la derecha');
console.log('--------------------------------------------------');
};
function factorial(number){
return number===0 && 1 || number * factorial(number-1);
};
function endzeros(number){
var count = 0;
for (var i=5; number/i>=1; i *= 5){
count += number/i;
}
return Math.floor(count);
};
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<script src="factorial.js" type="application/javascript"></script>
<script src="ajedrez.js" type="application/javascript"></script>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment