Skip to content

Instantly share code, notes, and snippets.

@kusano
Created May 11, 2013 18:18
Show Gist options
  • Save kusano/5560870 to your computer and use it in GitHub Desktop.
Save kusano/5560870 to your computer and use it in GitHub Desktop.
#include <string>
#include <vector>
using namespace std;
// BEGIN CUT HERE
#include <iostream>
#include <sstream>
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v);
// END CUT HERE
vector<string> F;
int n;
vector<bool> V;
int BT( int p )
{
V[p] = true;
if ( p==1 )
return 0;
int ret = 9999;
for ( int i=0; i<n; i++ )
if ( F[p][i]=='Y' && !V[i] )
ret = min( ret, BT(i)+1 );
return ret;
}
class DancingFoxes{public:
int minimalDays( vector <string> friendship )
{
F = friendship;
n = (int)friendship.size();
V = vector<bool>(n);
int d = BT(0);
if ( d==9999 )
return -1;
int ans = 0;
int x = 1;
while ( x<d )
{
x += (x+1)/2;
ans++;
}
return ans;
}};
// 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(_, DancingFoxes().minimalDays(friendship));}
int main(){
CASE(0)
string friendship_[] = {"NNY",
"NNY",
"YYN"};
vector <string> friendship(friendship_, friendship_+sizeof(friendship_)/sizeof(*friendship_));
int _ = 1;
END
CASE(1)
string friendship_[] = {"NNNNN",
"NNYYY",
"NYNYY",
"NYYNY",
"NYYYN"};
vector <string> friendship(friendship_, friendship_+sizeof(friendship_)/sizeof(*friendship_));
int _ = -1;
END
CASE(2)
string friendship_[] = {"NNYYNN",
"NNNNYY",
"YNNNYN",
"YNNNNY",
"NYYNNN",
"NYNYNN"};
vector <string> friendship(friendship_, friendship_+sizeof(friendship_)/sizeof(*friendship_));
int _ = 2;
END
CASE(3)
string friendship_[] = {"NNYNNNNYN",
"NNNNYNYNN",
"YNNYNYNNN",
"NNYNYNYYN",
"NYNYNNNNY",
"NNYNNNYNN",
"NYNYNYNNN",
"YNNYNNNNY",
"NNNNYNNYN"};
vector <string> friendship(friendship_, friendship_+sizeof(friendship_)/sizeof(*friendship_));
int _ = 3;
END
CASE(4)
string friendship_[] = {"NY",
"YN"};
vector <string> friendship(friendship_, friendship_+sizeof(friendship_)/sizeof(*friendship_));
int _ = 0;
END
}
// END CUT HERE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment