Jump to content

Archived

This topic is now archived and is closed to further replies.

dougal85

2D Array/Matrix

Recommended Posts

[!--fonto:Verdana--][span style=\"font-family:Verdana\"][!--/fonto--]
Hi,

[b]First[/b] post! :)

I am having a problem with my PHP.

I am trying to make a [b]8x8[/b] grid, with boolean values that i can then check and count how many true values are in each column, row and diagonal.

At the moment i have managed to get the grid/matrix successfully and also i have managed to check the rows and columns. So thats all cool.

However i am having trouble working out the best way to check the diagonals.

I have noticed that there is a bit of a patern with them, for example some of them as you follow it through either the x or the y value is going down and the other is going up.

I just dont know how best to put this into code.

Any suggestions would be greatly appreciated!

Thanks
Dougal[!--fontc--][/span][!--/fontc--]

Share this post


Link to post
Share on other sites
to traverse a 2d array(must be a i x i array)
[code]<?php

$array = array(array(1,0,1,0),array(1,0,0,0),array(0,0,1,1),array(1,0,1,1);

$arrsize = count($array[0]);

$tlbrcount = NULL; var to store number of true values in tl - br diag
$bltrcount = NULL; var to store number of true values in bl - tr diag

for ($i = 0; $i < $arrsize; $i++) {

//tl - br loop
$tlbrcont = $array[$i][$i] == true ?  $tlbrcont + 1 : $tlbrcont;

//bl - tr loop
$bltrcont = $array[$i][($arrsize - 1) - $i] == true ?  $bltrcont + 1 : $bltrcont;

}

?>[/code]

There you go - took me 5 minutes to post that!!!!!!!! should start charging.

Share this post


Link to post
Share on other sites
Cool! thats really good! thanks

However, am i right in thinking thats the diagonals from the opposite corners.

I also need to check the other diagonals in the array - they are whats getting me stuck the most!

Any further suggestions?

thats a cool start! thanks

Dougal

Share this post


Link to post
Share on other sites
so ALL diagonlas?

-ie any diagonal starting from an element on the outside of the matrix?

from top left to bottom right? or in all directions?

Share this post


Link to post
Share on other sites
[!--quoteo(post=368698:date=Apr 26 2006, 03:04 AM:name=ToonMariner)--][div class=\'quotetop\']QUOTE(ToonMariner @ Apr 26 2006, 03:04 AM) [snapback]368698[/snapback][/div][div class=\'quotemain\'][!--quotec--]
so ALL diagonlas?

-ie any diagonal starting from an element on the outside of the matrix?

from top left to bottom right? or in all directions?
[/quote]

all directions.

basically, items need to be placed into the matrix... however no more than two can be in any diagonal line same with horizontal and vertical.

ie. this example has a diagonal with 3

0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0
0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0

*EDIT* i realise that example is quite hard to see, but you should get the idea :)

Share this post


Link to post
Share on other sites
Hello,

I am not sure if i understand it correctly. still no harm in trying....

to check for any diagonal elements, you will have to navigate thru each element of the array and check diagonal elements for each.

example : suppose the current array element u r checking is


arr( x , y )

the possible digonal elements are

arr ( x-1, y-1 )
arr ( x+1, y-1 )
arr ( x-1, y+1 )
arr ( x+1, y+1 )

out of which i think you need not check for arr ( x-1, y-1 ) and arr ( x+1, y-1 ) as while navigating if you start from x=0,y=0 these 2 elements would have been already checked. so u need to check only the remaining 2 elements. if any of these 2 is same as the arr( x , y ) then you have ur diagonals

hth

sorry if i confused u more :) was just trying to help :)

Share this post


Link to post
Share on other sites
[!--quoteo(post=368792:date=Apr 26 2006, 12:22 PM:name=samshel)--][div class=\'quotetop\']QUOTE(samshel @ Apr 26 2006, 12:22 PM) [snapback]368792[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Hello,

I am not sure if i understand it correctly. still no harm in trying....

to check for any diagonal elements, you will have to navigate thru each element of the array and check diagonal elements for each.

example : suppose the current array element u r checking is
arr( x , y )

the possible digonal elements are

arr ( x-1, y-1 )
arr ( x+1, y-1 )
arr ( x-1, y+1 )
arr ( x+1, y+1 )

out of which i think you need not check for arr ( x-1, y-1 ) and arr ( x+1, y-1 ) as while navigating if you start from x=0,y=0 these 2 elements would have been already checked. so u need to check only the remaining 2 elements. if any of these 2 is same as the arr( x , y ) then you have ur diagonals

hth

sorry if i confused u more :) was just trying to help :)
[/quote]

omg, thats actually a really good idea. I think i can see how that would works, since i know the upper and lower limits i think i should be able to get some sort of loop to go through all that.

The only thing is it will probably end up checking all the diagonals multiple times but i could put it in my documentation that its doing that just to be sure! hehe :)

hmm, guess i could try and work it out to see what the minimum number of elements i would need to check for it.

Thanks! I'm gonna give this a shot tonight.

Share this post


Link to post
Share on other sites
Hello,

as i said,

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
out of which i think you need not check for arr ( x-1, y-1 ) and arr ( x+1, y-1 ) as while navigating if you start from x=0,y=0 these 2 elements would have been already checked. so u need to check only the remaining 2 elements. if any of these 2 is same as the arr( x , y ) then you have ur diagonals
[/quote]

u need not check arr ( x-1, y-1 ) and arr ( x+1, y-1 ) , so u will not check repeatedly....:) ...give it a shot, i think it should work.

hth

Share this post


Link to post
Share on other sites
[!--quoteo(post=368807:date=Apr 26 2006, 01:19 PM:name=samshel)--][div class=\'quotetop\']QUOTE(samshel @ Apr 26 2006, 01:19 PM) [snapback]368807[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Hello,

as i said,
u need not check arr ( x-1, y-1 ) and arr ( x+1, y-1 ) , so u will not check repeatedly....:) ...give it a shot, i think it should work.

hth
[/quote]


sorry i didnt see that!

Thanks, i'm just going to give it a shot now - i'll let you know how i get on.

Share this post


Link to post
Share on other sites
also realised that;

(x-1, y+1) only needs start from the elements on the left and top
(x+1, y+1) only needs to start on the elements on the left and bottom.

just coding it now, fingers crossed

Share this post


Link to post
Share on other sites

×

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.