小红开锁
。。。。。。
std::pair<int , int> indx[4] = {{0,1} ,{1 , 0},{0,-1} , {-1 ,0}} ; int n ; void modify(int x , std::vector<std::vector<char>> &g) { std::pair<int , int> pre = {x , x} , now = {x, x + 1} ; char p = g[x][x] ; int k = 0 ; while ( now.first != x || now.second != x ) { swap(p , g[now.first][now.second]) ; int nx = now.first + indx[k].first , ny = now.second + indx[k].second ; if ( nx < x || nx > 2 * n - x + 1 || ny < x || ny > 2 * n - x + 1 ) k = (k + 1) % 4 ; nx = now.first + indx[k].first , ny = now.second + indx[k].second ; now = {nx , ny} ; } swap(p, g[x][x]) ; } bool check(int x,int y,std::vector<std::vector<char>> &g) { if ( y == 1 ) { for(int i = x ; i <= n ; ++ i) if (g[i][x] != 'X' || g[x][i] != 'X') return false ; } else if ( y == 2 ) { for(int i = x , j = 2 * n - x + 1 ; i <= n ; ++ i , -- j) if (g[i][2 * n - x + 1] != 'X' || g[x][j] != 'X' ) return false ; } else if ( y == 3 ) { for(int i = 2 * n - x + 1 , j = 2 * n - x + 1 ; i > n ; -- i , -- j) if ( g[i][2 * n - x + 1] != 'X' || g[2 * n - x + 1][j] != 'X') return false ; } else { for(int i = 2 * n - x + 1 , j = x ; i > n ; -- i , ++ j) if (g[i][x] != 'X' || g[2 * n - x + 1][j] != 'X') return false ; } return true ; } void solve() { cin >> n ; std::vector<std::vector<char> > g(2 * n + 1 , std::vector<char> (2 * n + 1 ,'O')); for(int i = 1 ; i <= 2 * n ; ++ i) { string str ; cin >> str ; for(int j = 1 ; j <= 2 * n ; ++ j) { g[i][j] = str[j - 1] ; } } int ans = inf ; for(int k = 1 ; k <= 4 ; ++ k) { auto a = g ; int num = 0 ; for(int i = 1 ; i <= n ; ++ i) { while ( !check(i , k , a) ) { ++ num ; modify(i , a) ; } } ans = min(ans , num) ; } cout << ans << '\n' ; }