Test instructions
There are n lights m switches
The on and off states of each lamp can control whether a light is lit
Give n rows, which represent each lamp
Which state of the switch can make the first I light
Ideas:
Here's a question to be aware of.
If the on state of Switch 1 and the on state of Switch 2 can make the 1th light
Then the switch 1, 2 at the same time on the number 1th lamp is also bright. It means that as long as there is a switch to make the light light, the light is on.
Simple DLX Repeat Overlay
Behavior of two states per switch 2*m line, listed as n lights
At the same time as the search, mark which switch was used.
Then the other state is not going to work.
Code:
#include "stdio.h" #include "algorithm" #include "string.h" #include "iostream" #include "queue" #include "map" #include " Vector "#include" string "using namespace std; #define N 1005*1005#define RN 1005#define CN 1005int us[rn];struct dlx{int N,m,c; int u[n],d[n],l[n],r[n],row[n],col[n]; int H[RN],S[CN],CNT,ANS[RN]; void init (int _n,int _m) {n=_n; M=_m; for (int i=0; i<=m; i++) {s[i]=0; U[i]=d[i]=i; L[i]= (i==0?m:i-1); R[i]= (i==m?0:i+1); } c=m; for (int i=1; i<=n; i++) h[i]=-1; } void link (int x,int y) {C + +; Row[c]=x; Col[c]=y; s[y]++; U[c]=u[y]; D[c]=y; D[u[y]]=c; U[y]=c; if (h[x]==-1) h[x]=l[c]=r[c]=c; else {l[c]=l[h[x]]; R[C]=H[X]; R[l[h[x]]]=c; L[h[x]]=c; }} void del (int x) {for (int i=d[x]; i!=x; I=d[i]) {r[l[i]]=R[i]; L[r[i]]=l[i]; }} void Rec (int x) {for (int i=u[x]; i!=x; I=u[i]) {r[l[i]]=i; L[r[i]]=i; }} int USED[CN]; int h () {int sum=0; for (int i=r[0]; i!=0; i=r[i]) used[i]=0; for (int i=r[0]; i!=0; I=r[i]) {if (used[i]==0) {sum++; Used[i]=1; For (int. j=d[i]; j!=i; j=d[j]) for (int k=r[j]; k!=j; k=r[k]) used[col[k]]=1; }} return sum; } int Dance (int x) {//if (X+h () >=cnt) return 0; if (r[0]==0) {cnt=x; Cnt=min (CNT,X); return 1; } int now=r[0]; for (int i=r[0]; i!=0; I=r[i]) {if (S[i]<s[now]) now=i; } for (int i=d[now]; i!=now; I=d[i]) {if (us[(row[i]-1)/2+1]==0) {us[(R ow[i]-1)/2+1]=1; Del (i); Ans[x]=row[i]; for (int j=r[i]; j!=i; J=r[j]) del (j); if (dance (x+1)) return 1; for (int j=l[i]; j!=i; j=l[j]) rec (j); Rec (i); us[(row[i]-1)/2+1]=0; }} return 0; }} Dlx;int Main () {int n,m; while (scanf ("%d%d", &n,&m)!=-1) {dlx.init (2*m,n); for (int i=1; i<=n; i++) {int k; scanf ("%d", &k); while (k--) {int x; Char y[21]; scanf ("%d%s", &x,y); int tep=x*2-1; if (strcmp (Y, "OFF") ==0) tep++; Dlx.link (Tep,i); }} memset (Us,0,sizeof (US)); int f=dlx.dance (0); if (f==0) puts ("-1"); else {memset (us,0,sizeof (US)); for (int i=0;i<dlx.cnt;i++) {us[(dlx.ans[i]-1)/2+1]=dlx.ans[i]%2; } for (int i=1;i<=m;i++) printf (i==m? ") %s\n ":"%s ", us[i]==0?" OFF ":" On "); }} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
[DLX Repeat overlay] hdu 2828 Lamp