Source.cpp
#include "Header.h"
RailsOnTiles::RailsOnTiles()
{
m_direction = NORTH;
m_tile = B;
}
void RailsOnTiles::placeInfoChange(TILENUMBER outside1, TILENUMBER outside2, TILENUMBER outside3, DIRECTIONS direction, TILEMOVE tilemove)
{
if(m_tile == outside1 || m_tile == outside2 || m_tile == outside3){
m_direction = OutSide;
return;
}
m_direction = direction;
m_tile = (TILENUMBER)((int)m_tile + tilemove);
}
void RailsOnTiles::moveUpper()
{
placeInfoChange(A, B, C, SOUTH, UPPER);
}
void RailsOnTiles::moveLeft()
{
placeInfoChange(A, D, G, EAST, LEFT);
}
void RailsOnTiles::moveRight()
{
placeInfoChange(C, F, I, WEST, RIGHT);
}
void RailsOnTiles::moveUnder()
{
placeInfoChange(G, H, I, NORTH, UNDER);
}
void RailsOnTiles::tile0Course()
{
switch(m_direction){
case EAST : moveLeft(); break;
case SOUTH : moveUpper();break;
case WEST : moveRight();break;
case NORTH : moveUnder();
}
}
void RailsOnTiles::tile1Course()
{
switch(m_direction){
case EAST : moveUpper();break;
case SOUTH : moveLeft(); break;
case WEST : moveUnder();break;
case NORTH : moveRight();
}
}
void RailsOnTiles::tile2Course()
{
switch(m_direction){
case EAST : moveUnder();break;
case SOUTH : moveRight();break;
case WEST : moveUpper();break;
case NORTH : moveLeft();
}
}
const char RailsOnTiles::moveTile(int& tileType)
{
switch(tileType){
case 0 : tile0Course(); break;
case 1 : tile1Course(); break;
case 2 : tile2Course();
}
return m_tile + 'A';
}
const std::string RailsOnTiles::solve(const std::string& input)
{
std::string result = "B";
while(m_direction != OutSide){
int tileType = input.at(m_tile) - '0';
result += moveTile(tileType);
}
result.erase(--result.end());
return result;
}
void test(const std::string& input, const std::string& output)
{
static int testNumber = 1;
RailsOnTiles ROT;
std::string result = ROT.solve(input);
std::cout << testNumber << ":" << result << " 正解は " << output << std::endl;
if(result != output){
std::cout << "test is NG!" << std::endl;
}
testNumber++;
}
int main()
{
test("101221102","BEDGHIFEH");
return 0;
}
Header.h
#ifndef _HEADER_H
#define _HEADER_H
#include<iostream>
#include<string>
class RailsOnTiles
{
public:
RailsOnTiles();
const std::string solve(const std::string& input);
private:
enum DIRECTIONS{ EAST, SOUTH, WEST, NORTH ,OutSide};
enum TILEMOVE{UPPER = -3, LEFT = -1, RIGHT = 1, UNDER = 3};
enum TILENUMBER{A, B, C, D, E, F, G, H, I};
DIRECTIONS m_direction;
TILENUMBER m_tile;
const char moveTile(int& tileType);
void moveUpper();
void moveLeft();
void moveRight();
void moveUnder();
void tile0Course();
void tile1Course();
void tile2Course();
void placeInfoChange(TILENUMBER outside1, TILENUMBER outside2, TILENUMBER outside3, DIRECTIONS direction, TILEMOVE tilemove);
};
#endif