Jump to content

[SOLVED] Possible


Dane

Recommended Posts

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.

 

problem.gif

 

I know its a long shot.

 

If PHP cannot do it do any of you know how to achieve this?

 

Cheers

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 :D

Thanks again guys.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.