CSU 1469 Change The Handles acm 模拟题

来源:互联网 发布:淘宝店铺封了怎么激活 编辑:程序博客网 时间:2024/05/22 02:48


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 ≤ iN, 1 ≤ jM). However, this also changes states of all handles in rowi and all handles in columnj.

  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 withM 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 followQ lines with two integersi, j (1 ≤ iN, 1 ≤jM), 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

22 4--------41 11 11 12 45 5+--+-++--+--+-++-----++-+33 21 43 3

Sample Output

6
14

Hint

  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;}

0 0
原创粉丝点击