博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
InPlace Transition of a matrix
阅读量:6280 次
发布时间:2019-06-22

本文共 2641 字,大约阅读时间需要 8 分钟。

Problem illustration:

given a n*n matrix, print its transition, for example , 90 degree clockwise,using only constant additional space

analysis:

using O(n^2) space is common,but for constant assistant space, we need to adopt constant space

solution:

use in place replacement, 

take

1 2 3 4 5

6 7 8 9 10

11 12 13 14 15

16 17 18 19 20

for example

the expected output is

21 16 11 6 1

22 17 12 7 2
23 18 13 8 3
24 19 14 9 4
25 20 15 10 5

after transition,value (0,0) is expected to be transferred to pos(0,4),while(0,4)→(4,4),(4,4) to (4,0),(4,0)to (0,0),which is the same with our 

so this four elements are called a cycle(for more clear explanation, please refer to situ permutation)

 

two keypoints:

1.with the same example,since we use (0,0) to cover (4,0),then the original value at (4,0) must be stored before coverage or the whole cycle will be initialized

to the cycle header

2.edge condition handling

 

source code for above example:

#include
#include
#include
#include
#include
#include
using namespace std;const int maxn = 100;int matrix1[maxn][maxn];int matrix2[maxn][maxn];void trans(int h,int w){ for(int i = 0; i < h; ++i) { for(int j = 0; j < w; ++j) { //要旋转的元素是m1[i+1][j+1],转过去的位置是m2[j][n-i] matrix2[j][w-i-1] = matrix1[i][j]; } } return;}inline void getPos(int orix,int oriy,int *newx,int *newy){ (*newx) = oriy; (*newy) = 4 - orix;// return 0;}void inPlaceTrans(int i,int j){ //(i,j)为转换的起点 int total = 25; int cnt = 0; int x = -1,y = -1; int orix = i,oriy = j; bool visit[5][5]; memset(visit,0,sizeof(visit)); visit[i][j] = 1; int tmp1,tmp2; while(cnt < total){ //我去,移反了,所有的值都initialize成圈头值了 //a new cycle orix = i; oriy = j; getPos(i,j,&x,&y); //(i,j)的目标地址 visit[i][j] = true;//这个位置的元素已经被移到目标地址 tmp1 = matrix1[i][j]; while(!(x == orix && y == oriy)){ //这个圈没有到头 tmp2 = matrix1[x][y]; matrix1[x][y] = tmp1; ++cnt; //移过去了一个元素 i = x; j = y; visit[i][j] = true; tmp1 = tmp2; getPos(i,j,&x,&y); } matrix1[x][y] = tmp1; bool get = false; for(int u = 0; u < 5; ++u){ for(int v = 0; v < 5; ++v){ if(visit[u][v] == 0){ //这个元素还没有被移到目标地址 i = u; j = v; get = true; break; } } if(get) break; } if(!get){ //所有元素都被移到目标地址 break; } }}int main(){ freopen("1.txt","r",stdin); freopen("2.txt","w",stdout); cout << "helloworld\n"; for(int i = 0; i < 5; ++i) { for(int j = 0; j < 5; ++j) { scanf("%d",matrix1[i]+j); } } inPlaceTrans(0,0); for(int i = 0; i < 5; ++i) { for(int j = 0; j < 5; ++j) { printf("%d ",matrix1[i][j]); } cout << endl; } cout << "hello world\n"; return 0;}

  

 

 

 

 

转载于:https://www.cnblogs.com/warmfrog/p/3695011.html

你可能感兴趣的文章
Linux系统固定IP配置
查看>>
配置Quartz
查看>>
Linux 线程实现机制分析
查看>>
继承自ActionBarActivity的activity的activity theme问题
查看>>
设计模式01:简单工厂模式
查看>>
项目经理笔记一
查看>>
Hibernate一对一外键双向关联
查看>>
mac pro 入手,php环境配置总结
查看>>
MyBatis-Plus | 最简单的查询操作教程(Lambda)
查看>>
rpmfusion 的国内大学 NEU 源配置
查看>>
spring jpa 配置详解
查看>>
IOE,为什么去IOE?
查看>>
java 用反射简单应用,将Object简单转换成map
查看>>
Storm中的Worker
查看>>
dangdang.ddframe.job中页面修改表达式后进行检查
查看>>
Web基础架构:负载均衡和LVS
查看>>
Linux下c/c++相对路径动态库的生成与使用
查看>>
SHELL实现跳板机,只允许用户执行少量允许的命令
查看>>
SpringBoot 整合Redis
查看>>
2014上半年大片早知道
查看>>