[10267] Graphical Editor





Problem Link : http://acm.uva.es/p/v102/10267.html










■ Problem
– 간단한 문자그림 편집기라고 보면될듯…

■ Solution
– 주어진 명령어대로 필드를 바꿔주면 됨.
– F명령어는 재귀로…

■ Critical Input
– 입력데이터의 크기가 같은 xㅍ또는 y데이터가 2 4 처럼 오름차순이 아니고  
   4 2 처럼 내림차순일때 처리…





[ Source Code ]

#include <stdio.h>

// 필요한 함수 선언

void f_I ( char [][251], int, int );
void f_L ( char [][251], int, int, char );
void f_V ( char [][251], int, int, int, char );
void f_H ( char [][251], int, int, int, char );
void f_K ( char [][251], int, int, int, int, char );
void f_F ( char [][251], int, int, int, int, char, char );
void f_S ( char [][251], int, int, char * );

int main ( void )
{

// 필요한 변수 선언
char command, defaultColor, color, name[12], field[251][251];
int m, n, x, y, x1, y1, x2, y2, temp;

// 루프돌면서 X나올때까지 명령을 받아들인다.
while ( 1 ) {
scanf ( %c, &command );

// 명령어가 ‘X’이면 프로그램 종료
if ( command == ‘X’ ) {
break;
}

// X이외 명령어에 따라 입력받고 함수들 호출
switch ( command ) {
case ‘I’ :
scanf ( %d%d, &m, &n );
f_I ( field, m, n );
break;

case ‘C’ :
f_I ( field, m, n );
break;

case ‘L’ :
scanf ( %d%d, &x, &y );
getchar ();
scanf ( %c, &color );
f_L ( field, x, y, color );
break;

case ‘V’ :
scanf ( %d%d%d, &x, &y1, &y2 );
getchar ();
scanf ( %c, &color );
if ( y1 > y2 ) {
temp = y2;
y2 = y1;
y1 = temp;
}
f_V ( field, x, y1, y2, color );
break;

case ‘H’ :
scanf ( %d%d%d, &x1, &x2, &y );
getchar ();
scanf ( %c, &color );
if ( x1 > x2 ) {
temp = x2;
x2 = x1;
x1 = temp;
}
f_H ( field, x1, x2, y, color );
break;

case ‘K’ :
scanf ( %d%d%d%d, &x1, &y1, &x2, &y2 );
getchar ();
scanf ( %c, &color );
if ( x1 > x2 ) {
temp = x2;
x2 = x1;
x1 = temp;
}
if ( y1 > y2 ) {
temp = y2;
y2 = y1;
y1 = temp;
}
f_K ( field, x1, y1, x2, y2, color );
break;

case ‘F’ :
scanf ( %d%d, &x, &y );
getchar ();
scanf ( %c, &color );
defaultColor = field[y][x];
if ( color == defaultColor ) break;
f_F ( field, x, y, m, n, defaultColor, color );
break;

case ‘S’ :
scanf ( %s, name );
f_S ( field, m, n, name );
break;

default :
continue;
break;

}
}

return 0;

}

// I명령용 함수
void f_I ( char f_field[][251], int f_x, int f_y )
{

int loop_x, loop_y;

for ( loop_x = 1 ; loop_x <= f_x ; loop_x++ ) {
for ( loop_y = 1 ; loop_y <= f_y ; loop_y++ ) {
f_field[loop_y][loop_x] = ‘O’;
}
}

}

// L명령용 함수
void f_L ( char f_field[][251], int f_x, int f_y, char f_color )
{

f_field[f_y][f_x] = f_color;

}

// V명령용 함수
void f_V ( char f_field[][251], int f_x, int f_y1, int f_y2, char f_color )
{

int loop_y;

for ( loop_y = f_y1 ; loop_y <= f_y2 ; loop_y++ ) {
f_field[loop_y][f_x] = f_color;
}

}

// H명령용 함수
void f_H ( char f_field[][251], int f_x1, int f_x2, int f_y, char f_color )
{

int loop_x;

for ( loop_x = f_x1 ; loop_x <= f_x2 ; loop_x++ ) {
f_field[f_y][loop_x] = f_color;
}

}

// K명령용 함수
void f_K ( char f_field[][251], int f_x1, int f_y1, int f_x2, int f_y2, char f_color )
{

int loop_x, loop_y;

for ( loop_y = f_y1 ; loop_y <= f_y2 ; loop_y++ ) {
for ( loop_x = f_x1 ; loop_x <= f_x2 ; loop_x++ ) {
f_field[loop_y][loop_x] = f_color;
}
}

}

// F명령용 함수
void f_F ( char f_field[][251], int f_x, int f_y, int m, int n, char f_color, char f_changeColor )
{
f_field[f_y][f_x] = f_changeColor;

if ( f_x > 1 && f_field[f_y][f_x-1] == f_color )
f_F ( f_field, f_x-1, f_y, m, n, f_color, f_changeColor );
if ( f_x < m && f_field[f_y][f_x+1] == f_color )
f_F ( f_field, f_x+1, f_y, m, n, f_color, f_changeColor );
if ( f_y > 1 && f_field[f_y-1][f_x] == f_color )
f_F ( f_field, f_x, f_y-1, m, n, f_color, f_changeColor );
if ( f_y < n && f_field[f_y+1][f_x] == f_color )
f_F ( f_field, f_x, f_y+1, m, n, f_color, f_changeColor );

}

// S명령용 함수
void f_S ( char f_field[][251], int f_m, int f_n, char *f_name )
{

int loop_x, loop_y;

printf ( %s\n, f_name );
for ( loop_y = 1 ; loop_y <= f_n ; loop_y++ ) {
for ( loop_x = 1 ; loop_x <= f_m ; loop_x++ ) {
printf ( %c, f_field[loop_y][loop_x] );
}
printf ( \n );
}

}

댓글 남기기