试题编号: | 201512-3 |
试题名称: | 画图 |
时间限制: | 1.0s |
内存限制: | 256.0MB |
问题描述: | 问题描述 用 ASCII 字符来画图是一件有趣的事情,并形成了一门被称为 ASCII Art 的艺术。例如,下图是用 ASCII 字符画出来的 CSPRO 字样。 输入格式 第1行有三个整数m, n和q。m和n分别表示画布的宽度和高度,以字符为单位。q表示画图操作的个数。 输出格式 输出有n行,每行m个字符,表示依次执行这q个操作后得到的画图结果。 样例输入 4 2 3 样例输出 AAAA 样例输入 16 13 9 样例输出 ................ 评测用例规模与约定 所有的评测用例满足:2 ≤ m, n ≤ 100,0 ≤ q ≤ 100,0 ≤ x < m(x表示输入数据中所有位置的x坐标),0 ≤ y < n(y表示输入数据中所有位置的y坐标)。 |
题解 根据输入条件来进行两个操作,注意一个点就是绘制线的时候判断原来的状态:方向一样或者是交叉点‘+’;
package cn.wzy.draw;import java.util.Scanner;/*** Create by Wzy* on 2018/7/25 18:45* 不短不长八字刚好*/
public class Main {//画图 90分static char[][] map;static class Point {int x;int y;public Point() {}public Point(int x, int y) {this.x = x;this.y = y;}}static void init(int n, int m) {map = new char[n][m];for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {map[i][j] = '.';}}}static void compare(Point a, Point b) {if (a.x == b.x) {if (a.y > b.y) {int temp = a.y;a.y = b.y;b.y = temp;}}if (a.y == b.y) {if (a.x > b.x) {int temp = a.x;a.x = b.x;b.x = temp;}}}static void drawLine(Point start, Point end) {compare(start, end);if (start.x == end.x) {//纵向划线‘|’for (int i = start.y; i <= end.y; i++) {if (map[start.x][i] == '-')map[start.x][i] = '+';else if (map[start.x][i] != '+')map[start.x][i] = '|';}}else {//横向划线‘—’for (int i = start.x; i <= end.x; i++) {if (map[i][start.y] == '|')map[i][start.y] = '+';else if (map[i][start.y] != '+')map[i][start.y] = '-';}}}static void cover(int x, int y, char c) {if (x < 0 || x >= map.length|| y < 0 || y >= map[0].length|| map[x][y] == '+'|| map[x][y] == '-'|| map[x][y] == '|'|| map[x][y] == c) {return;}map[x][y] = c;cover(x - 1, y, c);cover(x + 1, y, c);cover(x, y + 1, c);cover(x, y - 1, c);}static void print(int n, int m) {for (int j = m - 1; j >= 0; j--) {for (int i = 0; i < n; i++) {System.out.print(map[i][j]);}System.out.println();}
// System.out.println();}public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int n = scanner.nextInt();//heightint m = scanner.nextInt();//widthint time = scanner.nextInt();init(n, m);for (int i = 0; i < time; i++) {int type = scanner.nextInt();if (type == 0) {//划线Point start = new Point(scanner.nextInt(), scanner.nextInt());Point end = new Point(scanner.nextInt(), scanner.nextInt());drawLine(start, end);
// print(n,m);}else {int x = scanner.nextInt();int y = scanner.nextInt();char c = scanner.next().charAt(0);cover(x,y,c);
// print(n,m);}}print(n,m);}
}