F
F
fjaerj122021-03-13 12:45:58
C++ / C#
fjaerj12, 2021-03-13 12:45:58

Where is the error in the algorithm for creating a flat convex figure?

Task:
Consider an infinite sheet of checkered paper. Let's paint some set of cells black. Now we want to fill in the minimum number of cells so that many black cells become convex.

Recall that a geometric figure Φ is called convex if, for any points A from Φ and B from Φ with real coordinates, the segment [AB] belongs to Φ.

Specifications Input
The first line of the input file INPUT.TXT contains two numbers N and M (1 ≤ N, M ≤ 100) — the size of a piece of paper where all the black cells are. Each of the next N lines contains M characters "*" or ".". The symbol "*" denotes a black cell, and "." white.

Output
In the output file OUTPUT.TXT print a convex set containing the minimum number of additionally colored black cells in exactly N lines of M characters "*" or "." in each.

Examples

Input:
2 4
..*.
.**.
Output:
.**.
.**.

Input:
4 3
.*.
.*.
.*.
.*.
Output:
.*.
.*.
.*.
.*.

Most of the input data is processed correctly, but when you try to pass on the site, incorrect answers appear.
Idea: find the minimum and maximum rows and columns containing the asterisk character, then display what is inside the boundaries found with asterisks, and the rest with dots.

#include <iostream>
#include <fstream>
#include <string>

int main()
{
    int rows = 0, columns = 0;

    std::ifstream file("INPUT.txt");
    std::cin >> rows;
    std::cin >> columns;


    int rMax = 0, rMin = 101, cMax = 0, cMin = 101;
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < columns; j++)
        {
            char c;
            file >> c;
            if (c == '*' && i < rMin) 
            {
                rMin = i;
            }
            if (c == '*' && i > rMax) 
            {
                rMax = i;
            }
            if (c == '*' && j < cMin)
            {
                cMin = j;
            }
            if (c == '*' && j > cMax) 
            {
                cMax = j;
            }
        }
    }
    file.close();

    std::ofstream fout("OUTPUT.txt");

    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < columns; j++)
        {
            if (i >= rMin && rMax >= i && cMax >= j && cMin <= j)
            {
                fout << "*";
            }
            else
            {
                fout << ".";
            }
        }
        
        fout << std::endl;     
    } 
    fout.close();
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2021-03-13
@fjaerj12

For the future: write together with the problem your idea of ​​a solution.
Remove unnecessary variables. Also, why are you there i % columns?
It looks like you need to output to output.txt, and not to stdout.
Also, I advise you to always output a newline, and do not skip it on the last line of the output.
To avoid problems with whitespace characters - read in two nested loops to rows and columns one char (and not string to eof). This will simplify the input and make it safer to any artifacts in the tests.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question