「2つの矩形に含まれるマス目の数 2017.2.22 問題」をC++で解いた

http://qiita.com/tatsuo98se/items/5d4a096410d8f71cf683 をがんばってC++で解いた。



```{cpp}
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>


struct Rect
{
  int t;
  int l;
  int b;
  int r;
  
  Rect() {}
  Rect(int tt, int ll, int bb, int rr)
  {
    if (tt > bb) std::swap(tt, bb);
    if (ll > rr) std::swap(ll, rr);
    
    t = tt;
    l = ll;
    b = bb;
    r = rr;
  }
  
  long long Area()
  {
    return ((long long)(b-t)) * ((long long)(r-l));
  }
};


Rect Intersect(Rect Rect1, Rect Rect2)
{
  // return the rectangle defined as the intersect of two rectangles
  // if the intersect is empty, then return Rect(0,0,0,0)

  if (Rect1.b < Rect2.t || Rect2.b < Rect1.t || Rect1.r < Rect2.l || Rect2.r < Rect1.l) 
    return Rect(0, 0, 0, 0);  // no intersect 
  
  int t = (Rect1.t >= Rect2.t) ? Rect1.t : Rect2.t;
  int l = (Rect1.l >= Rect2.l) ? Rect1.l : Rect2.l;
  int b = (Rect1.b <= Rect2.b) ? Rect1.b : Rect2.b;
  int r = (Rect1.r <= Rect2.r) ? Rect1.r : Rect2.r;
  return Rect(t, l, b, r);
  
}


long long CoveredBy2of3Rects(Rect &Rect1, Rect &Rect2, Rect &Rect3)
{
  Rect R12 = Intersect(Rect1, Rect2);
  Rect R13 = Intersect(Rect1, Rect3);
  Rect R23 = Intersect(Rect2, Rect3);
  Rect R123 = Intersect(R12, Rect3);
  
  return R12.Area() + R13.Area() + R23.Area() - 3*R123.Area();
}



std::string int_to_str(int num)
{
  std::stringstream ss;
  ss << num;
  return ss.str();
}

std::string int_to_str(long long num)
{
  std::stringstream ss;
  ss << num;
  return ss.str();
}

int str_to_int(const std::string &str) {
  std::stringstream ss(str);
  int num;
  ss >> num;
  return num;
}


std::vector<int> parse_input(std::string &input)
{
  std::vector<int> out;
  int start = 0;
  int pos = 0;
  while (pos < input.size())
  {
    if (input[pos] == ',' || input[pos] == '-' || input[pos] == '/') {
      out.push_back(str_to_int(input.substr(start, pos-start)));
      pos++;
      start = pos;
    } else {
      pos++;
    }
    if (pos >= input.size()) out.push_back(str_to_int(input.substr(start)));

  }
  return out;
}


long long solver(std::string &input)
{
  std::vector<int> numbers = parse_input(input);
  
  
  if (numbers.size() != 12) {
    std::cout << "invalid input!\n";
    return 1;
  }
  
  Rect R1 = Rect(numbers[0], numbers[1], numbers[2]+1, numbers[3]+1);
  Rect R2 = Rect(numbers[4], numbers[5], numbers[6]+1, numbers[7]+1);
  Rect R3 = Rect(numbers[8], numbers[9], numbers[10]+1, numbers[11]+1);
  long long ans = CoveredBy2of3Rects(R1, R2, R3);
  
  return ans;
}


void test(std::string input, std::string out)
{
  long long myans = solver(input);
  if (int_to_str(myans) == out) {
    std::cout << "YAY! my answer = " << myans << " as expected\n";
  } else {
    std::cout << "BOO! my answer = " << myans << " where " << out << " is expected\n";
  }
}


int main()
{

/*0*/ test( "5,1-7,9/3,2-8,6/0,5-9,5", "15" );
/*1*/ test( "0,0-9,9/0,0-9,9/0,0-9,9", "0" );
/*2*/ test( "0,0-9,9/0,0-0,9/1,0-9,9", "100" );
/*3*/ test( "2,5-7,6/0,5-7,7/2,0-8,6", "0" );
/*4*/ test( "1,9-4,9/4,9-7,9/0,3-7,4", "1" );
/*5*/ test( "6,1-6,9/5,0-7,4/5,1-7,2", "6" );
/*6*/ test( "4,0-9,8/5,1-6,8/0,2-9,7", "28" );
/*7*/ test( "2,8-8,9/7,9-8,9/8,3-8,9", "2" );
/*8*/ test( "3,3-9,4/0,1-8,4/1,2-8,9", "12" );
/*9*/ test( "2,1-8,3/0,1-3,7/8,3-8,4", "7" );
/*10*/ test( "5,4-6,9/0,0-6,0/5,3-9,8", "10" );
/*11*/ test( "1,1-9,7/1,1-3,8/3,8-7,9", "22" );
/*12*/ test( "2,4-6,7/3,2-7,8/1,0-9,4", "24" );
/*13*/ test( "0,2-1,5/8,1-8,3/1,8-6,8", "0" );
/*14*/ test( "5,2-9,5/9,1-9,2/8,0-8,6", "5" );
/*15*/ test( "5,0-6,4/2,1-6,4/3,8-3,9", "8" );
/*16*/ test( "0,4-6,9/4,1-6,9/7,6-9,7", "18" );
/*17*/ test( "0,0-5,5/0,1-2,8/5,3-9,4", "17" );
/*18*/ test( "0,2-5,6/5,6-8,7/0,1-2,6", "16" );
/*19*/ test( "7,2-8,4/1,0-6,8/1,3-7,6", "26" );
/*20*/ test( "4,3-9,3/0,0-6,5/0,0-4,8", "31" );
/*21*/ test( "3,4-4,6/2,2-4,8/2,0-8,4", "11" );
/*22*/ test( "1,2-6,5/0,5-4,7/2,8-2,9", "4" );
/*23*/ test( "4,1-7,5/2,1-9,9/1,7-2,9", "23" );
/*24*/ test( "1,6-5,6/0,3-5,7/0,2-2,6", "13" );
/*25*/ test( "1,3-3,4/1,4-3,4/9,2-9,9", "3" );
/*26*/ test( "6,3-7,6/2,2-2,3/1,3-9,8", "9" );
/*27*/ test( "2,2-9,7/1,8-9,8/2,2-8,9", "49" );
/*28*/ test( "1,2-6,9/7,6-9,9/4,3-9,9", "33" );
/*29*/ test( "6,0-7,5/0,4-3,8/1,4-5,8", "15" );
/*30*/ test( "2,0-9,7/0,5-3,8/5,1-7,7", "27" );
/*31*/ test( "1,2-8,7/3,1-4,3/2,3-5,8", "20" );
/*32*/ test( "1,0-7,7/0,1-5,4/0,0-2,3", "19" );
/*33*/ test( "2,0-3,7/1,1-3,7/5,3-5,9", "14" );
/*34*/ test( "7,2-9,8/1,0-6,8/0,2-9,9", "63" );
/*35*/ test( "1,1-5,3/0,3-8,7/2,3-8,7", "32" );
/*36*/ test( "3,4-6,6/1,0-9,1/4,0-9,9", "21" );
/*37*/ test( "0,0-4,7/0,5-5,9/0,2-4,5", "25" );
/*38*/ test( "1,1-9,9/2,2-7,4/2,4-7,7", "30" );
/*39*/ test( "3,2-9,9/2,0-6,6/0,5-8,9", "36" );
/*40*/ test( "0,1-8,8/0,5-9,8/2,3-2,4", "38" );
/*41*/ test( "0,0-8,6/4,3-9,9/7,1-9,9", "29" );
/*42*/ test( "0,0-8,8/2,4-9,8/0,1-9,2", "53" );


/*0*/ test( "0,0-999999,999999/0,0-999999,999999/0,0-999999,999999", "0" );    
/*1*/ test( "0,0-999999,999999/0,0-0,999999/1,0-999999,999999", "1000000000000" );    
/*2*/ test( "655822,899981-788586,907370/262080,47983-540321,834515/327083,392880-474728,550251", "23235346312" );    
/*3*/ test( "332855,375381-953091,410430/311649,171289-637710,912139/113674,426911-658531,763493", "120432128946" );    
/*4*/ test( "718118,613550-931221,983155/84495,98906-214393,787796/159096,14702-416478,360207", "14449477996" );    
/*5*/ test( "809,351021-589370,525004/161241,390394-302773,958450/78299,230856-310951,941900", "80430542457" );    
/*6*/ test( "140869,614312-559945,677721/424067,131309-645701,709237/22556,149538-902108,566473", "101023697750" );    
/*7*/ test( "655941,129163-859649,985280/446717,159679-977047,839232/172790,559592-492587,741630", "146781576755" );    
/*8*/ test( "17192,474077-715775,513703/177518,159867-395312,530025/331315,181585-695872,274046", "14547945541" );    
/*9*/ test( "898603,252482-932681,923548/158284,561187-881317,881298/291114,46375-975112,431448", "6099016393" );    
/*10*/ test( "460763,311625-742309,415024/708446,250395-980176,802067/190828,650489-798525,855977", "17155773920" );    
/*11*/ test( "76405,197443-189085,423768/218793,635517-608524,914533/68011,184211-238902,819853", "29209657076" );    
/*12*/ test( "602585,166640-862797,212028/844554,91101-971982,642729/130166,132319-619895,387625", "1613805895" );    
/*13*/ test( "570213,345485-759193,657391/214801,517728-943628,936004/412700,32310-853707,623589", "65618672419" );    
/*14*/ test( "251546,411491-781481,812128/271544,210234-536793,313835/174707,164436-353882,432689", "10699927141" );    
/*15*/ test( "768602,340406-876104,451915/531084,97975-614595,607952/297417,452002-545499,950992", "2248189616" );    
/*16*/ test( "824147,181473-928495,890221/673583,148595-734839,191460/532038,851156-892611,920296", "2674653690" );    
/*17*/ test( "507676,577270-927426,825282/385783,352947-643538,691954/243369,815194-385405,859966", "15581448155" );    
/*18*/ test( "388419,439549-745538,669102/262281,742072-609988,970128/533573,137288-596245,670546", "14386837842" );    
/*19*/ test( "663239,518782-856702,589433/758115,84450-903932,642088/337647,24284-583025,565986", "6965439376" );    
/*20*/ test( "191239,779501-419016,973110/461699,265809-619813,400896/295357,343268-995965,426583", "9112009335" );    
/*21*/ test( "481413,405591-580708,502557/548827,255673-775012,641036/426629,627433-838039,929163", "6168536238" );    
/*22*/ test( "9920,498487-514382,683023/226772,29017-628472,646705/311712,341208-824675,813994", "86680486267" );    
/*23*/ test( "607788,403841-668184,518365/91263,688091-637506,857258/24319,559457-663076,919412", "92407004992" );    
/*24*/ test( "77084,799236-139505,979891/754122,279472-794364,868062/150527,608666-281632,750541", "0" );    
/*25*/ test( "169296,83569-620267,759983/460604,265256-945013,954017/317013,98311-745837,121044", "85884450562" );    
/*26*/ test( "129661,44653-592189,297222/219053,537474-316753,980074/157470,756816-370610,907480", "14720121165" );    
/*27*/ test( "297941,386287-716792,725006/241452,450763-567051,962750/195744,679475-346109,722435", "74159512604" );    
/*28*/ test( "168144,237088-329400,634496/10381,564404-835828,912626/449243,161014-923398,676107", "54486189445" );    
/*29*/ test( "107162,121311-807174,529239/344730,331975-894427,731597/581880,770044-619288,914307", "91224212925" );    
/*30*/ test( "412012,102200-935128,257111/111516,225570-444846,539065/784025,129787-872324,570078", "12278479070" );    
/*31*/ test( "800011,636792-837944,845950/7799,3048-945539,551037/197775,295503-609840,482184", "76925305012" );    
/*32*/ test( "127131,26333-180762,397870/27576,261600-816030,833590/301519,411147-645517,557943", "57806507475" );    
/*33*/ test( "32758,293294-988342,514135/468,543068-318854,548069/27211,318362-114542,567550", "16448211254" );    
/*34*/ test( "100410,724118-597622,745585/183566,413277-751642,938721/672678,271783-953373,854704", "43746337696" );    
/*35*/ test( "617281,172835-638934,558464/725936,550832-893293,968391/243005,644233-783866,774370", "7539024478" );    
/*36*/ test( "306059,461338-761333,479832/59110,334011-979200,367681/201997,335634-587440,531481", "17556869402" );    
/*37*/ test( "198441,770394-533721,965743/7845,436240-747623,621749/345710,713519-465402,866708", "11528231295" );    
/*38*/ test( "856417,156222-995259,695178/552046,8773-879184,347237/114065,393927-827128,775960", "4349052288" );    
/*39*/ test( "298522,174473-586813,447826/513017,410991-576739,639088/180342,283427-453079,776103", "27756635628" );    
/*40*/ test( "676607,92015-788738,889720/351236,32426-799213,692704/334633,87130-927627,121184", "76070680990" );    
/*41*/ test( "112863,136827-415321,945235/278661,676383-304033,860715/404074,160748-970146,761914", "11439007625" );
  return 0;
}

```