| Task: | Fraktaali |
| Sender: | HeikkiSimojoki |
| Submission time: | 2017-10-14 21:50:48 +0300 |
| Language: | C++ |
| Status: | READY |
| Result: | 100 |
| group | verdict | score |
|---|---|---|
| #1 | ACCEPTED | 10 |
| #2 | ACCEPTED | 10 |
| #3 | ACCEPTED | 10 |
| #4 | ACCEPTED | 10 |
| #5 | ACCEPTED | 10 |
| #6 | ACCEPTED | 10 |
| #7 | ACCEPTED | 10 |
| #8 | ACCEPTED | 10 |
| #9 | ACCEPTED | 10 |
| #10 | ACCEPTED | 10 |
| test | verdict | time | group | |
|---|---|---|---|---|
| #1 | ACCEPTED | 0.05 s | 1 | details |
| #2 | ACCEPTED | 0.05 s | 2 | details |
| #3 | ACCEPTED | 0.06 s | 3 | details |
| #4 | ACCEPTED | 0.06 s | 4 | details |
| #5 | ACCEPTED | 0.05 s | 5 | details |
| #6 | ACCEPTED | 0.06 s | 6 | details |
| #7 | ACCEPTED | 0.05 s | 7 | details |
| #8 | ACCEPTED | 0.06 s | 8 | details |
| #9 | ACCEPTED | 0.05 s | 9 | details |
| #10 | ACCEPTED | 0.06 s | 10 | details |
Compiler report
input/code.cpp: In function 'Fractal* make_fractal(int)': input/code.cpp:49:1: warning: control reaches end of non-void function [-Wreturn-type] } ^
Code
#include <iostream>
#include <cmath>
#include <vector>
//Canvas where we'll draw the fractals.
std::vector<std::vector<bool>> canvas;
//W is the width of the fractal in characters, global of simplicity's sake
int w;
struct Fractal{
//Upper left, upper right, lower left, lower right. These are nullprts if the fractal is a leaf node in the tree
Fractal *l1, *r1, *l2, *r2;
//False='#', true='.'
bool flipped;
void flip(){
flipped = !flipped;
if(l1){
l1->flip(); r1->flip(); l2->flip(); r2->flip();
}
}
};
//Creates a fractal of dimension n
Fractal* make_fractal(int n){
Fractal* frac = new Fractal;
frac->flipped = false;
if(n == 1){
frac->l1 = frac->r1 = frac->l2 = frac->r2 = nullptr;
return frac;
}
if(n != 1){
n--;
//Optimization, 3 out of 4 fractals are the same so why use the memory
frac->l1 = frac->r1 = frac->l2 = make_fractal(n);
// frac->r1 = make_fractal(n);
// frac->l2 = make_fractal(n);
frac->r2 = make_fractal(n);
frac->r2->flip();
return frac;
}
}
//Draws a character onto the canvas, the coordinate system uses the bottom left corner as origin
void draw(int x, int y, bool flipped){
canvas[w - y - 1][x] = flipped;
};
//Draws a fractal. Works by calling itself on all of the fractal's components, and shrinking the area where the fractal is.
//x1 and x2 are the minimum and maximum coordinates of the fractal, they get smaller when one goes deeper into the fractal, until they hit equality at the leaf node
//y1 and y2 work the same way
void drawFractal(Fractal* frac, int x1, int x2, int y1, int y2){
if(frac->l1 != nullptr)
{
drawFractal(frac->l1, x1, x1 + (x2-x1+1)/2-1, y1 + (y2-y1+1)/2, y2);
drawFractal(frac->r1, x1 + (x2-x1+1)/2, x2, y1 + (y2-y1+1)/2, y2);
drawFractal(frac->l2, x1, x1 + (x2-x1+1)/2, y1, y1 + (y2-y1+1)/2);
drawFractal(frac->r2, x1 + (x2-x1+1)/2, x2, y1, y1 + (y2-y1+1)/2);
}
else{
draw(x1, y1, frac->flipped);
}
}
int main(){
int n;
std::cin >> n;
w = pow(2, n - 1);
//Init the canvas
for(int i = 0; i < w; i++){
canvas.push_back(std::vector<bool>(w));
}
//Make the fractal
Fractal* frac = make_fractal(n);
//Draw the fractal onto the canvas, use the full canvas for the first coordinates
drawFractal(frac, 0, w-1, 0, w-1 );
//Finally output the canvas
for(int y = 0; y < w; y++){
for(int x = 0; x < w; x++){
std::cout << (canvas[y][x] ? "." : "#");
}
std::cout << "\n";
}
}Test details
Test 1
Group: 1
Verdict: ACCEPTED
| input |
|---|
| 1 |
| correct output |
|---|
| # |
| user output |
|---|
| # |
Test 2
Group: 2
Verdict: ACCEPTED
| input |
|---|
| 2 |
| correct output |
|---|
| ## #. |
| user output |
|---|
| ## #. |
Test 3
Group: 3
Verdict: ACCEPTED
| input |
|---|
| 3 |
| correct output |
|---|
| #### #.#. ##.. #..# |
| user output |
|---|
| #### #.#. ##.. #..# |
Test 4
Group: 4
Verdict: ACCEPTED
| input |
|---|
| 4 |
| correct output |
|---|
| ######## #.#.#.#. ##..##.. #..##..# ####.... ... |
| user output |
|---|
| ######## #.#.#.#. ##..##.. #..##..# ####.... ... |
Test 5
Group: 5
Verdict: ACCEPTED
| input |
|---|
| 5 |
| correct output |
|---|
| ################ #.#.#.#.#.#.#.#. ##..##..##..##.. #..##..##..##..# ####....####.... ... |
| user output |
|---|
| ################ #.#.#.#.#.#.#.#. ##..##..##..##.. #..##..##..##..# ####....####.... ... |
Test 6
Group: 6
Verdict: ACCEPTED
| input |
|---|
| 6 |
| correct output |
|---|
| ##############################... |
| user output |
|---|
| ##############################... |
Test 7
Group: 7
Verdict: ACCEPTED
| input |
|---|
| 7 |
| correct output |
|---|
| ##############################... |
| user output |
|---|
| ##############################... |
Test 8
Group: 8
Verdict: ACCEPTED
| input |
|---|
| 8 |
| correct output |
|---|
| ##############################... |
| user output |
|---|
| ##############################... |
Test 9
Group: 9
Verdict: ACCEPTED
| input |
|---|
| 9 |
| correct output |
|---|
| ##############################... |
| user output |
|---|
| ##############################... |
Test 10
Group: 10
Verdict: ACCEPTED
| input |
|---|
| 10 |
| correct output |
|---|
| ##############################... |
| user output |
|---|
| ##############################... |
