牛客 美团编程真题 棋子翻转 外卖笔试题

题目描述

在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]]


  • 这年头应聘个外卖员还要会写代码。。。
#ifdef debug
#include <time.h>
#endif

#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>

#define MAXN ((int)1e5+7)
#define ll long long int
#define INF (0x7f7f7f7f)
#define fori(lef, rig) for(int i=lef; i<=rig; i++)
#define forj(lef, rig) for(int j=lef; j<=rig; j++)
#define fork(lef, rig) for(int k=lef; k<=rig; k++)
#define QAQ (0)

using namespace std;

#define show(x...) \
    do { \
       cout << "\033[31;1m " << #x << " -> "; \
       err(x); \
    } while (0)

void err() { cout << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }

namespace FastIO{

    char print_f[105];
    void read() {}
    void print() { putchar('\n'); }

    template <typename T, typename... T2>
       inline void read(T &x, T2 &... oth) {
           x = 0;
           char ch = getchar();
           ll f = 1;
           while (!isdigit(ch)) {
               if (ch == '-') f *= -1; 
               ch = getchar();
           }
           while (isdigit(ch)) {
               x = x * 10 + ch - 48;
               ch = getchar();
           }
           x *= f;
           read(oth...);
       }
    template <typename T, typename... T2>
       inline void print(T x, T2... oth) {
           ll p3=-1;
           if(x<0) putchar('-'), x=-x;
           do{
                print_f[++p3] = x%10 + 48;
           } while(x/=10);
           while(p3>=0) putchar(print_f[p3--]);
           putchar(' ');
           print(oth...);
       }
} // namespace FastIO
using FastIO::print;
using FastIO::read;

int dr[] = { 1, -1, 0, 0 },
    dc[] = { 0, 0, 1, -1 };
class Flip {
public:
    vector<vector<int> > flipChess(vector<vector<int> > A,
                                   vector<vector<int> > f) {
        int n = f.size();
        for(int i=0; i<n; i++) {
            int r = f[i][0] - 1, c = f[i][1] - 1;
            for(int j=0; j<4; j++) {
                int nr = r + dr[j], nc = c + dc[j];
                if(nr>=0 && nc>=0 && nr<4 && nc<4)
                    A[nr][nc] ^= 1;
            }
        }
        return A;
    }
};

#ifdef debug
signed main() {
    vector<vector<int> > vec = {{0,0,1,1},{1,0,1,0},{0,1,1,0},{0,0,1,0}};
    Flip f;
    vec = f.flipChess(vec, {{2,2}, {3,3}, {4,4}});
    for(int i=0; i<4; i++) {
        for(int j=0; j<4; j++) {
            printf("%d ", vec[i][j]);
        }
        printf("\n");
    }




   return 0;
}
#endif 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值