cs1020 lab 1 discussion. given a matrix of size n x n and some operations of rotation or...
TRANSCRIPT
CS1020 Lab 1 Discussion
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.
Problem 1: Transformation
Rotate by 90 degree0 1 2 0 1 2 0 1 2
0 1 2 3 0 1 0 7 4 1 1 4 5 6 1 2 1 8 5 2
2 7 8 9 2 3 2 9 6 3
We process all the numbers in the original matrix one by one, using a for loop.
j1
i1 i2
j2
i2
j2
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?
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?
Number Before Rotation After Rotation
1 0 0 2 0
2 1 0 2 1
3 2 0 2 2
4 0 1 1 0
5 1 1 1 1
6 2 1 1 2
7 0 2 0 0
8 1 2 0 1
9 2 2 0 2
Number Before Rotation After Rotation
1 0 0 2 0
2 1 0 2 1
3 2 0 2 2
4 0 1 1 0
5 1 1 1 1
6 2 1 1 2
7 0 2 0 0
8 1 2 0 1
9 2 2 0 2
Why Is It So?
[𝑥 ′𝑦 ′ ]=[ cos𝜃 sin 𝜃− sin𝜃 cos𝜃] [𝑥𝑦 ]
Interested? Take MA1101R
[𝑥 ′𝑦 ′ ]=[ 0 1−1 0 ][𝑥𝑦 ][𝑥 ′𝑦 ′ ]=[ 𝑦−𝑥 ]
Number Before Rotation After Rotation
1 0 0 2 0
2 1 0 2 1
3 2 0 2 2
4 0 1 1 0
5 1 1 1 1
6 2 1 1 2
7 0 2 0 0
8 1 2 0 1
9 2 2 0 2
- Rotation 180 (clockwise)= 2 x Rotation 90 (clockwise).- Rotation 270 (clockwise)= 3 x Rotation 90 (clockwise).
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 - 1 - i] = matrix[i][j]; }}degree--; //decrement the “degree”
} //don’t forget to replace the content of matrix with temp}
degree = input/90
There is also a pattern for reflection Reflect by X-axis
0 1 2 0 1 2 0 7 4 1 0 9 6 3 1 8 5 2 1 8 5 2
2 9 6 3 2 7 4 1
Reflect by Y-axis
j1
i1
j2
i2
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 - 1 - i][j]; } }}
CS1020 Sit-in Lab #1 (AY2011/2012 SEM2)
Problem 2 - Land
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
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
15
Why not the other way (i.e. from 1x1 to NxN)?
16
Implementation
17
Given:
Find the number of palindromes that are constructed horizontally, vertically, or diagonally.
18
a b b a
s a d f
q w e r
R x C matrix with each cell containing a lower-case letter
R
C
Problem 3 – Counting Palindromes
What is a palindrome?A string is called a palindrome if it is
read the same backward or forward.
algorithms
radar
level
refer
fly
science
What is a palindrome?A string is called a palindrome if it is
read the same backward or forward.
algorithms
radar
level
refer
fly
science
Solution Read & store input:
Brute-Force Solution:
21
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.
2-dimensional array of size R*C to represent each cell in its respective row and column.
a b b a
s a d f
q w e r
a b b a
s a d f
q w e r
a b b a
s a d f
q w e r
a b b a
s a d f
q w e r
a b b a
s a d f
q w e r
a b b a
s a d f
q w e r
a b b a
s a d f
q w e r
a b b a
s a d f
q w e r
a b b a
s a d f
q w e r
a b b a
s a d f
q w e r
a b b a
s a d f
q w e r
a b b a
s a d f
q w e r
Solution Generating the rows and columns is easy:
i = 0j = 0
a b b a
s a d f
q w e r
appendedString = ai = 0j = 1i = 0j = 2i = 0j = 3
for each row i, column j:
appendedString = abappendedString = abbappendedString = abba
Just loop through each rows and construct the string
Constructing Diagonals
String constructDiagonal(int x, int y) {
String current = "";
while (isInside(x, y)) {
current += matr[x][y];
x += 1; //advance
y += 1;
}
return current;
}
Start from the leftmost point, and try to extend to the lower right if that cell is still inside the
matrix.
Constructing Diagonals
String constructDiagonal(int x, int y) {
String current = "";
while (isInside(x, y)) {
current += matr[x][y];
x += 1; //advance
y += 1;
}
return current;
}
Start from the leftmost point, and try to extend to the lower right if that cell is still inside the
matrix.
x = 0y = 0
a b b a
s a d f
q w e r
appendedString = a
while isInside(x,y)
Constructing Diagonals
x = 1y = 1
appendedString = aax = 2y = 2
appendedString = aaex = 3y = 3
Warning!!!
Do not count the single letter palindromes multiple times
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
Idea: if the string “aaa” is a palindrome, is “XaaaX” a palindrome as well if X is equal to X?
V2
M A K A NN O O NN O O N M A K A N
The End…
Well, for the presentation that is…
You still need to implement the solutions
Good Luck! :)