c++-棋子翻转

题目描述:

在4x4的棋盘上摆满了黑白棋子,黑白两色的位置和数目随机其中左上角坐标为(1,1),右下角坐标为(4,4),现在依次有一些翻转操作,要对一些给定支点坐标为中心的上下左右四个棋子的颜色进行翻转,请计算出翻转后的棋盘颜色。

给定两个数组Af,分别为初始棋盘和翻转位置。其中翻转位置共有3个。请返回翻转后的棋盘。

测试样例:
[[0,0,1,1],[1,0,1,0],[0,1,1,0],[0,0,1,0]],[[2,2],[3,3],[4,4]]
返回:[[0,1,1,1],[0,0,1,0],[0,1,1,0],[0,0,1,0]]

这道题非常简单,不过多解释


class Flip {
public:
    vector<vector<int> > flipChess(vector<vector<int> > A, vector<vector<int> > f) {
        // write code here
        int i=2;
        while(i>=0) {
            int x=f[i][0]-1;
            int y=f[i][1]-1;
            if(x-1>=0){
                A[x-1][y]=A[x-1][y]==1?0:1;
            }
            if(x+1<=3){
                A[x+1][y]=A[x+1][y]==1?0:1;
            }
            if(y-1>=0){
                A[x][y-1]=A[x][y-1]==1?0:1;
            }
            if(y+1<=3){
                A[x][y+1]=A[x][y+1]==1?0:1;
            }
            i--;
        }
        return A;
    }
};



posted on 2017-12-14 14:10  sichenzhao  阅读(282)  评论(0编辑  收藏  举报

导航