4fd800b3a8 2011-02-23 kinaba: #include <iostream> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <iomanip> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <map> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <algorithm> 4fd800b3a8 2011-02-23 kinaba: #include <numeric> 4fd800b3a8 2011-02-23 kinaba: #include <iterator> 4fd800b3a8 2011-02-23 kinaba: #include <complex> 4fd800b3a8 2011-02-23 kinaba: #include <queue> 4fd800b3a8 2011-02-23 kinaba: #include <stack> 4fd800b3a8 2011-02-23 kinaba: #include <cmath> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int dy[] = {-1,1, 0,0,-1,-1, 1,1}; 4fd800b3a8 2011-02-23 kinaba: int dx[] = { 0,0,-1,1,-1, 1,-1,1}; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class LandAndSea 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: vector<int> howManyIslands(vector<string> seaMap) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: const int Y = seaMap.size(); 4fd800b3a8 2011-02-23 kinaba: const int X = seaMap[0].size(); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // paint 4fd800b3a8 2011-02-23 kinaba: vector< vector<int> > idMap(Y, vector<int>(X, -1)); 4fd800b3a8 2011-02-23 kinaba: int nIsland = 0; 4fd800b3a8 2011-02-23 kinaba: for(int y=0; y<Y; ++y) 4fd800b3a8 2011-02-23 kinaba: for(int x=0; x<X; ++x) 4fd800b3a8 2011-02-23 kinaba: if( seaMap[y][x]=='x' && idMap[y][x]==-1 ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int id = (idMap[y][x] = nIsland++); 4fd800b3a8 2011-02-23 kinaba: stack< pair<int,int> > S; 4fd800b3a8 2011-02-23 kinaba: S.push( make_pair(y,x) ); 4fd800b3a8 2011-02-23 kinaba: while( !S.empty() ) { 4fd800b3a8 2011-02-23 kinaba: pair<int,int> p = S.top(); S.pop(); 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<8; ++i) { 4fd800b3a8 2011-02-23 kinaba: int yy = p.first +dy[i]; 4fd800b3a8 2011-02-23 kinaba: int xx = p.second+dx[i]; 4fd800b3a8 2011-02-23 kinaba: if( 0<=yy && yy<Y && 0<=xx && xx<X ) 4fd800b3a8 2011-02-23 kinaba: if( seaMap[yy][xx]=='x' && idMap[yy][xx]==-1 ) 4fd800b3a8 2011-02-23 kinaba: idMap[yy][xx] = id, S.push( make_pair(yy,xx) ); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // I is contained by J 4fd800b3a8 2011-02-23 kinaba: vector<int> refered(nIsland); 4fd800b3a8 2011-02-23 kinaba: vector<int> ct(nIsland, -1); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: set<int> outer_world; 4fd800b3a8 2011-02-23 kinaba: while( outer_world.size() < nIsland ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: set<int> outer2; 4fd800b3a8 2011-02-23 kinaba: for(int I=0; I<nIsland; ++I) 4fd800b3a8 2011-02-23 kinaba: if( !outer_world.count(I) ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: stack< pair<int,int> > S; 4fd800b3a8 2011-02-23 kinaba: set< pair<int,int> > vis; 4fd800b3a8 2011-02-23 kinaba: for(int y=0; y<Y; ++y) 4fd800b3a8 2011-02-23 kinaba: for(int x=0; x<X; ++x) 4fd800b3a8 2011-02-23 kinaba: if( idMap[y][x] == I ) 4fd800b3a8 2011-02-23 kinaba: S.push( make_pair(y,x) ), vis.insert(S.top()); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int sID = -1; 4fd800b3a8 2011-02-23 kinaba: while( !S.empty() ) { 4fd800b3a8 2011-02-23 kinaba: pair<int,int> p = S.top(); S.pop(); 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<4; ++i) { 4fd800b3a8 2011-02-23 kinaba: int yy = p.first+dy[i]; 4fd800b3a8 2011-02-23 kinaba: int xx = p.second+dx[i]; 4fd800b3a8 2011-02-23 kinaba: if( !(0<=yy && yy<Y && 0<=xx && xx<X) ) 4fd800b3a8 2011-02-23 kinaba: goto notContained; 4fd800b3a8 2011-02-23 kinaba: int ssID = idMap[yy][xx]; 4fd800b3a8 2011-02-23 kinaba: if( outer_world.count(ssID) ) 4fd800b3a8 2011-02-23 kinaba: {sID=ssID; goto notContained;} 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: pair<int,int> pp(yy,xx); 4fd800b3a8 2011-02-23 kinaba: if( !vis.count(pp) && seaMap[yy][xx]=='.' ) 4fd800b3a8 2011-02-23 kinaba: S.push(pp), vis.insert(S.top()); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: continue; 4fd800b3a8 2011-02-23 kinaba: notContained: 4fd800b3a8 2011-02-23 kinaba: outer2.insert(I); 4fd800b3a8 2011-02-23 kinaba: if(sID != -1) { 4fd800b3a8 2011-02-23 kinaba: ct[I] = sID; 4fd800b3a8 2011-02-23 kinaba: refered[sID]++; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: outer_world.insert(outer2.begin(), outer2.end()); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // topological sort 4fd800b3a8 2011-02-23 kinaba: vector<int> level(nIsland, -1); 4fd800b3a8 2011-02-23 kinaba: for(int L=0;;++L) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: vector<int> victim; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: for(int I=0; I<nIsland; ++I) 4fd800b3a8 2011-02-23 kinaba: if( level[I] == -1 && refered[I] == 0 ) 4fd800b3a8 2011-02-23 kinaba: level[I] = L, victim.push_back(I); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: if( victim.size() == 0 ) 4fd800b3a8 2011-02-23 kinaba: break; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<victim.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int J = ct[victim[i]]; 4fd800b3a8 2011-02-23 kinaba: if( J != -1 ) 4fd800b3a8 2011-02-23 kinaba: refered[J]--; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector<int> ans; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<level.size(); ++i) { 4fd800b3a8 2011-02-23 kinaba: if( level[i]+1 > ans.size() ) 4fd800b3a8 2011-02-23 kinaba: ans.resize( level[i]+1 ); 4fd800b3a8 2011-02-23 kinaba: ans[level[i]]++; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return ans; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); } 4fd800b3a8 2011-02-23 kinaba: private: 4fd800b3a8 2011-02-23 kinaba: template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: void verify_case(int Case, const vector <int> &Expected, const vector <int> &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(Received) << endl; } } 4fd800b3a8 2011-02-23 kinaba: void test_case_0() { string Arr0[] = {"x"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1 }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(0, Arg1, howManyIslands(Arg0)); } 4fd800b3a8 2011-02-23 kinaba: void test_case_1() { string Arr0[] = { 4fd800b3a8 2011-02-23 kinaba: "xxxxx", 4fd800b3a8 2011-02-23 kinaba: "x...x", 4fd800b3a8 2011-02-23 kinaba: "x.x.x", 4fd800b3a8 2011-02-23 kinaba: "x...x", 4fd800b3a8 2011-02-23 kinaba: "xxxxx" 4fd800b3a8 2011-02-23 kinaba: }; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1, 1 }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(1, Arg1, howManyIslands(Arg0)); } 4fd800b3a8 2011-02-23 kinaba: void test_case_2() { string Arr0[] = { 4fd800b3a8 2011-02-23 kinaba: "xxxxx", 4fd800b3a8 2011-02-23 kinaba: "x...x", 4fd800b3a8 2011-02-23 kinaba: "x.x.x", 4fd800b3a8 2011-02-23 kinaba: "x...x", 4fd800b3a8 2011-02-23 kinaba: "xxxxx", 4fd800b3a8 2011-02-23 kinaba: "xxxxx", 4fd800b3a8 2011-02-23 kinaba: "x...x", 4fd800b3a8 2011-02-23 kinaba: "x.x.x", 4fd800b3a8 2011-02-23 kinaba: "x...x", 4fd800b3a8 2011-02-23 kinaba: "xxxxx" 4fd800b3a8 2011-02-23 kinaba: }; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {2, 1 }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(2, Arg1, howManyIslands(Arg0)); } 4fd800b3a8 2011-02-23 kinaba: void test_case_3() { string Arr0[] = { 4fd800b3a8 2011-02-23 kinaba: "..", 4fd800b3a8 2011-02-23 kinaba: ".." 4fd800b3a8 2011-02-23 kinaba: }; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); vector <int> Arg1; verify_case(3, Arg1, howManyIslands(Arg0)); } 4fd800b3a8 2011-02-23 kinaba: void test_case_4() { string Arr0[] = { 4fd800b3a8 2011-02-23 kinaba: "............", 4fd800b3a8 2011-02-23 kinaba: ".......xxxx.", 4fd800b3a8 2011-02-23 kinaba: "..xxx.x...x.", 4fd800b3a8 2011-02-23 kinaba: "..x..x..x.x.", 4fd800b3a8 2011-02-23 kinaba: "..x.x.x...x.", 4fd800b3a8 2011-02-23 kinaba: "..xx...xxx.." 4fd800b3a8 2011-02-23 kinaba: }; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1, 1 }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(4, Arg1, howManyIslands(Arg0)); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: int main() { LandAndSea().run_test(-1); } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE