cs1020 lab 1 discussion. given a matrix of size n x n and some operations of rotation or...

32
CS1020 Lab 1 Discussion

Upload: heather-gallagher

Post on 13-Jan-2016

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

CS1020 Lab 1 Discussion

Page 2: 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

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

Page 3: 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

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

Page 4: 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

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?

Page 5: 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

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?

Page 6: 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

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

Page 7: 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

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

Page 8: 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

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

Page 9: 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

- Rotation 180 (clockwise)= 2 x Rotation 90 (clockwise).- Rotation 270 (clockwise)= 3 x Rotation 90 (clockwise).

Page 10: 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

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

Page 11: 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

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

Page 12: 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

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]; } }}

Page 13: 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

CS1020 Sit-in Lab #1 (AY2011/2012 SEM2)

Problem 2 - Land

Page 14: 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

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

Page 15: 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

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)?

Page 16: 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

16

Page 17: 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

Implementation

17

Page 18: 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

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

Page 19: 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

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

Page 20: 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

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

Page 21: 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

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.

Page 22: 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

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

Page 23: 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

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

Page 24: 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

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

Page 25: 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

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

Page 26: 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

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.

Page 27: 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

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.

Page 28: 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

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

Page 29: 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

Warning!!!

Do not count the single letter palindromes multiple times

Page 30: 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

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

Page 31: 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

The End…

Well, for the presentation that is…

You still need to implement the solutions

Page 32: 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

Good Luck! :)