1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Posted at

ひねりなく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;
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?