InPlace Transition of a matrix

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:

?





1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

#include<iostream>

#include<cstdio>

#include<string.h>

#include<cstring>

#include<string>

#include<algorithm>

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;

}

  

InPlace Transition of a matrix,布布扣,bubuko.com

时间: 2024-11-10 12:40:15

InPlace Transition of a matrix的相关文章

Python 解leetcode:48. Rotate Image

题目描述:把一个二维数组顺时针旋转90度: 思路: 对于数组每一圈进行旋转,使用m控制圈数: 每一圈的四个元素顺时针替换,可以直接使用Python的解包,使用k控制每一圈的具体元素: class Solution(object): def rotate(self, matrix): """ :type matrix: List[List[int]] :rtype: void Do not return anything, modify matrix in-place inst

48. 旋转图像

题目: 给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使用另一个矩阵来旋转图像. 之前在面试题中看了zip函数. 这里直接运用,一行代码. 1 class Solution: 2 def rotate(self, matrix: List[List[int]]) -> None: 3 """ 4 Do not return anything, modify matr

20191230-20200102总结(1)

LeetCode66. Plus One - Easy LeetCode67. Add Binary - Easy LeetCode69. Sqrt(x) - Easy 二分找 LeetCode70. Climbing Stairs - Easy dp LeetCode71. Simplify Path - Medium Given an absolute path for a file (Unix-style), simplify it. Or in other words, convert

LeetCode——48. 旋转图像

给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使用另一个矩阵来旋转图像. 示例 1: 给定 matrix = [ [1,2,3], [4,5,6], [7,8,9] ], 原地旋转输入矩阵,使其变为: [ [7,4,1], [8,5,2], [9,6,3] ] 示例 2: 给定 matrix = [ [ 5, 1, 9,11], [ 2, 4, 8,10], [13, 3, 6, 7], [

[LeetCode] Pyramid Transition Matrix 金字塔转变矩阵

We are stacking blocks to form a pyramid. Each block has a color which is a one letter string, like `'Z'`. For every block of color `C` we place not in the bottom row, we are placing it on top of a left block of color `A` and right block of color `B`

73. Set Matrix Zeroes &amp;&amp; 289. Game of Life

73. Set Matrix Zeroes Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Hide Tags Array Hide Similar Problems (M) Game of Life public class Solution { //Only consider the zeros that exist originally. public

[LintCode] Set Matrix Zeros

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Example Given a matrix [ [1,2], [0,3] ], return[[0,2],[0,0]] Challenge Did you use extra space?A straight forward solution using O(mn) space is probably a b

CSS动画 animation与transition

一.区分容易混淆的几个属性和值 先区分一下css中的几个属性:animation(动画).transition(过渡).transform(变形).translate(移动). CSS3中的transform(变形)属性用于内联元素和块级元素,可以旋转.扭曲.缩放.移动元素,它的属性值有以下五个:旋转rotate.扭曲skew.缩放scale和移动translate以及矩阵变形matrix: transform(变形)是CSS3中的元素的属性,而translate只是transform的一个属性

C++_Eigen函数库用法笔记——Matrix and Vector Arithmetic

Addition and subtraction Scalar multiplication and division Transposition Matrix-matrix and matrix-vector multiplication Trace(求迹的和)   Addition and subtraction binary operator + as in a+b binary operator - as in a-b unary operator - as in -a compound