DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

A Solution For The "Graphical Editor" Problem

03.18.2008
| 5706 views |
  • submit to reddit
        A solution for the "Graphical Editor" problem.
 
Problem description:
<a href="http://icpcres.ecs.baylor.edu/onlinejudge/external/102/10267.html">http://icpcres.ecs.baylor.edu/onlinejudge/external/102/10267.html</a>

Author: <a href="http://joanatrindade.wikidot.com">Joana Matos Fonseca da Trindade</a>
Date: 2008.03.12

/* 
 * Solution for the "Graphical Editor" problem.
 * UVa ID: 10267
 */
#include <stdio.h>

#define MAX 250
#define OFFSET 1
#define DOS_NAME 12

/* global image bounds */
int n, m;

/* fills a rectangle with the specified color */
int fillRectangle(int m_ini, int n_ini, int m_end, int n_end, char color, char pTable[][MAX+OFFSET]) {
	int i, j;
	for (i = n_ini; i <= n_end; i++) {
		for (j = m_ini; j <= m_end; j++) {
			pTable[i][j] = color;
		}
	}
	return 0;
}

/* fills a region R with the specified color */
int fillRegion(int x, int y, char oldColor, char newColor, char pTable[][MAX+OFFSET]) {	
	/* (x,y) is in region R */
	pTable[y][x] = newColor;
	
	/* recursively check all 4 directions for neighbours of (x,y) with same color */
	if ((pTable[y][x-1] == oldColor) && (x > OFFSET)) {         
		fillRegion(x-1, y, oldColor, newColor, pTable);
	}
	if ((pTable[y][x+1] == oldColor) && (x < m)) {       
		fillRegion(x+1, y, oldColor, newColor, pTable);
	}
	if ((pTable[y-1][x] == oldColor) && (y > OFFSET)) {        
		fillRegion(x, y-1, oldColor, newColor, pTable);
	}
	if ((pTable[y+1][x] == oldColor) && (y < n)) {        
		fillRegion(x, y+1, oldColor, newColor, pTable);
	}
	return 0;
}

/* outputs the image */
int printImage(int m, int n, char pTable[][MAX+OFFSET]) {
	int i, j;	
	for (i = OFFSET; i < n+OFFSET; i++) {
		for (j = OFFSET; j < m+OFFSET; j++ ) {
			printf("%c", pTable[i][j]);
		}
		printf("\n");
	}
	return 0;
}

/* main */
int main (int argc, const char * argv[]) {
	/* the image */
	char image[MAX+OFFSET][MAX+OFFSET];

	/* editor command */
	char command;
	
	/* coords */
	int x1, x2, y1, y2, tmp;
	
	/* colors */
	char color, oldColor;
	
	/* filename */
	char filename[DOS_NAME+1];
			
	while(scanf("%c", &command) != EOF) {		
		/* X, terminates the session */
		if (command == 'X') {
			return 0;
		}		
		switch (command) {
			/* create image */
			case 'I' :
				scanf("%d %d", &m, &n);
				fillRectangle(1, 1, m, n, 'O', image);
				break;
			
			/* clear image */
			case 'C' :
				fillRectangle(1, 1, m, n, 'O', image);
				break;
			
			/* colors a pixel */
			case 'L' :
				scanf("%d %d %c", &x1, &y1, &color);
				image[y1][x1] = color;
				break;
			
			/* draw vertical segment */
			case 'V' :
				scanf("%d %d %d %c", &x1, &y1, &y2, &color);
				if (y2 >= y1)
					fillRectangle(x1, y1, x1, y2, color, image);
				else
					fillRectangle(x1, y2, x1, y1, color, image);
				break;
			
			/* draw horizontal segment */
			case 'H' : 
				scanf("%d %d %d %c", &x1, &x2, &y1, &color);
				if (x2 >= x1)
					fillRectangle(x1, y1, x2, y1, color, image);
				else
					fillRectangle(x2, y1, x1, y1, color, image);
				break;
			
			/* draw rectangle */
			case 'K' : 
				scanf("%d %d %d %d %c", &x1, &y1, &x2, &y2, &color);
				if (x1 >= x2) {
					tmp = x1;
					x1 = x2;
					x2 = tmp;
				}
				if (y1 >= y2) {
					tmp = y1;
					y1 = y2;
					y2 = tmp;
				}
				fillRectangle(x1, y1, x2, y2, color, image);
				break;
			
			/* fill */
			case 'F' :
				scanf("%d %d %c", &x1, &y1, &color);
				oldColor = image[y1][x1];
				if (oldColor != color) {
					fillRegion(x1, y1, oldColor, color, image);
				}
				break;

			/* fill */
			case 'S' :
				scanf("%s", &filename);
				printf("%s\n", filename);
				printImage(m, n, image);
				break;			
		
			default: 
				break;
		}		
	}
	
	return 0;
}