Submission details
Task:Monikulmio
Sender:Luhpossu
Submission time:2025-11-01 21:23:20 +0200
Language:C++ (C++20)
Status:READY
Result:84
Feedback
groupverdictscore
#184
Test results
testverdicttimescore
#1ACCEPTED0.00 s10details
#2ACCEPTED0.00 s10details
#3ACCEPTED0.00 s10details
#4ACCEPTED0.00 s10details
#5ACCEPTED0.00 s10details
#6ACCEPTED0.00 s7details
#7ACCEPTED0.00 s10details
#8ACCEPTED0.00 s7details
#9ACCEPTED0.01 s10details
#100.01 s0details

Compiler report

input/code.cpp: In function 'int main(int, char**)':
input/code.cpp:183:13: warning: 'prevY' may be used uninitialized in this function [-Wmaybe-uninitialized]
  183 |     drawLine(prevX, prevY, firstX, firstY, grid);
      |     ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
input/code.cpp:183:13: warning: 'prevX' may be used uninitialized in this function [-Wmaybe-uninitialized]
input/code.cpp:183:13: warning: 'firstY' may be used uninitialized in this function [-Wmaybe-uninitialized]
input/code.cpp:183:13: warning: 'firstX' may be used uninitialized in this function [-Wmaybe-uninitialized]

Code

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <stdexcept>
#include <string>
#include <vector>

using namespace std;
using pi = pair<int, int>;

class Grid {
    char *data;
public:
    const int width;
    const int height;

    Grid(int width, int height) : data(new char[width * height]) , width(width), height(height) {
        for (int i = 0; i < width * height; i++) {
            data[i] = ' ';
        }
    }

    inline int getIndex(int x, int y) {
        return (y - 1) * width + x - 1;
    }

    inline bool inside(int x, int y) {
        return x >= 1 && x <= width && y >= 1 && y <= height;
    }

    char get(int x, int y) {
        const int i = getIndex(x, y);
        return data[i];
    }

    void set(int x, int y, char value) {
        const int i = getIndex(x, y);
        data[i] = value;
    }

    void print() {
        for (int y = 1; y <= height; y++) {
            for (int x = 1; x <= width; x++) {
                std::cout << get(x, y);
            }

            std::cout << std::endl;
        }
    }
};

map<pi, char> paints;

char getDirection(int dx, int dy) {
    if (dx == 0) return '|';
    if (dy == 0) return '=';
    if (dy < 0) {
        if (dx < 0) return '\\';
        else return '/';
    } else {
        if (dx < 0) return '/';
        else return '\\';
    }
}

void drawLine(int prevX, int prevY, int x, int y, Grid& grid) {
    int dx = x - prevX;
    int dy = y - prevY;
    int steps = std::max(std::abs(dx), std::abs(dy));

    char direction = getDirection(dx, dy);

    if (steps == 1 && dy != 0 && dx != 0) {
        paints[{ prevX + dx, prevY }] = '1';
        paints[{ prevX, y + dy }] = '2';
    }

    for (int step = 0; step < steps - 1; step++) {
        if (dx != 0) dx -= dx / std::abs(dx);
        if (dy != 0) dy -= dy / std::abs(dy);

        int x = prevX + dx;
        int y = prevY + dy;

        grid.set(x, y, direction);

        if (dy > 0) {
            paints[{ x + 1, y }] = '1';
            paints[{ x - 1, y }] = '2';
        } else {
            paints[{ x + 1, y }] = '2';
            paints[{ x - 1, y }] = '1';
        }

        if (dx > 0) {
            paints[{ x, y + 1 }] = '2';
            paints[{ x, y - 1 }] = '1';
        } else {
            paints[{ x, y + 1 }] = '1';
            paints[{ x, y - 1 }] = '2';
        }
    }
}

void paint(Grid& grid, int x, int y, char type) {
    if (!grid.inside(x, y) || grid.get(x, y) != ' ') return;

    grid.set(x, y, type);


    paint(grid, x - 1, y, type);
    paint(grid, x + 1, y, type);
    paint(grid, x, y - 1, type);
    paint(grid, x, y + 1, type);
}

