csu 1469 Change The Handles

                                      Change The Handles

  hint步骤来的,粗暴却不简单,有很多重复的情况,TLE。。。

  将每行每列的变化次数记录一下,mod2之后如果为”0“ 则改行或者该列不需要变化,若为“1”则取反, 为得到棋盘最后的状态,逐行进行改变,这样能保证每个不包含在Q里面的点最多改变1,次(Q里面的点在scanf的时候就进行改变,这里面Q(<=10^5)的时间复杂度是不能省的),同时也能保证这部分任务 的最坏时间复杂度为N*M。

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) (1iN, 1jM). 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 (2N, M1000) 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 ≤ 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

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.


#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;

char qq[1010][1011];
long hang[1010];
long lie[1010];
char ch(char a)
{
    if(a=='+') return '-';
    else return '+';
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        while(n--)
        {
        int h,l,x,y;
        long i,j;
        scanf("%d%d",&h,&l);
        memset(qq,0,sizeof(qq));
        memset(hang,0,sizeof(hang));
        memset(lie,0,sizeof(lie));
        for(i=0;i<h;i++)
        {
           scanf("%s",qq[i]);
        }
        long change;
        scanf("%ld",&change);
        for(i=0;i<change;i++) {scanf("%d%d",&x,&y);qq[x-1][y-1]=ch(qq[x-1][y-1]); hang[x-1]++; lie[y-1]++;}
        for(i=0;i<h;i++) {hang[i]%=2; //printf("%d ",hang[i]);
        }
        for(i=0;i<l;i++) {lie[i]%=2;//printf("%d ",lie[i]);
        }
        for(i=0;i<h;i++)
        {
            for(j=0;j<l;j++)
            {
                if(hang[i]+lie[j]==1) qq[i][j]=ch(qq[i][j]);
            }
        }
        long count=0;
        for(i=0;i<h;i++)
        {
          //  printf("\n");
            for(j=0;j<l;j++)
            {
             //   printf("%c",qq[i][j]);
            if(qq[i][j]=='+') count++;
        }
        }

       printf("%ld\n",count);
    }
    }
    return 0;
}

       原谅我英语不好。。。不习惯用row和line。尴尬  咳 。。  慢慢改  不着急


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值