201512-3 画图

实现

#include <cstdio>
#define MAXN 0x6f

char map[MAXN][MAXN];
int row, col, ops_num;

void paint_line(int x1, int y1, int x2, int y2) {
    char line;
    if (x1 == x2 && y1 != y2) {
        int step = (y1 < y2)?1:-1;
        int y = y1;
        if (map[x1][y1] != '|' 
        && map[x1][y1] != '+') {
            map[x1][y1]='-';
        } else {
            map[x1][y1] = '+';
        }
        do {
            y += step;
            if (map[x1][y] != '|' 
            && map[x1][y] != '+') {
                map[x1][y] = '-';
            } else {
                map[x1][y] = '+';
            }
        } while (y != y2);
    } else if (x1 != x2 && y1 == y2) {
        int step = (x1 < x2)?1:-1;
        int x = x1;
        if (map[x1][y1] != '-' 
        && map[x1][y1] != '+') {
            map[x1][y1]='|';
        } else {
            map[x1][y1] = '+';
        }
        do {
            x += step;
            if (map[x][y1] != '-' 
            && map[x][y1] != '+') {
                map[x][y1]='|';
            } else {
                map[x][y1] = '+';
            }
        } while (x != x2);
    } else {
        return ;
    }

}

void paint_content(int x, int y, char ch) {
    if (x < 0 || x >= row 
    || y < 0 || y >= col) {
        return;
    }
    if (map[x][y] == '-'
    || map[x][y] == '|'
    || map[x][y] == '+'
    || map[x][y] == ch) {
        return ;
    }
    map[x][y] = ch;
    paint_content(x+1,y,ch);
    paint_content(x,y+1,ch);
    paint_content(x-1,y,ch);
    paint_content(x,y-1,ch);
}

int main() {
    
    scanf("%d%d%d",&col,&row,&ops_num);
    for (int i = 0;i < row;++i) {
        for (int j = 0;j < col;++j) {
            map[i][j]='.';
        }
    }

    for (int i = 0;i < ops_num;++i) {
        int op_type;
        scanf("%d",&op_type);

        switch (op_type)
        {
            case 0:
                int x1, y1, x2, y2;
                scanf("%d%d%d%d",&y1,&x1,&y2,&x2);
                x1 = row - 1 - x1;
                x2 = row - 1 - x2;
                paint_line(x1,y1,x2,y2);
                break;
            case 1:
                int x, y;
                char ch;
                scanf("%d%d %c",&y,&x,&ch);
                x = row - 1 - x;
                paint_content(x,y,ch);
                break;
            default:
                break;
        }
    }
    for (int i = 0;i < row;++i) {
        for (int j = 0;j < col;++j) {
            printf("%c",map[i][j]);
        }
        printf("
");
    } 
}
原文地址:https://www.cnblogs.com/amonqsq/p/13581348.html