Artifact Content
Not logged in

Artifact 6617dfc0554ca88522877f050130fc2dc2dc11c3


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef complex<double> CMP;

const int dy[] = {-1,+1,0,0};
const int dx[] = {0,0,-1,+1};

template<typename T>
class IdGen
{
	map<T, int> v2id_;
	vector<T>   id2v_;
public:
	int v2id(const T& v) {
		if( !v2id_.count(v) ) { v2id_[v] = size(); id2v_.push_back(v); }
		return v2id_[v];
	}
	const T& id2v(int i) const { return id2v_[i]; }
	int size() const { return id2v_.size(); }
};

template<typename Vert, typename Flow, int NV=2048>
class MaxFlow
{
	IdGen<Vert> idgen;
	vector<int> G[NV];
	Flow F[NV][NV];

public:
	void addEdge( Vert s_, Vert t_, Flow f )
	{
		const int s = idgen.v2id(s_), t = idgen.v2id(t_);
		G[s].push_back(t);
		G[t].push_back(s);
		F[s][t] = f;
		F[t][s] = 0;
	}

	Flow calc( Vert s_, Vert t_ )
	{
		const int S = idgen.v2id(s_), D = idgen.v2id(t_);
		for( Flow total=0 ;; ) {
			// Do BFS and compute the level for each node.
			int LV[NV] = {0};
			vector<int> Q(1, S);
			for(int lv=1; !Q.empty(); ++lv) {
				vector<int> Q2;
				for(size_t i=0; i!=Q.size(); ++i) {
					const vector<int>& ne = G[Q[i]];
					for(size_t j=0; j!=ne.size(); ++j)
						if( F[Q[i]][ne[j]] && !LV[ne[j]] && ne[j]!=S )
							LV[ne[j]]=lv, Q2.push_back(ne[j]);
				}
				Q.swap(Q2);
			}

			// Destination is now unreachable. Done.
			if( !LV[D] )
				return total;

			// Iterating DFS.
			bool blocked[NV] = {};
			total += dinic_dfs( S, D, LV, 0x7fffffff, blocked );
		}
	}

private:
	Flow dinic_dfs( int v, int D, int LV[], Flow flow_in, bool blocked[] )
	{
		Flow flow_out = 0;
		for(size_t i=0; i!=G[v].size(); ++i) {
			int u = G[v][i];
			if( LV[v]+1==LV[u] && F[v][u] ) {
				Flow f = min(flow_in-flow_out, F[v][u]);
				if( u==D || !blocked[u] && (f=dinic_dfs(u,D,LV,f,blocked))>0 ) {
					F[v][u]  -= f;
					F[u][v]  += f;
					flow_out += f;
					if( flow_in == flow_out ) return flow_out;
				}
			}
		}
		blocked[v] = (flow_out==0);
		return flow_out;
	}
};

class Block3Checkers { public:
	int blockThem(vector <string> board)
	{
		int H = board.size();
		int W = board[0].size();
		for(int y=0; y<H; ++y)
		for(int x=0; x<W; ++x)
			if(board[y][x] == 'A') {
				for(int d=0; d<4; ++d) {
					int xx = x+dx[d];
					int yy = y+dy[d];
					if(0<=xx&&xx<W&&0<=yy&&yy<H&&board[yy][xx]=='A')
						return 100;
				}
			}

		vector<pair<int,int> > ps, as;
		for(int y=0; y<H; ++y)
		for(int x=0; x<W; ++x)
			if(board[y][x]=='.')
				ps.push_back(make_pair(y,x));
			else if(board[y][x]=='A')
				as.push_back(make_pair(y,x));

		vector<vector<int> > rf;
		if(ok(board,as,rf))
			return 0;
		for(int i=0; i<ps.size(); ++i) {
			int y = ps[i].first;
			int x = ps[i].second;
			board[y][x] = 'N';
			if(ok(board,as,rf))
				return 1;
			board[y][x] = '.';
		}
		int best = 6;
		for(int i=0; i<ps.size(); ++i)
		for(int k=i+1; k<ps.size(); ++k) {
			int y1 = ps[i].first;
			int x1 = ps[i].second;
			int y2 = ps[k].first;
			int x2 = ps[k].second;
			board[y1][x1] = 'N';
			board[y2][x2] = 'N';
			if(ok(board,as,rf))
				return 2;
			if(rf[as[0].first][as[0].first] == (1<<0))
				best = min(best, 2+split(board, as[1], as[2]));
			if(rf[as[1].first][as[1].first] == (1<<1))
				best = min(best, 2+split(board, as[2], as[0]));
			if(rf[as[2].first][as[2].first] == (1<<2))
				best = min(best, 2+split(board, as[0], as[1]));
			board[y1][x1] = '.';
			board[y2][x2] = '.';
		}

		return best;
	}

