[CSP 201512-3] Paint

Paint:

Question number 201512-3
Questions Name Paint
time limit 1.0s
Memory Limit 256.0MB

Subject description:

ASCII characters are used to draw a funny thing, and formed one of the art is known as ASCII Art. For example, the following figure is drawn with ASCII characters CSPRO words.
Here Insert Picture Description
This question requires programming with ASCII characters to a drawing program, supports the following two operations:

1, drawing a line: given the coordinates of two endpoints, draw a line segment connecting two endpoints. To ensure simplicity title of each segment are drawn horizontally or vertically. Level segment with the character - to draw a vertical line with the character | to draw. If a horizontal line and a vertical line segment intersect at a certain position, the position of intersection with the + character instead.
2, the filling: The filling is given a starting position coordinates and character needs to be filled, from the starting position, with the character adjacent to the filling position, or until it encounters the edges of the canvas painting has a good line. Note that only need to consider the position adjacent to the vertical and horizontal four directions, as shown below, and only the characters @ 4 * adjacent characters in FIG.
Here Insert Picture Description

Input formats:

The first line contains three integers m, n and q. m and n represent the width and height of the canvas, in units of characters. q represents the number of drawing operations.

The second row to row q + 1, each row is one of two forms:

1,0 x1 y1 x2 y2: Videos showing the operation of the line, (x1, y1) and (x2, y2) are the two ends of the segment, or satisfies x1 = x2 and y1 ≠ y2, and y1 = y2 or x1 ≠ x2 .
2, 1 xyc: indicates the filling operation, (x, y) is the starting position, guaranteed not to fall on any existing segments; C to fill characters, it is sensitive.

Is the lower left corner of the canvas coordinates (0, 0) position, to the right of the direction of increasing x-coordinate, the y coordinate increases upward direction. This q operations sequentially performed in the order presented data. All positions are initially canvas characters. (Decimal point).

Output formats:

There are n output lines of m characters, drawing showing the result of sequentially performing these operations q obtained.

Example:

Sample input 1:

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

Sample output 1:

AAAA
A--A

Sample input 2:

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 2:

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

Data Scale:

Reviews satisfy all use cases: 2 ≤ m, n ≤ 100,0 ≤ q ≤ 100,0 ≤ x <m (x represents the x-coordinate positions of all the input data), 0 ≤ y <n (y represent all the input data y position coordinates).

Ideas:

According to the data size, the data can know the maximum desired range of coordinates (0 to 100), thus a two-dimensional array of characters available canvas char vis [size] [size] FIG. Then there is the question of the transformation of coordinates, how to coordinate links with the two-dimensional array, a two-dimensional array to imagine two-dimensional plane, because the (0,0) in the lower left corner, and therefore do not have to change the x coordinate, and y coordinates strain size-y correspondence to the two-dimensional array, on the other hand for the two-dimensional array is represented by a first high-dimensional canvas, a second dimension represents long, x should correspond to the second dimension, the first dimension and y should correspond.
For drawing a line operation, first to determine the coordinates of the converted horizontal or vertical line is drawn, followed by the judge responsible for the content when drawing lines, has decided to draw a line or a sign of the cross.
For the filling operation, is used here dfs practice, the starting point to point around the turn filled.

Reflection:

1, a two-dimensional array of first analog canvas not thought x, y conversion problem, take the first dimension as a direct x, y as the second dimension, the output results in a different region from the region of operation.
2, when a straight line is drawn, care should be met Analyzing '+', and ought not to be '+' to be modified.
3, during the filling operation, dfs is not the first time used but the use of bfs, but using bfs judge has jumbled determine whether a neighboring node expansion conditions, and these conditions are related to the relationship, leading to a timeout, after change DFS used, mostly in the relationship determination conditions or can reduce operating time.

Code:

#include <iostream>
#include<cstdio>
#include<string.h>
using namespace std;
const int size=100+10;
int m,n;
char vis[size][size];
void swap(int &a,int &b)
{
 if(a<b)
  return;
 int c=a;
 a=b;
 b=c;
}
void op1(int x,int y,char stuff)
{
 if(x<0||x>=m||y>=size||y<size-n)
  return;
 if(vis[y][x]==stuff||vis[y][x]=='|'||vis[y][x]=='-'||vis[y][x]=='+')
  return;
 vis[y][x]=stuff;
 op1(x+1,y,stuff);
 op1(x-1,y,stuff);
 op1(x,y+1,stuff);
 op1(x,y-1,stuff);
}
int main(int argc, char** argv) {
 int q;
 int x0=0,y0=size-1;
 scanf("%d%d%d",&m,&n,&q);
 memset(vis,'.',size*size*sizeof(char));
 for(int i=0;i<q;i++)
 {
  int op;
  scanf("%d",&op);
  if(op==1)
  {
   int x,y;char stuff;
   scanf("%d%d %c",&x,&y,&stuff);
   x=x0+x;y=y0-y;
   op1(x,y,stuff);
  }
  else
  {
   int x1,y1,x2,y2;
   scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
   x1=x0+x1;x2=x0+x2;
   y1=y0-y1;y2=y0-y2;
   if(x1==x2)
   {
    swap(y1,y2);
    for(int i=y1;i<=y2;i++)
    {
     if(vis[i][x1]!='-'&&vis[i][x1]!='+')
      vis[i][x1]='|';
     else
      vis[i][x1]='+';
    }
   }
   else
   {
    swap(x1,x2);
    for(int i=x1;i<=x2;i++)
    {
     if(vis[y1][i]!='|'&&vis[y1][i]!='+')
      vis[y1][i]='-';
     else
      vis[y1][i]='+';
    }
   }
  }
 }
 for(int i=size-n;i<size;i++)
 {
  for(int j=0;j<m;j++)
   printf("%c",vis[i][j]);
  printf("\n");
 } 
 return 0;
}
Published 24 original articles · won praise 8 · views 530

Guess you like

Origin blog.csdn.net/weixin_44034698/article/details/104905590