棋子翻转

问题描述:
在4x4的棋盘上摆满了黑白棋子,黑白两色的位置和数目随机其中左上角坐标为(1,1),右下角坐标为(4,4),现在依次有一些翻转操作,要对一些给定支点坐标为中心的上下左右四个棋子的颜色进行翻转,请计算出翻转后的棋盘颜色。
给定两个数组A和f,分别为初始棋盘和翻转位置。其中翻转位置共有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]]

代码如下:

import java.util.*;

public class Flip {
    public int[][] flipChess(int[][] A, int[][] f) {
        // write code here
        int row = A.length;
        int col = A[0].length;

        for(int i = 0;i<f.length;i++){
            int pointRow = f[i][0]-1;
            int pointCol = f[i][1]-1;

            int[] rows = {pointRow-1,pointRow,pointRow+1,pointRow};
            int[] cols = {pointCol,pointCol+1,pointCol,pointCol-1};

            for(int j = 0;j<4;j++){
                if(rows[j]>=0 && rows[j]<row && cols[j]>=0 && cols[j]< col)
                    A[rows[j]][cols[j]] = A[rows[j]][cols[j]] == 1?0:1;
            }
        }
        return A;
    }
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值