csp 201512-3 画图

题目:
 用 ASCII 字符来画图是一件有趣的事情,并形成了一门被称为 ASCII Art 的艺术。
  编程实现一个用 ASCII 字符来画图的程序,支持以下两种操作:
  Ÿ 画线:给出两个端点的坐标,画一条连接这两个端点的线段。简便起见题目保证要画的每条线段都是水平或者竖直的。水平线段用字符 - 来画,竖直线段用字符 | 来画。如果一条水平线段和一条竖直线段在某个位置相交,则相交位置用字符 + 代替。
  Ÿ 填充:给出填充的起始位置坐标和需要填充的字符,从起始位置开始,用该字符填充相邻位置,直到遇到画布边缘或已经画好的线段。注意这里的相邻位置只需要考虑上下左右 4 个方向。
输入:
 第1行有三个整数m, n和q。m和n分别表示画布的宽度和高度,以字符为单位。q表示画图操作的个数。
  第2行至第q + 1行,每行是以下两种形式之一:
  Ÿ 0 x1 y1 x2 y2:表示画线段的操作,(x1, y1)和(x2, y2)分别是线段的两端,满足要么x1 = x2 且y1 ≠ y2,要么 y1 = y2 且 x1 ≠ x2。
  Ÿ 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
样例输出:
在这里插入图片描述
评测用例规模与约定  
所有的评测用例满足:2 ≤ m, n ≤ 100,0 ≤ q ≤ 100,0 ≤ x < m(x表示输入数据中所有位置的x坐标),0 ≤ y < n(y表示输入数据中所有位置的y坐标)。
这题其实不难,关键是数据范围小,我们可以很方便地用100×100的二维数组表示这个画布。对于’-‘和’|'只需要按照循环输入就行了;而对于填充的字符,则用bfs可以解决。
结构体node:

struct node{
 int first,second;
};

划线函数operation1:

void operation1(int x1,int y1,int x2,int y2)
{
 if(x1==x2)//画竖线 
 {
  int a=max(y1,y2);
  int b=min(y1,y2);
  
  for(int i=b;i<=a;i++)
  {
   if(map[x1][i]=='-')//如果已经有横线,则变成‘+’ 
   {
    map[x1][i]='+';
   }
   else if(map[x1][i]=='+')//+不能覆盖 
   {
    continue;
   }
   else
   {
    map[x1][i]='|';
   }
  }
 }
 else//画横线 
 {
  int a=max(x1,x2);
  int b=min(x1,x2);
  
  for(int i=b;i<=a;i++)
  {
   if(map[i][y1]=='|')//如果已经有竖线,则变成‘+’ 
   {
    map[i][y1]='+';
   }
   else if(map[i][y1]=='+')//+不能覆盖 
   {
    continue;
   }
   else
   {
    map[i][y1]='-';
   }
  }
 }
} 

填充函数operation2:

void operation2(int x,int y,char c)//bfs填充 
{
 //偏移量 
 node offset[4]; 
 offset[0].first=0;offset[0].second=1;//上 
 offset[1].first=1;offset[1].second=0;//右 
 offset[2].first=0;offset[2].second=-1;//下 
 offset[3].first=-1;offset[3].second=0;//左 
 
 queue<node> q;
 node here;
 here.first=x;
 here.second=y;
 
 q.push(here);
 map[here.first][here.second]=c;//先将起始点填充 
 node current;
 
 while(!q.empty())//bfs 
 {
  current=q.front();
  q.pop();
  
  for(int i=0;i<4;i++)//遍历当前点的上、右、下、左 
  {
   node temp;
   temp.first=current.first+offset[i].first;
   temp.second=current.second+offset[i].second;
   
   if(temp.first<0||temp.first>=m||temp.second<0||temp.second>=n)//不能越过边界 
   {
    continue;
   }
   else if(map[temp.first][temp.second]=='|'||map[temp.first][temp.second]=='-'||map[temp.first][temp.second]=='+')//遇到线都不能覆盖 
   {
    continue;
   }
   else
   {
    if(map[temp.first][temp.second]!=c)//如果这个点已经被当前样式 c 覆盖了,就不再重复覆盖,如果是不同样式,则可以覆盖 
    {
     map[temp.first][temp.second]=c;
     q.push(temp); 
    }
   } 
  }
 }
}

