题目描述
在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