52a9b915ea 2022-07-08 kinaba: #include <iostream> 52a9b915ea 2022-07-08 kinaba: #include <sstream> 52a9b915ea 2022-07-08 kinaba: #include <iomanip> 52a9b915ea 2022-07-08 kinaba: #include <vector> 52a9b915ea 2022-07-08 kinaba: #include <string> 52a9b915ea 2022-07-08 kinaba: #include <map> 52a9b915ea 2022-07-08 kinaba: #include <set> 52a9b915ea 2022-07-08 kinaba: #include <algorithm> 52a9b915ea 2022-07-08 kinaba: #include <numeric> 52a9b915ea 2022-07-08 kinaba: #include <iterator> 52a9b915ea 2022-07-08 kinaba: #include <functional> 52a9b915ea 2022-07-08 kinaba: #include <complex> 52a9b915ea 2022-07-08 kinaba: #include <queue> 52a9b915ea 2022-07-08 kinaba: #include <stack> 52a9b915ea 2022-07-08 kinaba: #include <cmath> 52a9b915ea 2022-07-08 kinaba: #include <cassert> 52a9b915ea 2022-07-08 kinaba: #include <tuple> 52a9b915ea 2022-07-08 kinaba: using namespace std; 52a9b915ea 2022-07-08 kinaba: typedef long long LL; 52a9b915ea 2022-07-08 kinaba: typedef complex<double> CMP; 52a9b915ea 2022-07-08 kinaba: 52a9b915ea 2022-07-08 kinaba: class TwoDimensionalSort { public: 52a9b915ea 2022-07-08 kinaba: vector <int> sortLetters(vector <string> board) 52a9b915ea 2022-07-08 kinaba: { 52a9b915ea 2022-07-08 kinaba: vector<int> ans; 52a9b915ea 2022-07-08 kinaba: int next_x = 0; 52a9b915ea 2022-07-08 kinaba: for (int y = 0; y < 26; ++y) { 52a9b915ea 2022-07-08 kinaba: int np = 26 - count(board[y].begin(), board[y].end(), '.'); 52a9b915ea 2022-07-08 kinaba: // shift to [next_x .. next_x+np) 52a9b915ea 2022-07-08 kinaba: for (;;) { 52a9b915ea 2022-07-08 kinaba: int done = 0; 52a9b915ea 2022-07-08 kinaba: for (int x = 0, i = 0; x < 26; ++x) { 52a9b915ea 2022-07-08 kinaba: if (board[y][x] != '.') { 52a9b915ea 2022-07-08 kinaba: int xx = next_x + i; 52a9b915ea 2022-07-08 kinaba: if (xx == x) 52a9b915ea 2022-07-08 kinaba: done++; 52a9b915ea 2022-07-08 kinaba: else if(board[y][xx]=='.'){ 52a9b915ea 2022-07-08 kinaba: int l = min(x, xx) + 1; 52a9b915ea 2022-07-08 kinaba: int r = max(x, xx); 52a9b915ea 2022-07-08 kinaba: if (count(board[y].begin() + l, board[y].begin() + r, '.') == r - l) { 52a9b915ea 2022-07-08 kinaba: ans.push_back(y); 52a9b915ea 2022-07-08 kinaba: ans.push_back(x); 52a9b915ea 2022-07-08 kinaba: ans.push_back(y); 52a9b915ea 2022-07-08 kinaba: ans.push_back(xx); 52a9b915ea 2022-07-08 kinaba: board[y][xx] = board[y][x]; 52a9b915ea 2022-07-08 kinaba: board[y][x] = '.'; 52a9b915ea 2022-07-08 kinaba: break; 52a9b915ea 2022-07-08 kinaba: } 52a9b915ea 2022-07-08 kinaba: } 52a9b915ea 2022-07-08 kinaba: ++i; 52a9b915ea 2022-07-08 kinaba: } 52a9b915ea 2022-07-08 kinaba: } 52a9b915ea 2022-07-08 kinaba: if (done == np) 52a9b915ea 2022-07-08 kinaba: break; 52a9b915ea 2022-07-08 kinaba: } 52a9b915ea 2022-07-08 kinaba: next_x += np; 52a9b915ea 2022-07-08 kinaba: } 52a9b915ea 2022-07-08 kinaba: 52a9b915ea 2022-07-08 kinaba: for (int y = 0; y < 26; ++y) { 52a9b915ea 2022-07-08 kinaba: for (int x = 0; x < 26; ++x) { 52a9b915ea 2022-07-08 kinaba: if (board[y][x] != '.') { 52a9b915ea 2022-07-08 kinaba: int yy = board[y][x] - 'A'; 52a9b915ea 2022-07-08 kinaba: if (y != yy) { 52a9b915ea 2022-07-08 kinaba: ans.push_back(y); 52a9b915ea 2022-07-08 kinaba: ans.push_back(x); 52a9b915ea 2022-07-08 kinaba: ans.push_back(yy); 52a9b915ea 2022-07-08 kinaba: ans.push_back(x); 52a9b915ea 2022-07-08 kinaba: } 52a9b915ea 2022-07-08 kinaba: } 52a9b915ea 2022-07-08 kinaba: } 52a9b915ea 2022-07-08 kinaba: } 52a9b915ea 2022-07-08 kinaba: cerr << ans.size() / 4 << endl; 52a9b915ea 2022-07-08 kinaba: return ans; 52a9b915ea 2022-07-08 kinaba: } 52a9b915ea 2022-07-08 kinaba: }; 52a9b915ea 2022-07-08 kinaba: 52a9b915ea 2022-07-08 kinaba: // BEGIN CUT HERE 52a9b915ea 2022-07-08 kinaba: #include <ctime> 52a9b915ea 2022-07-08 kinaba: double start_time; string timer() 52a9b915ea 2022-07-08 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 52a9b915ea 2022-07-08 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 52a9b915ea 2022-07-08 kinaba: { os << "{ "; 52a9b915ea 2022-07-08 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 52a9b915ea 2022-07-08 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 52a9b915ea 2022-07-08 kinaba: void verify_case(const vector <int>& Expected, const vector <int>& Received) { 52a9b915ea 2022-07-08 kinaba: bool ok = (Expected == Received); 52a9b915ea 2022-07-08 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 52a9b915ea 2022-07-08 kinaba: cerr << "\to: " << Expected << endl << "\tx: " << Received << endl; } } 52a9b915ea 2022-07-08 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 52a9b915ea 2022-07-08 kinaba: #define END verify_case(_, TwoDimensionalSort().sortLetters(board));} 52a9b915ea 2022-07-08 kinaba: int main(){ 52a9b915ea 2022-07-08 kinaba: 52a9b915ea 2022-07-08 kinaba: CASE(0) 52a9b915ea 2022-07-08 kinaba: string board_[] = {"..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: ".........................."}; 52a9b915ea 2022-07-08 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 52a9b915ea 2022-07-08 kinaba: vector <int> _; 52a9b915ea 2022-07-08 kinaba: END 52a9b915ea 2022-07-08 kinaba: CASE(1) 52a9b915ea 2022-07-08 kinaba: string board_[] = {"..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "......B...................", 52a9b915ea 2022-07-08 kinaba: "..............Q...........", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: ".........................."}; 52a9b915ea 2022-07-08 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 52a9b915ea 2022-07-08 kinaba: int __[] = {3, 14, 3, 17 }; 52a9b915ea 2022-07-08 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 52a9b915ea 2022-07-08 kinaba: END 52a9b915ea 2022-07-08 kinaba: CASE(2) 52a9b915ea 2022-07-08 kinaba: string board_[] = {"..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: ".....BCDE.................", 52a9b915ea 2022-07-08 kinaba: ".....F....................", 52a9b915ea 2022-07-08 kinaba: ".....G.A..................", 52a9b915ea 2022-07-08 kinaba: ".....H....................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: ".........................."}; 52a9b915ea 2022-07-08 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 52a9b915ea 2022-07-08 kinaba: int __[] = {5, 7, 5, 9, 5, 9, 2, 9 }; 52a9b915ea 2022-07-08 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 52a9b915ea 2022-07-08 kinaba: END 52a9b915ea 2022-07-08 kinaba: CASE(3) 52a9b915ea 2022-07-08 kinaba: string board_[] = {"..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: ".....BCED.................", 52a9b915ea 2022-07-08 kinaba: ".....F....................", 52a9b915ea 2022-07-08 kinaba: ".....G.A..................", 52a9b915ea 2022-07-08 kinaba: ".....H....................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: ".........................."}; 52a9b915ea 2022-07-08 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 52a9b915ea 2022-07-08 kinaba: int __[] = {3, 7, 2, 7, 2, 7, 2, 11, 5, 7, 0, 7, 2, 11, 3, 11 }; 52a9b915ea 2022-07-08 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 52a9b915ea 2022-07-08 kinaba: END 52a9b915ea 2022-07-08 kinaba: CASE(4) 52a9b915ea 2022-07-08 kinaba: string board_[] = { 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: "..............P...........", 52a9b915ea 2022-07-08 kinaba: "..............Q...........", 52a9b915ea 2022-07-08 kinaba: "..............N...........", 52a9b915ea 2022-07-08 kinaba: "..............X...........", 52a9b915ea 2022-07-08 kinaba: "..............E...........", 52a9b915ea 2022-07-08 kinaba: "..............H...........", 52a9b915ea 2022-07-08 kinaba: "..............L...........", 52a9b915ea 2022-07-08 kinaba: "..............O...........", 52a9b915ea 2022-07-08 kinaba: "..............J...........", 52a9b915ea 2022-07-08 kinaba: "..............B...........", 52a9b915ea 2022-07-08 kinaba: "..............Y...........", 52a9b915ea 2022-07-08 kinaba: "..............A...........", 52a9b915ea 2022-07-08 kinaba: "..............T...........", 52a9b915ea 2022-07-08 kinaba: "..............I...........", 52a9b915ea 2022-07-08 kinaba: "..............D...........", 52a9b915ea 2022-07-08 kinaba: "..............G...........", 52a9b915ea 2022-07-08 kinaba: "..............U...........", 52a9b915ea 2022-07-08 kinaba: "..............S...........", 52a9b915ea 2022-07-08 kinaba: "..............K...........", 52a9b915ea 2022-07-08 kinaba: "..............C...........", 52a9b915ea 2022-07-08 kinaba: "..........................", 52a9b915ea 2022-07-08 kinaba: ".........................." }; 52a9b915ea 2022-07-08 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 52a9b915ea 2022-07-08 kinaba: int __[] = { -1 }; 52a9b915ea 2022-07-08 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 52a9b915ea 2022-07-08 kinaba: END 52a9b915ea 2022-07-08 kinaba: /* 52a9b915ea 2022-07-08 kinaba: CASE(5) 52a9b915ea 2022-07-08 kinaba: string board_[] = ; 52a9b915ea 2022-07-08 kinaba: vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 52a9b915ea 2022-07-08 kinaba: int __[] = ; 52a9b915ea 2022-07-08 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 52a9b915ea 2022-07-08 kinaba: END 52a9b915ea 2022-07-08 kinaba: */ 52a9b915ea 2022-07-08 kinaba: } 52a9b915ea 2022-07-08 kinaba: // END CUT HERE