Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1020 Lab 1 Discussion.

Similar presentations


Presentation on theme: "CS1020 Lab 1 Discussion."β€” Presentation transcript:

1 CS1020 Lab 1 Discussion

2 Problem 1: Transformation
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.

3 Rotate by 90 degree We process all the numbers in the original matrix one by one, using a for loop. j1 j2 j2 i1 i2 i2

4 Do you see any pattern? 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) #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?

5 Do you see any pattern? 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) #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?

6 Number Before Rotation After Rotation 1 2 3 4 5 6 7 8 9

7 Number Before Rotation After Rotation 1 2 3 4 5 6 7 8 9

8 π‘₯β€² 𝑦′ = cos πœƒ sin πœƒ βˆ’ sin πœƒ cos πœƒ π‘₯ 𝑦 π‘₯β€² 𝑦′ = 0 1 βˆ’1 0 π‘₯ 𝑦
π‘₯β€² 𝑦′ = βˆ’ π‘₯ 𝑦 π‘₯β€² 𝑦′ = 𝑦 βˆ’π‘₯ Number Before Rotation After Rotation 1 2 3 4 5 6 7 8 9 Why Is It So? Interested? Take MA1101R 

9 - Rotation 180 (clockwise)= 2 x Rotation 90 (clockwise).

10 degree = input/90 void rotate(int degree) { int[][] temp = new int[size][size]; while (degree > 0) { for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { temp[j][size i] = matrix[i][j]; } degree--; //decrement the β€œdegree” //don’t forget to replace the content of matrix with temp

11 There is also a pattern for reflection Reflect by X-axis 0 1 2 0 1 2
Reflect by Y-axis j1 j2 i1 i2

12 void reflectX() { int[][] temp = new int[size][size]; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { temp[i][j] = matrix[size i][j]; }

13 Problem 2 - Land CS1020 Sit-in Lab #1 (AY2011/2012 SEM2)

14 Given an NxN square with trees occupying some cells.
Find the length of the largest square with no trees inside. (Find the number of squares having length S and exactly k trees inside) 14 14

15 Why not the other way (i.e. from 1x1 to NxN)?
Solution Read & store input: Tree data: Create array NxN to store all the position of trees Exhaustive search! For each possible size (NxN to 1x1) For each square of that size in the grid (e.g. 2x2 square, 3x3 square) Check if the square has no trees in it Why not the other way (i.e. from 1x1 to NxN)? 15 15

16 16 16

17 Implementation 17 17

18 R x C matrix with each cell containing a lower-case letter
Problem 3 – Counting Palindromes Given: Find the number of palindromes that are constructed horizontally, vertically, or diagonally. R x C matrix with each cell containing a lower-case letter a b s d f q w e r R C 18 18

19 What is a palindrome? AΒ string is called a palindromeΒ if it is read the same backward or forward. algorithms refer radar fly level science

20 What is a palindrome? AΒ string is called a palindromeΒ if it is read the same backward or forward. algorithms refer radar fly level science

21 Solution Read & store input: Brute-Force Solution:
2-dimensional array of size R*C to represent each cell in its respective row and column. Use a separate isPalindrome(String) function to decide if a given string is a palindrome. Then, iterate through all rows, columns, and diagonals by executing this function on all possible strings in the matrix. Accumulate the total in a variable. 21 21

22 a b s d f q w e r a b s d f q w e r a b s d f q w e r a b s d f q w e r

23 a b s d f q w e r a b s d f q w e r a b s d f q w e r a b s d f q w e r

24 a b s d f q w e r a b s d f q w e r a b s d f q w e r a b s d f q w e r

25 Just loop through each rows and construct the string
Solution Generating the rows and columns is easy: Just loop through each rows and construct the string a b s d f q w e r for each row i, column j: i = 0 j = 3 i = 0 j = 2 i = 0 j = 1 i = 0 j = 0 appendedString = abba appendedString = abb appendedString = ab appendedString = a 25

26 Constructing Diagonals
Start from the leftmost point, and try to extend to the lower right if that cell is still inside the matrix. String constructDiagonal(int x, int y) { String current = ""; while (isInside(x, y)) { current += matr[x][y]; x += 1; //advance y += 1; } return current;

27 Constructing Diagonals
Start from the leftmost point, and try to extend to the lower right if that cell is still inside the matrix. String constructDiagonal(int x, int y) { String current = ""; while (isInside(x, y)) { current += matr[x][y]; x += 1; //advance y += 1; } return current;

28 Constructing Diagonals
b s d f q w e r while isInside(x,y) x = 2 y = 2 x = 1 y = 1 x = 0 y = 0 x = 3 y = 3 appendedString = aae appendedString = a appendedString = aa

29 Do not count the single letter palindromes multiple times
Warning!!! Do not count the single letter palindromes multiple times

30 The isPalindrome(String) function
If String length is even, start from position (n/2) and (n/2 + 1). Check if they are of the same value. Repeat until the whole string is checked. If length is odd, disregard the middle letter and iterate from floor(n/2 -1) and ceil(n/2 + 1). V1 N O O N N O O N M A K A N M A K A N Idea: if the string β€œaaa” is a palindrome, is β€œXaaaX” a palindrome as well if X is equal to X? V2

31 Well, for the presentation that is…
The End… Well, for the presentation that is… You still need to implement the solutions 

32 Good Luck! :)

33


Download ppt "CS1020 Lab 1 Discussion."

Similar presentations


Ads by Google