CCF CSP 201512-3 画图。c++

在这里插入图片描述
sample input:
4 2 3
1 0 0 B
0 1 0 2 0
1 0 0 A

sample output:
在这里插入图片描述
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:
在这里插入图片描述
测试用例满足:2 ≤ m, n ≤ 100,0 ≤ q ≤ 100,0 ≤ x < m(x表示输入数据中所有位置的x坐标),0 ≤ y < n(y表示输入数据中所有位置的y坐标)

思路:

  • 由于初始时全部的位置都是 · ,所以在完成行列的输入后要先进行数组的初始化,可以直接循环,也可以用memset函数
  • 在接下来的q行中,首先判断首数字,进行画线还是填充操作
  • 画线:首先通过输入的x1,x2,y1,y2来判断是画一条竖线还是一条横线。如果在画竖线,判断当前画到的位置是不是交叉点,是交叉点就画 + ,不是交叉点就画 | ;如果在画横线,判断当前是不是交叉点,是交叉嗲就画 + ,不是就画 -
  • 填充:由于给定的填充起始点一定在图形内并且不在线上,所以对于这个点来说,就有可能向四个方向进行延伸。所以定义一个dir数组,注意里面 i,i+1的定义值,需要和四方向的x,y变化值挂钩;写一个向四个方向的循环,结束填充条件为遇到边线或者遇到的点已经完成了填充
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
int row;
int col;
char mem[111][111];
int dir[8]={0,1,-1,0,0,-1,1,0};
void draw(int x1,int x2,int y1,int y2)
{
 if(x1==x2)//画竖线
 {
  int tempy1=row-y1-1;
  int tempy2=row-y2-1;
  int tempx=x1;
  if(tempy1>tempy2)
   swap(tempy1,tempy2);
  for(int i=tempy1;i<=tempy2;i++)
  {
   if(mem[i][tempx]!='-'&&mem[i][tempx]!='+')
    mem[i][tempx]='|';
   else mem[i][tempx]='+';
  }
  } 
 else if(y1==y2)
 {
  int tempy=row-y1-1;
  int tempx1=x1;
  int tempx2=x2;
  if(tempx2<tempx1)
   swap(tempx1,tempx2);
  for(int i=tempx1;i<=tempx2;i++)
  {
   if(mem[tempy][i]=='|'||mem[tempy][i]=='+')
    mem[tempy][i]='+';
   else mem[tempy][i]='-';
  }   
 }
}
void fill(int x,int y,char c)//填充 
{
 if(x<0||x>=row||y<0||y>=col) return;
 if(mem[x][y]==c) return ;
 if(mem[x][y]!='-'&&mem[x][y]!='|'&&mem[x][y]!='+')
 {
  mem[x][y]=c;
  for(int i=0;i<8;i+=2)
  {
   int tx=x+dir[i];
   int ty=y+dir[i+1];
   fill(tx,ty,c);
  }
 }
 else return;
}
int main()
{
 int q=0;
 scanf("%d %d %d",&col,&row,&q);
 for(int i=0;i<row;i++)
  for(int j=0;j<col;j++)
   mem[i][j]='.';
 for(int i=0;i<q;i++)
 {
  int option;
  scanf("%d",&option);
  if(option==0)
  {
   int x1,x2,y1,y2;
   scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
   draw(x1,x2,y1,y2);
  }
  else if(option==1)
  {
   int x,y;
   char c;
   scanf("%d %d %c",&x,&y,&c);
   int tempx=x;
   int tempy=row-y-1;
   fill(tempy,tempx,c);
  }
 }
 for(int i=0;i<row;i++)
 {
  for(int j=0;j<col;j++)
   cout<<mem[i][j];
  cout<<endl;
 }
 return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值