題目: UVa - 10267 - Graphical Editor

題目說明

實作一個模擬圖像編輯的程式,根據指令做出對應的操作。

解題思路

根據題目的表格做出對應的操作即可,需要注意的是,題目的 X1、X2 和 Y1、Y2,並沒有設定誰大誰小,所以要記得判斷。填色則使用 DFS 即可,注意邊界條件。

參考解法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <bits/stdc++.h>

using namespace std;

static auto __ = []
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
return 0;
}();

int M, N;
int X1, Y1;
int X2, Y2;
int d[] = { 1, 0, -1, 0, 1 };
char C;
char G[251][251];
string fileName, trash;

void drawRectangle(int x1, int y1, int x2, int y2, int reset = 0)
{
if (reset) C = 'O';
if (x1 > x2) swap(x1, x2);
if (y1 > y2) swap(y1, y2);

for (int i = y1; i <= y2; ++i) for (int j = x1; j <= x2; ++j)
G[i][j] = C;
}

void resetGraph() { drawRectangle(1, 1, M, N, 1); }

void verticalLine() { drawRectangle(X1, Y1, X1, Y2); }

void horizontalLine() { drawRectangle(X1, Y1, X2, Y1); }

void floodFill(int x, int y, char present)
{
if (x < 1 || x > M || y < 1 || y > N) return;
if (G[y][x] == C || G[y][x] != present) return;

G[y][x] = C;
for (int i = 0; i < 4; ++i)
floodFill(x + d[i], y + d[i + 1], present);
}

void printGraph()
{
for (int i = 1; i <= N; ++i)
{
for (int j = 1; j <= M; ++j) cout << G[i][j];
cout << '\n';
}
}

int main()
{
char cmd;

while (cin >> cmd, cmd != 'X') switch (cmd)
{
case 'I':
cin >> M >> N;
resetGraph();
break;

case 'C':
resetGraph();
break;

case 'L':
cin >> X1 >> Y1 >> G[Y1][X1];
break;

case 'V':
cin >> X1 >> Y1 >> Y2 >> C;
verticalLine();
break;

case 'H':
cin >> X1 >> X2 >> Y1 >> C;
horizontalLine();
break;

case 'K':
cin >> X1 >> Y1 >> X2 >> Y2 >> C;
drawRectangle(X1, Y1, X2, Y2);
break;

case 'F':
cin >> X1 >> Y1 >> C;
floodFill(X1, Y1, G[Y1][X1]);
break;

case 'S':
cin >> fileName;
cout << fileName << '\n';
printGraph();
break;

default:
cin.ignore();
getline(cin, trash);
break;
}
}