LaunchPad

链接:https://ac.nowcoder.com/acm/contest/3665/D
来源:牛客网

题目描述
Hery is a boy with strong practical abilities. Nowadays,he designed a LaunchPad which is not same as what you ever seen.
The LaunchPad is a touch screen divided into squares of N rows and M columns. Each square has two states of light and shade. And all squares are shady initially. When you touch the square in row X and column Y, the state of all the squares in the row and column will change. Now he wants to know how many squares are light on the LaunchPad after he makes multiple touches.
输入描述:
The first line of input contains two integers N,M (1\leq N,M\leq 1000)N,M(1≤N,M≤1000) denoting the rows and columns of LaunchPad.
The second line of input contains single integer Q (0\leq Q\leq 10^6)Q(0≤Q≤10
6
) denoting the times of touch.
On the next Q lines,describe X and Y - the position of the square being touched. (1\leq X \leq N,1\leq Y\leq M)(1≤X≤N,1≤Y≤M)
输出描述:
Print one line - the number of the squares that is on light state.
示例1
输入
1 1
1
1 1
输出
1
示例2
输入
2 4
2
2 4
1 4
输出
6
说明
在这里插入图片描述
开关灯取反问题。
一开始都是暗的,如果在操作过程中这个格子的亮暗变化了奇数次,那么最后这个格子就是亮的。反之,是暗的。

x[i]记录第i行的灯被操作了几次
y[j]记录第j列的灯被操作了几次
z[i][j]记录[i][j]这个位置被操作了几次

你可能会想,在操作[i][j]这个位置的时候,x[i]已经加了,y[j]也已经加了,为什么还要记录z[i][j]呢?

但是如果只用x数组和y数组就能表示的话,那么对[i][j]来说,它是不是弄了两次,那就又回到了开始的状态,但操作[i][j]的时候,肯定会让它的状态取反,所以用z数组来记录一次操作。它再加上一个x[i],y[j]不影响奇偶性。

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int x[1010];
int y[1010];
int z[1010][1010];
int main(){
int n,m;
while(scanf("%d %d",&n,&m)!=EOF)
{
	memset(x,0,sizeof(x));
	memset(y,0,sizeof(y));
	memset(z,0,sizeof(z));
	int q;
	scanf("%d",&q);
	while(q--)
	{
		int a,b;
		scanf("%d %d",&a,&b);
		 x[a]++;
		 y[b]++;
		 z[a][b]++;
	}
	int ans=0;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			if((x[i]+y[j]+z[i][j])%2!=0) ans++;  
		}   //是奇数,就代表这个格子是亮的,就让它加1
	}
	printf("%d\n",ans);
}
	return 0;
}

发布了8 篇原创文章 · 获赞 0 · 访问量 150

猜你喜欢

转载自blog.csdn.net/Erin_jwx/article/details/104044518