UVa Problem 10267 Graphical Editor (图形化编辑器)

  1. // Graphical Editor (图形化编辑器)   
  2. // PC/UVa IDs: 110105/10267, Popularity: B, Success rate: low Level: 1   
  3. // Verdict: Accepted   
  4. // Submission Date: 2011-05-16   
  5. // UVa Run Time: 0.060s   
  6. //   
  7. // 版权所有(C)2011,邱秋。metaphysis # yeah dot net   
  8.   
  9. #include <iostream>   
  10. #include <string>   
  11. #include <sstream>   
  12. #include <vector>   
  13. #include <iterator>   
  14.       
  15. using namespace std;  
  16.       
  17. #define SWAP(x, y, type) \   
  18.     {\  
  19.         if (x > y)\  
  20.         {\  
  21.             type tmp = x;\  
  22.             x = y;\  
  23.             y = tmp;\  
  24.         }\  
  25.     }  
  26.       
  27. // 输出图形矩阵的内容。   
  28. ostream& operator<<(ostream &out, const vector < vector < char > > &matrix)  
  29. {  
  30.     for (vector < vector < char > >::const_iterator p = matrix.begin();   
  31.         p != matrix.end(); p++)  
  32.     {  
  33.         copy((*p).begin(), (*p).end(), ostream_iterator < char >(out, ""));  
  34.         out << endl;  
  35.     }  
  36.   
  37.     return out;  
  38. }  
  39.       
  40. // 重置字符矩阵。   
  41. void reset(vector < vector < char > > &matrix)  
  42. {  
  43.     // 注意是初始化为大写字母O而不是数字0.   
  44.     for (vector < vector < char > >::iterator p = matrix.begin();   
  45.         p != matrix.end(); p++)  
  46.         fill((*p).begin(), (*p).end(), 'O');  
  47. }  
  48.       
  49. // 设置点(x,y)的颜色为c。   
  50. void set_pixel(vector < vector < char > > &matrix, int x, int y, char c)  
  51. {  
  52.     matrix[y][x] = c;  
  53. }  
  54.       
  55. // 填充左上角为(x1,y1),右下角为(x2,y2)的矩形区域为颜色c。   
  56. void fill_rect(vector < vector < char > > &matrix, int x1, int y1,   
  57.     int x2, int y2, char c)  
  58. {  
  59.     for (int i = y1; i <= y2; i++)  
  60.         for (int j = x1; j <= x2; j++)  
  61.             matrix[i][j] = c;  
  62. }  
  63.       
  64. // 填充包含点(x,y)的区域为颜色new_color。   
  65. void fill_region(vector < vector < char > > &matrix, int x, int y,   
  66.     char old_color, char new_color)  
  67. {  
  68.     // 当新旧颜色相同时需要结束递归,否则会形成无限循环。   
  69.     if (old_color == new_color)  
  70.         return;  
  71.       
  72.     matrix[y][x] = new_color;  
  73.       
  74.     if (x > 0)  
  75.         if (matrix[y][x - 1] == old_color)  
  76.             fill_region(matrix, x - 1, y, old_color, new_color);  
  77.       
  78.     if (x < matrix[y].size() - 1)  
  79.         if (matrix[y][x + 1] == old_color)  
  80.             fill_region(matrix, x + 1, y, old_color, new_color);  
  81.       
  82.     if (y > 0)  
  83.         if (matrix[y - 1][x] == old_color)  
  84.             fill_region(matrix, x, y - 1, old_color, new_color);  
  85.       
  86.     if (y < matrix.size() - 1)  
  87.         if (matrix[y + 1][x] == old_color)  
  88.             fill_region(matrix, x, y + 1, old_color, new_color);  
  89. }  
  90.       
  91. int main(int argc, char *argv[])  
  92. {  
  93.     // 使用二维向量来保存字符矩阵。   
  94.     vector < vector < char > > matrix;  
  95.     string line;  
  96.     // 用于表示图像的一些变量。   
  97.     int width, height, x, y, x1, x2, y1, y2;  
  98.     char command, color;  
  99.       
  100.     while (getline(cin, line), line[0] != 'X')  
  101.     {  
  102.         istringstream iss(line);  
  103.       
  104.         // 读入命令。   
  105.         iss >> command;  
  106.         switch (command)  
  107.         {  
  108.             // 初始化图像为宽为width,高为height。   
  109.             case 'I':  
  110.                 iss >> width >> height;  
  111.       
  112.                 matrix.clear();  
  113.                 matrix.resize(height);  
  114.                 for (vector < vector < char > >::iterator   
  115.                     p = matrix.begin(); p != matrix.end(); p++)  
  116.                     (*p).resize(width);  
  117.   
  118.                 reset(matrix);  
  119.                 break;  
  120.             // 重置矩阵。       
  121.             case 'C':  
  122.                 reset(matrix);  
  123.                 break;  
  124.             // 设置某点颜色。     
  125.             case 'L':  
  126.                 iss >> x >> y >> color;  
  127.                 x--;  
  128.                 y--;  
  129.   
  130.                 set_pixel(matrix, x, y, color);  
  131.                 break;  
  132.             // 绘制竖线。   
  133.             case 'V':  
  134.                 iss >> x >> y1 >> y2 >> color;  
  135.                 x--;  
  136.                 y1--;  
  137.                 y2--;  
  138.   
  139.                 SWAP(y1, y2, int);  
  140.   
  141.                 fill_rect(matrix, x, y1, x, y2, color);  
  142.                 break;  
  143.             // 绘制水平线。      
  144.             case 'H':  
  145.                 iss >> x1 >> x2 >> y >> color;  
  146.                 x1--;  
  147.                 x2--;  
  148.                 y--;  
  149.   
  150.                 SWAP(x1, x2, int);  
  151.   
  152.                 fill_rect(matrix, x1, y, x2, y, color);  
  153.                 break;  
  154.             // 填充矩形区域。     
  155.             case 'K':  
  156.                 iss >> x1 >> y1 >> x2 >> y2 >> color;  
  157.                 x1--;  
  158.                 y1--;  
  159.                 x2--;  
  160.                 y2--;  
  161.   
  162.                 SWAP(x1, x2, int);  
  163.                 SWAP(y1, y2, int);  
  164.   
  165.                 fill_rect(matrix, x1, y1, x2, y2, color);  
  166.                 break;  
  167.             // 填充包含点(x,y)的区域为颜色color。      
  168.             case 'F':  
  169.                 iss >> x >> y >> color;  
  170.                 x--;  
  171.                 y--;  
  172.   
  173.                 fill_region(matrix, x, y, matrix[y][x], color);  
  174.                 break;  
  175.             // 按 MSDOS 8.3命名格式输出文件名。       
  176.             case 'S':  
  177.                 string file_name;  
  178.                 iss >> file_name;  
  179.                   
  180.                 if (file_name.length() > 12)  
  181.                     file_name = file_name.substr(0, 12);  
  182.                 cout << file_name << endl;  
  183.                 cout << matrix;  
  184.                 break;  
  185.         }  
  186.     }  
  187.       
  188.     return 0;  
  189. }  
