Dane Posted January 4, 2008 Share Posted January 4, 2008 Hey guys, Im just wondering wether or not this is actually possible. Say i have numbers. 1 2 3 4 5 6 and i have a square box with the top, right, left and bottom each being a number.... 1 box could be.... 4 3 2 1 another box could be 3 1 2 2 another box could be 2 2 6 6 Can i generate PHP code that will match each other together. For example the box 3 1 2 2 would join to this one 2 2 6 6 to give 3 2 1 2 2 6 2 6 Basically, all numbers must attach to each other correctly. I know its a long shot. If PHP cannot do it do any of you know how to achieve this? Cheers Quote Link to comment https://forums.phpfreaks.com/topic/84531-solved-possible/ Share on other sites More sharing options...
effigy Posted January 4, 2008 Share Posted January 4, 2008 What if there's a conflict where more than one box can match up on more than one side? Quote Link to comment https://forums.phpfreaks.com/topic/84531-solved-possible/#findComment-430655 Share on other sites More sharing options...
Dane Posted January 4, 2008 Author Share Posted January 4, 2008 Each square could have the same numbers.. But each square will have a unique place.. A square can contain 4 4 4 6 So it would need to match up all numbers correctly. But the likely hood 2 squares will fit perfectly in one position is unlikely.. Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/84531-solved-possible/#findComment-430661 Share on other sites More sharing options...
effigy Posted January 4, 2008 Share Posted January 4, 2008 Sure, it's possible, but I don't have the time to dive into this right now. Quote Link to comment https://forums.phpfreaks.com/topic/84531-solved-possible/#findComment-430666 Share on other sites More sharing options...
GingerRobot Posted January 4, 2008 Share Posted January 4, 2008 So are the 4 (are there always 4?) boxes already known? And you need to see how (if?) they can fit together as you've described? Quote Link to comment https://forums.phpfreaks.com/topic/84531-solved-possible/#findComment-430669 Share on other sites More sharing options...
Dane Posted January 4, 2008 Author Share Posted January 4, 2008 Yes, There are always 4 values to the box. Boxes already have the value yes. And yes GingerRobot, i need to know how and if it can be done mate. Need to be pointed in the right direction. Ok thanks effigy, when you have more time mate Thanks again guys. Quote Link to comment https://forums.phpfreaks.com/topic/84531-solved-possible/#findComment-430690 Share on other sites More sharing options...
GingerRobot Posted January 4, 2008 Share Posted January 4, 2008 My solution: <?php error_reporting(E_ALL); class solve_problem { var $box; var $topleft = 0;//number of box in top left - 0,1,2,3 var $attempt = 1;//attempts with box in top top left 0-6 function __construct($box){ $this->box = $box; $this->solve(); } function check(){ if($this->box[0][1] == $this->box[1][3] && $this->box[1][2] == $this->box[2][0] && $this->box[2][3] == $this->box[3][1] && $this->box[3][0] == $this->box[0][3]){ return true; }else{ return false; } } function move(){ if(++$this->attempt==7){ $this->attempt = 1; if(++$this->topleft==4){ return false;//cannot work } } //work out top right $topright = ceil($this->attempt/2) - 1; if($topright>=$this->topleft){ $topright++; } //work out bottom right //lowest available on trials 3&5, middle on 1&6, highest on 2,4 if($this->attempt==3 ||$this->attempt==5){ $bottomright = 0; } if($this->attempt==1 ||$this->attempt==6){ $bottomright = 1; } if($this->attempt==2 ||$this->attempt==4){ $bottomright = 2; } if($bottomright>=$this->topleft){ $bottomright++; } //bottom left is whats left $possible = array(0,1,2,3); $used = array($this->topleft,$topright,$bottomright); $temp = array_diff($possible,$used); sort($temp); //remove keys $bottomleft = $temp[0]; //move around $newbox[0] = $this->box[$this->topleft];//new top left $newbox[1] = $this->box[$topright]; //echo '<pre>'.print_r($this->box[2],1).'</pre>'; $newbox[2] = $this->box[$bottomright]; $newbox[3] = $this->box[$bottomleft]; //echo '<pre>'.print_r($newbox,1).'</pre>'; $this->box =$newbox; return true; } function output(){ echo '<table border="1" width="200"> <tr> <td width="20"> </td><td width="60" align="center">'.$this->box[0][0].'</td><td width="20"> </td> <td width="20"> </td><td width="60" align="center">'.$this->box[1][0].'</td><td width="20"> </td> </tr> <tr> <td width="20" height="60">'.$this->box[0][3].'</td><td width="60" align="center"> </td><td width="20">'.$this->box[0][1].'</td> <td width="20">'.$this->box[1][3].'</td><td width="60" align="center"> </td><td width="20">'.$this->box[1][1].'</td> </tr> <tr> <td width="20"> </td><td width="60" align="center">'.$this->box[0][2].'</td><td width="20"> </td> <td width="20"> </td><td width="60" align="center">'.$this->box[1][2].'</td><td width="20"> </td> </tr> <tr> <td width="20"> </td><td width="60" align="center">'.$this->box[3][0].'</td><td width="20"> </td> <td width="20"> </td><td width="60" align="center">'.$this->box[2][0].'</td><td width="20"> </td> </tr> <tr> <td width="20" height="60">'.$this->box[3][3].'</td><td width="60" align="center"> </td><td width="20">'.$this->box[3][1].'</td> <td width="20">'.$this->box[2][3].'</td><td width="60" align="center"> </td><td width="20">'.$this->box[2][1].'</td> </tr> <tr> <td width="20"> </td><td width="60" align="center">'.$this->box[3][2].'</td><td width="20"> </td> <td width="20"> </td><td width="60" align="center">'.$this->box[2][2].'</td><td width="20"> </td> </tr> </table>'; } function solve(){ $solved=false; while($solved===false){ if($this->check()){ $solved=true; break; }else{ if(!($this->move())){ break; } } } if($solved){ echo 'problem solved:<br />'; $this->output(); }else{ echo 'problem could not be solved'; } } } //boxes: TL,TR,BR,BL //numbers: T,R,B,L $box[0] = array(0,6,2,0); $box[1] = array(0,0,5,6); $box[2] = array(5,0,0,2); $box[3] = array(2,2,0,0); new solve_problem($box); ?> Didn't comment it much, but its basically brute force. Only 24 posibilities, so i figure try them all. Most messy bit was the output, which is just simple for now. Gave it a go with one of the test data you posted. Found a different solution to the image, but a valid one nonetheless. Give it a go. Quote Link to comment https://forums.phpfreaks.com/topic/84531-solved-possible/#findComment-430756 Share on other sites More sharing options...
Dane Posted January 4, 2008 Author Share Posted January 4, 2008 Thanks a lot for your work and time. Im using this on my test server but nothing is outputting. Quote Link to comment https://forums.phpfreaks.com/topic/84531-solved-possible/#findComment-430762 Share on other sites More sharing options...
GingerRobot Posted January 4, 2008 Share Posted January 4, 2008 I wonder, is your test server running PHP 4 or 5? If its 4, try: <?php error_reporting(E_ALL); class solve_problem { var $box; var $topleft = 0;//number of box in top left - 0,1,2,3 var $attempt = 1;//attempts with box in top top left 0-6 function solve_problem($box){ $this->box = $box; $this->solve(); } function check(){ if($this->box[0][1] == $this->box[1][3] && $this->box[1][2] == $this->box[2][0] && $this->box[2][3] == $this->box[3][1] && $this->box[3][0] == $this->box[0][3]){ return true; }else{ return false; } } function move(){//this moves the boxes around in the 4*4 grid if(++$this->attempt==7){//tried 6 times with current number in top left, so change and reset $this->attempt = 1; if(++$this->topleft==4){ return false;//cannot work } } //work out top right $topright = ceil($this->attempt/2) - 1; if($topright>=$this->topleft){ $topright++; } //work out bottom right //lowest available on trials 3&5, middle on 1&6, highest on 2,4 if($this->attempt==3 ||$this->attempt==5){ $bottomright = 0; } if($this->attempt==1 ||$this->attempt==6){ $bottomright = 1; } if($this->attempt==2 ||$this->attempt==4){ $bottomright = 2; } if($bottomright>=$this->topleft){ $bottomright++; } //bottom left is whats left $possible = array(0,1,2,3); $used = array($this->topleft,$topright,$bottomright); $temp = array_diff($possible,$used); sort($temp); //remove keys $bottomleft = $temp[0]; //move around $newbox[0] = $this->box[$this->topleft];//new top left $newbox[1] = $this->box[$topright]; //echo '<pre>'.print_r($this->box[2],1).'</pre>'; $newbox[2] = $this->box[$bottomright]; $newbox[3] = $this->box[$bottomleft]; //echo '<pre>'.print_r($newbox,1).'</pre>'; $this->box =$newbox; return true; } function output(){ echo '<table border="1" width="200"> <tr> <td width="20"> </td><td width="60" align="center">'.$this->box[0][0].'</td><td width="20"> </td> <td width="20"> </td><td width="60" align="center">'.$this->box[1][0].'</td><td width="20"> </td> </tr> <tr> <td width="20" height="60">'.$this->box[0][3].'</td><td width="60" align="center"> </td><td width="20">'.$this->box[0][1].'</td> <td width="20">'.$this->box[1][3].'</td><td width="60" align="center"> </td><td width="20">'.$this->box[1][1].'</td> </tr> <tr> <td width="20"> </td><td width="60" align="center">'.$this->box[0][2].'</td><td width="20"> </td> <td width="20"> </td><td width="60" align="center">'.$this->box[1][2].'</td><td width="20"> </td> </tr> <tr> <td width="20"> </td><td width="60" align="center">'.$this->box[3][0].'</td><td width="20"> </td> <td width="20"> </td><td width="60" align="center">'.$this->box[2][0].'</td><td width="20"> </td> </tr> <tr> <td width="20" height="60">'.$this->box[3][3].'</td><td width="60" align="center"> </td><td width="20">'.$this->box[3][1].'</td> <td width="20">'.$this->box[2][3].'</td><td width="60" align="center"> </td><td width="20">'.$this->box[2][1].'</td> </tr> <tr> <td width="20"> </td><td width="60" align="center">'.$this->box[3][2].'</td><td width="20"> </td> <td width="20"> </td><td width="60" align="center">'.$this->box[2][2].'</td><td width="20"> </td> </tr> </table>'; } function solve(){ $solved=false; while($solved===false){ if($this->check()){//solution found $solved=true; break; }else{//no solution found if(!($this->move())){//all posibilities ended - cannot be solved break; } } } if($solved){ echo 'problem solved:<br />'; $this->output(); }else{ echo 'problem could not be solved'; } } } //boxes: TL,TR,BR,BL //numbers: T,R,B,L $box[0] = array(0,6,2,0); $box[1] = array(0,0,5,6); $box[2] = array(5,0,0,2); $box[3] = array(2,2,0,0); new solve_problem($box); ?> Quote Link to comment https://forums.phpfreaks.com/topic/84531-solved-possible/#findComment-430771 Share on other sites More sharing options...
Dane Posted January 4, 2008 Author Share Posted January 4, 2008 Very nice. Im am using 4 yes. I will upgrade hehe. Again thankyou very much for your time. Quote Link to comment https://forums.phpfreaks.com/topic/84531-solved-possible/#findComment-430773 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.