CCF-CSP题解 201512-3 画图
画图时思路应该清晰一点。我是将坐标\((x,y)\)映射到\(canvas[y][x]\)上。
连线注意\(+\)号的情况,填充写好\(dfs\)就好了。
#include <bits/stdc++.h>
const int maxn = 100;
using namespace std;
int m, n, q;
char canvas[maxn + 5][maxn + 5];
int vis[maxn + 5][maxn + 5];
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
void dfs(int x, int y, char c)
{
if (vis[y][x])
return;
vis[y][x] = 1;
canvas[y][x] = c;
for (int i = 0; i <= 3; i++)
{
int xx = x + dir[i][0], yy = y + dir[i][1];
if (xx >= 0 && xx <= m - 1 && yy >= 0 && yy <= n - 1 &&
canvas[yy][xx] != '|' && canvas[yy][xx] != '-' && canvas[yy][xx] != '+')
dfs(xx, yy, c);
}
}
int main()
{
scanf("%d%d%d", &m, &n, &q);
for (int i = 0; i <= n - 1; i++)
for (int j = 0; j <= m - 1; j++)
canvas[i][j] = '.';
while (q--)
{
int op;
scanf("%d", &op);
if (op == 0)
{
int x1, y1, x2, y2;
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
if (x1 == x2)
{
for (int i = min(y1, y2); i <= max(y1, y2); i++)
{
if (canvas[i][x1] == '-' || canvas[i][x1] == '+')
canvas[i][x1] = '+';
else
canvas[i][x1] = '|';
}
}
else
{
for (int i = min(x1, x2); i <= max(x1, x2); i++)
{
if (canvas[y1][i] == '|' || canvas[y1][i] == '+')
canvas[y1][i] = '+';
else
canvas[y1][i] = '-';
}
}
}
else
{
int x, y;
char c[2];
scanf("%d%d%s", &x, &y, c);
memset(vis, 0, sizeof(vis));
dfs(x, y, c[0]);
}
}
for (int i = n - 1; i >= 0; i--)
{
for (int j = 0; j <= m - 1; j++)
printf("%c", canvas[i][j]);
printf("\n");
}
return 0;
}