This question is question a of the Shanghai semi-finals in. Although it is very watery, it cannot be searched directly. So we can see that the requirement of the question is that when the number of operations is greater than 3, more is output. Then we can perform three operations on the final state enumeration to achieve all the states, this should be well written with extensive search. We can use the VIS array directly for this record, and there is no need to use any struct.
During enumeration, because we only have 32 sides, we can manually enter these 32 sides, and then perform enumeration operations directly during the extensive search process, however, the requirement in the question is that the lights at the two ends of the edge must be in different States before they can be operated. I almost got wa to death in this place, so sad! Finally, use the VIS array to determine the corresponding output.
Paste my code below (the code should be concise and clear ):
#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <queue>using namespace std;int a[32]={1,3,2,1,10,2, 4, 1,3, 9, 9, 11, 6,14,6, 8, 5,13,5, 7, 13,15,5,7, 2,4,12,10,3,1,11,9};int b[32]={2,4,4,3,12,10,12,9,11,11,10,12, 8,16,14,16,7,15,13,15,14,16,6,8, 6,8,16,14,7,5,15,13};int vis[1000000];int getState(int *st){ int ans=0; for(int i=0;i<16;i++) { if(st[i]) { ans|=(1<<i); } } return ans;}void bfs(int s){ vis[s]=0; queue<int> q; q.push(s); while(!q.empty()) { int fr=q.front(); q.pop(); if(vis[fr]>=3) continue; for(int i=0;i<32;i++) { int temp=fr; int j1=fr&(1<<(a[i]-1)); int j2=fr&(1<<(b[i]-1)); if(j1==j2) continue; temp^=(1<<(a[i]-1)); temp^=(1<<(b[i]-1)); if(vis[temp]==-1) { vis[temp]=vis[fr]+1; if(vis[temp]<3) q.push(temp); } } }}int main(){ int st[16]={0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1}; int s=getState(st); memset(vis,-1,sizeof(vis)); bfs(s); int T; scanf("%d",&T); for(int cas=1;cas<=T;cas++) { for(int i=0;i<16;i++) scanf("%d",&st[i]); s=getState(st); printf("Case #%d: ",cas); if(vis[s]==-1||vis[s]>3) printf("more\n"); else printf("%d\n",vis[s]); } return 0;}