4fd800b3a8 2011-02-23 kinaba: #include <iostream> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <iomanip> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <map> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <algorithm> 4fd800b3a8 2011-02-23 kinaba: #include <numeric> 4fd800b3a8 2011-02-23 kinaba: #include <iterator> 4fd800b3a8 2011-02-23 kinaba: #include <functional> 4fd800b3a8 2011-02-23 kinaba: #include <complex> 4fd800b3a8 2011-02-23 kinaba: #include <queue> 4fd800b3a8 2011-02-23 kinaba: #include <stack> 4fd800b3a8 2011-02-23 kinaba: #include <cmath> 4fd800b3a8 2011-02-23 kinaba: #include <cassert> 4fd800b3a8 2011-02-23 kinaba: #include <cstring> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: typedef complex<double> CMP; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: static const int dy[] = {-1,+1,0,0}; 4fd800b3a8 2011-02-23 kinaba: static const int dx[] = {0,0,-1,+1}; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class MazeWandering { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: double expectedTime(vector <string> maze) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int Y = maze.size(); 4fd800b3a8 2011-02-23 kinaba: int X = maze[0].size(); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector< vector<int> > d(Y, vector<int>(X, -1)); 4fd800b3a8 2011-02-23 kinaba: vector< pair<int,int> > q, Q; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: for(int y=0; y<Y; ++y) 4fd800b3a8 2011-02-23 kinaba: for(int x=0; x<X; ++x) 4fd800b3a8 2011-02-23 kinaba: if( maze[y][x] == '*' ) { 4fd800b3a8 2011-02-23 kinaba: d[y][x] = 0; 4fd800b3a8 2011-02-23 kinaba: q.push_back( make_pair(y,x) ); 4fd800b3a8 2011-02-23 kinaba: Q.push_back( make_pair(y,x) ); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: for(int step=1; !q.empty(); ++step) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: vector< pair<int,int> > q2; 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<q.size(); ++j) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int qy = q[j].first; 4fd800b3a8 2011-02-23 kinaba: int qx = q[j].second; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<4; ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int y = qy+dy[i], x = qx+dx[i]; 4fd800b3a8 2011-02-23 kinaba: if( 0<=y && y<Y && 0<=x && x<X && maze[y][x]=='.' && d[y][x]==-1 ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: d[y][x] = step; 4fd800b3a8 2011-02-23 kinaba: q2.push_back( make_pair(y,x) ); 4fd800b3a8 2011-02-23 kinaba: Q.push_back( make_pair(y,x) ); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: q2.swap(q); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector< vector<int> > w(Y, vector<int>(X, 0)); 4fd800b3a8 2011-02-23 kinaba: for(int j=Q.size()-1; j>0; --j) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int qy = Q[j].first; 4fd800b3a8 2011-02-23 kinaba: int qx = Q[j].second; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<4; ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int y = qy+dy[i], x = qx+dx[i]; 4fd800b3a8 2011-02-23 kinaba: if( 0<=y && y<Y && 0<=x && x<X && maze[y][x]!='X' && d[y][x] > d[qy][qx] ) 4fd800b3a8 2011-02-23 kinaba: w[qy][qx] += w[y][x] + 1; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: double esum = 0.0; 4fd800b3a8 2011-02-23 kinaba: vector< vector<double> > e(Y, vector<double>(X, 0.0)); 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<Q.size(); ++j) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int qy = Q[j].first; 4fd800b3a8 2011-02-23 kinaba: int qx = Q[j].second; 4fd800b3a8 2011-02-23 kinaba: if( d[qy][qx] > 0 ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<4; ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int y = qy+dy[i], x = qx+dx[i]; 4fd800b3a8 2011-02-23 kinaba: if( 0<=y && y<Y && 0<=x && x<X && maze[y][x]!='X' && d[y][x] < d[qy][qx] ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: e[qy][qx] = e[y][x] + 1 + 2*w[qy][qx]; 4fd800b3a8 2011-02-23 kinaba: esum += e[qy][qx]; 4fd800b3a8 2011-02-23 kinaba: break; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return esum / Q.size(); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: #include <ctime> 4fd800b3a8 2011-02-23 kinaba: double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: 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(); } 4fd800b3a8 2011-02-23 kinaba: 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;} 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> struct Case_ { Case_(){start_time=clock();} }; 4fd800b3a8 2011-02-23 kinaba: char Test_(...); 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<0>) { 4fd800b3a8 2011-02-23 kinaba: string maze_[] = {"*", 4fd800b3a8 2011-02-23 kinaba: "."}; 4fd800b3a8 2011-02-23 kinaba: vector <string> maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_)); 4fd800b3a8 2011-02-23 kinaba: double RetVal = 0.5; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, MazeWandering().expectedTime(maze)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<1>) { 4fd800b3a8 2011-02-23 kinaba: string maze_[] = {"*.."}; 4fd800b3a8 2011-02-23 kinaba: vector <string> maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_)); 4fd800b3a8 2011-02-23 kinaba: double RetVal = 2.3333333333333335; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, MazeWandering().expectedTime(maze)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<2>) { 4fd800b3a8 2011-02-23 kinaba: string maze_[] = {"...", 4fd800b3a8 2011-02-23 kinaba: "X*X", 4fd800b3a8 2011-02-23 kinaba: "..."}; 4fd800b3a8 2011-02-23 kinaba: vector <string> maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_)); 4fd800b3a8 2011-02-23 kinaba: double RetVal = 4.857142857142857; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, MazeWandering().expectedTime(maze)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<3>) { 4fd800b3a8 2011-02-23 kinaba: string maze_[] = {".*.", 4fd800b3a8 2011-02-23 kinaba: ".XX", 4fd800b3a8 2011-02-23 kinaba: "..."}; 4fd800b3a8 2011-02-23 kinaba: vector <string> maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_)); 4fd800b3a8 2011-02-23 kinaba: double RetVal = 13.714285714285714; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, MazeWandering().expectedTime(maze)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<4>) { 4fd800b3a8 2011-02-23 kinaba: string maze_[] = {"*........", 4fd800b3a8 2011-02-23 kinaba: "XXX.XXXX.", 4fd800b3a8 2011-02-23 kinaba: ".XX.X....", 4fd800b3a8 2011-02-23 kinaba: ".....XX.X"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_)); 4fd800b3a8 2011-02-23 kinaba: double RetVal = 167.2608695652174; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, MazeWandering().expectedTime(maze)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<5>) { 4fd800b3a8 2011-02-23 kinaba: string maze_[] = {"X*X"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_)); 4fd800b3a8 2011-02-23 kinaba: double RetVal = 0; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, MazeWandering().expectedTime(maze)); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); } 4fd800b3a8 2011-02-23 kinaba: template<> void Run_<-1>() {} 4fd800b3a8 2011-02-23 kinaba: int main() { Run_<0>(); } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE 4fd800b3a8 2011-02-23 kinaba: