flip a 2d array
I'm trying to get a 2D array to a form, so I can work with it like I need. I just cant modify it correctly..
Say I have a 2D array:
0 1 0 1 0
0 0 1 1 1
0 0 0 3 0
0 0 0 0 1
0 开发者_如何学C0 0 0 0
How can I flip it to its symmetrical, so at the moment position 0,1 is 1 and therefore position 1,0 would be 1?
does this make seance?
I need to do this so I can work out Euler tour
You could just access it in a flipped way.
x = 3;
y = 5;
// Ask for x,y element
$normal = $myarray[$x][$y];
// Ask for x,y in the flipped array by asking for y,x
$flipped_access = $myarray[$y][$x];
Something like this (pseudo code)?
for x = 0 to width
for y = 0 to x-1
swap( array[x][y], array[y][x] )
Although depending on what your environment is, there might be special methods to do just that.
Isn't it same as transposing a square matrix?
(for i=0; i < order; i++) {
for(j =i; j < order; j++) {
temp = arr[i][j];
arr[i][j] = arr[j][i];
arr[j][i] = temp;
}
}
精彩评论