Sie sind auf Seite 1von 4

//Datos de inicio

<?php

define('N', 9);
define('WAIT_TIME', 3);
define('TRACE', true);
$GLOBALS['TOTAL_SOLUTIONS'] = 0;
$GLOBALS['SOLUTIONS'] = array();
$square = array();
for ($i = 0; $i < N; ++$i) {
for ($j = 0; $j < N; ++$j) {
$square[$i][$j] = 0;
}
}

echo 'NxN latin square with N='.N.'. Let\'s start filling with colors..'.PHP_EOL;
latinSquare($square, 1);
echo 'Total solutions found: '.$GLOBALS['TOTAL_SOLUTIONS'].PHP_EOL;

//Funcion principal

function latinSquare($square, $color)


{
if (TRACE) {
echo '---> latingSquare! Filling square with $color='.$color.PHP_EOL;
}

foreach (completions($square, $color) as $possibleSolution) {


if (possible($possibleSolution)) {
if (isSolution($possibleSolution)) { // Procces solution..
if (TRACE) {
echo '=====>> SOLUTION FOUND!'.PHP_EOL;
writeToScreen($possibleSolution);
sleep(WAIT_TIME);
}
++$GLOBALS['TOTAL_SOLUTIONS'];
$GLOBALS['SOLUTIONS'][] = $possibleSolution;
} else {
if (completable($possibleSolution)) {
if ($color + 1 <= N) {
latinSquare($possibleSolution, $color + 1);
}
}
}
}
}
}

//Funciones auxiliares

function writeToScreen($square)
{
for ($i = 0; $i < N; ++$i) {
for ($j = 0; $j < N; ++$j) {
echo str_pad($square[$i][$j], 3, ' ', STR_PAD_BOTH);
}
echo PHP_EOL;
}
}

// Only check if it doesn't have any 0s.


function isSolution($square)
{
return !completable($square);
}

// Check if it has any 0s.


function completable($square)
{
for ($i = 0; $i < N; ++$i) {
for ($j = 0; $j < N; ++$j) {
if ($square[$i][$j] == 0) {
return true;
}
}
}

return false;
}

// Check if it has the same color in any row or col.


function possible($square)
{
for ($i = 0; $i < N; ++$i) {
// cols
$cont = array_fill(1, N, 0);
for ($j = 0; $j < N; ++$j) {
if ($square[$j][$i] != 0) {
++$cont[$square[$j][$i]];
}
}
foreach ($cont as $value) {
if ($value > 1) {
return false;
}
}

// rows
$cont = array_fill(1, N, 0);
for ($j = 0; $j < N; ++$j) {
if ($square[$i][$j] != 0) {
++$cont[$square[$i][$j]];
}
}
foreach ($cont as $value) {
if ($value > 1) {
return false;
}
}
}

return true;
}
// Fill squares with colors and return array with all possible fillings.
function completions($square, $color)
{
$completions = array();

for ($j = 0; $j < N; ++$j) {


if ($square[0][$j] == 0) {
$square[0][$j] = $color;

completionsRecursive($completions, $square, $color, 1);

$square[0][$j] = 0;
}
}

return $completions;
}
function completionsRecursive(&$completions, $square, $color, $row)
{
for ($j = 0; $j < N; ++$j) {
if ($square[$row][$j] == 0) {
$square[$row][$j] = $color;
if (possible($square)) {
if ($row + 1 < N) {
completionsRecursive($completions, $square, $color, $row + 1);
} else {
$completions[] = $square;
}
}
$square[$row][$j] = 0;
}
}
}

______________________

otro
_______________________

class Cuadrado{

private int valor,i;


private int [] array ;

//se agarra el valor


public Cuadrado(int v){

valor=v;

public int regresarValor(){

return valor;
}

//se guardan los valores en el arreglo


public void guardarValores(int w){

int [] array = new int [w];

for (i=0;i<=valor;i++){

array[i]=i;

}
//recorrer los numeros
public void recorrerValores(){

//aqui imprimo lso valores actualez


for(i=0;i<array.length;i++){

System.out.print(array[i+1] + " " );


System.out.println();

//aqui se recorre
int [] array2 = new int [valor] ;
for (i=0 ; i < array.length ; i++){
if (i==array.length-1){

array2[1]=array[i];

}else{

array2[i] = array[i+1];

}
}

for (i=0; i< array2.length;i++)

array2[i] = array[i];

Das könnte Ihnen auch gefallen