看着挺简单的题,实际做起来也不难,就是有些小地方容易遗漏。比如填充时和划线时容易漏判‘+’,填充算法用递归即可
#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);
}