Main Menu |
|
|
Forums |
|
|
Programming
Contest |
|
|
Documentation
|
|
|
Partner
Sites |
|
|
Sponsors |
|
|
|
Code of barry
<?php
function getmicrotime(){
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
function solution($d) {
for ($i=0; $i<strlen($d); $i++) $a[]=$d[$i];
sort($a);
return join('',$a);
}
function flip(&$d) {
global $op_str;
$op_str .= 'F';
$d = strrev($d);
//echo " F -> $d <br>";
}
function shuff(&$d) {
global $op_str;
$op_str .= 'S';
$n = strlen($d)>>1;
$a = substr($d,0,$n);
$b = substr($d,$n);
$d = '';
for ($i=0; $i<$n; $i++) {
$d .= $b[$i].$a[$i];
}
//echo " S -> $d <br>";
}
function cut(&$d) {
global $op_str;
$op_str .= 'C';
$n = strlen($d)>>1;
$a = substr($d,0,$n);
$b = substr($d,$n);
$d = $b.$a;
//echo " C -> $d <br>";
}
function processDeck(&$d,$ops='') {
global $sol,$rsol,$ssol,$srsol,$csol,$crsol,$op_str;
$orig = $d;
$sol = solution($d);
$rsol=$sol;
flip($rsol);
$ssol = $sol;
shuff($ssol);
$srsol = $rsol;
shuff($srsol);
$csol = $sol;
cut($csol);
$crsol = $rsol;
cut($crsol);
//echo "DECK: $d<br>";
/*
$op_str = '';
if (($d==$sol)||($d==$rsol)||($d==$ssol)||($d==$srsol)||($d==$csol)||($d==$crsol)) {
echo "SOLVED ********** <br>";
return;
}
*/
$oplimit = 25;
for ($c1=-7; $c1<$oplimit; $c1++) {
for ($c2=$c1+1; $c2<$oplimit; $c2++) {
if (abs($c2-$c1)<2) continue;
for ($c3=$c2+1; $c3<$oplimit; $c3++) {
if (abs($c2-$c3)<2) continue;
if (abs($c1-$c3)<2) continue;
for ($c4=$c3+1; $c4<$oplimit; $c4++) {
if (abs($c4-$c3)<2) continue;
if (abs($c4-$c2)<2) continue;
if (abs($c4-$c1)<2) continue;
for ($c5=$c4+1; $c5<$oplimit; $c5++) {
if (abs($c4-$c5)<2) continue;
if (abs($c3-$c5)<2) continue;
if (abs($c2-$c5)<2) continue;
if (abs($c1-$c5)<2) continue;
for ($c6=$c5+1; $c6<$oplimit; $c6++) {
if (abs($c6-$c5)<2) continue;
if (abs($c6-$c4)<2) continue;
if (abs($c6-$c3)<2) continue;
if (abs($c6-$c2)<2) continue;
if (abs($c6-$c1)<2) continue;
for ($c7=$c6+1; $c7<$oplimit; $c7++) {
if (abs($c6-$c7)<2) continue;
if (abs($c5-$c7)<2) continue;
if (abs($c4-$c7)<2) continue;
if (abs($c3-$c7)<2) continue;
if (abs($c2-$c7)<2) continue;
if (abs($c1-$c7)<2) continue;
$op = 0;
$d = $orig;
$op_str = '';
while (1) {
$a = ($op==$c1)||($op==$c2)||($op==$c3)||($op==$c4)||($op==$c5)||($op==$c6)||($op==$c7) ? 'C' : 'S';
switch ($a) {
case 'C':
cut($d);
break;
case 'S':
shuff($d);
break;
}
$op++;
if (($d==$sol)||($d==$rsol)||($d==$csol)||($d==$crsol)) {
//echo "SOLVED ********** <br>";
return;
}
if ($op>$oplimit) {
//echo "$op_str $c1 $c2 $c3 $c4 $c5 $c6 $c7 <br>";
break;
}
} //while
} //c7
} //c6
} //c5
} //c4
} //c3
} //c2
} //c1
}
?>
<html>
<!-- Creation date: 21/03/03 -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>PHP coding contest Mar-Apr 2003</title>
<meta name="Description" content="Entry for card shuffle contest">
<meta name="Keywords" content="">
<meta name="Author" content="Barand [barryaandrew@aol.com]">
<meta name="Generator" content="AceHTML 4 Pro">
</head>
<body background="blutextb.gif">
<?
$start_time = getmicrotime();
$decks = file('deck.txt');
$d = trim($decks[0]);
processDeck($d);
switch ($d) {
case $sol:
//echo "$d Stopped at solved<br>";
break;
case $rsol :
//echo "$d Stopped at rsol<br>";
flip($d);
break;
case $csol:
//echo "$d Stopped at csol<br>";
cut($d);
break;
case $crsol:
//echo "$d Stopped at crsol<br>";
cut($d);
flip($d);
break;
case $ssol:
//echo "$d Stopped at ssol<br>";
while ($d!=$sol) shuff($d);
break;
case $srsol:
//echo "$d Stopped at srsol<br>";
flip($d);
while ($d!=$sol) shuff($d);
break;
default:
//echo "$d NOT solved<br>";
}
$ln = strlen($op_str);
print ("$op_str $ln");
$end_time = getmicrotime();
//echo "<br>".number_format($end_time - $start_time, 3)." seconds<br>"
?>
</body>
</html>
Back to results
|
|
|