SRM466 Div2 Hard(1000) MatrixGame
行の交換と列の交換は独立している。任意の回数交換して良いので、任意の並べ替えができる。幅と高さが最大8なので並べ替えは高々40320通り。
列の並び替えを全て試して、行を辞書順に整列。得られた行列のうち辞書順最小のものを返す。
#include <string> #include <vector> #include <algorithm> using namespace std; class MatrixGame { public: vector <string> getMinimal( vector <string> matrix ); }; vector <string> MatrixGame::getMinimal( vector <string> matrix ) { int N = (int)matrix.size(); int M = (int)matrix[0].size(); vector<string> answer = matrix; vector<int> perm; for ( int i=0; i<M; i++ ) perm.push_back( i ); do { vector<string> t = matrix; for ( int n=0; n<N; n++ ) for ( int m=0; m<M; m++ ) t[n][m] = matrix[n][perm[m]]; sort( t.begin(), t.end() ); answer = min( answer, t ); } while ( next_permutation( perm.begin(), perm.end() ) ); return answer; }