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 <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: 4fd800b3a8 2011-02-23 kinaba: bool is_ps( LL v ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: LL sv = (LL)sqrt( double(v) ); 4fd800b3a8 2011-02-23 kinaba: return (sv-1)*(sv-1) == v 4fd800b3a8 2011-02-23 kinaba: || sv*sv == v 4fd800b3a8 2011-02-23 kinaba: || (sv+1)*(sv+1) == v; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class FindingSquareInTable { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: int findMaximalSquare(vector <string> table) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: LL ans = -1; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int Y = table.size(); 4fd800b3a8 2011-02-23 kinaba: int X = table[0].size(); 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: for(int dy=-Y; dy<Y; ++dy) 4fd800b3a8 2011-02-23 kinaba: for(int dx=-X; dx<X; ++dx) 4fd800b3a8 2011-02-23 kinaba: if(dy||dx) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: LL v = 0; 4fd800b3a8 2011-02-23 kinaba: for(int i=0;; ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int yy = y+dy*i; 4fd800b3a8 2011-02-23 kinaba: int xx = x+dx*i; 4fd800b3a8 2011-02-23 kinaba: if( xx<0 || X<=xx || yy<0 || Y<=yy ) 4fd800b3a8 2011-02-23 kinaba: break; 4fd800b3a8 2011-02-23 kinaba: int d = table[yy][xx]-'0'; 4fd800b3a8 2011-02-23 kinaba: v = v*10 + d; 4fd800b3a8 2011-02-23 kinaba: if( is_ps(v) ) 4fd800b3a8 2011-02-23 kinaba: ans = max(v, ans); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return ans; 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 int &Expected, const int &Received) { if (Expected == Received) 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 table_[] = {"123", 4fd800b3a8 2011-02-23 kinaba: "456"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> table(table_, table_+sizeof(table_)/sizeof(*table_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 64; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, FindingSquareInTable().findMaximalSquare(table)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<1>) { 4fd800b3a8 2011-02-23 kinaba: string table_[] = {"00000", 4fd800b3a8 2011-02-23 kinaba: "00000", 4fd800b3a8 2011-02-23 kinaba: "00200", 4fd800b3a8 2011-02-23 kinaba: "00000", 4fd800b3a8 2011-02-23 kinaba: "00000"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> table(table_, table_+sizeof(table_)/sizeof(*table_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 0; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, FindingSquareInTable().findMaximalSquare(table)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<2>) { 4fd800b3a8 2011-02-23 kinaba: string table_[] = {"3791178", 4fd800b3a8 2011-02-23 kinaba: "1283252", 4fd800b3a8 2011-02-23 kinaba: "4103617", 4fd800b3a8 2011-02-23 kinaba: "8233494", 4fd800b3a8 2011-02-23 kinaba: "8725572", 4fd800b3a8 2011-02-23 kinaba: "2937261"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> table(table_, table_+sizeof(table_)/sizeof(*table_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 320356; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, FindingSquareInTable().findMaximalSquare(table)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<3>) { 4fd800b3a8 2011-02-23 kinaba: string table_[] = {"135791357", 4fd800b3a8 2011-02-23 kinaba: "357913579", 4fd800b3a8 2011-02-23 kinaba: "579135791", 4fd800b3a8 2011-02-23 kinaba: "791357913", 4fd800b3a8 2011-02-23 kinaba: "913579135"} 4fd800b3a8 2011-02-23 kinaba: ; 4fd800b3a8 2011-02-23 kinaba: vector <string> table(table_, table_+sizeof(table_)/sizeof(*table_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 9; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, FindingSquareInTable().findMaximalSquare(table)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<4>) { 4fd800b3a8 2011-02-23 kinaba: string table_[] = {"553333733", 4fd800b3a8 2011-02-23 kinaba: "775337775", 4fd800b3a8 2011-02-23 kinaba: "777537775", 4fd800b3a8 2011-02-23 kinaba: "777357333", 4fd800b3a8 2011-02-23 kinaba: "755553557", 4fd800b3a8 2011-02-23 kinaba: "355533335", 4fd800b3a8 2011-02-23 kinaba: "373773573", 4fd800b3a8 2011-02-23 kinaba: "337373777", 4fd800b3a8 2011-02-23 kinaba: "775557777"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> table(table_, table_+sizeof(table_)/sizeof(*table_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = -1; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, FindingSquareInTable().findMaximalSquare(table)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<5>) { 4fd800b3a8 2011-02-23 kinaba: string table_[] = {"257240281", 4fd800b3a8 2011-02-23 kinaba: "197510846", 4fd800b3a8 2011-02-23 kinaba: "014345401", 4fd800b3a8 2011-02-23 kinaba: "035562575", 4fd800b3a8 2011-02-23 kinaba: "974935632", 4fd800b3a8 2011-02-23 kinaba: "865865933", 4fd800b3a8 2011-02-23 kinaba: "684684987", 4fd800b3a8 2011-02-23 kinaba: "768934659", 4fd800b3a8 2011-02-23 kinaba: "287493867"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> table(table_, table_+sizeof(table_)/sizeof(*table_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 95481; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, FindingSquareInTable().findMaximalSquare(table)); } 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: