[Blue Bridge Cup] [algorithm improves VIP] lights game
Time limit: 1Sec Memory Limit: 128MB submitted: 4 Solution: 1
Title Description
9 lights and switches 9, are numbers from 1 to 9.
Each switch can control several lamps, a pressing state of the lamp will change its control (no light becomes bright, the light does not shine into).
As follows:
first a switching control of the second, fourth lamp;
second switching control of the first, third, fifth lights;
third switching control of the second, sixth lamp;
fourth switching control The first, fifth, seventh lights;
fifth switching control of the second, fourth, sixth, eighth lights;
sixth switch controlling the third, fifth, ninth lights;
seventh switch controls the fourth, eighth lights;
eighth switch controls the fifth, seventh, ninth lights;
ninth switch control sixth, eighth lamp.
At the beginning of all the lights are off, the switch is closed. After a number of switches required is pressed, so that only four lights on.
Entry
no
Export
The output of all the possible scenarios, one per line embodiment, each row has nine characters, from left to right of the character i denotes the i th state of the switch ( "0" off, "1" means open), lexicographical ordering output. The following sample output only part of the program.
Sample input
no
Sample Output
000001011 000001110 000001111
Problem-solving ideas:
Be sure to read the clear meaning of the questions, search enumerate all of the switching state.
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int a[10],vis[10];
void check( ){
for( int i=1;i<=9;i++){
if( vis[i] ){
if( i == 1 ){
a[2] = !a[2];
a[4] = !a[4];
}
else if( i == 2 ){
a[1] = !a[1];
a[3] = !a[3];
a[5] = !a[5];
}
else if( i == 3 ){
a[2] = !a[2];
a[6] = !a[6];
}
else if( i==4 ){
a[1] = !a[1];
a[5] = !a[5];
a[7] = !a[7];
}
else if( i==5 ){
a[2] = !a[2];
a[4] = !a[4];
a[6] = !a[6];
a[8] = !a[8];
}
else if( i==6 ){
a[3] = !a[3];
a[5] = !a[5];
a[9] = !a[9];
}
else if( i==7 ){
a[4] = !a[4];
a[8] = !a[8];
}
else if( i == 8 ){
a[5] = !a[5];
a[7] = !a[7];
a[9] = !a[9];
}
else if( i==9 ){
a[6] = !a[6];
a[8] = !a[8];
}
}
}
int cnt =0;
for( int i=1;i<=9;i++){
if( a[i] ){
cnt++;
}
}
if( cnt == 4 ){
for( int i=1;i<=9;i++){
printf("%d",vis[i]);
}
printf("\n");
}
}
void dfs( int n ){
if( n == 10 ){
check() ;
memset(a,0,sizeof(a));
return ;
}
for( int j=0;j<2;j++){
vis[n] = j;
dfs( n+1);
}
}
int main(void){
memset(a,0,sizeof(a));
memset(vis,0,sizeof(vis));
dfs( 1 );
return 0;
}