# 題目: UVa 10267 - Graphical Editor
# 題目說明
你需要實做一個編輯圖像的程式
INPUT:
共有 9 種指令
I M N
: 生成一個N * M
大小的圖,所有字元預設為O
(大寫 O)C
: 清除圖,將所有字元設為O
L X Y C
: 將圖的(X, Y)
設為C
V X Y1 Y2 C
: 畫垂直線(X, Y1)
至(X, Y2)
H X1 X2 Y C
: 畫水平線(X1, Y)
至(X2, Y)
K X1 Y1 X2 Y2 C
: 畫矩形(X1, Y1)
至(X2, Y2)
F X Y C
: 填滿顏色,將(X, Y)
與之相鄰的同色填為C
S Name
: 寫入檔案名稱,並印出X
: 結束程式
OUTPUT:
輸出當前的檔案名稱及圖
# 解題方法
針對每個指令做出相對應動作
比較需要注意的是指令 F
需要用 dfs
填色
# 參考程式碼
#include <iostream> | |
#include <string> | |
#include <memory.h> | |
using namespace std; | |
char G[251][251]; | |
int M = 0, N = 0; | |
void dfs(int x, int y, char now, char c) | |
{ | |
if (y < 1 || y > N || x < 1 || x > M || G[y][x] != now) return; | |
G[y][x] = c; | |
dfs(x + 1, y, now, c); | |
dfs(x - 1, y, now, c); | |
dfs(x, y + 1, now, c); | |
dfs(x, y - 1, now, c); | |
} | |
int main() | |
{ | |
// fast io | |
ios::sync_with_stdio(false); | |
cin.tie(nullptr); | |
cout.tie(nullptr); | |
char cmd, C, Now; | |
int x1, x2, y1, y2; | |
string Name, str; | |
memset(G, 'O', sizeof(G)); | |
while (cin >> cmd, cmd != 'X') | |
{ | |
switch (cmd) | |
{ | |
case 'I': | |
cin >> M >> N; | |
memset(G, 'O', sizeof(G)); | |
break; | |
case 'C': | |
memset(G, 'O', sizeof(G)); | |
break; | |
case 'L': | |
cin >> x1 >> y1 >> C; | |
G[y1][x1] = C; | |
break; | |
case 'V': | |
cin >> x1 >> y1 >> y2 >> C; | |
if (y1 > y2) swap(y1, y2); | |
for (int i = y1; i <= y2; ++i) G[i][x1] = C; | |
break; | |
case 'H': | |
cin >> x1 >> x2 >> y1 >> C; | |
if (x1 > x2) swap(x1, x2); | |
for (int i = x1; i <= x2; ++i) G[y1][i] = C; | |
break; | |
case 'K': | |
cin >> x1 >> y1 >> x2 >> y2 >> C; | |
if (y1 > y2) swap(y1, y2); | |
if (x1 > x2) swap(x1, x2); | |
for (int i = y1; i <= y2; ++i) | |
for (int j = x1; j <= x2; ++j) G[i][j] = C; | |
break; | |
case 'F': | |
cin >> x1 >> y1 >> C; | |
if (G[y1][x1] != C) dfs(x1, y1, G[y1][x1], C); | |
break; | |
case 'S': | |
cin >> Name; | |
cout << Name << "\n"; | |
for (int i = 1; i <= N; ++i) | |
{ | |
for (int j = 1; j <= M; ++j) cout << G[i][j]; | |
cout << "\n"; | |
} | |
break; | |
default: | |
cin.ignore(); | |
getline(cin, str); | |
break; | |
} | |
} | |
return 0; | |
} |
# 參考資料
https://blog.csdn.net/mobius_strip/article/details/71172828