// Graphical Editor (图形化编辑器)
// PC/UVa IDs: 110105/10267, Popularity: B, Success rate: low Level: 1
// Verdict: Accepted
// Submission Date: 2011-05-16
// UVa Run Time: 0.060s
//
// 版权所有(C)2011,邱秋。metaphysis # yeah dot net

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <iterator>
	
using namespace std;
	
#define SWAP(x, y, type) \
	{\
		if (x > y)\
		{\
			type tmp = x;\
			x = y;\
			y = tmp;\
		}\
	}
	
// 输出图形矩阵的内容。
ostream& operator<<(ostream &out, const vector < vector < char > > &matrix)
{
	for (vector < vector < char > >::const_iterator p = matrix.begin(); 
		p != matrix.end(); p++)
	{
		copy((*p).begin(), (*p).end(), ostream_iterator < char >(out, ""));
		out << endl;
	}

	return out;
}
	
// 重置字符矩阵。
void reset(vector < vector < char > > &matrix)
{
	// 注意是初始化为大写字母O而不是数字0.
	for (vector < vector < char > >::iterator p = matrix.begin(); 
		p != matrix.end(); p++)
		fill((*p).begin(), (*p).end(), 'O');
}
	
// 设置点(x,y)的颜色为c。
void set_pixel(vector < vector < char > > &matrix, int x, int y, char c)
{
	matrix[y][x] = c;
}
	
