Advertisement
oaktree

territory.cpp

Jun 4th, 2016
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. struct rectangle {
  5.     int bottom_left_x,
  6.         bottom_left_y,
  7.         top_right_x,
  8.         top_right_y;
  9. };
  10.  
  11. bool check_territory(const std::vector<rectangle>& graph, const rectangle& to_check);
  12. bool are_ints(const std::string& str);
  13.  
  14. int main() {
  15.     std::vector<rectangle> graph;
  16.     std::string input;
  17.     rectangle new_rect;
  18.     while(true) {
  19.         std::cout << "[!] Input graph coords. ";
  20.         std::cin >> input;
  21.  
  22.         if (input.size() != 4 || !are_ints(input)) {
  23.             std::cout << "[!] ERROR: Invalid Input. Try again..." << std::endl;
  24.             continue;
  25.         }
  26.  
  27.         new_rect = {
  28.             int(input[0]) - 48,
  29.             int(input[1]) - 48,
  30.             int(input[2]) - 48,
  31.             int(input[3]) - 48
  32.         };
  33.  
  34.         if (check_territory(graph,new_rect)) {
  35.             graph.push_back(new_rect);
  36.             std::cout << "[!] This rectangle will fit. "
  37.                 << "Adding to graph... Ctrl+C to finish. "
  38.                 << std::endl;
  39.         } else {
  40.             std::cout << "[!] This rectangle does not fit. Try again."
  41.                 << std::endl;
  42.         }
  43.     }
  44.     return 0;
  45. }
  46.  
  47. bool check_territory(const std::vector<rectangle>& graph, const rectangle& to_check) {
  48.     if (graph.size() == 0) return true;
  49.  
  50.     for (auto& rect : graph) {
  51.         if ( !( to_check.bottom_left_x < rect.bottom_left_x
  52.                 || to_check.bottom_left_y < rect.bottom_left_y )
  53.             || !( to_check.top_right_x > rect.top_right_x
  54.                 || to_check.top_right_y > rect.top_right_y )
  55.             || !( to_check.bottom_left_x < rect.top_right_x
  56.                 || to_check.bottom_left_y < rect.top_right_y )
  57.             || !( to_check.top_right_x > rect.bottom_left_x
  58.                 || to_check.top_right_y > rect.bottom_left_y )
  59.             || !( to_check.bottom_left_x > rect.top_right_x
  60.                 || to_check.top_right_y < rect.bottom_left_y )
  61.             || !( to_check.top_right_x < rect.bottom_left_x
  62.                 || to_check.bottom_left_y > rect.top_right_y )
  63.             )
  64.         {
  65.             return false;
  66.         }
  67.     }
  68.  
  69.     return true;
  70. }
  71.  
  72. bool are_ints(const std::string& str) {
  73.     for (auto& c : str)
  74.         if (c < '0' || c > '9')
  75.             return false;
  76.  
  77.     return true;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement