CCF-画图

看着挺简单的题,实际做起来也不难,就是有些小地方容易遗漏。比如填充时和划线时容易漏判‘+’,填充算法用递归即可

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
void display(vector< vector<char> > &G);
void fill(vector< vector<char> > &G, int row, int column, char ch);
void draw(vector< vector<char> > &G, int x1, int y1, int x2, int y2);
int main()
{
	vector< vector<char> > G;
	int width, height;
	int num;
	int command;
	cin >> width >> height >> num;
	G.resize(height);
	for (int i = 0; i < G.size(); i++)
	{
		for (int j = 0; j < width; j++)
		{
			G[i].push_back('.');
		}
	}
	while (num--)
	{
		cin >> command;
		if (command == 0)
		{
			int x1, x2, y1, y2;
			int row1, col1, row2, col2;
			cin >> x1 >> y1 >> x2 >> y2;
			row1 = G.size() - 1 - y1;
			col1 = x1;
			row2 = G.size() - 1 - y2;
			col2 = x2;
			draw(G, row1, col1, row2, col2);
		}
		else
		{
			char ch;
			int x, y;
			int row, col;
			cin >> x >> y >> ch;
			row = G.size() - 1 - y;
			col = x;
			fill(G, row, col, ch);
		}
	}
	display(G);
	return 0;
}
void display(vector< vector<char> > &G)
{
	for (int i = 0; i < G.size(); i++)
	{
		for (int j = 0; j < G[i].size(); j++)
		{
			cout << G[i][j];
		}
		cout << endl;
	}
}
void draw(vector< vector<char> > &G, int x1, int y1, int x2, int y2)
{
	if (x1 == x2)      //horizental
	{
		if (y1 > y2)
		{
			for (int i = y2; i <= y1; i++)
			{
				if (G[x1][i] == '|' || G[x1][i] == '+')
				{
					G[x1][i] = '+';
				}
				else
				{
					G[x1][i] = '-';
				}
			}
		}
		else
		{
			for (int i = y1; i <= y2; i++)
			{
				if (G[x1][i] == '|' || G[x1][i] == '+')
				{
					G[x1][i] = '+';
				}
				else
				{
					G[x1][i] = '-';
				}
			}
		}
	}
	if (y1 == y2)      //vertical
	{
		if (x1 > x2)
		{
			for (int i = x2; i <= x1; i++)
			{
				if (G[i][y1] == '-' || G[i][y1] == '+')
				{
					G[i][y1] = '+';
				}
				else
				{
					G[i][y1] = '|';
				}
			}
		}
		else
		{
			for (int i = x1; i <= x2; i++)
			{
				if (G[i][y1] == '-' || G[i][y1] == '+')
				{
					G[i][y1] = '+';
				}
				else
				{
					G[i][y1] = '|';
				}
			}
		}
	}
}

void fill(vector< vector<char> > &G, int row, int column, char ch)
{
	if (row < 0 || row >= G.size() || column < 0 || column >= G[row].size())
	{
		return;
	}
	if (G[row][column] == '|' || G[row][column] == '-' || G[row][column] == '+')
	{
		return;
	}
	if (G[row][column] == ch)
	{
		return;
	}
	G[row][column] = ch;
	fill(G, row - 1, column, ch);
	fill(G, row + 1, column, ch);
	fill(G, row, column - 1, ch);
	fill(G, row, column + 1, ch);
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值