-
#include <iostream>
-
#include <string>
-
#include <deque>
-
-
int Width, Height;
-
char Image[251][251];
-
-
void StringTokenizer( const std::string &Str, std::deque< std::string > &Token, const std::string &Delimiter );
-
-
void NewImage( std::deque< std::string > &Token );
-
void ClearImage( std::deque< std::string > &Token );
-
void SetPixel( std::deque< std::string > &Token );
-
void VerticalLine( std::deque< std::string > &Token );
-
void HorizonLine( std::deque< std::string > &Token );
-
void Square( std::deque< std::string > &Token );
-
void MagicWord( std::deque< std::string > &Token );
-
void SaveImage( std::deque< std::string > &Token );
-
void ExitProgram( std::deque< std::string > &Token );
-
-
void NearstFill( int X, int Y, char Color, char OldColor );
-
-
int main( int argc, char **argv )
-
{
-
char InputString[ 256 ];
-
-
while( true ) {
-
-
std::cin.getline( InputString, 255 );
-
std::deque< std::string > TokenContainer;
-
-
StringTokenizer( InputString, TokenContainer, "\t " );
-
-
std::string &Cmd = TokenContainer.front();
-
if( Cmd == "I" )
-
NewImage( TokenContainer );
-
else if( Cmd == "C" )
-
ClearImage( TokenContainer );
-
else if( Cmd == "L" )
-
SetPixel( TokenContainer );
-
else if( Cmd == "V" )
-
VerticalLine( TokenContainer );
-
else if( Cmd == "H" )
-
HorizonLine( TokenContainer );
-
else if( Cmd == "K" )
-
Square( TokenContainer );
-
else if( Cmd == "F" )
-
MagicWord( TokenContainer );
-
else if( Cmd == "S" )
-
SaveImage( TokenContainer );
-
else if( Cmd == "X" )
-
ExitProgram( TokenContainer );
-
}
-
-
return 0;
-
}
-
-
void StringTokenizer( const std::string &Str, std::deque< std::string > &Token, const std::string &Delimiter )
-
{
-
std::string::size_type LastPos = Str.find_first_not_of( Delimiter, 0 );
-
std::string::size_type Pos = Str.find_first_of( Delimiter, LastPos );
-
-
while( std::string::npos != Pos || std::string::npos != LastPos )
-
{
-
Token.push_back( Str.substr( LastPos, Pos - LastPos ) );
-
LastPos = Str.find_first_not_of( Delimiter, Pos );
-
Pos = Str.find_first_of( Delimiter, LastPos );
-
}
-
}
-
-
void NewImage( std::deque< std::string > &Token )
-
{
-
Width = atoi(Token[1].c_str());
-
Height = atoi(Token[2].c_str());
-
-
ClearImage( Token );
-
}
-
-
void ClearImage( std::deque< std::string > &Token )
-
{
-
for( int i = 0; i <= Height; ++i )
-
for( int j = 0; j <= Width; ++j )
-
Image[ i ][ j ] = 'O';
-
}
-
-
void SetPixel( std::deque< std::string > &Token )
-
{
-
int X = atoi(Token[1].c_str());
-
int Y = atoi(Token[2].c_str());
-
char Color = *Token[3].c_str();
-
-
Image[ Y ][ X ] = Color;
-
}
-
-
void VerticalLine( std::deque< std::string > &Token )
-
{
-
int X = atoi(Token[1].c_str());
-
int Y = atoi(Token[2].c_str());
-
int Y2 = atoi(Token[3].c_str());
-
char Color = *Token[4].c_str();
-
-
if( Y2 < Y )
-
std::swap( Y, Y2 );
-
-
for( int i = Y; i <= Y2; ++i )
-
Image[ i ][ X ] = Color;
-
}
-
-
void HorizonLine( std::deque< std::string > &Token )
-
{
-
int X = atoi(Token[1].c_str());
-
int X2 = atoi(Token[2].c_str());
-
int Y = atoi(Token[3].c_str());
-
char Color = *Token[4].c_str();
-
-
if( X2 < X )
-
std::swap( X, X2 );
-
-
for( int i = X; i <= X2; ++i )
-
Image[ Y ][ i ] = Color;
-
}
-
-
void Square( std::deque< std::string > &Token )
-
{
-
int X = atoi(Token[1].c_str());
-
int X2 = atoi(Token[2].c_str());
-
int Y = atoi(Token[3].c_str());
-
int Y2 = atoi(Token[4].c_str());
-
char Color = *Token[4].c_str();
-
-
if( X2 < X )
-
std::swap( X, X2 );
-
-
if( Y2 < 2 )
-
std::swap( Y, Y2 );
-
-
for( int i = Y; i <= Y2; ++i )
-
for( int j = X; j <= X2; ++j )
-
Image[ i ][ j ] = Color;
-
}
-
-
void MagicWord( std::deque< std::string > &Token )
-
{
-
int X = atoi(Token[1].c_str());
-
int Y = atoi(Token[2].c_str());
-
char Color = *Token[3].c_str();
-
char OldColor = Image[ Y ][ X ];
-
-
NearstFill( X, Y, Color, OldColor );
-
}
-
-
void SaveImage( std::deque< std::string > &Token )
-
{
-
std::cout << Token[1] << std::endl;
-
-
for( int i = 1; i <= Height; ++i ) {
-
for( int j = 1; j <= Width; ++j )
-
std::cout << Image[ i ][ j ];
-
-
std::cout << std::endl;
-
}
-
}
-
-
void ExitProgram( std::deque< std::string > &Token )
-
{
-
exit(0);
-
}
-
-
void NearstFill( int X, int Y, char Color, char OldColor )
-
{
-
if( Image[ Y ][ X ] == OldColor ) {
-
-
Image[ Y ][ X ] = Color;
-
-
if( X > 1 )
-
NearstFill( X - 1, Y, Color, OldColor );
-
-
if( X < Width )
-
NearstFill( X + 1, Y, Color, OldColor );
-
-
if( Y > 1 )
-
NearstFill( X, Y - 1, Color, OldColor );
-
-
if( Y < Height )
-
NearstFill( X, Y + 1, Color, OldColor );
-
}
-
}
댓글을 달아 주세요