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: class PiecesMover { public: 4fd800b3a8 2011-02-23 kinaba: typedef vector< pair<int,int> > state; 4fd800b3a8 2011-02-23 kinaba: int getMinimumMoves(vector <string> board) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: set<state> V; 4fd800b3a8 2011-02-23 kinaba: vector<state> Q(1, to_state(board)); 4fd800b3a8 2011-02-23 kinaba: V.insert( Q.front() ); 4fd800b3a8 2011-02-23 kinaba: for(int step=0; !Q.empty(); ++step) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: vector<state> Q_next; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<Q.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: const state& b = Q[i]; 4fd800b3a8 2011-02-23 kinaba: if( is_connected(b) ) 4fd800b3a8 2011-02-23 kinaba: return step; 4fd800b3a8 2011-02-23 kinaba: const vector<state>& next = next_states(b); 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<next.size(); ++j) 4fd800b3a8 2011-02-23 kinaba: if( V.insert(next[j]).second ) 4fd800b3a8 2011-02-23 kinaba: Q_next.push_back(next[j]); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: Q.swap(Q_next); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: assert(false); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: state to_state( const vector<string>& b ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: state s; 4fd800b3a8 2011-02-23 kinaba: for(int y=0; y<5; ++y) 4fd800b3a8 2011-02-23 kinaba: for(int x=0; x<5; ++x) 4fd800b3a8 2011-02-23 kinaba: if( b[y][x] == '*' ) 4fd800b3a8 2011-02-23 kinaba: s.push_back( make_pair(y,x) ); 4fd800b3a8 2011-02-23 kinaba: sort( s.begin(), s.end() ); 4fd800b3a8 2011-02-23 kinaba: return s; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: bool is_connected( const state& s ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int N = s.size(); 4fd800b3a8 2011-02-23 kinaba: vector< vector<int> > G(N, vector<int>(N)); 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<N; ++i) 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<N; ++j) 4fd800b3a8 2011-02-23 kinaba: if( i == j ) 4fd800b3a8 2011-02-23 kinaba: G[i][j] = 0; 4fd800b3a8 2011-02-23 kinaba: else 4fd800b3a8 2011-02-23 kinaba: G[i][j] = (abs(s[i].first-s[j].first)+abs(s[i].second-s[j].second) == 1 ? 1 : 100); 4fd800b3a8 2011-02-23 kinaba: for(int k=0; k<N; ++k) 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<N; ++i) 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<N; ++j) 4fd800b3a8 2011-02-23 kinaba: G[i][j] = min(G[i][j], G[i][k]+G[k][j]); 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<N; ++i) 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<N; ++j) 4fd800b3a8 2011-02-23 kinaba: if( G[i][j] > N ) 4fd800b3a8 2011-02-23 kinaba: return false; 4fd800b3a8 2011-02-23 kinaba: return true; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector<state> next_states( const state& s ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: vector<state> result; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<s.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: for(int d=0; d<4; ++d) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: set< pair<int,int> > x; 4fd800b3a8 2011-02-23 kinaba: for(int k=0; k<s.size(); ++k) 4fd800b3a8 2011-02-23 kinaba: if( i != k ) 4fd800b3a8 2011-02-23 kinaba: x.insert( s[k] ); 4fd800b3a8 2011-02-23 kinaba: else 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: int ny = s[k].first+dy[d]; 4fd800b3a8 2011-02-23 kinaba: int nx = s[k].second+dx[d]; 4fd800b3a8 2011-02-23 kinaba: if( 0<=ny && ny<5 && 0<=nx && nx<5 ) 4fd800b3a8 2011-02-23 kinaba: x.insert( make_pair(ny, nx) ); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: if( x.size() == s.size() ) 4fd800b3a8 2011-02-23 kinaba: result.push_back( state(x.begin(), x.end()) ); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return result; 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() 4fd800b3a8 2011-02-23 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 4fd800b3a8 2011-02-23 kinaba: { os << "{ "; 4fd800b3a8 2011-02-23 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 4fd800b3a8 2011-02-23 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 4fd800b3a8 2011-02-23 kinaba: void verify_case(const int& Expected, const int& Received) { 4fd800b3a8 2011-02-23 kinaba: bool ok = (Expected == Received); 4fd800b3a8 2011-02-23 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 4fd800b3a8 2011-02-23 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 4fd800b3a8 2011-02-23 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 4fd800b3a8 2011-02-23 kinaba: #define END verify_case(_, PiecesMover().getMinimumMoves(board));} 4fd800b3a8 2011-02-23 kinaba: int main(){ 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: CASE(0) 4fd800b3a8 2011-02-23 kinaba: string board_[] = {".....", 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: ; 4fd800b3a8 2011-02-23 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 1; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(1) 4fd800b3a8 2011-02-23 kinaba: string board_[] = {".....", 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: ; 4fd800b3a8 2011-02-23 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 0; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(2) 4fd800b3a8 2011-02-23 kinaba: string board_[] = {"*...*", 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: ; 4fd800b3a8 2011-02-23 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 12; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: /* 4fd800b3a8 2011-02-23 kinaba: CASE(3) 4fd800b3a8 2011-02-23 kinaba: string board_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 4fd800b3a8 2011-02-23 kinaba: int _ = ; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(4) 4fd800b3a8 2011-02-23 kinaba: string board_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 4fd800b3a8 2011-02-23 kinaba: int _ = ; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: */ 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE