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;
}