Ultraviolet A Problem 10267 Graphical Editor (Graphical Editor)

Source: Internet
Author: User
Tags dot net
// Graphical Editor (Graphical Editor) // PC/Ultraviolet IDs: 110105/10267, popularity: B, success rate: Low Level: 1 // verdict: accepted // submission date: 2011-05-16 // UV Run Time: 0.060 S // copyright (c) 2011, Qiu. 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; \}\} // output the content of the graphic matrix. Ostream & operator <(ostream & out, const vector <char> & matrix) {for (vector <char> >:: const_iterator P = matrix. begin (); P! = Matrix. end (); P ++) {copy (* P ). begin (), (* P ). end (), ostream_iterator <char> (Out, ""); out <Endl;} return out ;}// reset the character matrix. Void reset (vector <char> & matrix) {// note that the initialization is an uppercase letter O instead of a number 0.for (vector <char> :: iterator P = matrix. begin (); P! = Matrix. end (); P ++) Fill (* P ). begin (), (* P ). end (), 'O');} // set the color of the vertex (x, y) to C. Void set_pixel (vector <char> & matrix, int X, int y, char c) {matrix [y] [x] = C ;} // fill in the upper left corner (x1, Y1), and the lower right corner (X2, Y2) of the rectangular area is the color C. Void fill_rect (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 ;} // fill in the area containing points (x, y) with the color new_color. Void fill_region (vector <char> & matrix, int X, int y, char old_color, char new_color) {// end recursion when both old and new colors are required, otherwise, an infinite loop is formed. 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 (matri X [Y + 1] [x] = old_color) fill_region (matrix, X, Y + 1, old_color, new_color);} int main (INT argc, char * argv []) {// use a two-dimensional vector to save the character matrix. Vector <vector <char> matrix; string line; // variables used to represent images. Int width, height, X, Y, x1, x2, Y1, Y2; char command, color; while (Getline (CIN, line), line [0]! = 'X') {istringstream ISS (line); // read the command. ISS> command; Switch (command) {// The initialization image is width and height. Case 'I': ISS> width> height; matrix. clear (); matrix. resize (height); For (vector <char> >:: iterator P = matrix. begin (); P! = Matrix. End (); P ++) (* P). Resize (width); reset (matrix); break; // reset the matrix. Case 'C': reset (matrix); break; // set the color of a vertex. Case 'l': ISS> x> Y> color; X --; y --; set_pixel (matrix, X, Y, color); break; // draw a vertical line. Case 'V': ISS> x> Y1> Y2> color; X --; Y1 --; Y2 --; swap (Y1, Y2, INT ); fill_rect (matrix, X, Y1, X, Y2, color); break; // draw a horizontal line. Case 'H': ISS> x1> X2> Y> color; X1 --; x2 --; y --; swap (x1, x2, INT ); fill_rect (matrix, X1, Y, X2, Y, color); break; // fill the rectangular area. 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; // fill in the contain point (x, y) the area is color. Case 'F': ISS> x> Y> color; X --; y --; fill_region (matrix, X, Y, matrix [y] [X], color); break; // output file name in msdos 8.3 naming format. 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 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.