Download CS1020E discussion slides for Lab 1 problems

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Human height wikipedia , lookup

Computational phylogenetics wikipedia , lookup

Rotation matrix wikipedia , lookup

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 j1
01 2
0 1 2 3
0
1
1 4 5 6
1
2
2 7 8 9
2
3
i1
i2
j2
0
0 7
1 8
2 9
1
4
5
6
2
1
2
3
i2
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)
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
 Reflect by X-axis
0 1 2
j1
0 7 4 1
0
1 8 5 2
1
2 9 6 3
2
i1

Reflect by Y-axis
for reflection
0
9
8
7
i2
1
6
5
4
2
3
2
1
j2
0
0 7
1 8
2 9
i1

1
4
5
6
2
1
2
3
j1
0
0 1
1 2
2 3
1
4
5
6
2
7
8
9
j2
i2
Do you see any pattern?
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; i<n; i++){
if(height[i]>tallHeight){
tallHeight=height[i];
tallWeight=weight[i];
tallName=name[i];
}
likewise for short;
}
double getBMI() {
return (double)weight / (double) (height * height / 10000.0);
}
For 2 decimal points, you can use C-style printf
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