[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

要求:对任意的N*N矩阵,均可计算。(2<=n<=1000)

...全文
193 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
LoveCreatesBeauty 2004-10-07
  • 打赏
  • 举报
回复
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])

33,006

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