UVa Problem 10267 Graphical Editor (圖形化編輯器)

來源:互聯網
上載者:User
// Graphical Editor (圖形化編輯器)// PC/UVa IDs: 110105/10267, Popularity: B, Success rate: low Level: 1// Verdict: Accepted// Submission Date: 2011-05-16// UVa Run Time: 0.060s//// 著作權(C)2011,邱秋。metaphysis # yeah dot net#include <iostream>#include <string>#include <sstream>#include <vector>#include <iterator>using namespace std;#define SWAP(x, y, type) \{\if (x > y)\{\type tmp = x;\x = y;\y = tmp;\}\}// 輸出圖形矩陣的內容。ostream& operator<<(ostream &out, const vector < vector < char > > &matrix){for (vector < vector < char > >::const_iterator p = matrix.begin(); p != matrix.end(); p++){copy((*p).begin(), (*p).end(), ostream_iterator < char >(out, ""));out << endl;}return out;}// 重設字元矩陣。void reset(vector < vector < char > > &matrix){// 注意是初始化為大寫字母O而不是數字0.for (vector < vector < char > >::iterator p = matrix.begin(); p != matrix.end(); p++)fill((*p).begin(), (*p).end(), 'O');}// 設定點(x,y)的顏色為c。void set_pixel(vector < vector < char > > &matrix, int x, int y, char c){matrix[y][x] = c;}// 填充左上方為(x1,y1),右下角為(x2,y2)的矩形地區為顏色c。void fill_rect(vector < vector < char > > &matrix, int x1, int y1, int x2, int y2, char c){for (int i = y1; i <= y2; i++)for (int j = x1; j <= x2; j++)matrix[i][j] = c;}// 填充包含點(x,y)的地區為顏色new_color。void fill_region(vector < vector < char > > &matrix, int x, int y, char old_color, char new_color){// 當新舊顏色相同時需要結束遞迴,否則會形成無限迴圈。if (old_color == new_color)return;matrix[y][x] = new_color;if (x > 0)if (matrix[y][x - 1] == old_color)fill_region(matrix, x - 1, y, old_color, new_color);if (x < matrix[y].size() - 1)if (matrix[y][x + 1] == old_color)fill_region(matrix, x + 1, y, old_color, new_color);if (y > 0)if (matrix[y - 1][x] == old_color)fill_region(matrix, x, y - 1, old_color, new_color);if (y < matrix.size() - 1)if (matrix[y + 1][x] == old_color)fill_region(matrix, x, y + 1, old_color, new_color);}int main(int argc, char *argv[]){// 使用二維向量來儲存字元矩陣。vector < vector < char > > matrix;string line;// 用於表示映像的一些變數。int width, height, x, y, x1, x2, y1, y2;char command, color;while (getline(cin, line), line[0] != 'X'){istringstream iss(line);// 讀入命令。iss >> command;switch (command){// 初始化映像為寬為width,高為height。case 'I':iss >> width >> height;matrix.clear();matrix.resize(height);for (vector < vector < char > >::iterator p = matrix.begin(); p != matrix.end(); p++)(*p).resize(width);reset(matrix);break;// 重設矩陣。case 'C':reset(matrix);break;// 設定某點顏色。case 'L':iss >> x >> y >> color;x--;y--;set_pixel(matrix, x, y, color);break;// 繪製豎線。case 'V':iss >> x >> y1 >> y2 >> color;x--;y1--;y2--;SWAP(y1, y2, int);fill_rect(matrix, x, y1, x, y2, color);break;// 繪製水平線。case 'H':iss >> x1 >> x2 >> y >> color;x1--;x2--;y--;SWAP(x1, x2, int);fill_rect(matrix, x1, y, x2, y, color);break;// 填充矩形地區。case 'K':iss >> x1 >> y1 >> x2 >> y2 >> color;x1--;y1--;x2--;y2--;SWAP(x1, x2, int);SWAP(y1, y2, int);fill_rect(matrix, x1, y1, x2, y2, color);break;// 填充包含點(x,y)的地區為顏色color。case 'F':iss >> x >> y >> color;x--;y--;fill_region(matrix, x, y, matrix[y][x], color);break;// 按 MSDOS 8.3命名格式輸出檔案名。case 'S':string file_name;iss >> file_name;if (file_name.length() > 12)file_name = file_name.substr(0, 12);cout << file_name << endl;cout << matrix;break;}}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.