UVa 10267 Graphical Editor

【题号】UVa 10267 - Graphical Editor

【题目描述】

Problem

The simple graphical editor deals with a rectangular table M×N (1<=M,N<=250). Each pixel of the table has its colour. The picture is formed from this square pixels.

The problem is to write a program, which simulates an interactive work of the graphical editor.

Input

Input consists of the editor commands, one per line. Each command is represented by one Latin capital placed in the very beginning of the line. If the command supposes parameters, all the parameters will be given in the same line separated by space. As the parameters there may be: the coordinates of the pixel - two integers, the first one is the column number and belongs to 1..M, the second one is the row number and belongs to 1..N, the origin is in the upper left corner of the table; the colour - the Latin capital; file name - in MSDOS 8.3 format.

The editor deals with the following commands:

I M NCreates a new table M×N. All the pixels are colored in white (O).
CClears the table. The size remains the same. All the pixels became white (O).
L X Y CColors the pixel with coordinates (X,Y) in colour C.
V X Y1 Y2 CDraws the vertical segment in the column X between the rows Y1 and Y2 inclusive in colour C.
H X1 X2 Y CDraws the horizontal segment in the row between the columns X1 and X2 inclusive in colour C.
K X1 Y1 X2 Y2 CDraws the filled rectangle in colour C(X1,Y1) is the upper left corner, (X2,Y2) is the lower right corner of the rectangle.
F X Y CFills the region with the colour C. The region R to be filled is defined as follows. The pixel (X,Y) belongs to this region. The other pixel belongs to the region R if and only if it has the same colour as pixel (X,Y) and a common side with any pixel which belongs to this region.
S NameWrites the picture in the file Name.
XTerminates the session.

Output

Every time the command S NAME meets, you should output the file name NAME and the current table, row by row. Each row is represented by a pixels' colours series, see the output sample.

Errors

If as a command there will be a character different from I, C, L, V, H, K, F, S, X, the editor should ignore the whole line and pass to the next command.

In case of other errors the program behaviour is unpredictable.

Sample Input

I 5 6
L 2 3 A
S one.bmp
G 2 3 J
F 3 3 J
V 2 3 4 W
H 3 4 2 Z
S two.bmp
X

Sample Output

one.bmp
OOOOO
OOOOO
OAOOO
OOOOO
OOOOO
OOOOO
two.bmp
JJJJJ
JJZZJ
JWJJJ
JWJJJ
JJJJJ
JJJJJ


 
 
Alexander Denisjuk, 2002

【类型】

简单题,但略坑

【分析】

题目不难,可是数据确实有点坑。

第一个注意:表格第7行那个填充区域R的那个描述:需要注意,填充区域R,若像素(x,y)属于R,那么所有与(x,y)有公共边并且颜色相同的像素都属于R;这样需要深搜一下,从g[y-1][x-1]点DFS遍历一下,走过的路全部涂上颜色。

第二个注意:X1不一定比X2小,Y1也不一定比Y2小。

 

【源代码】

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
using namespace std;
const int Max = 250 + 5;
int n,m; //行号和列号 
char g[Max][Max];
int vis[Max][Max]; //标记 
char cmmd[20]; 
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};

void draw1(int M, int N) //创建像素矩阵 
{
	n = N;
	m = M;
	memset(g, 'O', sizeof(g));
}

void draw2() //清除图像,像素涂成白色 
{
	memset(g, 'O', sizeof(g)); //注意白色是'O'不是'0' 
}

void draw3(int x, int y, char c) //把g[y-1][x-1]涂成白色
{
	g[y-1][x-1] = c;
}

void draw4(int x, int y1, int y2, char c){ //将g[(y1-1)~(y2-1) or (y2-1)~(y1-1)][x-1] 
	if(y1 > y2)
		swap(y1, y2);
	for(int i = y1-1; i <= y2-1; i++)
	{
		g[i][x-1] = c;
	}
}

void draw5(int x1, int x2, int y, char c)//将g[y-1][(x1-1)~(x2-1) or (x2-1)~(x1-1)] 
{
	if(x1 > x2)
		swap(x1,x2);
	for(int i = x1-1; i <= x2-1; i++)
	{
		g[y-1][i] = c;
	}
}

//左上角为(y1-1,x1-1),右下角为(y2-1,x2-1)的矩形涂满颜色c 
void draw6(int x1, int y1, int x2, int y2, char c)  
{
	for(int i=y1-1; i<=y2-1; i++)
	{
		for(int j=x1-1; j<=x2-1; j++)
		{
			g[i][j] = c;
		} 
	}
}

char k;//中心点g[y-1][x-1]的颜色 
void draw7(int x, int y, char c) //考虑和g[y-1][x-1]一同属于区域R的像素点 
{
	int tx,ty;
	for(int i=0; i<4; i++)
	{
		tx = x + dir[i][0];
		ty = y + dir[i][1];
		if(tx >= 0 && tx < n && ty >= 0 && ty < m && !vis[tx][ty] && g[tx][ty] == k)
		{
			g[tx][ty] = c;
			vis[tx][ty] = 1;
			draw7(tx, ty, c);
		}
	}
}

void draw8(char str[]) //输出文件名及像素矩阵 
{
	printf("%s\n",str);
	for(int i=0; i<n; i++)
	{
		for(int j=0; j<m; j++)
		{
			printf("%c",g[i][j]);
		}
		printf("\n");
	}
}
int main()
{
	int x,y,x1,y1,x2,y2,M,N;
	char c;
	while(scanf("%s",cmmd) && cmmd[0] != 'X')
	{
		if(cmmd[0] == 'I') 
		{
			scanf("%d %d",&M,&N);
			draw1(M, N);
		}
		else if(cmmd[0] == 'C')
		{
			draw2();
		}
		else if(cmmd[0] == 'L')
		{
			scanf("%d %d %c",&x,&y,&c);
			draw3(x, y, c);
		}
		else if(cmmd[0] == 'V')
		{
			scanf("%d %d %d %c",&x,&y1,&y2,&c);
			draw4(x, y1, y2, c);
		}
		else if(cmmd[0] == 'H')
		{
			scanf("%d %d %d %c",&x1,&x2,&y,&c);
			draw5(x1, x2, y, c);
		}
		else if(cmmd[0] == 'K')
		{
			scanf("%d %d %d %d %c",&x1,&y1,&x2,&y2,&c);
			draw6(x1, y1, x2, y2, c); 
		}
		else if(cmmd[0] == 'F')
		{
			scanf("%d %d %c",&x,&y,&c);
			memset(vis, 0 ,sizeof(vis));
			k = g[y-1][x-1];
			g[y-1][x-1] = c;
			vis[y-1][x-1] = 1;
			draw7(y-1, x-1, c);
		} 
		else if(cmmd[0] == 'S')
		{
			scanf("%s",cmmd);
			draw8(cmmd);
		}
		else //不合法 
		{
			gets(cmmd); //将后面的输入完 
		}
	}
	return 0; 
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值