题解 | #棋子翻转#
棋子翻转
https://www.nowcoder.com/practice/a8c89dc768c84ec29cbf9ca065e3f6b4
#include <utility> #include <vector> class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param A int整型vector<vector<>> * @param f int整型vector<vector<>> * @return int整型vector<vector<>> */ vector<vector<int> > flipChess(vector<vector<int> >& A, vector<vector<int> >& f) { for (int i = 0 ; i < f.size() ; i++ ) { pair <int, int> p ; p = make_pair(f[i][0], f[i][1]) ; reverse(A, p) ; } return A ; } void reverse(vector<vector<int>>& A, pair<int, int> p ) { int dx[4] = {1, 0, -1, 0} ; int dy[4] = {0, 1, 0, -1} ; // A是棋盘 for (int i = 0 ; i < A.size() ; i++) { for (int j = 0 ; j < A[i].size() ; j++ ) { if (i == (p.first - 1) && (j == p.second - 1 ) ) { for (int k = 0 ; k < 4 ; k++) { int d_x = i + dx[k] ; int d_y = j + dy[k] ; //cout << d_x << " " << d_y << endl ; if (d_x < A.size() && d_y < A[i].size()) { if (A[d_x][d_y] == 1 ) { A[d_x][d_y] = 0 ; } else { A[d_x][d_y] = 1 ; } } } } } } } };#互联网没坑了,还能去哪里?#