	bool ok(const vector<string>& B, vector<pair<int,int> >& as, vector<vector<int> >& rf)
	{
		int H = B.size();
		int W = B[0].size();
		rf.assign(H, vector<int>(W, 0));
		for(int i=0; i<as.size(); ++i) 
		{
			int ay = as[i].first;
			int ax = as[i].second;
			rf[ay][ax] |= (1<<i);
			queue<pair<int,int> > q;
			q.push(as[i]);
			while(!q.empty()) {
				int y = q.front().first;
				int x = q.front().second;
				q.pop();
				for(int d=0; d<4; ++d) {
					int yy = y+dy[d];
					int xx = x+dx[d];
					if(0<=yy&&yy<H&&0<=xx&&xx<W&&B[y][x]!='N'&&!(rf[yy][xx]&(1<<i))) {
						rf[yy][xx] |= 1<<i;
						q.push(make_pair(yy,xx));
					}
				}
			}
		}
		for(int i=0; i<as.size(); ++i)
			if(rf[as[i].first][as[i].second] != (1<<i))
				return false;
		return true;
	}

	int split(const vector<string>& B, pair<int,int> S, pair<int,int> D)
	{
		int OUT = 0, IN = 1;
		typedef pair<int, pair<int,int> > Vert;
		MaxFlow<Vert, int>* mf = new MaxFlow<Vert,int>;
		int H = B.size();
		int W = B[0].size();
		for(int y=0; y<H; ++y)
		for(int x=0; x<W; ++x)
			if(B[y][x] != 'N') {
				pair<int,int> p(y,x);
				for(int d=0; d<4; ++d) {
					int xx = x+dx[d];
					int yy = y+dy[d];
					if(0<=xx&&xx<W&&0<=yy&&yy<H&&B[yy][xx]!='N')
						mf->addEdge(make_pair(OUT, p), make_pair(IN, make_pair(yy,xx)), 1);
				}
				if(p!=S && p!=D)
					mf->addEdge(make_pair(IN, p), make_pair(OUT, p), 1);
			}
		int m = mf->calc(make_pair(OUT,S), make_pair(IN,D));
		delete mf;
		return m;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, Block3Checkers().blockThem(board));}
int main(){

CASE(0)
	string board_[] = {"A......A",
 "...N.N..",
 ".NNN.NN.",
 "NNNN.N.N",
 "N.NN.NNN",
 ".NNN.NNN",
 "NNN...NN",
 ".NN.A..N"};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int _ = 1; 
END
CASE(1)
	string board_[] = {".....A",
 "......",
 "......",
 "NNNNNN",
 "A.....",
 "A....."};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int _ = 100; 
END
CASE(2)
	string board_[] = {"A.N",
 "NNA",
 "AN."};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int _ = 0; 
END
CASE(3)
	string board_[] = {"......NA",
 ".NNNN.N.",
 ".N......",
 "....NNNN",
 "........",
 ".N..NNN.",
 "......N.",
 "A.N....A"}
;
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int _ = 3; 
END
/*
CASE(4)
	string board_[] = ;
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int _ = ; 
END
CASE(5)
	string board_[] = ;
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int _ = ; 
END
*/
}
// END CUT HERE