第7回オフラインリアルタイムどう書くの参考問題の回答例。C++ で。

お題はこちら http://qiita.com/items/7b08b0eb9ef84d7cfe49
不満。もっとよい行列の表現方法があるような気がしてならないのだけれど、今のところ全然思いつかない…。

```cpp:

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

template<typename T>
struct Comparator
{
    const std::size_t index;

    Comparator(std::size_t index) : index(index) {}

    bool operator () (const T& lhs, const T& rhs)
    {
        return lhs[index] < rhs[index];
    }
};

static const int init[] =
{
    4, 1, 4, 2, 1, 3,
    7, 3, 2, 0, 5, 0,
    2, 3, 6, 0, 6, 7,
    6, 4, 5, 7, 5, 1,
    3, 1, 6, 6, 2, 4,
    6, 0, 5, 5, 5, 1
};

typedef std::vector<int> Row;

class Matrix
{
public:
    Matrix() : rows_()
    {
        for(int i = 0; i < 6; ++i)
        {
            Row row(init + i * 6, init + (i + 1) * 6);
            rows_.push_back(row);
        }
    }

    int at(int row, int col) const
    {
        return rows_[row][col];
    }

    void transpose()
    {
        Matrix m(*this);

        for(int row = 0; row < 6; ++row)
        {
            for(int col = 0; col < 6; ++col)
            {
                rows_[row][col] = m.rows_[col][row];
            }
        }
    }

    void sortRowsByIndex(std::size_t key)
    {
        std::sort(rows_.begin(), rows_.end(), Comparator<Row>(key));
    }

private:
    std::vector<Row> rows_;
};

void test(const std::string& input, const std::string& expected)
{
    static const std::string IndexChars = "ABCDEFuvwxyz";

    Matrix m;

    for(std::string::const_iterator i = input.begin(); i != input.end(); ++i)
    {
        std::size_t index = IndexChars.find(*i);

        if(index < 6)
        {
            m.transpose();
            m.sortRowsByIndex(index);
            m.transpose();
        }
        else
        {
            m.sortRowsByIndex(index - 6);
        }
    }

    std::ostringstream oss;
    for(int col = 0; col < 6; ++col)
    {
        oss << m.at(0, col);
    }

    std::cout << "input: " << std::setw(10) << input
              << "  expected: " << expected
              << "  actual: " << oss.str() << " => "
              << (oss.str() == expected ? "pass" : "failure") << "\n";
}

int main(int, char* [])
{
    std::cout << std::left;

    /* 0*/ test( "AvEx",      "305027");
    /* 1*/ test( "A",         "112344");
    /* 2*/ test( "C",         "241413");
    /* 3*/ test( "F",         "134214");
    /* 4*/ test( "u",         "236067");
    /* 5*/ test( "w",         "732050");
    /* 6*/ test( "y",         "414213");
    /* 7*/ test( "yx",        "732050");
    /* 8*/ test( "ux",        "236067");
    /* 9*/ test( "EF",        "131424");
    /*10*/ test( "DF",        "134124");
    /*11*/ test( "Au",        "055165");
    /*12*/ test( "uA",        "023667");
    /*13*/ test( "By",        "234114");
    /*14*/ test( "yB",        "114342");
    /*15*/ test( "yBy",       "357020");
    /*16*/ test( "yByB",      "350072");
    /*17*/ test( "AuBvCw",    "131244");
    /*18*/ test( "FAuFBvFCw", "300527");
    /*19*/ test( "AuBv",      "112344");
    /*20*/ test( "CwDx",      "515056");
    /*21*/ test( "FzyE",      "324114");
    /*22*/ test( "uAwDyB",    "114324");
    /*23*/ test( "zExCvF",    "073520");
    /*24*/ test( "uFxEv",     "002357");
    /*25*/ test( "DyCwB",     "076362");

    return 0;
}
```