0048d1d21b 2013-11-01 kinaba: #include <iostream> 0048d1d21b 2013-11-01 kinaba: #include <sstream> 0048d1d21b 2013-11-01 kinaba: #include <iomanip> 0048d1d21b 2013-11-01 kinaba: #include <vector> 0048d1d21b 2013-11-01 kinaba: #include <string> 0048d1d21b 2013-11-01 kinaba: #include <map> 0048d1d21b 2013-11-01 kinaba: #include <set> 0048d1d21b 2013-11-01 kinaba: #include <algorithm> 0048d1d21b 2013-11-01 kinaba: #include <numeric> 0048d1d21b 2013-11-01 kinaba: #include <iterator> 0048d1d21b 2013-11-01 kinaba: #include <functional> 0048d1d21b 2013-11-01 kinaba: #include <complex> 0048d1d21b 2013-11-01 kinaba: #include <queue> 0048d1d21b 2013-11-01 kinaba: #include <stack> 0048d1d21b 2013-11-01 kinaba: #include <cmath> 0048d1d21b 2013-11-01 kinaba: #include <cassert> 0048d1d21b 2013-11-01 kinaba: #include <tuple> 0048d1d21b 2013-11-01 kinaba: using namespace std; 0048d1d21b 2013-11-01 kinaba: typedef long long LL; 0048d1d21b 2013-11-01 kinaba: typedef complex<double> CMP; 0048d1d21b 2013-11-01 kinaba: 0048d1d21b 2013-11-01 kinaba: typedef int vert; 0048d1d21b 2013-11-01 kinaba: typedef vert edge; 0048d1d21b 2013-11-01 kinaba: typedef vector<edge> edges; 0048d1d21b 2013-11-01 kinaba: typedef vector<edges> graph; 0048d1d21b 2013-11-01 kinaba: 0048d1d21b 2013-11-01 kinaba: bool augment( graph& G, int v, vector<vert>& matchTo, bool visited[] ) 0048d1d21b 2013-11-01 kinaba: { 0048d1d21b 2013-11-01 kinaba: for(int i=0; i<G[v].size(); ++i) { 0048d1d21b 2013-11-01 kinaba: vert u = G[v][i]; 0048d1d21b 2013-11-01 kinaba: if( visited[u] ) continue; 0048d1d21b 2013-11-01 kinaba: visited[u] = true; 0048d1d21b 2013-11-01 kinaba: 0048d1d21b 2013-11-01 kinaba: if( matchTo[u]<0 || augment(G, matchTo[u], matchTo, visited) ) 0048d1d21b 2013-11-01 kinaba: { matchTo[v]=u, matchTo[u]=v; return true; } 0048d1d21b 2013-11-01 kinaba: } 0048d1d21b 2013-11-01 kinaba: return false; 0048d1d21b 2013-11-01 kinaba: } 0048d1d21b 2013-11-01 kinaba: 0048d1d21b 2013-11-01 kinaba: template<int NV> 0048d1d21b 2013-11-01 kinaba: int biMatch( graph& G, int L ) // [0,L):left, [L,?):right 0048d1d21b 2013-11-01 kinaba: // only left->right edges are used during computation 0048d1d21b 2013-11-01 kinaba: { 0048d1d21b 2013-11-01 kinaba: vector<vert> matchTo(G.size(), -1); 0048d1d21b 2013-11-01 kinaba: int ans = 0; 0048d1d21b 2013-11-01 kinaba: for(vert v=0; v<L; ++v) { 0048d1d21b 2013-11-01 kinaba: bool visited[NV] = {}; 0048d1d21b 2013-11-01 kinaba: if( augment(G, v, matchTo, visited) ) 0048d1d21b 2013-11-01 kinaba: ++ans; 0048d1d21b 2013-11-01 kinaba: } 0048d1d21b 2013-11-01 kinaba: return ans; 0048d1d21b 2013-11-01 kinaba: } 0048d1d21b 2013-11-01 kinaba: 0048d1d21b 2013-11-01 kinaba: class FoxAndGo3 { public: 0048d1d21b 2013-11-01 kinaba: int maxEmptyCells(vector <string> board) 0048d1d21b 2013-11-01 kinaba: { 0048d1d21b 2013-11-01 kinaba: const int N = board.size(); 0048d1d21b 2013-11-01 kinaba: 0048d1d21b 2013-11-01 kinaba: vector< vector<int> > ID(N, vector<int>(N)); 0048d1d21b 2013-11-01 kinaba: int Nblank=0, Nwhite=0; 0048d1d21b 2013-11-01 kinaba: for(int y=0; y<N; ++y) 0048d1d21b 2013-11-01 kinaba: for(int x=0; x<N; ++x) 0048d1d21b 2013-11-01 kinaba: if(board[y][x]=='.') 0048d1d21b 2013-11-01 kinaba: Nblank++; 0048d1d21b 2013-11-01 kinaba: else if(board[y][x]=='o') 0048d1d21b 2013-11-01 kinaba: Nwhite++; 0048d1d21b 2013-11-01 kinaba: int IDblank = 0, IDwhite = Nblank; 0048d1d21b 2013-11-01 kinaba: for(int y=0; y<N; ++y) 0048d1d21b 2013-11-01 kinaba: for(int x=0; x<N; ++x) 0048d1d21b 2013-11-01 kinaba: if(board[y][x]=='.') 0048d1d21b 2013-11-01 kinaba: ID[y][x] = IDblank++; 0048d1d21b 2013-11-01 kinaba: else if(board[y][x]=='o') 0048d1d21b 2013-11-01 kinaba: ID[y][x] = IDwhite++; 0048d1d21b 2013-11-01 kinaba: 0048d1d21b 2013-11-01 kinaba: const int dy[] = {-1,+1,0,0}; 0048d1d21b 2013-11-01 kinaba: const int dx[] = {0,0,-1,+1}; 0048d1d21b 2013-11-01 kinaba: graph G(Nblank+Nwhite); 0048d1d21b 2013-11-01 kinaba: for(int y=0; y<N; ++y) 0048d1d21b 2013-11-01 kinaba: for(int x=0; x<N; ++x) 0048d1d21b 2013-11-01 kinaba: if(board[y][x]=='o') 0048d1d21b 2013-11-01 kinaba: for(int d=0; d<4; ++d) { 0048d1d21b 2013-11-01 kinaba: int yy = y+dy[d], xx = x+dx[d]; 0048d1d21b 2013-11-01 kinaba: if(0<=yy&&yy<N&&0<=xx&&xx<N&&board[yy][xx]=='.') 0048d1d21b 2013-11-01 kinaba: G[ID[yy][xx]].push_back(ID[y][x]); 0048d1d21b 2013-11-01 kinaba: } 0048d1d21b 2013-11-01 kinaba: return Nblank + Nwhite - biMatch<2500>(G, Nblank); 0048d1d21b 2013-11-01 kinaba: } 0048d1d21b 2013-11-01 kinaba: }; 0048d1d21b 2013-11-01 kinaba: 0048d1d21b 2013-11-01 kinaba: // BEGIN CUT HERE 0048d1d21b 2013-11-01 kinaba: #include <ctime> 0048d1d21b 2013-11-01 kinaba: double start_time; string timer() 0048d1d21b 2013-11-01 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 0048d1d21b 2013-11-01 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 0048d1d21b 2013-11-01 kinaba: { os << "{ "; 0048d1d21b 2013-11-01 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 0048d1d21b 2013-11-01 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 0048d1d21b 2013-11-01 kinaba: void verify_case(const int& Expected, const int& Received) { 0048d1d21b 2013-11-01 kinaba: bool ok = (Expected == Received); 0048d1d21b 2013-11-01 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 0048d1d21b 2013-11-01 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 0048d1d21b 2013-11-01 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 0048d1d21b 2013-11-01 kinaba: #define END verify_case(_, FoxAndGo3().maxEmptyCells(board));} 0048d1d21b 2013-11-01 kinaba: int main(){ 0048d1d21b 2013-11-01 kinaba: 0048d1d21b 2013-11-01 kinaba: CASE(0) 0048d1d21b 2013-11-01 kinaba: string board_[] = {"o.o", 0048d1d21b 2013-11-01 kinaba: ".o.", 0048d1d21b 2013-11-01 kinaba: "o.o"}; 0048d1d21b 2013-11-01 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 0048d1d21b 2013-11-01 kinaba: int _ = 5; 0048d1d21b 2013-11-01 kinaba: END 0048d1d21b 2013-11-01 kinaba: CASE(1) 0048d1d21b 2013-11-01 kinaba: string board_[] = {"...", 0048d1d21b 2013-11-01 kinaba: ".o.", 0048d1d21b 2013-11-01 kinaba: "..."} 0048d1d21b 2013-11-01 kinaba: ; 0048d1d21b 2013-11-01 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 0048d1d21b 2013-11-01 kinaba: int _ = 8; 0048d1d21b 2013-11-01 kinaba: END 0048d1d21b 2013-11-01 kinaba: CASE(2) 0048d1d21b 2013-11-01 kinaba: string board_[] = {"xxxxx", 0048d1d21b 2013-11-01 kinaba: "xxoxx", 0048d1d21b 2013-11-01 kinaba: "xo.ox", 0048d1d21b 2013-11-01 kinaba: "xxoxx", 0048d1d21b 2013-11-01 kinaba: "xxxxx"} 0048d1d21b 2013-11-01 kinaba: ; 0048d1d21b 2013-11-01 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 0048d1d21b 2013-11-01 kinaba: int _ = 4; 0048d1d21b 2013-11-01 kinaba: END 0048d1d21b 2013-11-01 kinaba: CASE(3) 0048d1d21b 2013-11-01 kinaba: string board_[] = {".xox.", 0048d1d21b 2013-11-01 kinaba: ".o.ox", 0048d1d21b 2013-11-01 kinaba: "x.o.o", 0048d1d21b 2013-11-01 kinaba: "ox.ox", 0048d1d21b 2013-11-01 kinaba: ".ox.."} 0048d1d21b 2013-11-01 kinaba: ; 0048d1d21b 2013-11-01 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 0048d1d21b 2013-11-01 kinaba: int _ = 12; 0048d1d21b 2013-11-01 kinaba: END 0048d1d21b 2013-11-01 kinaba: CASE(4) 0048d1d21b 2013-11-01 kinaba: string board_[] = {"o.o.o", 0048d1d21b 2013-11-01 kinaba: ".ox..", 0048d1d21b 2013-11-01 kinaba: "oxxxo", 0048d1d21b 2013-11-01 kinaba: "..x..", 0048d1d21b 2013-11-01 kinaba: "o.o.o"} 0048d1d21b 2013-11-01 kinaba: ; 0048d1d21b 2013-11-01 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 0048d1d21b 2013-11-01 kinaba: int _ = 12; 0048d1d21b 2013-11-01 kinaba: END 0048d1d21b 2013-11-01 kinaba: CASE(5) 0048d1d21b 2013-11-01 kinaba: string board_[] = {"...", 0048d1d21b 2013-11-01 kinaba: "...", 0048d1d21b 2013-11-01 kinaba: "..."}; 0048d1d21b 2013-11-01 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 0048d1d21b 2013-11-01 kinaba: int _ = 9; 0048d1d21b 2013-11-01 kinaba: END 0048d1d21b 2013-11-01 kinaba: /* 0048d1d21b 2013-11-01 kinaba: CASE(6) 0048d1d21b 2013-11-01 kinaba: string board_[] = ; 0048d1d21b 2013-11-01 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 0048d1d21b 2013-11-01 kinaba: int _ = ; 0048d1d21b 2013-11-01 kinaba: END 0048d1d21b 2013-11-01 kinaba: CASE(7) 0048d1d21b 2013-11-01 kinaba: string board_[] = ; 0048d1d21b 2013-11-01 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 0048d1d21b 2013-11-01 kinaba: int _ = ; 0048d1d21b 2013-11-01 kinaba: END 0048d1d21b 2013-11-01 kinaba: */ 0048d1d21b 2013-11-01 kinaba: } 0048d1d21b 2013-11-01 kinaba: // END CUT HERE