UVa——110105 Graphical Editor(模拟)
解题思路:这是一个模拟题,其中命令F刚开始理解错了,这点是本题得难点所在。F命令为:把区域R填充为颜色(C)。R定义如下:若像素(X,Y)属于R,则所有与(X,Y)有公共边且颜色相同的像素也属于R。其实这跟我们以前做的搜索题能联系在一块儿,可以用递归实现。注意:图像的边界问题也要考虑。
View Code
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream>
2 using namespace std;
3 char map[255][255];
4 int n,m;
5 void fill(char map[255][255],int x,int y,int cold,int cnew) //命令F(搜索)
6 {
7 if(cold==cnew) return ;
8 map[y][x]=cnew;
9 if(x>1)
10 if(map[y][x-1]==cold)
11 fill(map,x-1,y,cold,cnew);
12 if(x<m)
13 if(map[y][x+1]==cold)
14 fill(map,x+1,y,cold,cnew);
15 if(y>1)
16 if(map[y-1][x]==cold)
17 fill(map,x,y-1,cold,cnew);
18 if(y<n)
19 if(map[y+1][x]==cold)
20 fill(map,x,y+1,cold,cnew);
21 }
22 int main()
23 {
24 int i,j,x1,y1,x2,y2,t;
25 char name[10],ch,cor;
26 while(cin>>ch)
27 {
28 if(ch=='X') break;
29 else if(ch=='I')
30 {
31 cin>>m>>n;
32 for(i=1;i<=n;i++)
33 for(j=1;j<=m;j++)
34 map[i][j]='O';
35 }
36 else if(ch=='C')
37 {
38 for(i=1;i<=n;i++)
39 for(j=1;j<=m;j++)
40 map[i][j]='O';
41 }
42 else if(ch=='L')
43 {
44 cin>>x1>>y1>>cor;
45 if(y1<=n&&x1<=m)
46 map[y1][x1]=cor;
47 }
48 else if(ch=='V')
49 {
50 cin>>x1>>y1>>y2>>cor;
51 if(y1>y2) { t=y1; y1=y2; y2=t; }
52 for(i=y1;i<=y2;i++)
53 map[i][x1]=cor;
54 }
55 else if(ch=='H')
56 {
57 cin>>x1>>x2>>y1>>cor;
58 if(x1>x2) { t=x1; x1=x2; x2=t; }
59 for(i=x1;i<=x2;i++)
60 map[y1][i]=cor;
61 }
62 else if(ch=='K')
63 {
64 cin>>x1>>y1>>x2>>y2>>cor;
65 if(x1>x2) { t=x1; x1=x2; x2=t;}
66 if(y1>y2) { t=y1; y1=y2; y2=t;}
67 for(i=y1;i<=y2;i++)
68 for(j=x1;j<=x2;j++)
69 map[i][j]=cor;
70 }
71 else if(ch=='F')
72 {
73 cin>>x1>>y1>>cor;
74 fill(map,x1,y1,map[y1][x1],cor);
75 }
76 else if(ch=='S')
77 {
78 cin>>name;
79 cout<<name<<endl;
80 for(i=1;i<=n;i++)
81 {
82 for(j=1;j<=m;j++)
83 cout<<map[i][j];
84 cout<<endl;
85 }
86 }
87 }
88 return 0;
89 }
2 using namespace std;
3 char map[255][255];
4 int n,m;
5 void fill(char map[255][255],int x,int y,int cold,int cnew) //命令F(搜索)
6 {
7 if(cold==cnew) return ;
8 map[y][x]=cnew;
9 if(x>1)
10 if(map[y][x-1]==cold)
11 fill(map,x-1,y,cold,cnew);
12 if(x<m)
13 if(map[y][x+1]==cold)
14 fill(map,x+1,y,cold,cnew);
15 if(y>1)
16 if(map[y-1][x]==cold)
17 fill(map,x,y-1,cold,cnew);
18 if(y<n)
19 if(map[y+1][x]==cold)
20 fill(map,x,y+1,cold,cnew);
21 }
22 int main()
23 {
24 int i,j,x1,y1,x2,y2,t;
25 char name[10],ch,cor;
26 while(cin>>ch)
27 {
28 if(ch=='X') break;
29 else if(ch=='I')
30 {
31 cin>>m>>n;
32 for(i=1;i<=n;i++)
33 for(j=1;j<=m;j++)
34 map[i][j]='O';
35 }
36 else if(ch=='C')
37 {
38 for(i=1;i<=n;i++)
39 for(j=1;j<=m;j++)
40 map[i][j]='O';
41 }
42 else if(ch=='L')
43 {
44 cin>>x1>>y1>>cor;
45 if(y1<=n&&x1<=m)
46 map[y1][x1]=cor;
47 }
48 else if(ch=='V')
49 {
50 cin>>x1>>y1>>y2>>cor;
51 if(y1>y2) { t=y1; y1=y2; y2=t; }
52 for(i=y1;i<=y2;i++)
53 map[i][x1]=cor;
54 }
55 else if(ch=='H')
56 {
57 cin>>x1>>x2>>y1>>cor;
58 if(x1>x2) { t=x1; x1=x2; x2=t; }
59 for(i=x1;i<=x2;i++)
60 map[y1][i]=cor;
61 }
62 else if(ch=='K')
63 {
64 cin>>x1>>y1>>x2>>y2>>cor;
65 if(x1>x2) { t=x1; x1=x2; x2=t;}
66 if(y1>y2) { t=y1; y1=y2; y2=t;}
67 for(i=y1;i<=y2;i++)
68 for(j=x1;j<=x2;j++)
69 map[i][j]=cor;
70 }
71 else if(ch=='F')
72 {
73 cin>>x1>>y1>>cor;
74 fill(map,x1,y1,map[y1][x1],cor);
75 }
76 else if(ch=='S')
77 {
78 cin>>name;
79 cout<<name<<endl;
80 for(i=1;i<=n;i++)
81 {
82 for(j=1;j<=m;j++)
83 cout<<map[i][j];
84 cout<<endl;
85 }
86 }
87 }
88 return 0;
89 }