Exams _2015_3 drawing CSP (BFS application scenario in the exam csp)

Overview

Topic Overview

BFS algorithm is relatively simple and elementary algorithms, but because of its ease of implementation to address some of the problems in addressing the multidimensional space map, frequently appeared in many programming title. In solving the problem, if devised a BFS DFS algorithm or similar, if they can prove the algorithm in space and time complexity of the conditions are met, we can achieve decisive. If you find that some data can not be met, and that no other good ideas, but also can be used in the CSP as a way to get part of the score.

Original title description

Description of the problem
with ASCII characters to draw is a funny thing, and formed one of the art is known as ASCII Art.
This requires programming a problem with the ASCII character drawing program to support 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.

INPUT

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:

  • 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.
  • 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

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

O Case

A sample
Input:

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

Output:

AAAA
A ---- A

Sample two
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

Output:
Here Insert Picture Description

Title repeat

The number of operations to canvas size (width length m × n) to be output and set to be in the q. And all points of the canvas is initialized to '' '', the initial state in which no drawing lines.
There are two operations:
the operation is the prompt is 0 line drawing operation from the input (x1, y1) to (x2, y2) objects, and attention to ensure the completion of input data subject of a line drawing drawn by certain is a straight line, that is, there must x1 == x2 or y1 == y2.
Operation prompt is 1, is a filling operation, starting from a given starting point (x, y) input pad character, or reach the canvas drawn boundary line fill stop. (Obviously revision mazes)
output, the output of each character position, the following situations:
1, vertical lines, a horizontal line, outputs "+"
2, there are no horizontal vertical output "-"
3, there is no horizontal vertical output "|"
4, no horizontal no vertical line, with the filling, output fill character
. "" 5, no horizontal no vertical line, no fill, output
and should be noted that the output the canvas is output beginning at the top of the canvas, to pay attention to traverse the parameters of output.
Background and topics related to the string, the string appears to be a problem, but as long as roughly understanding the problem we will find that this is a typical BFS topic. The main study is for data storage skill, and can be part of the BFS after conversion into the same problem and the labyrinth board problem scenario, there is not much difficulty in algorithm design.

Problem-solving ideas

Overview ideas

About input and output section just some of the special sentence, plus here is not explained.
About drawing a line operation, since certain provisions draw a straight line, just with different situations and to store the line, there is no difficulty in algorithm design. Line drawing function operation is as follows:

void make_line(int x_1,int y_1,int x_2,int y_2)
{
    if(x_1==x_2)//纵向划线
    {
        if(y_1==y_2)//不划线
        return;
        else if(y_1>y_2)//向下划线
        {
            for(int i=y_2;i<=y_1;i++)
            {
                a[x_1][i].long_direct=1;
            }
        }
        else
        {
            for(int i=y_1;i<=y_2;i++)//向上划线
            {
                a[x_1][i].long_direct=1;
            }
        }
    }
    else if(y_1==y_2)//横向划线
    {
        if(x_1>x_2)//向左划线
        {
            for(int i=x_2;i<=x_1;i++)
            {
                a[i][y_1].wide_direct=1;
            }
        }
        else
        {
            for(int i=x_1;i<=x_2;i++)
            {
                a[i][y_1].wide_direct=1;
            }
        }   
    }
}

In filling part, the core algorithm is the maze of the board. It is noted that, following a few more boundary conditions:
. 1, only the positive axis xy BFS performed, i.e. to meet the BFS to meet the horizontal and vertical coordinates of the point> 0 =
2, the point can not exceed the BFS canvas range, horizontal and vertical coordinates satisfy <width, length
. 3, only the BFS encountered during horizontal and vertical scribe scribing satisfy flag == 0, only push the point into the queue, and to pay attention to the back cover filling in front of the filling.
Filling part of the code as follows:

