Data structure:
bool grid[m+1][n+1] , B is false, G is true
bool rowCommand[m+1], false is not change, true is change for ith row
bool colCommand[n+1], false is not change, true is change for ith column.
input R and C command, only do once change for ith row or column.
for loop the rowCommand[m+1], if ith is true, change grid[i][x] to !grid[i][x]
for loop thecolCommand[n+1], if ith is true, change grid[x][i] to !grid[x][i]
count grid[m+1][n+1] "true", this is the result.
#include <iostream>
#include <string.h>
using namespace std;
int main() {
int m = 0;
int n = 0;
cin >> m;
cin >> n;
bool rcmds[m+1];
bool ccmds[n+1];
memset(rcmds, false, m+1);
memset(ccmds, false, n+1);
int k;
cin >> k;
char letter;
int num;
for(int i = 0; i < k; i++){
cin >> letter;
cin >> num;
if(letter == 'R'){
rcmds[num] = !rcmds[num];
}
else{
ccmds[num] = !ccmds[num];
}
}
bool grid[m+1][n+1];
for(int i = 1; i <= m; i++){
for(int j = 1; j <= n; j++){
grid[i][j] = false;
}
}
for(int i = 1; i <= m; i++){
if(rcmds[i]){
for(int j = 1; j <= n; j++){
grid[i][j] = !grid[i][j];
}
}
}
for(int i = 1; i <= n; i++){
if(ccmds[i]){
for(int j = 1; j <= m; j++){
grid[j][i] = !grid[j][i];
}
}
}
int result = 0;
for(int i = 1; i <=m; i++){
for(int j = 1; j <= n; j++){
if(grid[i][j]){
result += 1;
}
}
}
cout<<result<<endl;
return 0;
}
Kommentare