Help us understand the problem. What is going on with this article?

第23回オフラインリアルタイムどう書く を C++ で解く

More than 5 years have passed since last update.

ひねりなくC++で解いてみました。
もう少し工夫をしたかったのですが、途中でもたついたら、ここに到達するだけで1時間を消費してしまいました。

お題はこちらです。

#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>

struct Cell
{
    int value;
    int length;

    Cell() : value(0), length(0) {}
};

int get_length(Cell table[5][5], int row, int col)
{
    if(table[row][col].length == 0)
    {
        table[row][col].length = 1;

        if((row > 0) && (table[row][col].value < table[row - 1][col].value))
        {
            table[row][col].length = std::max(get_length(table, row - 1, col) + 1, table[row][col].length);
        }

        if((row < 4) && (table[row][col].value < table[row + 1][col].value))
        {
            table[row][col].length = std::max(get_length(table, row + 1, col) + 1, table[row][col].length);
        }

        if((col > 0) && (table[row][col].value < table[row][col - 1].value))
        {
            table[row][col].length = std::max(get_length(table, row, col - 1) + 1, table[row][col].length);
        }

        if((col < 4) && (table[row][col].value < table[row][col + 1].value))
        {
            table[row][col].length = std::max(get_length(table, row, col + 1) + 1, table[row][col].length);
        }
    }

    return table[row][col].length;
}

std::string solve(const std::string& input)
{
    Cell table[5][5];
    for(int row = 0; row < 5; ++row)
    {
        for(int col = 0; col < 5; ++col)
        {
            table[row][col].value = input[row * 6 + col] - '0';
        }
    }

    int length = 0;
    for(int row = 0; row < 5; ++row)
    {
        for(int col = 0; col < 5; ++col)
        {
            length = std::max(get_length(table, row, col), length);
        }
    }

    std::ostringstream oss;
    oss << length;

    return oss.str();
}

void test(const std::string& input, const std::string& expected)
{
    std::string actual = solve(input);
    if(expected == actual)
    {
        std::cout << "." << std::flush;
    }
    else
    {
        std::cout << "\ninput: " << input << ", expected: " << expected << ", acutal: " << actual << std::endl;
    }
}

int main(int argc, char* argv[])
{
    /*0*/ test( "01224/82925/69076/32298/21065", "6" );
    /*1*/ test( "03478/12569/03478/12569/03478", "10" );
    /*2*/ test( "09900/28127/87036/76545/87650", "10" );
    /*3*/ test( "77777/77777/77777/77777/77777", "1" );
    /* ... */
    /*50*/ test( "02489/77571/84873/03879/84460", "7" );

    std::cout << std::endl;

    return 0;
}
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away