solution1(暴力)
暴力蓝桥杯可以过,虽然理论上会超时~
#include<iostream>
using namespace std;
const int maxn = 2010;
int a[maxn][maxn] = {0};//0白棋,1黑棋
int main(){int n, m, x1, x2, y1, y2;scanf("%d%d", &n, &m);while(m--){scanf("%d%d%d%d", &x1, &y1, &x2, &y2);for(int i = x1; i <= x2; i++){for(int j = y1; j <= y2; j++){if(a[i][j]) a[i][j] = 0;else a[i][j] = 1;}}}for(int i = 1; i <= n; i++){for(int j = 1; j <= n; j++){printf("%d", a[i][j]);}printf("\n");}return 0;
}
solution2(差分)
记录操作次数,把对区间加一转化为差分序列的两点操作。
关于数值:偶数次取反为0,奇数次取反为1,可由求余2实现。
#include<iostream>
using namespace std;
const int maxn = 2010;
int a[maxn][maxn] = {0};
int main(){int n, m, x1, x2, y1, y2;scanf("%d%d", &n, &m);while(m--){scanf("%d%d%d%d", &x1, &y1, &x2, &y2);for(int x = x1; x <= x2; x++){a[x][y1]++;a[x][y2 + 1]--;}}for(int i = 1; i <= n; i++){for(int j = 1; j <= n; j++){a[i][j] += a[i][j - 1];printf("%d", a[i][j] % 2);}printf("\n");}return 0;
}