画图 (FloodFill Algorithm)全网首发!
#include <iostream>
#include <vector>
using namespace std;
int n, m, q, x, y, x1, yy1, x2, y2, op;
char c;
void print(vector<vector<char>>& mat) {
for (int y = 0; y < m; ++y) {
for (int x = 0; x < n; ++x) printf("%c", mat[y][x]);
printf("\n");
}
}
// depth first search
void dfs(vector<vector<char>>& grid,
vector<vector<int>>& visited,
int x, int y, char c) {
if (x < 0 || y < 0 || x == n || y == m
|| grid[y][x] == '-' || grid[y][x] == '|' || grid[y][x] == '+'
|| visited[y][x])
return;
grid[y][x] = c; // fill
visited[y][x] = 1; // mark as visited
dfs(grid, visited, x - 1, y, c);
dfs(grid, visited, x + 1, y, c);
dfs(grid, visited, x, y - 1, c);
dfs(grid, visited, x, y + 1, c);
}
int main(void) {
scanf("%d %d %d", &n, &m, &q);
vector<vector<char>> grid(m, vector<char>(n, '.'));
while (q--) {
cin >> op;
if (op == 0) {
cin >> x1 >> yy1 >> x2 >> y2;
if (x1 > x2) swap(x1, x2);
if (yy1 > y2) swap(yy1, y2);
if (x1 == x2) { // 画竖线
for (int y = m - 1 - yy1; y >= m - 1 - y2; --y)
grid[y][x1] = grid[y][x1] == '-' || grid[y][x1] == '+' ? '+' : '|';
} else { // 画横线
for (int x = x1; x <= x2; ++x)
grid[m - 1 - yy1][x] = grid[m - 1 - yy1][x] == '|' || grid[m - 1 - yy1][x] == '+' ? '+' : '-';
}
} else {
cin >> x >> y >> c;
vector<vector<int>> visited(m, vector<int>(n));
dfs(grid, visited, x, m - 1 - y, c);
}
}
print(grid);
return 0;
}