ADDED SRM/TCO22-3-U/1A.cpp Index: SRM/TCO22-3-U/1A.cpp ================================================================== --- SRM/TCO22-3-U/1A.cpp +++ SRM/TCO22-3-U/1A.cpp @@ -0,0 +1,257 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class TwoDimensionalSort { public: + vector sortLetters(vector board) + { + vector ans; + int next_x = 0; + for (int y = 0; y < 26; ++y) { + int np = 26 - count(board[y].begin(), board[y].end(), '.'); + // shift to [next_x .. next_x+np) + for (;;) { + int done = 0; + for (int x = 0, i = 0; x < 26; ++x) { + if (board[y][x] != '.') { + int xx = next_x + i; + if (xx == x) + done++; + else if(board[y][xx]=='.'){ + int l = min(x, xx) + 1; + int r = max(x, xx); + if (count(board[y].begin() + l, board[y].begin() + r, '.') == r - l) { + ans.push_back(y); + ans.push_back(x); + ans.push_back(y); + ans.push_back(xx); + board[y][xx] = board[y][x]; + board[y][x] = '.'; + break; + } + } + ++i; + } + } + if (done == np) + break; + } + next_x += np; + } + + for (int y = 0; y < 26; ++y) { + for (int x = 0; x < 26; ++x) { + if (board[y][x] != '.') { + int yy = board[y][x] - 'A'; + if (y != yy) { + ans.push_back(y); + ans.push_back(x); + ans.push_back(yy); + ans.push_back(x); + } + } + } + } + cerr << ans.size() / 4 << endl; + return ans; + } +}; + +// BEGIN CUT HERE +#include +double start_time; string timer() + { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } +template ostream& operator<<(ostream& os, const vector& v) + { os << "{ "; + for(typename vector::const_iterator it=v.begin(); it!=v.end(); ++it) + os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } +void verify_case(const vector & Expected, const vector & 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(_, TwoDimensionalSort().sortLetters(board));} +int main(){ + +CASE(0) + string board_[] = {"..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + ".........................."}; + vector board(board_, board_+sizeof(board_)/sizeof(*board_)); + vector _; +END +CASE(1) + string board_[] = {"..........................", + "..........................", + "......B...................", + "..............Q...........", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + ".........................."}; + vector board(board_, board_+sizeof(board_)/sizeof(*board_)); + int __[] = {3, 14, 3, 17 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(2) + string board_[] = {"..........................", + "..........................", + "..........................", + ".....BCDE.................", + ".....F....................", + ".....G.A..................", + ".....H....................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + ".........................."}; + vector board(board_, board_+sizeof(board_)/sizeof(*board_)); + int __[] = {5, 7, 5, 9, 5, 9, 2, 9 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(3) + string board_[] = {"..........................", + "..........................", + "..........................", + ".....BCED.................", + ".....F....................", + ".....G.A..................", + ".....H....................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + "..........................", + ".........................."}; + vector board(board_, board_+sizeof(board_)/sizeof(*board_)); + int __[] = {3, 7, 2, 7, 2, 7, 2, 11, 5, 7, 0, 7, 2, 11, 3, 11 }; + vector _(__, __+sizeof(__)/sizeof(*__)); + END + CASE(4) + string board_[] = { + "..........................", + "..........................", + "..........................", + "..........................", + "..............P...........", + "..............Q...........", + "..............N...........", + "..............X...........", + "..............E...........", + "..............H...........", + "..............L...........", + "..............O...........", + "..............J...........", + "..............B...........", + "..............Y...........", + "..............A...........", + "..............T...........", + "..............I...........", + "..............D...........", + "..............G...........", + "..............U...........", + "..............S...........", + "..............K...........", + "..............C...........", + "..........................", + ".........................." }; + vector board(board_, board_+sizeof(board_)/sizeof(*board_)); + int __[] = { -1 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +/* +CASE(5) + string board_[] = ; + vector board(board_, board_+sizeof(board_)/sizeof(*board_)); + int __[] = ; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +*/ +} +// END CUT HERE