#  Given a matrix of size N x N and some operations of rotation or reflection, determine the final state of the matrix.  The operations can be: ◦ Rotate.

## Presentation on theme: " Given a matrix of size N x N and some operations of rotation or reflection, determine the final state of the matrix.  The operations can be: ◦ Rotate."— Presentation transcript:

 Given a matrix of size N x N and some operations of rotation or reflection, determine the final state of the matrix.  The operations can be: ◦ Rotate by X degree, X = 90, 180, or 270 ◦ Reflect across the x-axis. ◦ Reflect across the y-axis.

 Rotate by 90 degree 0 1 2 0 1 2 0 1 2 01 2 3 0 1 0 7 4 1 14 5 6 1 2 1 8 5 2 27 8 9 2 3 2 9 6 3 We process all the numbers in the original matrix one by one, using a for loop. Let (j1, i1) be the coordinate of a number. #1 is at (0, 0), after rotation of 90 degree clockwise, it will be at index (2, 0) j1 i1i2 j2 i2 j2

#2 is at (1, 0), after rotation of 90 degree clockwise, it will be at index (2, 1) #3 is at (2, 0), after rotation of 90 degree clockwise, it will be at index (2, 2) Do you see any pattern? - Rotation 180 (clockwise)= 2 x Rotation 90 (clockwise). - Rotation 270 (clockwise)= 3 x Rotation 90 (clockwise). Hence, we only need to know 90 degree rotation clockwise

void rotate(int degree) { int temp[size][size]; while (degree > 0) { for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { temp[j][size - 1 - i] = matrix[i][j]; }

There is also a pattern for reflection  Reflect by X-axis 0 1 2 07 4 1 0 9 6 3 18 5 2 1 8 5 2 29 6 3 2 7 4 1  Reflect by Y-axis j1 i1 j2 i2

0 1 2 07 4 1 0 1 4 7 18 5 2 1 2 5 8 29 6 3 2 3 6 9  Do you see any pattern? j1 i1 j2 i2 void reflectX() { int temp[size][size]; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { temp[i][j] = matrix[size - 1 - i][j]; }

 Given a group of people with different heights and weights, determine the tallest and shortest person, compute their BMI  You don’t really need sorting in this problem, just compare which person is the tallest and shortest, then compute their BMI.

tallName, tallHeight=-1, tallWeight; shortName, shortHeight=1e9, shortWeight; for(i=0; itallHeight){ tallHeight=height[i]; tallWeight=weight[i]; tallName=name[i]; } likewise for short; }

For 2 decimal points, you can use C-style printf double getBMI() { return (double)weight / (double) (height * height / 10000.0); } printf(“%.2f”, answer); Format the answer with 2 decimal places in its decimal representation

 Given a square of NxN with trees occupy cells.  Find the length of largest square with no tree  Find the number of squares having length S and exactly k trees inside

 Read & store input: ◦ Tree data: Create array NxN to store all the position of trees  Exhaustive search! 12

 Try all possible sizes of the square that we want to test (the size can be from 1 to N)  Try all possible starting point of the square, i.e. by using nested loop (x, y) to fix the upper-left coordinate of the square.  Check whether the square with upper-left coordinate (x, y) and fixed size has no trees.

14

Download ppt " Given a matrix of size N x N and some operations of rotation or reflection, determine the final state of the matrix.  The operations can be: ◦ Rotate."

Similar presentations