注意填充颜色判断,避免无限递归。
#include <cstdio>
#include <iostream>
#include <vector>
#include <sstream>
#include <string>
using namespace std;
char bmp[252][252];
int M,N;//col,row
void ProcessC(string str)
{
for(int i=1;i<=M;i++)
{
for(int j=1;j<=N;j++)
{
bmp[j][i]='O';
}
}
}
void ProcessI(string str)
{
string c;
stringstream scin(str);
scin>>c>>M>>N;
ProcessC("C");
}
void ProcessL(string str)
{
char ch,c;
int x,y;
stringstream scin(str);
scin>>ch>>x>>y>>c;
bmp[y][x]=c;
}
void ProcessV(string str)
{
char ch,c;
int x,y1,y2,temp;
stringstream scin(str);
scin>>ch>>x>>y1>>y2>>c;
if(y2<y1)
{
temp=y1;
y1=y2;
y2=temp;
}
for(int i=y1;i<=y2;i++)
bmp[i][x]=c;
}
void ProcessH(string str)
{
char ch,c;
int x1,x2,y,temp;
stringstream scin(str);
scin>>ch>>x1>>x2>>y>>c;
if(x2<x1)
{
temp=x1;
x1=x2;
x2=temp;
}
for(int i=x1;i<=x2;i++)
bmp[y][i]=c;
}
void ProcessK(string str)
{
char ch,c;
int x1,x2,y1,y2,temp;
stringstream scin(str);
scin>>ch>>x1>>y1>>x2>>y2>>c;
for(int i=x1;i<=x2;i++)
for(int j=y1;j<=y2;j++)
bmp[j][i]=c;
}
void FillXYC(int x,int y,char oc,char c)
{
if(bmp[y][x]!=oc)
return;
bmp[y][x]=c;
if(x-1>0) FillXYC(x-1,y,oc,c);
if(x+1<=M) FillXYC(x+1,y,oc,c);
if(0<y-1) FillXYC(x,y-1,oc,c);
if(N>=y+1) FillXYC(x,y+1,oc,c);
}
void ProcessF(string str)
{
char ch,c,oc;
int x,y;
stringstream scin(str);
scin>>ch>>x>>y>>c;
oc=bmp[y][x];
if(oc!=c)FillXYC(x,y,oc,c);
}
void ProcessS(string str)
{
char ch;
string name;
stringstream scin(str);
scin>>ch>>name;
cout<<name<<endl;
for(int i=1;i<=N;i++)
{
for(int j=1;j<=M;j++)
{
cout<<bmp[i][j];
}
cout<<endl;
}
}
int main()
{
string cmd;
char ch;
while(1)
{
getline(cin,cmd);
if(cmd.length()==0)
continue;
ch=cmd[0];
if(ch=='X')
break;
switch(ch)
{
case 'I'://C, L, V, H, K, F, S
ProcessI(cmd);
break;
case 'C':
ProcessC(cmd);
break;
case 'L':
ProcessL(cmd);
break;
case 'V':
ProcessV(cmd);
break;
case 'H':
ProcessH(cmd);
break;
case 'K':
ProcessK(cmd);
break;
case 'F':
ProcessF(cmd);
break;
case 'S':
ProcessS(cmd);
break;
default:
break;
}
}
return 0;
}