Artifact c1dd4b267114bf1ff7ada24fa27555a51e809f61
#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>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;
class GreatExpectations {
public:
double expectedTime(vector <string> doors)
{
const int Y = doors.size()/2 +1;
const int X = doors[0].size() +1;
vector< vector<double> > e(Y, vector<double>(X, -1.0));
e[Y-1][X-1] = 0.0;
for(int k=0; k<X*Y; ++k)
for(int y=0; y<Y; ++y)
for(int x=0; x<X; ++x)
{
// neibours : (expected_time, door_prob)
vector< pair<double,double> > ep;
if( x+1<X && doors[y*2][x]!='0' && e[y][x+1]>=0 )
ep.push_back( make_pair(e[y][x+1], (doors[y*2][x]-'0')/8.0) );
if( 0<x && doors[y*2][x-1]!='0' && e[y][x-1]>=0 )
ep.push_back( make_pair(e[y][x-1], (doors[y*2][x-1]-'0')/8.0) );
if( y+1<Y && doors[y*2+1][x]!='0' && e[y+1][x]>=0 )
ep.push_back( make_pair(e[y+1][x], (doors[y*2+1][x]-'0')/8.0) );
if( 0<y && doors[y*2-1][x]!='0' && e[y-1][x]>=0 )
ep.push_back( make_pair(e[y-1][x], (doors[y*2-1][x]-'0')/8.0) );
if( ep.empty() ) continue;
sort( ep.begin(), ep.end() ); // better first
// choose k that minimizes the following E:
// E = (1+e[0])*p[0]
// + (1+e[1])*(1-p[0])*p[1]
// + (1+e[2])*(1-p[0])*(1-p[1])*p[2]
// + ...
// + (1+E) *(1-p[0])*(1-p[1])*...*(1-p[k])
double r=1.0, s=0.0, E=1e+50;
for(int i=0; i<ep.size(); ++i) {
double s2 = s + r * ep[i].second * (1 + ep[i].first);
double r2 = r * (1 - ep[i].second);
double E2 = (s2 + r2) / (1-r2);
if( E2 > E ) break;
r=r2, s=s2, E=E2;
}
if( e[y][x]<0 || e[y][x]>E )
e[y][x]=E;
}
return e[0][0];
}
};
// 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> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
int verify_case(const double &Expected, const double &Received) { double diff = Expected - Received; if (diff < 0) diff = -diff; if (diff < 1e-9) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;}
template<int N> struct Case_ { Case_(){start_time=clock();} };
char Test_(...);
int Test_(Case_<0>) {
string doors_[] = { "4",
"44",
"4" };
vector <string> doors(doors_, doors_+sizeof(doors_)/sizeof(*doors_));
double RetVal = 3.3333333333333335;
return verify_case(RetVal, GreatExpectations().expectedTime(doors)); }
int Test_(Case_<1>) {
string doors_[] = { "48",
"440",
"42",
"862",
"06" };
vector <string> doors(doors_, doors_+sizeof(doors_)/sizeof(*doors_));
double RetVal = 6.0;
return verify_case(RetVal, GreatExpectations().expectedTime(doors)); }
int Test_(Case_<2>) {
string doors_[] = { "0808",
"88888",
"0000",
"88888",
"8080" }
;
vector <string> doors(doors_, doors_+sizeof(doors_)/sizeof(*doors_));
double RetVal = 14.0;
return verify_case(RetVal, GreatExpectations().expectedTime(doors)); }
int Test_(Case_<3>) {
string doors_[] = { "2815",
"67686",
"1324",
"75767",
"6051" }
;
vector <string> doors(doors_, doors_+sizeof(doors_)/sizeof(*doors_));
double RetVal = 11.852782338798935;
return verify_case(RetVal, GreatExpectations().expectedTime(doors)); }
int Test_(Case_<4>) {
string doors_[] = { "823",
"2630",
"130" }
;
vector <string> doors(doors_, doors_+sizeof(doors_)/sizeof(*doors_));
double RetVal = -1.0;
return verify_case(RetVal, GreatExpectations().expectedTime(doors)); }
int Test_(Case_<5>) {
string doors_[] = { "0",
"44",
"4" }
;
vector <string> doors(doors_, doors_+sizeof(doors_)/sizeof(*doors_));
double RetVal = 4.0;
return verify_case(RetVal, GreatExpectations().expectedTime(doors)); }
template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); }
template<> void Run_<-1>() {}
int main() { Run_<0>(); }
// END CUT HERE