Graphical Editor

Graphical Editor

时间: 1ms        内存:64M

描述:

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

提示:

参考答案(内存最优[800]):

#include <stdio.h>
int main()
{
    char p,a[255][255]={"\0"},q,b[20]={'\0'},qt;
    int n,m,x,y,d,t;

    while(scanf("%c",&p)!=EOF)
    {
        if(p=='X')
            break;
        switch(p)
        {
            case 'I':scanf("%d%d",&n,&m);
                    for(int i=0;i<m;i++)
                        for(int j=0;j<n;j++)
                            a[i][j]='O';
                    break;
            case 'L':scanf("%d%d %c",&x,&y,&q);
                    a[y-1][x-1]=q;
                    break;
            case 'S':scanf("%s",b);
                    printf("%s\n",b);
                    for(int i=0;i<m;i++)
                        printf("%s\n",a[i]);
                    break;
            case 'C':for(int i=0;i<m;i++)
                        for(int j=0;j<n;j++)
                            a[i][j]='O';
                    break;
            case 'F':scanf("%d%d %c",&x,&y,&q);
                    qt=a[y-1][x-1];
                    for(int i=0;i<m;i++)
                        for(int j=0;j<n;j++)
                            if(a[i][j]==qt)
                                a[i][j]=q;
                    break;
            case 'V':scanf("%d%d%d %c",&x,&y,&d,&q);
                    if(y>d)
                    {
                        t=d;
                        d=y;
                        y=t;
                    }
                    for(int i=y-1;i<=d-1;i++)
                        a[i][x-1]=q;
                    break;
            case 'H':scanf("%d%d%d %c",&x,&d,&y,&q);
                    if(x>d)
                    {
                        t=d;
                        d=x;
                        x=t;
                    }
                    for(int i=x-1;i<=d-1;i++)
                        a[y-1][i]=q;
                    break;
        }
    }
    return 0;
}

参考答案(时间最优[0]):

#include <iostream>
#include <string.h>
using namespace std;
char a[251][251];
int main()
{
    //command
    char com;
    //C
    int M,N;
    //L
    int lx,ly;
    char lc;
    //V
    int vx,vy1,vy2;
    char vc;
    //H
    int hx1,hx2,hy;
    char hc;
    //K
    int kx1,kx2,ky1,ky2;
    char kc;
    //F
    int fx,fy;
    char fc,cc;
    //S
    string l;
    while(cin>>com){
        if(com=='X')    //遇到X退出
            break;
        if(com!='I' && com!='C' && com!='L' && com!='V' && com!='H' && com!='K' && com!='F' && com!='S'){    //其他命令退出
            getline(cin,l);
            continue;
        }
        switch(com){
        case 'I':
            cin>>M>>N;
            for(int i=1;i<=N;i++)   //创建M*N的空白(O)画板
                for(int j=1;j<=M;j++){
                    a[i][j]='O';
                }
            break;
            //默认全部为O
        case 'C':
            //清空所有色彩为O
            for(int i=1;i<=N;i++)   //清空画板
                for(int j=1;j<=M;j++){
                    a[i][j]='O';
                }
        case 'L':
            cin>>lx>>ly>>lc;
            a[ly][lx]=lc;   //将lx,ly位置的颜色填充为lc
            break;
        case 'V':
            cin>>vx>>vy1>>vy2>>vc;
            for(int i=vy1;i<=vy2;i++)
                a[i][vx]=vc;    //将x列vy1到vy2的像素颜色填充为vc
            break;
        case 'H':
            cin>>hx1>>hx2>>hy>>hc;
            for(int i=hx1;i<=hx2;i++)
                a[hy][i]=hc;    //将y行vx1到vx2的像素颜色填充为hc
            break;
        case 'K':
            cin>>kx1>>kx2>>ky1>>ky2>>kc;
            for(int i=ky1;i<=ky2;i++)   //填充kx1,kx2,ky1,ky2区域为kc颜色
                for(int j=kx1;j<=kx2;j++){
                    a[i][j]=kc;
                }
            break;
        case 'F':
            cin>>fx>>fy>>fc;
            cc=a[fy][fx];
            for(int i=1;i<=N;i++)   //填充画板与x,y点颜色相同的区域颜色为C
                for(int j=1;j<=M;j++){
                    if(a[i][j]==cc)
                        a[i][j]=fc;
                }
            break;
        case 'S':
            cin>>l;
            cout<<l<<endl;  //先输出文件名
            for(int i=1;i<=N;i++){   //显示
                for(int j=1;j<=M;j++)
                    cout<<a[i][j];
                cout<<endl;
            }
            break;
        }
    }
    return 0;
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

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