Switches(模拟)

In the control panel of an enormous amphitheatre, there are N switches, numbered from 1 to N, that control the MM light bulbs of the place, which are numbered from 1 to M. Note that the number of switches and light bulbs is not necessarily the same and this happens because each switch is associated not to a single light bulb, but to a set of light bulbs. When a switch is activated, each one of the bulbs associated to it is toggled. If the bulb is lit, then it will be switched off, otherwise it will be switched on.

Some bulbs are lit initially and the janitor in charge of the amphitheatre has to switch off all them. He started trying to press the switches randomly, but as soon as he figured out that it wouldn’t necessarily work, he decided to follow a fixed strategy. He will trigger the switches 1,2,3,…,N,1,2,3,… in other words, every time after triggering the switch number NN, he resumes the procedure from the switch number 1. He intends to keep pressing switches by the given strategy until all bulbs are switched off at the same time (in that moment he stops pressing the switches). Will his strategy work?

Given the bulbs which are initially lit and the sets of lamps associated to each switch, your program should compute the number of times the janitor will press switches. If by following the given strategy the janitor is never able to switch off all the lamps at the same time, your program should print -1.

Input
The first line contains two integers N and M (1≤N,M≤1000) representing, respectively, the number of switches and the number of light bulbs. The second line contains an integer L (1≤L≤M) followed by distinct integers Xi (1≤Xi≤M), representing the bulbs that are lit in the first place. Each of the following N rows contains an integer Ki (1≤Ki≤M) followed by Ki distinct integers Yi (1≤Yi≤M), representing the bulbs attached to switch i (1≤i≤N).

Output
Your program must print a single line containing an integer representing the number of times the janitor will press the switches by following the strategy described, until all the lights were off at the same time. If that never happens, print -1.

Examples
Input
6 3
2 1 3 //一开始有2个灯泡亮,1号和3号
3 1 2 3 //第一个开关控制3个灯泡,1号2号3号
2 1 3
2 1 2
2 2 3
1 2
3 1 2 3
Output
5

Input
3 3
2 2 3
1 3
2 1 2
1 2
Output
-1

N个开关,M个灯泡,每一个开关控制一个或多个灯泡,使灯泡状态改变(亮变灭,灭变亮)
问:最少需要多少次操作能使灯泡全灭,如果不可能使灯泡全灭输出-1

模拟题

#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
vector<int> v[1005];
bool a[1005],b[1005];
int main(){
	    int N,M;
	    scanf("%d%d",&N,&M);
		int L;
		scanf("%d",&L);
		for(int i=0;i<L;i++){
			int x;
			scanf("%d",&x);
			a[x]=b[x]=true;
		}
		for(int i=1;i<=N;i++){
			int s;
			scanf("%d",&s);
			while(s--){
				int x;
				scanf("%d",&x);
				v[i].push_back(x);
			}
		}
		bool flag=false;
		long long ans=0;
		while(1){
			for(int i=1;i<=N;i++){
				for(int j=0;j<v[i].size();j++){
					if(a[v[i][j]]==true){
						a[v[i][j]]=false;
						L--;
					}
					else{
						a[v[i][j]]=true;
						L++;
					}
				}
				ans++;
				if(L==0){//全灭
					flag=true;
					printf("%lld\n",ans);
					break;
				}
				if(i==N){   //某轮操作结束  
					int t=0;//现在灯泡状态与初始灯泡状态相同的数量
					for(int k=1;k<=N;k++){
						if(a[k]==b[k])
						t++;
						if(a[k]!=b[k])
						break;
					}
					if(t==N){//状态都相同,也就是说经过一些轮操作之后,灯泡状态又变回了初始状态,所以灯泡不可能全灭
						flag=true;
						printf("-1\n");
						break;
					}
				}
			}
			if(flag) break;
		}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值