CCF-201512-3-画图

这题搞字符艺术,模拟平时画图那样画线然后填充颜色。垂直线与水平线相交点变‘+’加号,填充用广搜,用深搜也行,不过最后一个用例会爆栈,导致运行出错。广搜我自己测试100*100的矩阵,然后广搜100遍都很快,不用一秒出结果,但是提交上去只有90,最后一个用例运行超时。。。很迷。
python 运行是比较慢,用java写提交就100了。

python代码

def draw_line(x1, y1, x2, y2, matrix):
    xlen = len(matrix)
    ylen = len(matrix[0])
    # 画线
    if x1 == x2:
        for y in range(min(y1, y2), max(y1, y2) + 1):
            if matrix[x1][y] == '-' or (x1 - 1 >= 0 and matrix[x1 - 1][y] == '-') or (
                    x1 + 1 < xlen and matrix[x1 + 1][y] == '-'):
                matrix[x1][y] = '+'
            else:
                matrix[x1][y] = '|'
    elif y1 == y2:
        for x in range(min(x1, x2), max(x1, x2) + 1):
            if matrix[x][y1] == '|' or (y1 - 1 >= 0 and matrix[x][y1 - 1] == '|') or (
                    y1 + 1 < ylen and matrix[x][y1 + 1] == '|'):
                matrix[x][y1] = '+'
            else:
                matrix[x][y1] = '-'


def fill_bfs(x, y, c, matrix):
    # 填充
    xlen = len(matrix)
    ylen = len(matrix[0])
    diret = [[1, 0], [-1, 0], [0, -1], [0, 1]]
    q = []
    if 0 <= x < xlen and \
            0 <= y < ylen and \
            matrix[x][y] != '|' and \
            matrix[x][y] != '-' and \
            matrix[x][y] != '+' and \
            matrix[x][y] != c:
        q.append([x, y])
    matrix[x][y] = c
    while q:
        node = q.pop(0)
        for d in diret:
            tx, ty = node[0] + d[0], node[1] + d[1]
            if 0 <= tx < xlen and \
                    0 <= ty < ylen and \
                    matrix[tx][ty] != '|' and \
                    matrix[tx][ty] != '-' and \
                    matrix[tx][ty] != '+' and \
                    matrix[tx][ty] != c:
                q.append([tx, ty])
                matrix[tx][ty] = c


# 主程序
m, n, q = map(int, input().split())
matrix = [['.' for i in range(n)] for j in range(m)]

for i in range(q):
    op = input().split()
    if op[0] == '0':
        draw_line(int(op[1]), int(op[2]), int(op[3]), int(op[4]), matrix)
    elif op[0] == '1':
        fill_bfs(int(op[1]), int(op[2]), op[3], matrix)

# 打印顺时针翻转的图
for j in range(n)[::-1]:
    for i in range(m):
        print(matrix[i][j], end='')
    print()

java代码:

import java.util.LinkedList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        int n = sc.nextInt();
        int q = sc.nextInt();
        char[][] matrix = new char[m][n];

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                matrix[i][j] = '.';
            }
        }

        for (int i = 0; i < q; i++) {
            int op = sc.nextInt();
            //划线
            if (op == 0) {
                int x1 = sc.nextInt(), y1 = sc.nextInt();
                int x2 = sc.nextInt(), y2 = sc.nextInt();
                draw_line(x1, y1, x2, y2, matrix);
            }
            //填充
            else {
                int x = sc.nextInt(), y = sc.nextInt();
                char c = sc.next().charAt(0);
                fill_char(x, y, c, matrix);
            }
        }

        for (int j = n - 1; j >= 0; j--) {
            for (int i = 0; i < m; i++) {
                System.out.print(matrix[i][j]);
            }
            System.out.println();
        }
    }

    public static void draw_line(int x1, int y1, int x2, int y2, char[][] matrix) {
        if (x1 == x2) {
            for (int j = Math.min(y1, y2); j <= Math.max(y1, y2); j++) {
                if (matrix[x1][j] == '-') {
                    matrix[x1][j] = '+';
                } else if (matrix[x1][j] != '+') {
                    matrix[x1][j] = '|';
                }
            }
        } else if (y1 == y2) {
            for (int j = Math.min(x1, x2); j <= Math.max(x1, x2); j++) {
                if (matrix[j][y1] == '|') {
                    matrix[j][y1] = '+';
                } else if (matrix[j][y1] != '+') {
                    matrix[j][y1] = '-';
                }
            }
        }
    }

    public static void fill_char(int x, int y, char c, char[][] matrix) {
        int[][] dires = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
        LinkedList<node> queue = new LinkedList<>();
        node start = new node(x, y);
        queue.offer(start);
        boolean[][] vis = new boolean[matrix.length][matrix[0].length];
        vis[x][y] = true;
        while (!queue.isEmpty()) {
            node tem = queue.poll();
            matrix[tem.x][tem.y] = c;
            for (int i = 0; i < dires.length; i++) {
                int nx = tem.x + dires[i][0];
                int ny = tem.y + dires[i][1];
                if (0 <= nx && nx < matrix.length &&
                        0 <= ny && ny < matrix[0].length &&
                        !vis[nx][ny] && matrix[nx][ny] != '-' &&
                        matrix[nx][ny] != '+' && matrix[nx][ny] != '|') {
                    queue.offer(new node(nx, ny));
                    vis[nx][ny] = true;
                }
            }
        }
    }
}

class node {
    int x;
    int y;

    node(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

//4 2 3
//        1 0 0 B
//        0 1 0 2 0
//        1 0 0 A
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值