您的位置:首页 > 其它

画图

2016-04-08 09:01 162 查看
问题描述

  用 ASCII 字符来画图是一件有趣的事情,并形成了一门被称为 ASCII Art 的艺术。例如,下图是用 ASCII 字符画出来的 CSPRO 字样。

  .._._.._.._…_..

  ././.||...|..../._..

  |.|…___.|.|).|.|).|.|.|.|

  |.|.).|.._/|...<|.|_|.|

  .____|_/||…||._\/.

  本题要求编程实现一个用 ASCII 字符来画图的程序,支持以下两种操作:

  Ÿ 画线:给出两个端点的坐标,画一条连接这两个端点的线段。简便起见题目保证要画的每条线段都是水平或者竖直的。水平线段用字符 - 来画,竖直线段用字符 | 来画。如果一条水平线段和一条竖直线段在某个位置相交,则相交位置用字符 + 代替。

  Ÿ 填充:给出填充的起始位置坐标和需要填充的字符,从起始位置开始,用该字符填充相邻位置,直到遇到画布边缘或已经画好的线段。注意这里的相邻位置只需要考虑上下左右 4 个方向,如下图所示,字符 @ 只和 4 个字符 * 相邻。

  .*.

  @

  .*.

输入格式

  第1行有三个整数m, n和q。m和n分别表示画布的宽度和高度,以字符为单位。q表示画图操作的个数。

  第2行至第q + 1行,每行是以下两种形式之一:

  Ÿ 0 x1 y1 x2 y2:表示画线段的操作,(x1, y1)和(x2, y2)分别是线段的两端,满足要么x1 = x2 且y1 ≠ y2,要么 y1 = y2 且 x1 ≠ x2。

  Ÿ 1 x y c:表示填充操作,(x, y)是起始位置,保证不会落在任何已有的线段上;c 为填充字符,是大小写字母。

  画布的左下角是坐标为 (0, 0) 的位置,向右为x坐标增大的方向,向上为y坐标增大的方向。这q个操作按照数据给出的顺序依次执行。画布最初时所有位置都是字符 .(小数点)。

输出格式

  输出有n行,每行m个字符,表示依次执行这q个操作后得到的画图结果。

样例输入

4 2 3
1 0 0 B
0 1 0 2 0
1 0 0 A


样例输出

AAAA
A--A


样例输入

16 13 9
0 3 1 12 1
0 12 1 12 3
0 12 3 6 3
0 6 3 6 9
0 6 9 12 9
0 12 9 12 11
0 12 11 3 11
0 3 11 3 1
1 4 2 C


样例输出

................
...+--------+...
...|CCCCCCCC|...
...|CC+-----+...
...|CC|.........`这里写代码片`
...|CC|.........
...|CC|.........
...|CC|.........
...|CC|.........
...|CC+-----+...
...|CCCCCCCC|...
...+--------+...
................


评测用例规模与约定

  所有的评测用例满足:2 ≤ m, n ≤ 100,0 ≤ q ≤ 100,0 ≤ x < m(x表示输入数据中所有位置的x坐标),0 ≤ y < n(y表示输入数据中所有位置的y坐标)。

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Scanner;

public class Main {

public static void init(char map[][], char obj, int n, int m) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
map[i][j] = obj;
}
}
}

public static void init(boolean map[][], boolean obj, int n, int m) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
map[i][j] = obj;
}
}
}

public static void drawLine(char[][] map, char ch, int x1, int y1, int x2,
int y2) {
for (int i = x1; i <= x2; i++) {
for (int j = y1; j <= y2; j++) {
if (map[i][j] == '|' && ch == '-' || map[i][j] == '-'
&& ch == '|' || map[i][j] == '+') {
map[i][j] = '+';
} else {
map[i][j] = ch;
}
}
}
}

public static void line(char[][] map, int n, int m, int x1, int y1, int x2,
int y2) {
if (x1 == x2 && y1 != y2) {
drawLine(map, '-', x1, Math.max(0, Math.min(y1, y2)), x1,
Math.min(m - 1, Math.max(y1, y2)));
} else if (y1 == y2 && x1 != x2) {
drawLine(map, '|', Math.max(0, Math.min(x1, x2)), y1,
Math.min(n - 1, Math.max(x1, x2)), y1);
}
}

public static void print(char[][] map, int n, int m) {
for (int i = n-1; i >=0; i--) {
for (int j = 0; j < m; j++) {
System.out.print(map[i][j]);
}
System.out.println();
}
}

private static class Node {
private int x = 0;
private int y = 0;

public Node(int x, int y) {
super();
this.x = x;
this.y = y;
}

public int getX() {
return x;
}

public int getY() {
return y;
}
}

private static final int dis[][] = { { 0, 1 }, { 1, 0 }, { 0, -1 },
{ -1, 0 } };

public static void fill(char[][] map, int n, int m, char ch, int x, int y) {
boolean vis[][] = new boolean
[m];
init(vis, false, n, m);
Deque<Node> queue = new ArrayDeque<>();
queue.add(new Node(x, y));
vis[x][y] = true;
map[x][y] = ch;
while (queue.isEmpty() == false) {
Node cur = queue.pollFirst();
for (int i = 0; i < 4; i++) {
int tx = cur.getX() + dis[i][0];
int ty = cur.getY() + dis[i][1];
if (tx >= 0 && tx < n && ty >= 0 && ty < m
&& vis[tx][ty] != true && map[tx][ty] != '-'
&& map[tx][ty] != '|' && map[tx][ty] != '+') {
vis[tx][ty] = true;
map[tx][ty] = ch;
queue.add(new Node(tx, ty));
}
}
}
}

public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
while (cin.hasNext()) {
int m = cin.nextInt();
int n = cin.nextInt();
int q = cin.nextInt();
char map[][] = new char
[m];
init(map, '.', n, m);
for (int i = 0; i < q; i++) {
int op = cin.nextInt();
switch (op) {
case 0:
int y1 = cin.nextInt();
int x1 = cin.nextInt();
int y2 = cin.nextInt();
int x2 = cin.nextInt();
line(map, n, m, x1, y1, x2, y2);
break;

default:
int y = cin.nextInt();
int x = cin.nextInt();
char ch = cin.next().charAt(0);
fill(map, n, m, ch, x, y);
break;
}
}
print(map, n, m);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: