ACM YTU 《挑战编程》第一章 入门 Problem E: Graphical Editor


Description

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.
Input

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.
Output

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.
Sample Input

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

Sample Output

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

#include <iostream>
#include<string>
using namespace std;
char str[255][255];
string name;
void print(int N,int M)
{
    //cout<<name<<endl;
    for(int i = 1;i<=N;i++)
    {
        for(int j = 1;j<=M;j++)
        {
            cout<<str[i][j];
        }
        cout<<endl;
    }
    for(int i = 1;i<=N;i++)
    {
        for(int j = 1;j<=M;j++)
        {
            str[i][j] = 'O';
        }
    }
}
int main()
{
    char ch;
    int X,Y,M,N;
    int X0 ,Y1,Y2 ,X1,X2,Y0;
    int x1,y1,x2,y2;
    int XF,YF;
    char CL,CV,CH,CK,CF;
    char CF_0;

    while(cin>>ch&&ch!='X')
    {

        switch(ch)
        {
            case 'I':
                cin>>M>>N;
                for(int i = 1;i<=N;i++)
                {
                    for(int j = 1;j<=M;j++)
                    {
                        str[i][j] = 'O';
                    }
                }
                break;
            case 'C':
                for(int i = 1;i<=M;i++)
                {
                    for(int j = 1;j<=N;j++)
                    {
                        str[i][j] = 'O';
                    }
                }
                break;
            case 'L':
                cin>>X>>Y>>CL;
                str[Y][X] = CL;
                break;
            case 'V':
                cin>>X0>>Y1>>Y2>>CV;
                for(int i = Y1;i<=Y2;i++)
                {
                    str[i][X0] = CV;
                }
                break;
            case 'H':
                cin>>X1>>X2>>Y0>>CH;
                for(int i = X1;i<=X2;i++)
                {
                    str[Y0][i] = CH;
                }
                break;
            case 'K':
                cin>>x1>>y1>>x2>>y2>>CK;
                for(int i = y1;i<=y2;i++)
                {
                    for(int j = x1;j<=x2;j++)
                        str[i][j] = CK;
                }
                break;
            case 'F':
                cin>>XF>>YF>>CF;
                CF_0=str[YF][XF];
                for(int i = 1;i<=N;i++)
                {
                    for(int j = 1;j<=M;j++)
                    {
                        if(str[i][j]==CF_0)
                            str[i][j] = CF;
                    }
                }
                break;
            case 'S':
                cin>>name;
                cout<<name<<endl;
                print(N,M);
            default:
                break;
        }
    }

    return 0;
}

这道题大概看懂意思后也就不难了,我觉得主要考察的应该是switch的流程控制吧。

//看题要仔细,题中是英文字母‘O’,而不是数字‘0’,我在这WA了好几次……




原文地址:https://www.cnblogs.com/gray1566/p/3704309.html