// 填充左上角为(x1,y1),右下角为(x2,y2)的矩形区域为颜色c。
void fill_rect(vector < vector < char > > &matrix, int x1, int y1, 
	int x2, int y2, char c)
{
	for (int i = y1; i <= y2; i++)
		for (int j = x1; j <= x2; j++)
			matrix[i][j] = c;
}
	
// 填充包含点(x,y)的区域为颜色new_color。
void fill_region(vector < vector < char > > &matrix, int x, int y, 
	char old_color, char new_color)
{
	// 当新旧颜色相同时需要结束递归,否则会形成无限循环。
	if (old_color == new_color)
		return;
	
	matrix[y][x] = new_color;
	
	if (x > 0)
		if (matrix[y][x - 1] == old_color)
			fill_region(matrix, x - 1, y, old_color, new_color);
	
	if (x < matrix[y].size() - 1)
		if (matrix[y][x + 1] == old_color)
			fill_region(matrix, x + 1, y, old_color, new_color);
	
	if (y > 0)
		if (matrix[y - 1][x] == old_color)
			fill_region(matrix, x, y - 1, old_color, new_color);
	
	if (y < matrix.size() - 1)
		if (matrix[y + 1][x] == old_color)
			fill_region(matrix, x, y + 1, old_color, new_color);
}
	
int main(int argc, char *argv[])
{
	// 使用二维向量来保存字符矩阵。
	vector < vector < char > > matrix;
	string line;
	// 用于表示图像的一些变量。
	int width, height, x, y, x1, x2, y1, y2;
	char command, color;
	
	while (getline(cin, line), line[0] != 'X')
	{
		istringstream iss(line);
	
		// 读入命令。
		iss >> command;
		switch (command)
		{
			// 初始化图像为宽为width,高为height。
			case 'I':
				iss >> width >> height;
	
				matrix.clear();
				matrix.resize(height);
				for (vector < vector < char > >::iterator 
					p = matrix.begin(); p != matrix.end(); p++)
					(*p).resize(width);

				reset(matrix);
				break;
			// 重置矩阵。	
			case 'C':
				reset(matrix);
				break;
			// 设置某点颜色。	
			case 'L':
				iss >> x >> y >> color;
				x--;
				y--;

				set_pixel(matrix, x, y, color);
				break;
			// 绘制竖线。
			case 'V':
				iss >> x >> y1 >> y2 >> color;
				x--;
				y1--;
				y2--;

				SWAP(y1, y2, int);

				fill_rect(matrix, x, y1, x, y2, color);
				break;
			// 绘制水平线。	
			case 'H':
				iss >> x1 >> x2 >> y >> color;
				x1--;
				x2--;
				y--;

				SWAP(x1, x2, int);

				fill_rect(matrix, x1, y, x2, y, color);
				break;
			// 填充矩形区域。	
			case 'K':
				iss >> x1 >> y1 >> x2 >> y2 >> color;
				x1--;
				y1--;
				x2--;
				y2--;

				SWAP(x1, x2, int);
				SWAP(y1, y2, int);

				fill_rect(matrix, x1, y1, x2, y2, color);
				break;
			// 填充包含点(x,y)的区域为颜色color。	
			case 'F':
				iss >> x >> y >> color;
				x--;
				y--;

				fill_region(matrix, x, y, matrix[y][x], color);
				break;
			// 按 MSDOS 8.3命名格式输出文件名。	
			case 'S':
				string file_name;
				iss >> file_name;
				
				if (file_name.length() > 12)
					file_name = file_name.substr(0, 12);
				cout << file_name << endl;
				cout << matrix;
				break;
		}
	}
	
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值