Artifact Content
Not logged in

Artifact 73a81491ebb9328c2423ab0c17456addef9b53a7


#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>
#include <cstring>
#ifdef __GNUC__
#include <ext/hash_map>
#define unordered_map __gnu_cxx::hash_map
#else
#include <unordered_map>
#endif
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

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(); }
};

typedef int           vert;
typedef vert          edge;
typedef vector<edge>  edges;
typedef vector<edges> graph;

bool augment( graph& G, int v, vector<vert>& matchTo, bool visited[] )
{
	for(int i=0; i<G[v].size(); ++i) {
		vert u = G[v][i];
		if( visited[u] ) continue;
		visited[u] = true;

		if( matchTo[u]<0 || augment(G, matchTo[u], matchTo, visited) )
			{ matchTo[v]=u, matchTo[u]=v; return true; }
	}
	return false;
}

template<int NV>
int biMatch( graph& G, int L ) // [0,L):left, [L,?):right
    // only left->right edges are used during computation
{
	vector<vert> matchTo(G.size(), -1);
	int ans = 0;
	for(vert v=0; v<L; ++v) {
		bool visited[NV] = {};
		if( augment(G, v, matchTo, visited) )
			++ans;
	}
	return ans;
}

class FoxBomb { public:
	int getMinimumCost(vector <string> grid)
	{
		int H = grid.size();
		int W = grid[0].size();

		IdGen< pair< int,pair<int,int> > > hline;
		for(int y=0; y<H; ++y)
			for(int sx=0; sx<W; )
			{
				if( grid[y][sx] == '#' )
					{++sx; continue;}
				int ex = sx+1;
				for(; ex<W && grid[y][ex]=='.'; ++ex) {}
				if( sx+1 < ex )
					hline.v2id( make_pair(y, make_pair(sx,ex)) );
				sx = ex;
			}

		IdGen< pair< int,pair<int,int> > > vline;
		for(int x=0; x<W; ++x)
			for(int sy=0; sy<H; )
			{
				if( grid[sy][x] == '#' )
					{++sy; continue;}
				int ey = sy+1;
				for(; ey<H && grid[ey][x]=='.'; ++ey) {}
				if( sy+1 < ey )
					vline.v2id( make_pair(x, make_pair(sy,ey)) );
				sy = ey;
			}

		graph G(hline.size() + vline.size());
		for(int i=0; i<hline.size(); ++i)
			for(int k=0; k<vline.size(); ++k)
			{
				const pair< int,pair<int,int> >& hh = hline.id2v(i);
				const pair< int,pair<int,int> >& vv = vline.id2v(k);
				int y = hh.first;
				int sx = hh.second.first;
				int ex = hh.second.second;
				int x = vv.first;
				int sy = vv.second.first;
				int ey = vv.second.second;
				if( sx<=x && x<ex && sy<=y && y<ey )
					G[i].push_back(hline.size()+k);
			}

		int dots = 0;
		for(int y=0; y<H; ++y)
			for(int x=0; x<W; ++x) if( grid[y][x]=='.' )
			{
				int dy[]={-1,+1,0,0};
				int dx[]={0,0,-1,+1};
				bool non_wall = false;
				for(int i=0; i<4; ++i) {
					int yy = y+dy[i];
					int xx = x+dx[i];
					if(0<=yy&&yy<H&&0<=xx&&xx<W&&grid[yy][xx]=='.')
						non_wall = true;
				}
				if(!non_wall)
					dots++;
			}

		return dots + G.size() - biMatch<2500>(G, hline.size());
	}
};

// 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(_, FoxBomb().getMinimumCost(grid));}
int main(){

CASE(0)
	string grid_[] = {"#..."
,"..##"
,"#.##"};
	  vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); 
	int _ = 2; 
END
CASE(1)
	string grid_[] = {".#.#.#."
,"......."
,".#.#.#."};
	  vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); 
	int _ = 4; 
END
CASE(2)
	string grid_[] = {"######################################"
,"######################################"
,"###.....................##############"
,"###.###################.###....#...###"
,"###.###################.###.######.###"
,"###.###################.###.######.###"
,"###.###################.###.######.###"
,"###.###################.###.######.###"
,"###.###################.###.######.###"
,"###.........####........###.######.###"
,"###########.###########.###........###"
,"###########.###########.##########.###"
,"###########.###########.##########.###"
,"###########.###########.##########.###"
,"###########.###########.##########.###"
,"##..........##..........##########.###"
,"#######################............###"
,"######################################"};
	  vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); 
	int _ = 9; 
END
CASE(3)
	string grid_[] = {".#."
,"..."
,"#.#"
,"..."
,".#."}
;
	  vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); 
	int _ = 5; 
END
CASE(4)
	string grid_[] = {"."};
	  vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); 
	int _ = 1; 
END
CASE(5)
string grid_[] = {"..."};
	  vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); 
	int _ = 1; 
END
CASE(6)
string grid_[] = {".",".","."};
	  vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); 
	int _ = 1; 
END
CASE(7)
string grid_[] = {
	"...#.",
	"####.",
	"...#."};
	  vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); 
	int _ = 3; 
END
}
// END CUT HERE