Artifact Content
Not logged in

Artifact e66e79bfc0293cc68080e3e717a3a09e4e69b8b2


#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;

class DropCoins { public:
	int getMinimum(vector <string> board, int K)
	{
		static const int INF = 0x3fffffff;
		int best = INF;
		int H = board.size();
		int W = board[0].size();
		vector< vector<int> > sum(H, vector<int>(W+1));
		for(int y=0; y<H; ++y)
			for(int x=0; x<W; ++x)
				sum[y][x+1] = sum[y][x] + (board[y][x]=='o' ? 1 : 0);

		for(int x=0; x<W; ++x)
			for(int X=x; X<=W; ++X)
				for(int y=0; y<H; ++y)
					for(int Y=y; Y<=H; ++Y)
					{
						// count
						int cnt = 0;
						for(int i=y; i<Y; ++i)
							cnt += sum[i][X]-sum[i][x];
						if( cnt == K )
						{
							int L = x;
							int R = W-X;
							int T = y;
							int B = H-Y;
							int move = L+R+min(L,R)+T+B+min(T,B);
							best = min(best, move);
						}
					}
		return best==INF ? -1 : best;
	}
};

// 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(_, DropCoins().getMinimum(board, K));}
int main(){

CASE(0)
	string board_[] = {".o.."
,"oooo"
,"..o."}
;
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int K = 3; 
	int _ = 2; 
END
CASE(1)
	string board_[] = {".....o"
,"......"
,"oooooo"
,"oooooo"
,"......"
,"o....."}
;
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int K = 12; 
	int _ = 3; 
END
CASE(2)
	string board_[] = {"...."
,".oo."
,".oo."
,"...."}
;
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int K = 3; 
	int _ = -1; 
END
CASE(3)
	string board_[] = {"......."
,"..ooo.."
,"ooooooo"
,".oo.oo."
,"oo...oo"}
;
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int K = 12; 
	int _ = 4; 
END
CASE(4)
	string board_[] = {"................."
,".ooooooo...oooo.."
,".ooooooo..oooooo."
,".oo.......oo..oo."
,".oo.......oo..oo."
,".ooooo.....oooo.."
,".ooooooo...oooo.."
,".....ooo..oo..oo."
,"......oo..oo..oo."
,".ooooooo..oooooo."
,".oooooo....oooo.."
,"................."}
;
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int K = 58; 
	int _ = 6; 
END
CASE(5)
	string board_[] = {"oooooooooooooooooooooooooooooo",
	"oooooooooooooooooooooooooooooo",
	"oooooooooooooooooooooooooooooo",
	"oooooooooooooooooooooooooooooo",
	"oooooooooooooooooooooooooooooo",
	"oooooooooooooooooooooooooooooo",
	"oooooooooooooooooooooooooooooo",
	"oooooooooooooooooooooooooooooo",
	"oooooooooooooooooooooooooooooo",
	"oooooooooooooooooooooooooooooo",
	"oooooooooooooooooooooooooooooo",
	"oooooooooooooooooooooooooooooo",
	"oooooooooooooooooooooooooooooo",
	"oooooooooooooooooooooooooooooo",
	"oooooooooooooooooooooooooooooo",
	"oooooooooooooooooooooooooooooo",
	"oooooooooooooooooooooooooooooo",
	"oooooooooooooooooooooooooooooo",
	"oooooooooooooooooooooooooooooo",
	"oooooooooooooooooooooooooooooo",
	"oooooooooooooooooooooooooooooo",
	"oooooooooooooooooooooooooooooo",
	"oooooooooooooooooooooooooooooo",
	"oooooooooooooooooooooooooooooo",
	"oooooooooooooooooooooooooooooo",
	"oooooooooooooooooooooooooooooo",
	"oooooooooooooooooooooooooooooo",
	"oooooooooooooooooooooooooooooo",
	"oooooooooooooooooooooooooooooo",
	"oooooooooooooooooooooooooooooo",
};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int K = 1; 
	int _ = 58; 
END
/*
CASE(6)
	string board_[] = ;
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int K = ; 
	int _ = ; 
END
*/
}
// END CUT HERE