画图(csp2015 12-3)

题意:
问题描述
\hspace{17pt}用 ASCII 字符来画图是一件有趣的事情,并形成了一门被称为 ASCII Art 的艺术。例如,下图是用 ASCII 字符画出来的 CSPRO 字样。

  ..____.____..____..____...___..
  ./.___/.___||.._.\|.._.\./._.\.
  |.|...\___.\|.|_).|.|_).|.|.|.|
  |.|___.___).|..__/|.._.<|.|_|.|
  .\____|____/|_|...|_|.\_\\___/.

本题要求编程实现一个用 ASCII 字符来画图的程序,支持以下两种操作:
画线:给出两个端点的坐标,画一条连接这两个端点的线段。简便起见题目保证要画的每条线段都是水平或者竖直的。水平线段用字符 - 来画,竖直线段用字符 | 来画。如果一条水平线段和一条竖直线段在某个位置相交,则相交位置用字符 + 代替。
填充:给出填充的起始位置坐标和需要填充的字符,从起始位置开始,用该字符填充相邻位置,直到遇到画布边缘或已经画好的线段。注意这里的相邻位置只需要考虑上下左右 4 个方向,如下图所示,字符 @ 只和 4 个字符 * 相邻。

  .*.
  *@*
  .*.

思路:
这题分为2部分:画线,填充。
画线并不难,就是一个模拟的过程,搞清楚怎么模拟,处理好一些小细节就可以。当前位置已有‘-’或‘|’的时候,需要改成‘+’。
填充的过程是bfs,记好bfs的过程就可以,没有需要特殊处理的地方,遇到边界或者线段停止。
总结:
有一个小细节,横纵坐标不要搞混。行对应y,列才对应x。
而且最后输出的时候是从上向下输出,有一些绕脑袋。
代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <climits>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;

const int N = 100 + 1;
struct node
{
    int x, y;
};
int vis[N][N];
char ch[N][N];
int n, m, q;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0}; //方向
queue<node> que;
void connect(int x1, int y1, int x2, int y2)
{
    if (x1 == x2)
    {
        if (y1 < y2)
            swap(y1, y2);
        for (int i = y2; i <= y1; i++)
        {
            if (ch[x1][i] == '-' || ch[x1][i] == '+')
                ch[x1][i] = '+';
            else
                ch[x1][i] = '|';
        }
    }
    else
    {
        if (x1 < x2)
            swap(x1, x2);
        for (int i = x2; i <= x1; i++)
        {
            if (ch[i][y1] == '|' || ch[i][y1] == '+')
                ch[i][y1] = '+';
            else
                ch[i][y1] = '-';
        }
    }
}
void fill(int x, int y, char a)
{
    que.push({x, y});
    memset(vis, 0, sizeof(vis));
    vis[x][y] = 1;
    ch[x][y] = a;
    while (!que.empty())
    {
        node backnode = que.front();
        que.pop();
        for (int i = 0; i < 4; i++)
        {
            int x1 = backnode.x + dx[i];
            int y1 = backnode.y + dy[i];
            if (x1 >= 0 && y1 >= 0 && x1 < m && y1 < n && !vis[x1][y1] && ch[x1][y1] != '|' && ch[x1][y1] != '-' && ch[x1][y1] != '+')
            {
                vis[x1][y1] = 1;
                ch[x1][y1] = a;
                que.push({x1, y1});
            }
        }
    }
}

int main()
{
    cin >> m >> n >> q;
    //初始化
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n; j++)
            ch[i][j] = '.';
    for (int i = 1; i <= q; i++)
    {
        int op;
        cin >> op;
        if (op == 0)
        {
            int x1, y1, x2, y2;
            cin >> x1 >> y1 >> x2 >> y2;
            connect(x1, y1, x2, y2);
        }
        else if (op == 1)
        {
            int x, y;
            cin >> x >> y;
            char c;
            cin >> c;
            fill(x, y, c);
        }
    }
    //需要按行输出,所以改一下输出的位置
    //行对应y,列才对应x
    for (int i = n - 1; i >= 0; i--)
    {
        for (int j = 0; j < m; j++)
        {
            cout << ch[j][i];
        }
        cout << endl;
    }
    //system("pasue");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值