# [Help]:一个矩阵的问题,高分求一个算法

Pudgy 2004-08-11 07:47:41
1 2 3
4 5 6
7 8 9

3 6 9
2 5 8
1 4 7

7 4 1
8 5 2
9 6 3

...全文
169 3 点赞 打赏 收藏 举报

3 条回复

Rotate a 3*3 array of int by 90 degrees clockwise

//http://www.cnblogs.com/lovecreatesbeauty/archive/2004/09/25/46508.aspx
//Rotate a 3*3 array of int by 90 degrees clockwise.
void rotation_90(int (*array)[3])
{
const int ROW=3;
const int COL=3;
int array_tmp[ROW][COL];

//Make a copy.
for(int i=0; i<ROW; i++)
for(int j=0; j<COL; j++)
array_tmp[i][j]=array[i][j];

//Rotation
for(int i=0; i<ROW; i++)
for(int j=0; j<COL; j++)
array[j][COL-1-i]=array_tmp[i][j];
}

• 打赏
• 举报

metaphor 2004-10-07
#include <iostream.h>
const int max(3);
void display(int m[max][max],int size){
for(int i=0;i<size;i++){
for(int j=0;j<size;j++)
cout<<m[i][j]<<" ";
cout<<endl;
}
}
void Clockwise(int m[max][max],int size){
int temp;
for(int i=0;i<(size+1)/2;i++){
for(int j=0;j<size/2;j++){
temp=m[i][j];
m[i][j]=m[size-j-1][i];
m[size-j-1][i]=m[size-i-1][size-j-1];
m[size-i-1][size-j-1]=m[j][size-i-1];
m[j][size-i-1]=temp;
}
}
}

void AntiClockwise(int m[max][max],int size){
int temp;
for(int i=0;i<(size+1)/2;i++){
for(int j=0;j<size/2;j++){
temp=m[j][size-i-1];
m[j][size-i-1]=m[size-i-1][size-j-1];
m[size-i-1][size-j-1]=m[size-j-1][i];
m[size-j-1][i]=m[i][j];
m[i][j]=temp;
}
}
}

main(){
int n=max;
int m[max][max]={
{1,2,3},
{4,5,6},
{7,8,9}
};

display(m,n);
cout<<endl;
AntiClockwise(m,n);
display(m,n);
Clockwise(m,n);
Clockwise(m,n);
cout<<endl;
display(m,n);

return 0;
}
• 打赏
• 举报

BillOB 2004-08-12
This depends on the data structure u use to store matrix .If i implement the matrix myself i would set a pointer to the head(the virtual [0,0] entry) and a rule to determine which way should the matrix be looked at.

e.g:
1 2 3 <----Pointer to head is [0,2] ,rule is VERTICAL.Thus we have represented the
4 5 6 the following matrix: 3 6 9 (A counter-clockwise transformation as u
7 8 9 2 5 8 mentioned)
1 4 7

We then only need an algorithm to translate input index(Vitual index say [1,2]) to its physcial index (e,g:[2,1]).This algorithm should be easy to figure out.(Simply use the rotation matrix on the input index [x,y])
• 打赏
• 举报

3.2w+

2004-08-11 07:47