若是凉夜已成梦

青春里 总有些事情要努力去做 总有些梦想要拼命去追。


  • 运维

  • 前端

  • 编程

  • 随笔

  • hust-oj

1498: Graphical Editor

发表于 2017-10-06   |   分类于 HUSTOJ   |   阅读次数 1,625

题目描述

Graphical editors such as Photoshop allow us to alter bit-mapped images in the same way that text editors allow us to modify documents. Images are represented as an M x N array of pixels, where each pixel has a given color.

Your task is to write a program which simulates a simple interactive graphical editor.

输入

The input consists of a sequence of editor commands, one per line. Each command is represented by one capital letter placed as the first character of the line. If the command needs parameters, they will be given on the same line separated by spaces.

Pixel coordinates are represented by two integers, a column number between 1…M and a row number between 1…N, where 1M, N250. The origin sits in the upper-left corner of the table. Colors are specified by capital letters.

I M N Create a new
M x N image with all pixels initially colored
white (O).

C Clear the table by setting all pixels white (O).
The size remains unchanged.

L X Y C Colors the pixel (X, Y) in color (C).

V X Y1 Y2 C Draw a vertical segment of color (C) in column X, between
the rows Y1 and Y2 inclusive.

H X1 X2 Y C Draw a horizontal segment of color (C) in the row Y, between
the columns X1 and X2 inclusive.

K X1 Y1 X2 Y2 C Draw a filled rectangle of color C, where
(X1, Y1) is the upper-left and (X2, Y2) the lower right corner.

F X Y C Fill the region R with the color C, where R is
defined as follows.
Pixel (X, Y) belongs to R.
Any other pixel
which is
the same color
as pixel (X, Y) and shares a common side with any
pixel in R also belongs to this region.

S Name Write the file name in MSDOS 8.3 format followed by the
contents of the current image.

X Terminate the session.

输出

On every command S NAME, print the filename NAME and contents of the current image. Each row is represented by the color contents of each pixel. See the sample output.

Ignore the entire line of any command defined by a character other than I, C, L, V, H, K, F, S, or X, and pass on to the next command. In case of other errors, the program behavior is unpredictable.

样例输入

I 5 6
L 2 3 A
S one.bmp
G 2 3 J
F 3 3 J
V 2 3 4 W
H 3 4 2 Z
S two.bmp
X


样例输出

one.bmp
OOOOO
OOOOO
OAOOO
OOOOO
OOOOO
OOOOO
two.bmp
JJJJJ
JJZZJ
JWJJJ
JWJJJ
JJJJJ
JJJJJ


参考代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char map[255][255];
int flag[255][255],m,n;
void DrawF(int y,int x,char c,char ch) 
{
    if (flag[y][x]) return;
    map[y][x]=c;
    flag[y][x]=1;
    if (y-1>=0 && map[y-1][y]==ch) DrawF(y-1,x,c,ch);
    if (y+1<n && map[y+1][x]==ch) DrawF(y+1,x,c,ch);
    if (x-1>=0 && map[y][x-1]==ch) DrawF(y,x-1,c,ch);
    if (x+1<m && map[y][x+1]==ch) DrawF(y,x+1,c,ch);
}
int main() 
{
    char ch,ch1,ch2;
    char name[200];
    int x,y,x1,y1,x2,y2,i,j,temp;
    memset(map,'',sizeof(map));
    while(scanf("%c",&ch)!=EOF&&ch!='X')
    {
        if(ch=='I')
        {
            scanf("%d%d",&m,&n);
            for(i=0;i<n;i++)
                for(j=0;j<m;j++)
                    map[i][j]='O';
        }
        else if(ch=='C')
        {
            for(i=0;i<n;i++)
                for(j=0;j<m;j++)
                    map[i][j]='O';
        }
        else if(ch=='L')
        {
            scanf("%d%d %c",&x,&y,&ch1);
            map[y-1][x-1]=ch1;
        }
        else if(ch=='V')
        {
            scanf("%d%d%d %c",&x,&y1,&y2,&ch1);
            if(y1>y2)
            {   temp=y1; y1=y2;  y2=temp;}
            for(i=y1-1;i<y2;i++)
                map[i][x-1]=ch1;
        }
        else if(ch=='H')
        {
            scanf("%d%d%d %c",&x1,&x2,&y,&ch1);
            if(x1>x2)
            {   temp=x1; x1=x2;  x2=temp;}
            for(i=x1-1;i<x2;i++)
                map[y-1][i]=ch1;
        }
        else if(ch=='K')
        {
            scanf("%d%d%d%d %c",&x1,&x2,&y1,&y2,&ch1);
            if(y1>y2)
            {   temp=y1; y1=y2;  y2=temp;}
            if(x1>x2)
            {   temp=x1; x1=x2;  x2=temp;}
            for(i=y1-1;i<y2;i++)
            {
                for(j=x1-1;j<x2;j++)
                    map[i][j]=ch1;
            }
        }
        else if(ch=='F')
        {
            scanf("%d%d %c",&x,&y,&ch1);
            memset(flag,0,sizeof(flag));
            ch2=map[y-1][x-1];
            DrawF(y-1,x-1,ch1,ch2);
        }
        else if(ch=='S')
        {
            getchar();
            gets(name);
            puts(name);
            for(i=0;i<n;i++)
            {
                for(j=0;j<m;j++)
                    printf("%c",map[i][j]);
                printf("n");
            }
        }
        else
            continue;
    }
    return 0;
}

解析

暂无

hustoj

发表评论 取消回复

邮箱地址不会被公开。 必填项已用*标注

*
*


hoxis wechat
著作权归作者所有
站点更新说明
  • 文章目录
  • 站点概览
若是凉夜已成梦

若是凉夜已成梦

青春里 总有些事情要努力去做 总有些梦想要拼命去追。

1904 日志
6 分类
12 标签
RSS
weibo github twitter facebook

友情链接

Dreams孤独患者 原站点 Skip
© 2017 若是凉夜已成梦
Powered by WordPress | 已运行
Theme By NexT.Mist