char getOutside(Grid& grid) {
    for (int x = 1; x <= grid.width; x++) {
        char c1 = grid.get(x, 1);
        if (c1 == '1') return '1';
        if (c1 == '2') return '2';

        char c2 = grid.get(x, grid.height);
        if (c2 == '1') return '1';
        if (c2 == '2') return '2';
    }

    for (int y = 2; y <= grid.height - 1; y++) {
        char c1 = grid.get(1, y);
        if (c1 == '1') return '1';
        if (c1 == '2') return '2';

        char c2 = grid.get(grid.width, y);
        if (c2 == '1') return '1';
        if (c2 == '2') return '2';
    }

    return '1';
}

void paintBackground(Grid& grid) {
    for (auto [pos, type] : paints) {
        paint(grid, pos.first, pos.second, type);
    }

    char outside = getOutside(grid);

    for (int x = 1; x <= grid.width; x++) {
        for (int y = 1; y <= grid.height; y++) {
            char c = grid.get(x, y);

            if (c == outside) grid.set(x, y, '.');
            else if (c == '1' || c == '2') grid.set(x, y, '#');
            else if (c == ' ') grid.set(x, y, '?');
        }
    }
}

int main([[maybe_unused]] int argc, [[maybe_unused]] char *argv[]) {
    int height, width, lines; std::cin >> height >> width >> lines;

    Grid grid(width, height);
    
    bool first = true;
    int firstX, firstY, prevX, prevY, x, y;
    for (int i = 0; i < lines; i++) {
        std::cin >> y >> x;

        grid.set(x, y, '*');

        if (!first) drawLine(prevX, prevY, x, y, grid);
        else {
            firstX = x;
            firstY = y;
            first = false;
        }

        prevX = x;
        prevY = y;
    }

    drawLine(prevX, prevY, firstX, firstY, grid);
    paintBackground(grid);

    grid.print();
}

Test details

Test 1 (public)

Verdict: ACCEPTED

input
8 9 5
5 2
2 5
5 8
7 8
...

correct output
.........
....*....
.../#\...
../###\..
.*#####*.
...

user output
.........
....*....
.../#\...
../###\..
.*#####*.
...

Test 2 (public)

Verdict: ACCEPTED

input
20 40 4
5 10
5 30
15 30
15 10

correct output
.................................

user output
.................................

Test 3 (public)

Verdict: ACCEPTED

input
20 40 29
8 7
13 2
14 2
9 7
...

correct output
.................................

user output
.................................

Test 4 (public)

Verdict: ACCEPTED

input
20 40 14
5 12
5 25
8 28
13 28
...

correct output
.................................

user output
.................................

Test 5 (public)

Verdict: ACCEPTED

input
20 40 12
3 20
7 16
7 9
11 13
...

correct output
.................................

user output
.................................

Test 6 (public)

Verdict: ACCEPTED

input
9 35 33
2 3
2 8
4 8
4 5
...

correct output
.................................

user output
.................................

Feedback: Lines are drawn correctly. Incorrect fill character on row 3, col 3: expected '#', got '.'

Test 7 (public)

Verdict: ACCEPTED

input
30 100 69
6 10
6 14
7 14
7 18
...

correct output
.................................

user output
.................................

Test 8 (public)

Verdict: ACCEPTED

input
40 60 192
11 3
11 5
10 6
11 7
...

correct output
.................................

user output
.................................

Feedback: Lines are drawn correctly. Incorrect fill character on row 3, col 30: expected '#', got '.'

Test 9 (public)

Verdict: ACCEPTED

input
50 100 142
1 1
1 7
1 11
1 14
...

correct output
*=====*===*==*...................

user output
*=====*===*==*...................

Test 10 (public)

Verdict:

input
100 100 1000
10 1
4 7
1 4
1 9
...

correct output
...*====*........................

user output
...*====*........................

Feedback: Incorrect character on row 51, col 100: expected '.', got '?'