Guest User

Two Dimensional

a guest
Jun 9th, 2021
62
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <vector>
  5.  
  6. std::vector <std::vector<char>> get2DMatrix() {
  7.     std::vector <std::vector<char>> output;
  8.     std::string lineInput;
  9.     getline(std::cin, lineInput);
  10.  
  11.     std::stringstream str(lineInput);
  12.     int token;
  13.     std::vector<int> size2D;
  14.     while (str >> token) {
  15.         size2D.push_back(token);
  16.     }
  17.     for (int i = 0; i < size2D[0]; i++) {
  18.         getline(std::cin, lineInput);
  19.         str.clear();
  20.         str.str(lineInput);
  21.         char c;
  22.  
  23.         std::vector<char> intLine;
  24.         while (str >> c) {
  25.             intLine.push_back(c);
  26.         }
  27.         output.push_back(intLine);
  28.     }
  29.     return output;
  30. }
  31.  
  32.  
  33.  
  34. void printMatrix(std::vector <std::vector<char>>& matrix) {
  35.     int xLength = matrix.size();
  36.     int yLength = matrix[0].size();
  37.     for (size_t i = 0; i < xLength; i++)
  38.     {
  39.         for (size_t j = 0; j < yLength; j++)
  40.         {
  41.             std::cout << matrix[i][j] << ' ';
  42.         }
  43.         std::cout << "\n";
  44.  
  45.     }
  46. }
  47.  
  48. void paintChar(char c, int y, int x, std::vector <std::vector<char>>& matrix) {
  49.     int xLength = matrix[0].size();
  50.     int yLength = matrix.size();
  51.     char change = matrix[x][y];
  52.     for (int i = x; i < xLength; i++) {
  53.         for (int j = y; j < yLength; j++) {
  54.             if (change == matrix[j][i]) matrix[j][i] = c;
  55.             else break;
  56.         }
  57.         for (int j = y-1; j >= 0; j--) {
  58.             if (change == matrix[j][i] || c == matrix[j][i]) matrix[j][i] = c;
  59.             else break;
  60.         }
  61.     }
  62.  
  63.     for (int i = x-1; i >=0; i--) {
  64.         for (int j = y; j < yLength; j++) {
  65.             if (change == matrix[j][i]) matrix[j][i] = c;
  66.             else break;
  67.         }
  68.         for (int j = y - 1; j >= 0; j--) {
  69.             if (change == matrix[j][i] ||c == matrix[j][i] ) matrix[j][i] = c;
  70.             else break;
  71.         }
  72.     }
  73. }
  74.  
  75. int main()
  76. {
  77.     auto matrix = get2DMatrix();
  78.  
  79.     char c;
  80.     int x, y;
  81.     std::cin >> c >> x >> y;
  82.  
  83.     paintChar(c, y, x, matrix);
  84.  
  85.     printMatrix(matrix);
  86.  
  87.     return 0;
  88. }
  89.  
RAW Paste Data