LoginSignup
0
0

More than 5 years have passed since last update.

第六回オフラインリアルタイム参考問題

Last updated at Posted at 2012-12-26

コードを修正しました。
解き方は修正前と同じですが、コードは短くなったので見やすくなったかと思います。


#define BOOST_TEST_MAIN
#include <boost/test/included/unit_test.hpp>

namespace{
using namespace std;
using namespace boost;
typedef pair<size_t, size_t> pair_t;

vector<pair_t> get_rect(const string& s){
    vector<pair_t> v;
    for(size_t i=0; i<s.length(); i+=3){
        if(s[i] == '-'){
            continue;
        }
        v.push_back(pair_t(lexical_cast<size_t>(s.substr(i,1)),lexical_cast<size_t>(s.substr(i+1,1))));
    }
    return v;
}

void set_pos(set<string>& s, pair_t x, pair_t y){
    for(size_t j=y.first; j<=y.second; ++j){
        for(size_t i=x.first; i<=x.second; ++i){
            s.insert(lexical_cast<string>(i) + lexical_cast<string>(j));
        }
    }
}

bool same(const set<string>& red, const string& green){
    set<string>::iterator found = find(red.begin(), red.end(), green);
    return found != red.end();
}

size_t solve(const string& s){
    vector<pair_t> pos = get_rect(s);
    set<string> red, green;
    for(size_t i=1; i<3; ++i){
        //長くてごめんなさい
        set_pos(red, pair_t(min(pos[0].first, pos[i].first), max(pos[0].first, pos[i].first)), pair_t(min(pos[0].second, pos[i].second), max(pos[0].second, pos[i].second)));
        set_pos(green, pair_t(min(pos[3].first, pos[i+3].first), max(pos[3].first, pos[i+3].first)), pair_t(min(pos[3].second, pos[i+3].second), max(pos[3].second, pos[i+3].second)));
    }
    return count_if(green.begin(), green.end(), bind(&same, red, _1));
}
}

//以下はテスト
BOOST_AUTO_TEST_CASE( solve_test1 ){
    BOOST_CHECK_EQUAL(solve("23-94-28,89-06-51"), 11);
}

BOOST_AUTO_TEST_CASE( solve_test2 ){
    BOOST_CHECK_EQUAL(solve("11-84-58,02-73-69"), 40);
}

BOOST_AUTO_TEST_CASE( solve_test3 ){
    BOOST_CHECK_EQUAL(solve("18-41-86,77-04-32"), 26);
}

BOOST_AUTO_TEST_CASE( solve_test4 ){
    BOOST_CHECK_EQUAL(solve("81-88-23,64-58-14"), 0);
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0