1469: Handles
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 258 Solved: 65
[ Submit][ Status][ Web Board]
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 an N·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 row i 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 integers N, M (2 ≤ N, M ≤ 1000) as defined above. Then follow N 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 integer Q (1 ≤ Q ≤ 105), meaning you will do Q change operations successively. Then follow Q lines with two integers i, 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
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.
Source
模拟题。
#include <stdio.h>
#include <string.h>
using namespace std;
char a[1005][1005];
int xx[1005],yy[1005];
int n,m;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
memset(xx,0,sizeof(xx));
memset(yy,0,sizeof(yy));
for(int i=0;i<n;i++)
scanf("%s",a[i]);
int q;
scanf("%d",&q);
while(q--)
{
int x,y;
scanf("%d%d",&x,&y);
a[x-1][y-1]=(a[x-1][y-1]=='+'?'-':'+');
xx[x-1]++;
yy[y-1]++;
}
for(int i=0;i<n;i++)
{
if(xx[i]%2!=0)
{
for(int j=0;j<m;j++)
a[i][j]=(a[i][j]=='+'?'-':'+');
}
}
for(int i=0;i<m;i++)
{
if(yy[i]%2!=0)
{
for(int j=0;j<n;j++)
a[j][i]=(a[j][i]=='+'?'-':'+');
}
}
int sum=0;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(a[i][j]=='+')
sum++;
printf("%d\n",sum);
}
return 0;
}