【Month Simulation Question 1】 201512-3 Drawing

topic:

Problem Description

Drawing pictures with ASCII characters is an interesting thing, and formed an art called ASCII Art. For example, the figure below is the CSPRO drawn with ASCII characters.

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

This question requires programming to implement a program that uses ASCII characters to draw
  pictures . It supports the following two operations: Draw a line: Give the coordinates of two endpoints, and draw a line segment connecting the two endpoints. For simplicity's sake, ensure that each line segment to be drawn is horizontal or vertical. The horizontal line segment is drawn with the character-, and the vertical line segment is drawn with the character |. If a horizontal line segment and a vertical line segment intersect at a certain position, the intersection position is replaced by the character +.
  Fill: Give the coordinates of the starting position of the filling and the character to be filled. Starting from the starting position, fill the adjacent position with the character until it meets the edge of the canvas or the line segment that has been drawn. Note that the adjacent position here only needs to consider the four directions of up, down, left, and right. As shown in the following figure, the character @ is only adjacent to 4 characters *.

.*.
*@*
.*.

Input format

Line 1 has three integers m, n and q. m and n represent the width and height of the canvas, respectively, in characters. q represents the number of drawing operations.
Line 2 to line q + 1, each line is one of the following two forms:
  0 x1 y1 x2 y2: represents the operation of drawing a line segment, (x1, y1) and (x2, y2) are the two ends of the line segment, respectively Satisfy either x1 = x2 and y1 ≠ y2, or y1 = y2 and x1 ≠ x2.
  1 xyc: indicates the filling operation, (x, y) is the starting position, to ensure that it will not fall on any existing line segment; c is the filling character, is a capital letter.
The lower left corner of the canvas is the location with coordinates (0, 0), the direction to the right is the increase in the x coordinate, and the direction to the y coordinate is the increase. The q operations are executed in the order given by the data. Initially, all positions on the canvas were characters. (Decimal point).

Output format

The output has n lines, with m characters in each line, indicating the drawing results obtained after sequentially performing these q operations.

Sample input

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

Sample output

AAAA
A--A

Sample input

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

Sample output

................
...+--------+...
...|CCCCCCCC|...
...|CC+-----+...
...|CC|.........
...|CC|.........
...|CC|.........
...|CC|.........
...|CC|.........
...|CC+-----+...
...|CCCCCCCC|...
...+--------+...
................

Evaluation use case size and agreement

All evaluation use cases satisfy: 2 ≤ m, n ≤ 100, 0 ≤ q ≤ 100, 0 ≤ x <m (x represents the x coordinate of all positions in the input data), 0 ≤ y <n (y represents all of the input data The y coordinate of the location).

Ideas and practices:

According to the two operations of drawing, two functions are correspondingly completed: Fill () is used for letter filling, and DrawLine () is used to draw a line segment.
Mainly maintain a 2-dimensional array. The initial value is -1 for all ".", -2 for "|", -3 for "-", -4 for "+", and values ​​greater than or equal to 0 are used to represent filled letters .
In the DrawLine () function, first make sure that the starting point value is less than the ending point value, then you can change the value of the array in a for loop, and pay attention to the appearance of "+".
The Fill () function will fill with a wide search letters, across the border case, whether the conditions may be filled, where two-dimensional array can be used to determine whether repeated, and 'represent letters in the sign by sign-'A array
end, all operations After completion, it will go through the array fil and print the corresponding character according to its value.

to sum up:

The problem is not difficult. At first 90 points, it was found that the judgment in the DrawLine () function was wrong. When the value in the array was originally -4 (ie "+"), it should remain unchanged.

Code:

#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;

const int dx[] = {0, 0, -1, 1};
const int dy[] = {-1, 1, 0, 0};
const int N = 105;
struct Point{
	int x, y;
	Point(int _x, int _y):x(_x), y(_y){}
};
int fil[N][N];
int m, n, q;
queue<Point> qu;

void Fill(int x, int y, char sign){
	//(x, y)为起点填充整个画布 
	if(fil[x][y] >= -1 && fil[x][y] != sign-'A'){
		qu.push(Point(x, y));
		fil[x][y] = sign-'A';
	}
	while(!qu.empty()){
		Point p = qu.front();
		qu.pop();
		for(int i = 0; i < 4; ++i){
			int tx = p.x+dx[i], ty = p.y+dy[i];
			if(tx>=0 && tx<m && ty>=0 && ty<n && fil[tx][ty]>=-1 && fil[tx][ty]!=sign-'A'){
				qu.push(Point(tx, ty));
				fil[tx][ty] = sign-'A';
			}
		}
	}
}

void DrawLine(int x1, int y1, int x2, int y2){
	if(x1 == x2){
		if(y1 > y2){  //确保y1 < y2 
			int temp = y1;
			y1 = y2;
			y2 = temp;
		}
		for(int i = y1; i <= y2; ++i){
			if(fil[x1][i] == -3 || fil[x1][i] == -4) fil[x1][i] = -4;  //注意=-4时 
			else fil[x1][i] = -2;  //-2代表|
		}
	}else if(y1 ==y2){
		if(x1 > x2){  //确保x1 < x2 
			int temp = x1;
			x1 = x2;
			x2 = temp;
		}
		for(int i = x1; i <= x2; ++i){
			if(fil[i][y1] == -2 || fil[i][y1] == -4) fil[i][y1] = -4;  //-4代表+ 
			else fil[i][y1] = -3;  //-3代表-
		}
	}
}

int main(){
	scanf("%d %d %d", &m, &n, &q);
	memset(fil, -1, sizeof(fil));  //初始为-1 代表. 
	for(int i = 0; i < q; ++i){
		bool oper;
		scanf("%d", &oper);
		if(oper == 1){
			int x, y;
			char c;
			scanf("%d %d %c", &x, &y, &c);
			while(!qu.empty()) qu.pop();
			Fill(x, y, c);
		}else if(oper == 0){
			int x1, y1, x2, y2;
			scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
			DrawLine(x1, y1, x2, y2);
		}
	}
	for(int j = n-1; j >= 0; --j){
		for(int i = 0; i < m; ++i){
			if(fil[i][j] == -1) printf(".");
			else if(fil[i][j] == -2) printf("|");
			else if(fil[i][j] == -3) printf("-");
			else if(fil[i][j] == -4) printf("+");
			else printf("%c", fil[i][j]+'A');
		}
		printf("\n");
	}
	return 0;
} 
Published 10 original articles · Likes0 · Visits 231

Guess you like

Origin blog.csdn.net/weixin_44898140/article/details/105022172
Recommended