以下是完整代码:

#include<iostream>
#include<queue>
using namespace std;
struct node{
 int first,second;
};
char map[120][120];
int m,n,q;
int max(int x,int y)
{
 return x<y?y:x;
}
int min(int x,int y)
{
 return x<y?x:y;
}
void operation1(int x1,int y1,int x2,int y2)
{
 if(x1==x2)//画竖线 
 {
  int a=max(y1,y2);
  int b=min(y1,y2);
  
  for(int i=b;i<=a;i++)
  {
   if(map[x1][i]=='-')//如果已经有横线,则变成‘+’ 
   {
    map[x1][i]='+';
   }
   else if(map[x1][i]=='+')//+不能覆盖 
   {
    continue;
   }
   else
   {
    map[x1][i]='|';
   }
  }
 }
 else//画横线 
 {
  int a=max(x1,x2);
  int b=min(x1,x2);
  
  for(int i=b;i<=a;i++)
  {
   if(map[i][y1]=='|')//如果已经有竖线,则变成‘+’ 
   {
    map[i][y1]='+';
   }
   else if(map[i][y1]=='+')//+不能覆盖 
   {
    continue;
   }
   else
   {
    map[i][y1]='-';
   }
  }
 }
} 

void operation2(int x,int y,char c)//bfs填充 
{
 //偏移量 
 node offset[4]; 
 offset[0].first=0;offset[0].second=1;//上 
 offset[1].first=1;offset[1].second=0;//右 
 offset[2].first=0;offset[2].second=-1;//下 
 offset[3].first=-1;offset[3].second=0;//左 
 
 queue<node> q;
 node here;
 here.first=x;
 here.second=y;
 
 q.push(here);
 map[here.first][here.second]=c;//先将起始点填充 
 node current;
 
 while(!q.empty())//bfs 
 {
  current=q.front();
  q.pop();
  
  for(int i=0;i<4;i++)//遍历当前点的上、右、下、左 
  {
   node temp;
   temp.first=current.first+offset[i].first;
   temp.second=current.second+offset[i].second;
   
   if(temp.first<0||temp.first>=m||temp.second<0||temp.second>=n)//不能越过边界 
   {
    continue;
   }
   else if(map[temp.first][temp.second]=='|'||map[temp.first][temp.second]=='-'||map[temp.first][temp.second]=='+')//遇到线都不能覆盖 
   {
    continue;
   }
   else
   {
    if(map[temp.first][temp.second]!=c)//如果这个点已经被当前样式 c 覆盖了,就不再重复覆盖,如果是不同样式,则可以覆盖 
    {
     map[temp.first][temp.second]=c;
     q.push(temp); 
    }
   } 
  }
 }
}

int main()
{
 cin>>m>>n>>q;
 
 for(int i=0;i<m;i++)//初始化 
 {
  for(int j=0;j<n;j++)
  {
   map[i][j]='.';
  }
 }
 
 int op;
 int x1,y1,x2,y2;
 int x,y;
 char c;
 for(int i=0;i<q;i++)
 {
  scanf("%d",&op);
  if(op==0)
  {
   scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
   operation1(x1,y1,x2,y2);
  }
  else
  {
   cin>>x>>y>>c;
   operation2(x,y,c);
  }
 }
 for(int i=n-1;i>=0;i--)//输出 
 {
  for(int j=0;j<m;j++)
  {
   printf("%c",map[j][i]);
  }
  printf("\n");
 }
} 
发布了24 篇原创文章 · 获赞 5 · 访问量 275

猜你喜欢

转载自blog.csdn.net/qq_45639151/article/details/105004952