PC/UVa 题号: 110105/10267 Graphical Editor (图形化编辑器)题解

时间:2023-12-04 15:53:44

#include<cstdio>
#include<iostream>
#include<string>
#include<algorithm>
#include<iterator>
#include<cstring>
#include<set>

using namespace std;
int m;
int n;
//bmp[y][x]
char bmp[251][251];

void swap_if_bigger(int& x, int& y)
{
    if(x>y)
        swap(x, y);
}

//I M N  Creates a new table M×N. All the pixels are colored in white (O)
void create()
{
    scanf("%d %d", &m, &n);
    memset(bmp, 'O', sizeof(bmp));
}

//C  Clears the table. The size remains the same. All the pixels became white (O).
void clear()
{
    memset(bmp, 'O', sizeof(bmp));
}

//L X Y C    Colors the pixel with coordinates (X,Y) in colour C.
void draw_pixel()
{
    int x,y;
    char color;
    scanf("%d %d %c", &x, &y, &color);
    bmp[y][x]=color;
}

//V X Y1 Y2 C    Draws the vertical segment in the column X between the rows Y1 and Y2 inclusive in colour C.
void draw_ver_seg()
{
    int x, y1, y2;
    char c;
    scanf("%d %d %d %c", &x, &y1, &y2, &c);
    swap_if_bigger(y1, y2);
    for(int y=y1;y<=y2;y++)
    {
        bmp[y][x]=c;
    }
}

//H X1 X2 Y C    Draws the horizontal segment in the row Y between the columns X1 and X2 inclusive in colour C.
void draw_hor_seg()
{
    int x1, x2, y;
    char c;
    scanf("%d %d %d %c", &x1, &x2, &y, &c);
    swap_if_bigger(x1, x2);
    for(int x=x1;x<=x2;x++)
    {
        bmp[y][x]=c;
    }
}

//K X1 Y1 X2 Y2 C    Draws the filled rectangle in colour C. (X1,Y1) is the upper left corner, (X2,Y2) is the lower right corner of the rectangle.
void draw_rect()
{
    int x1, y1, x2, y2;
    char c;
    scanf("%d %d %d %d %c", &x1, &y1, &x2, &y2, &c);
    swap_if_bigger(x1, x2);
    swap_if_bigger(y1, y2);
    for(int x=x1;x<=x2;x++)
        for(int y=y1;y<=y2;y++)
        {
            bmp[y][x]=c;
        }
}

//F X Y C    Fills the region with the colour C. The region R to be filled is defined as follows. The pixel (X,Y) belongs to this region. The other pixel belongs to the region R if and only if it has the same colour as pixel (X,Y) and a common side with any pixel which belongs to this region.
void _fill_region(int x, int y, char c)
{
    char old_color=bmp[y][x];
    if(old_color==c)
        return;
    bmp[y][x]=c;
    static int direction[4][2]={
        {-1, 0},
        {1, 0},
        {0, -1},
        {0, 1},
    };

    for(int i=0;i<4;i++)
    {
        int nx=x+direction[i][0];
        int ny=y+direction[i][1];

        if(nx<1 || nx>m || ny<1 || ny>n)
            continue;

        if(bmp[ny][nx]==old_color)
        {
           // cout<<nx<<" "<<ny<<endl;
            _fill_region(nx, ny, c);
        }
    }

}

void fill_region()
{
    int x,y;
    char c;
    scanf("%d %d %c", &x, &y, &c);
    _fill_region(x, y, c);
}

//S Name     Writes the picture in the file Name.
void save()
{
    string filename;
    cin>>filename;
    cout<<filename<<endl;
    for(int y=1;y<=n;y++)
    {
        for(int x=1;x<=m;x++)
        {
            cout<<bmp[y][x];
        }
        cout<<endl;
    }
}

 

int main()
{
    char cmd;
    char line[256];
    while(cin>>cmd)
    {
        if(cmd=='X')
            break;
        switch(cmd)
        {
            case 'I':
                create();
                break;
            case 'C':
                clear();
                break;
            case 'L':
                draw_pixel();
                break;
            case 'V':
                draw_ver_seg();
                break;
            case 'H':
                draw_hor_seg();
                break;
            case 'K':
                draw_rect();
                break;
            case 'F':
                fill_region();
                break;
            case 'S':
                save();
                break;
            default:
                gets(line);
                continue;
        }

    }

    return 0;
}