CCF 201512-3 画图

#include
using namespace std;
char Map[110][110];
bool vis[110][110];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int m, n;
void DFS(int x, int y, char C)
{
    if(Map[y][x]=='-' || Map[y][x]=='+' || Map[y][x]=='|')
        return ;
    Map[y][x] = C;
    vis[y][x] = 1;
    for(int i = 0; i < 4; i++){
        int xx = x + dx[i];
        int yy = y + dy[i];
        if(xx >= 0 && xx < m && yy >= 0 && yy < n && !vis[yy][xx])
            DFS(xx, yy, C);
    }
}


int main()
{
    int q;
    cin >> m >> n >> q;
    for(int i = 0; i < n; i++){    //初始化
        for(int j = 0; j < m; j++)
            Map[i][j] = '.';
    }
    while(q--){
        int op;
        cin >> op;
        if(op == 1){   //填充
            int x, y;
            char c;
            cin >> x >> y >> c;
            memset(vis, 0, sizeof(vis));
            y = n-1-y;
            DFS(x, y, c);
        }
        else{
            int x1, y1, x2, y2;
            cin >> x1 >> y1 >> x2 >> y2;
            if(x1 == x2) { //画竖线
                y1 = n-1-y1, y2 = n-1-y2;
                int x = x1, yy1 = min(y1, y2), yy2 = max(y1, y2);
                for(int i = yy1; i <= yy2; i++){
                    if(Map[i][x] == '-')
                        Map[i][x] = '+';
                    else if(Map[i][x] == '+')
                        Map[i][x] = '+';
                    else
                        Map[i][x] = '|';
                }

            }
            else {    //画横线
                int xx1 = min(x1, x2), xx2 = max(x1, x2);
                int y = n-1-y1;
                for(int j = xx1; j <= xx2; j++){
                    if(Map[y][j] == '|')
                        Map[y][j] = '+';
                    else if(Map[y][j] == '+')
                        Map[y][j] = '+';
                    else
                        Map[y][j] = '-';
                }
            }

        }
    }
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++)
            cout << Map[i][j];
        cout << endl;
    }
    return 0;
}

 

你可能感兴趣的:(CCF)