An inexplicable switch
- Have you ever played the "pull the light" game? Twenty-five lights are arranged in a 5 x 5 square. Each lamp has a switch, and the player can change its state. At each step, the player can change the state of a light. The game player changes the state of a lamp to produce a chain reaction: the lights next to the lamp should also change their state accordingly.
We use the number "1" to denote an open lamp and the number "0" to denote a closed lamp.
It's natural to think of starting from the first line, but when you point to the second line of the first line, you find that the first one has changed again. You can only enumerate all the operations in the first line (2 decisions per point, 5 points, 252 ^ 525 operations per point), and then click on the graph given by the title.
In line 2, we find that the first line has a point that is not 1, so we can only click on the point below (until the last line, we find that the first line is actually fixed, and the result after that is fixed, because each point must be 1).
If the last line is all 1, update minstep if it is.
State compression
- Compressed is the operation, not the lighted state.
In the first line, each light or no light is replaced by 1,0, such as 2(00010) light the fourth light.
details
- Back up an original array
#include<iostream> using namespace std; char ch[6][6]; char backup[6][6]; int next1[5][2]={0,0,0,1,1,0,0,-1,-1,0}; void click(int x,int y){ for(int i=0;i<5;i++){ int tx=x+next1[i][0]; int ty=y+next1[i][1]; if( tx>=1 && ty>=1 && tx<=5 && ty<=5) ch[tx][ty]^=1; } } int main(){ // freopen("a.txt","r",stdin); int t; cin>>t; getchar(); while(t--){ for(int i=1;i<=5;i++){ for(int j=1;j<=5;j++){ ch[i][j]=getchar(); } getchar(); } getchar();//An empty line for(int i=1;i<=5;i++) for(int j=1;j<=5;j++) backup[i][j]=ch[i][j]; int ans=0x3f3f3f3f; for(int states=0;states<(1<<5);states++){ int step=0; for(int j=4;j>=0;j--){ if( (states>>j) & 1 ){ click(1,5-j); step++; } } for(int i=2;i<=5;i++){ for(int j=1;j<=5;j++){ if( ch[i-1][j] == '0'){ step++; click(i,j); } } } int j=1; for(;j<=5;j++){ if(ch[5][j] == '0') break; } if( j == 6 ) ans=min(ans,step); for(int i=1;i<=5;i++) for(int j=1;j<=5;j++) ch[i][j]=backup[i][j]; } if(ans<=6) cout<<ans<<endl; else cout<<-1<<endl; } return 0; }