Description
There are N·M handles on the ground. Every handle can be in one of two states: open or closed. The handles are represented as anN·M matrix. You can change the state of handle in any location (i,j) (1 ≤ i ≤ N, 1 ≤ j ≤ M). However, this also changes states of all handles in rowi and all handles in column j.
The figure below shows how the states of handles changed.
Input
The first line contains an integer T (T > 0), giving the number of test cases.
For each test case, the first lines contains two integersN, M (2 ≤ N, M ≤ 1000) as defined above. Then followN lines with M characters describing the initial states of the handles. A symbol “+” denotes the handle in open state, “-” denotes the handle in closed state. The next line contains an integerQ (1 ≤ Q ≤ 105), meaning you will doQ change operations successively. Then follow Q lines with two integersi, j (1 ≤ i ≤ N, 1 ≤ j ≤ M), meaning you will change the state of handle in location (i,j).
Output
For each test case, output the number of handles which are in open state after doing all the operations.
Sample Input
2 2 4 ---- ---- 4 1 1 1 1 1 1 2 4 5 5 +--+- ++--+ --+-+ +---- -++-+ 3 3 2 1 4 3 3
Sample Output
614Hint
Explanation of sample 1: The states of the handles will be changed as the figure shown below.
Explanation of sample 2: The states of the handles will be changed as the figure shown below.
这是一道模拟题,由于数据量大,所以可以用一个二维数组来记录对某一个点的操作次数,如果为奇数则改变状态。
#include<iostream> #include<string> using namespace std; int op[100005][2]; bool c[1010][1010]; long long row[1010], arr[1010]; int main(){ int t; cin >> t; long long q; while (t--){ int n,m; string s; cin >> n>>m; for (int i = 1; i <= n;i++){ cin >> s; for (int j = 1; j <= m; j++){ if (s[j - 1] == '+')c[i][j] = true; else c[i][j] = false; } } cin >> q; for (int i = 0; i < q; i++){ cin >> op[i][0] >> op[i][1]; } for (int i = 0; i < 1010; i++)row[i] = 0; for (int i = 0; i < 1010; i++)arr[i] = 0; for (int i = 0; i < q; i++){ row[op[i][0]]++; arr[op[i][1]]++; c[op[i][0]][op[i][1]] = !c[op[i][0]][op[i][1]]; } for (int i = 1; i <= n; i++){ int k = row[i] % 2; if (k){ for (int j = 1; j <= m; j++){ c[i][j] = !c[i][j]; } } } for (int i = 1; i <= m; i++){ int k = arr[i] % 2; if (k){ for (int j = 1; j <= n; j++){ c[j][i] = !c[j][i]; } } } int total = 0; for (int i = 1; i <= n; i++){ for (int j = 1; j <= m; j++){ if (c[i][j])total++; } } cout << total << endl; } return 0; }