Never been to DZone Snippets before?

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

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

A solution for the "Graphical Editor" problem

A solution for the "Graphical Editor" problem.

Problem description:
http://icpcres.ecs.baylor.edu/onlinejudge/external/102/10267.html

Author: Joana Matos Fonseca da Trindade
Date: 2008.03.12

   1  
   2  /* 
   3   * Solution for the "Graphical Editor" problem.
   4   * UVa ID: 10267
   5   */
   6  #include <stdio.h>
   7  
   8  #define MAX 250
   9  #define OFFSET 1
  10  #define DOS_NAME 12
  11  
  12  /* global image bounds */
  13  int n, m;
  14  
  15  /* fills a rectangle with the specified color */
  16  int fillRectangle(int m_ini, int n_ini, int m_end, int n_end, char color, char pTable[][MAX+OFFSET]) {
  17  	int i, j;
  18  	for (i = n_ini; i <= n_end; i++) {
  19  		for (j = m_ini; j <= m_end; j++) {
  20  			pTable[i][j] = color;
  21  		}
  22  	}
  23  	return 0;
  24  }
  25  
  26  /* fills a region R with the specified color */
  27  int fillRegion(int x, int y, char oldColor, char newColor, char pTable[][MAX+OFFSET]) {	
  28  	/* (x,y) is in region R */
  29  	pTable[y][x] = newColor;
  30  	
  31  	/* recursively check all 4 directions for neighbours of (x,y) with same color */
  32  	if ((pTable[y][x-1] == oldColor) && (x > OFFSET)) {         
  33  		fillRegion(x-1, y, oldColor, newColor, pTable);
  34  	}
  35  	if ((pTable[y][x+1] == oldColor) && (x < m)) {       
  36  		fillRegion(x+1, y, oldColor, newColor, pTable);
  37  	}
  38  	if ((pTable[y-1][x] == oldColor) && (y > OFFSET)) {        
  39  		fillRegion(x, y-1, oldColor, newColor, pTable);
  40  	}
  41  	if ((pTable[y+1][x] == oldColor) && (y < n)) {        
  42  		fillRegion(x, y+1, oldColor, newColor, pTable);
  43  	}
  44  	return 0;
  45  }
  46  
  47  /* outputs the image */
  48  int printImage(int m, int n, char pTable[][MAX+OFFSET]) {
  49  	int i, j;	
  50  	for (i = OFFSET; i < n+OFFSET; i++) {
  51  		for (j = OFFSET; j < m+OFFSET; j++ ) {
  52  			printf("%c", pTable[i][j]);
  53  		}
  54  		printf("\n");
  55  	}
  56  	return 0;
  57  }
  58  
  59  /* main */
  60  int main (int argc, const char * argv[]) {
  61  	/* the image */
  62  	char image[MAX+OFFSET][MAX+OFFSET];
  63  
  64  	/* editor command */
  65  	char command;
  66  	
  67  	/* coords */
  68  	int x1, x2, y1, y2, tmp;
  69  	
  70  	/* colors */
  71  	char color, oldColor;
  72  	
  73  	/* filename */
  74  	char filename[DOS_NAME+1];
  75  			
  76  	while(scanf("%c", &command) != EOF) {		
  77  		/* X, terminates the session */
  78  		if (command == 'X') {
  79  			return 0;
  80  		}		
  81  		switch (command) {
  82  			/* create image */
  83  			case 'I' :
  84  				scanf("%d %d", &m, &n);
  85  				fillRectangle(1, 1, m, n, 'O', image);
  86  				break;
  87  			
  88  			/* clear image */
  89  			case 'C' :
  90  				fillRectangle(1, 1, m, n, 'O', image);
  91  				break;
  92  			
  93  			/* colors a pixel */
  94  			case 'L' :
  95  				scanf("%d %d %c", &x1, &y1, &color);
  96  				image[y1][x1] = color;
  97  				break;
  98  			
  99  			/* draw vertical segment */
 100  			case 'V' :
 101  				scanf("%d %d %d %c", &x1, &y1, &y2, &color);
 102  				if (y2 >= y1)
 103  					fillRectangle(x1, y1, x1, y2, color, image);
 104  				else
 105  					fillRectangle(x1, y2, x1, y1, color, image);
 106  				break;
 107  			
 108  			/* draw horizontal segment */
 109  			case 'H' : 
 110  				scanf("%d %d %d %c", &x1, &x2, &y1, &color);
 111  				if (x2 >= x1)
 112  					fillRectangle(x1, y1, x2, y1, color, image);
 113  				else
 114  					fillRectangle(x2, y1, x1, y1, color, image);
 115  				break;
 116  			
 117  			/* draw rectangle */
 118  			case 'K' : 
 119  				scanf("%d %d %d %d %c", &x1, &y1, &x2, &y2, &color);
 120  				if (x1 >= x2) {
 121  					tmp = x1;
 122  					x1 = x2;
 123  					x2 = tmp;
 124  				}
 125  				if (y1 >= y2) {
 126  					tmp = y1;
 127  					y1 = y2;
 128  					y2 = tmp;
 129  				}
 130  				fillRectangle(x1, y1, x2, y2, color, image);
 131  				break;
 132  			
 133  			/* fill */
 134  			case 'F' :
 135  				scanf("%d %d %c", &x1, &y1, &color);
 136  				oldColor = image[y1][x1];
 137  				if (oldColor != color) {
 138  					fillRegion(x1, y1, oldColor, color, image);
 139  				}
 140  				break;
 141  
 142  			/* fill */
 143  			case 'S' :
 144  				scanf("%s", &filename);
 145  				printf("%s\n", filename);
 146  				printImage(m, n, image);
 147  				break;			
 148  		
 149  			default: 
 150  				break;
 151  		}		
 152  	}
 153  	
 154  	return 0;
 155  }
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS