0
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.

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

0
Posted at

問題はこちらです。 http://qiita.com/items/0ddde0164a745cd09c34
ベタベタな解き方。もうちょっと工夫したい。


# include <string>
# include <sstream>

typedef int Direction;

static const int DOWN  = 0;
static const int RIGHT = 1;
static const int UP    = 2;
static const int LEFT  = 3;

std::string ord5(const std::string& s)
{
    int       x = 1;
    int       y = 0;
    Direction d = DOWN;

    std::ostringstream result;

    result << 'B';

    for(;;)
    {
        switch(d * 3 + s[y * 3 + x] - '0')
        {
        case  0: ++y; d = DOWN;  break;
        case  1: ++x; d = RIGHT; break;
        case  2: --x; d = LEFT;  break;
        case  3: ++x; d = RIGHT; break;
        case  4: ++y; d = DOWN;  break;
        case  5: --y; d = UP;    break;
        case  6: --y; d = UP;    break;
        case  7: --x; d = LEFT;  break;
        case  8: ++x; d = RIGHT; break;
        case  9: --x; d = LEFT;  break;
        case 10: --y; d = UP;    break;
        case 11: ++y; d = DOWN;  break;
        }
        
        if((x < 0) || (3 <= x) || (y < 0) || (3 <= y))
        {
            break;
        }

        result << static_cast<char>('A' + y * 3 + x);
    }

    return result.str();
}

テスト。


# include <gtest/gtest.h>
# include <string>

std::string ord5(const std::string& s);

TEST(ord5railsontilesTest, Test)
{
	ASSERT_EQ(std::string("BEDGHIFEH"),   ord5("101221102"));
	ASSERT_EQ(std::string("BEH"),         ord5("000000000"));
	ASSERT_EQ(std::string("BCF"),         ord5("111111111"));
	ASSERT_EQ(std::string("BAD"),         ord5("222222222"));
	ASSERT_EQ(std::string("BEFIHEDGH"),   ord5("000211112"));
	ASSERT_EQ(std::string("BADGHIFEBCF"), ord5("221011102"));
	ASSERT_EQ(std::string("BEHIFCBADEF"), ord5("201100112"));
	ASSERT_EQ(std::string("BEFIH"),       ord5("000111222"));
	ASSERT_EQ(std::string("BC"),          ord5("012012012"));
	ASSERT_EQ(std::string("BEDABCFI"),    ord5("201120111"));
	ASSERT_EQ(std::string("BADEHGD"),     ord5("220111122"));
	ASSERT_EQ(std::string("BADG"),        ord5("221011022"));
	ASSERT_EQ(std::string("BCFIHEBA"),    ord5("111000112"));
	ASSERT_EQ(std::string("BEFI"),        ord5("001211001"));
	ASSERT_EQ(std::string("BCFEHIF"),     ord5("111222012"));
	ASSERT_EQ(std::string("BADEHI"),      ord5("220111211"));
	ASSERT_EQ(std::string("BCFEBAD"),     ord5("211212212"));
	ASSERT_EQ(std::string("BEFC"),        ord5("002112210"));
	ASSERT_EQ(std::string("BEF"),         ord5("001010221"));
	ASSERT_EQ(std::string("BEFIHG"),      ord5("100211002"));
	ASSERT_EQ(std::string("BEFCBAD"),     ord5("201212121"));
}
0
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
0
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?