试题编号: | 201512-3 |
试题名称: | 画图 |
时间限制: | 1.0s |
内存限制: | 256.0MB |
问题描述: |
问题描述
用 ASCII 字符来画图是一件有趣的事情,并形成了一门被称为 ASCII Art 的艺术。例如,下图是用 ASCII 字符画出来的 CSPRO 字样。
..____.____..____..____...___.. ./.___/.___||.._.\|.._.\./._.\. |.|...\___.\|.|_).|.|_).|.|.|.| |.|___.___).|..__/|.._.<|.|_|.| .\____|____/|_|...|_|.\_\\___/. 本题要求编程实现一个用 ASCII 字符来画图的程序,支持以下两种操作: Ÿ 画线:给出两个端点的坐标,画一条连接这两个端点的线段。简便起见题目保证要画的每条线段都是水平或者竖直的。水平线段用字符 - 来画,竖直线段用字符 | 来画。如果一条水平线段和一条竖直线段在某个位置相交,则相交位置用字符 + 代替。 Ÿ 填充:给出填充的起始位置坐标和需要填充的字符,从起始位置开始,用该字符填充相邻位置,直到遇到画布边缘或已经画好的线段。注意这里的相邻位置只需要考虑上下左右 4 个方向,如下图所示,字符 @ 只和 4 个字符 * 相邻。 .*. *@* .*.
输入格式
第1行有三个整数m, n和q。m和n分别表示画布的宽度和高度,以字符为单位。q表示画图操作的个数。
第2行至第q + 1行,每行是以下两种形式之一: Ÿ 0 x 1 y 1 x 2 y 2:表示画线段的操作,(x 1, y 1)和(x 2, y 2)分别是线段的两端,满足要么x 1 = x 2 且y 1 ≠ y 2,要么 y 1 = y 2 且 x 1 ≠ x 2。 Ÿ 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.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int x=scanner.nextInt();
int y=scanner.nextInt();
int n=scanner.nextInt();
char[][] image=new char[y][x];
for (int i = 0; i < y; i++) {
Arrays.fill(image[i], '.');
}
dire[] director=new dire[4];
director[0]=new dire(1,0);
director[1]=new dire(0, 1);
director[2]=new dire(-1,0);
director[3]=new dire(0,-1);
for (int i = 0; i < n; i++) {
int chose=scanner.nextInt();
if (chose==1) {
int sy=scanner.nextInt();
int sx=scanner.nextInt();
char fillc=scanner.nextLine().toCharArray()[1];
fill(sx,sy,fillc,director,image,y,x);
}else if (chose==0) {
int x1=scanner.nextInt();
int y1=scanner.nextInt();
int x2=scanner.nextInt();
int y2=scanner.nextInt();
makeline(x1,y1,x2,y2,image);
}
}
for (int i =y-1 ; i >=0; i--) {
for (int j = 0; j < x; j++) {
System.out.print(image[i][j]);
}
System.out.print("\r\n");
}
}
private static void makeline(int x1, int y1, int x2, int y2, char[][] image) {
if (x1==x2) {
int start=Math.min(y1,y2);
int end=Math.max(y1, y2);
for (int i = start; i <= end; i++) {
if (image[i][x1]=='-') {
image[i][x1]='+';
}else {
image[i][x1]='|';
}
}
}else if (y1==y2) {
int start=Math.min(x1, x2);
int end=Math.max(x1, x2);
for (int i = start; i <= end; i++) {
if (image[y1][i]=='|') {
image[y1][i]='+';
}else {
image[y1][i]='-';
}
}
}
}
private static void fill(int sx, int sy, char fillc, dire[] director, char[][] image,int x,int y) {
image[sx][sy]=fillc;
for (int i = 0; i < 4; i++) {
int cx=sx+director[i].x;
int cy=sy+director[i].y;
if (cx>=0&&cx<x&&cy>=0&&cy<y
&&image[cx][cy]!='-'&&image[cx][cy]!='+'&&image[cx][cy]!='|'&&image[cx][cy]!=fillc) {
fill(cx, cy, fillc, director, image, x, y);
}
}
}
}
class dire{
int x;
int y;
public dire(int a,int b) {
x=a;
y=b;
}
}
实验中给的两个测试结果都通过了,但是提交之后运行错误,只有80分。
加了一个条件,即当画线的时候可能会遇到有+的情况,这时候将+不变,加上后,90分,不知道还有什么条件没有考虑到。
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int x=scanner.nextInt();
int y=scanner.nextInt();
int n=scanner.nextInt();
char[][] image=new char[y][x];
for (int i = 0; i < y; i++) {
Arrays.fill(image[i], '.');
}
dire[] director=new dire[4];
director[0]=new dire(1,0);
director[1]=new dire(0, 1);
director[2]=new dire(-1,0);
director[3]=new dire(0,-1);
for (int i = 0; i < n; i++) {
int chose=scanner.nextInt();
if (chose==1) {
int sy=scanner.nextInt();
int sx=scanner.nextInt();
char fillc=scanner.nextLine().toCharArray()[1];
fill(sx,sy,fillc,director,image,y,x);
}else if (chose==0) {
int x1=scanner.nextInt();
int y1=scanner.nextInt();
int x2=scanner.nextInt();
int y2=scanner.nextInt();
makeline(x1,y1,x2,y2,image);
}
}
for (int i =y-1 ; i >=0; i--) {
for (int j = 0; j < x; j++) {
System.out.print(image[i][j]);
}
System.out.print("\r\n");
}
}
private static void makeline(int x1, int y1, int x2, int y2, char[][] image) {
if (x1==x2) {
int start=Math.min(y1,y2);
int end=Math.max(y1, y2);
for (int i = start; i <= end; i++) {
if (image[i][x1]=='-'||image[i][x1]=='+') {
image[i][x1]='+';
}else {
image[i][x1]='|';
}
}
}else if (y1==y2) {
int start=Math.min(x1, x2);
int end=Math.max(x1, x2);
for (int i = start; i <= end; i++) {
if (image[y1][i]=='|'||image[y1][i]=='+') {
image[y1][i]='+';
}else {
image[y1][i]='-';
}
}
}
}
private static void fill(int sx, int sy, char fillc, dire[] director, char[][] image,int x,int y) {
image[sx][sy]=fillc;
for (int i = 0; i < 4; i++) {
int cx=sx+director[i].x;
int cy=sy+director[i].y;
if (cx>=0&&cx<x&&cy>=0&&cy<y
&&image[cx][cy]!='-'&&image[cx][cy]!='+'&&image[cx][cy]!='|'&&image[cx][cy]!=fillc) {
fill(cx, cy, fillc, director, image, x, y);
}
}
}
}
class dire{
int x;
int y;
public dire(int a,int b) {
x=a;
y=b;
}
}