CCF认证201512-3画图

在这里插入图片描述

BFS,这道题比较讨厌的地方是通常情况下一个二维数组的起点在左上角,而这道题的起点在左下角,那么在处理的过程中就需要对纵坐标也就是数组的第一维度进行特殊处理。

#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cstring>
#include <vector>
#include <sstream>
#include <queue>
using namespace std;
char a[110][110];
struct node{
    int x, y;
    char c;
    node(int xx, int yy, char cc)
    {
        x = xx;
        y = yy;
        c = cc;
    }
};
int X[4] = {0, 0, 1, -1};
int Y[4] = {1, -1, 0, 0};
int vis[110][110];
int m, n, q;
void BFS(int x, int y, char c)
{
    memset(vis, false, sizeof(vis));
    queue<node> q;
    q.push(node(x, n - y - 1, c));
    while(!q.empty())
    {
        node f = q.front();
        q.pop();
        for(int i = 0 ; i < 4 ; ++i)
        {
            int newX = f.x + X[i];
            int newY = f.y + Y[i];
            if(newX >= 0 && newX < m && n - newY - 1 >= 0 && n - newY - 1 < n && a[n - newY - 1][newX] != '|' && a[n - newY - 1][newX] != '-' && a[n - newY - 1][newX] != '+' && !vis[n - newY - 1][newX])
            {
                a[n - newY - 1][newX] = c;
                q.push(node(newX, newY, c));
                vis[n - newY - 1][newX] = true;
            }
        }
    }
}
int main()
{
    cin>>m>>n>>q;
    for(int i = n - 1 ; i >= 0 ; --i)
    {
        for(int j = 0 ; j < m ; ++j)
            a[i][j] = '.';
    }
    int type, x1, x2, y1, y2, x, y;
    char c;
    for(int i = 0 ; i < q ; ++i)
    {
        cin>>type;
        if(type == 0)
        {
            cin>>x1>>y1>>x2>>y2;
            if(x1 == x2)
            {
                if(y1 > y2)
                    swap(y1, y2);
                for(int j = y1 ; j <= y2 ; ++j)
                {
                    if(a[n - j - 1][x1] == '-')
                        a[n - j - 1][x1] = '+';
                    else if(a[n - j - 1][x1] != '+')
                        a[n - j - 1][x1] = '|';
                }
            }
            else if(y1 == y2)
            {
                y1 = n - y1 - 1;
                if(x1 > x2)
                    swap(x1, x2);
                for(int j = x1 ; j <= x2 ; ++j)
                {
                    if(a[y1][j] == '|')
                        a[y1][j] = '+';
                    else if(a[y1][j] != '+')
                    {
                        a[y1][j] = '-';
                    }
                }
            }
        }
        else if(type == 1)
        {
            cin>>x>>y>>c;
            y = n - y - 1;
            BFS(x, y, c);
        }
    }
    for(int i = 0 ; i < n ; ++i)
    {
        for(int j = 0 ; j < m ; ++j)
        {
            cout<<a[i][j];
        }
        cout<<endl;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值