void BFS_fill(int start_x,int start_y,char filling)
{
    if(a[start_x][start_y].wide_direct==1 || a[start_x][start_y].long_direct==1)
    {
        return;
    }
    a[start_x][start_y].filling=filling;

    pos start;
    start.x=start_x;
    start.y=start_y;
    q.push(start);
    while(!q.empty())
    {
        pos node=q.front();
        q.pop();

        for(int i=0;i<4;i++)
        {
            int new_node_x=node.x+dx[i];
            int new_node_y=node.y+dy[i];
            if(a[new_node_x][new_node_y].wide_direct==0 && a[new_node_x][new_node_y].long_direct==0 && a[new_node_x][new_node_y].filling!=filling && new_node_x<wideth && new_node_y<length && new_node_x>=0 && new_node_y>=0)
            {
                a[new_node_x][new_node_y].filling=filling;
                pos new_node;
                new_node.x=new_node_x;
                new_node.y=new_node_y;
                q.push(new_node);
            }
        }
    }
}

to sum up

This question quite water, as long as attention to good various boundary conditions, roll-over is not likely.

Topic Source (c ++)

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;

struct point
{
    int wide_direct;//横向线
    int long_direct;//纵向线
    char filling;
};

struct pos
{
    int x;
    int y;
};

int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};

point a[101][101];
int wideth,length=0;
queue<pos> q;

void BFS_fill(int start_x,int start_y,char filling)
{
    if(a[start_x][start_y].wide_direct==1 || a[start_x][start_y].long_direct==1)
    {
        return;
    }
    a[start_x][start_y].filling=filling;

    pos start;
    start.x=start_x;
    start.y=start_y;
    q.push(start);
    while(!q.empty())
    {
        pos node=q.front();
        q.pop();

        for(int i=0;i<4;i++)
        {
            int new_node_x=node.x+dx[i];
            int new_node_y=node.y+dy[i];
            if(a[new_node_x][new_node_y].wide_direct==0 && a[new_node_x][new_node_y].long_direct==0 && a[new_node_x][new_node_y].filling!=filling && new_node_x<wideth && new_node_y<length && new_node_x>=0 && new_node_y>=0)
            {
                a[new_node_x][new_node_y].filling=filling;
                pos new_node;
                new_node.x=new_node_x;
                new_node.y=new_node_y;
                q.push(new_node);
            }
        }
    }
}
void make_line(int x_1,int y_1,int x_2,int y_2)
{
    if(x_1==x_2)//纵向划线
    {
        if(y_1==y_2)//不划线
        return;
        else if(y_1>y_2)//向下划线
        {
            for(int i=y_2;i<=y_1;i++)
            {
                a[x_1][i].long_direct=1;
            }
        }
        else
        {
            for(int i=y_1;i<=y_2;i++)//向上划线
            {
                a[x_1][i].long_direct=1;
            }
        }
    }
    else if(y_1==y_2)//横向划线
    {
        if(x_1>x_2)//向左划线
        {
            for(int i=x_2;i<=x_1;i++)
            {
                a[i][y_1].wide_direct=1;
            }
        }
        else
        {
            for(int i=x_1;i<=x_2;i++)
            {
                a[i][y_1].wide_direct=1;
            }
        }   
    }
}
void output()
{
    for(int j=length-1;j>=0;j--)
    {
        for(int i=0;i<wideth;i++)
            {
                if(a[i][j].long_direct==1 && a[i][j].wide_direct==1)
                printf("+");
                else if(a[i][j].long_direct==1 && a[i][j].wide_direct==0)
                printf("|");
                else if(a[i][j].long_direct==0 && a[i][j].wide_direct==1)
                printf("-");
                else if(a[i][j].long_direct==0 && a[i][j].wide_direct==0)
                printf("%c",a[i][j].filling);
            }
            printf("\n");
    }
}
int main()
{
    int done_num=0;
    int control_num=0;
    int x_1,x_2,y_1,y_2=0;
    int x,y=0;
    char str;
    scanf("%d %d %d",&wideth,&length,&done_num);
    for(int i=0;i<wideth;i++)
        for(int j=0;j<length;j++)
            a[i][j].long_direct=0,a[i][j].wide_direct=0,a[i][j].filling='.';
    for(int i=0;i<done_num;i++)
    {
        scanf("%d",&control_num);
        if(control_num==0)
        {
            scanf("%d %d %d %d",&x_1,&y_1,&x_2,&y_2);
            make_line(x_1,y_1,x_2,y_2);
        }
        else
        {
            scanf("%d %d %c",&x,&y,&str);
            BFS_fill(x,y,str);
        }
    }
    output();
    return 0;
}
Published 17 original articles · won praise 2 · Views 1661

Guess you like

Origin blog.csdn.net/qq_43942251/article/details/104937139