POJ 1176 Party lamps (DFS)

Source: Internet
Author: User

For a bunch of lights. Provides four ways to change the status of the Lantern (On<=>off): A. Change the status of all the lights; b. Change the status of odd lights. C. Change the status of even colored lights; d. Change the status of 3k+1 (1,4,7,10 ...).

The number of lights given. Operation Times, and for a certain number of lights must be on, a few lights must be off the requirements, ask after a given number of operations, finally can meet the requirements of the status of how many kinds, output all meet the requirements of the Lantern state.

The number of operations in the original problem is 1<=c<=10000. This is assumed to be the search depth. It's rather scary. Fortunately there is a small mystery, can be greatly limited search times.

Considering those four operations, it is possible to find this characteristic:

1. Each operation in the sequence of operations is capable of being exchanged. namely Ab=ba;

2. Every two times the same operation effect is offset, namely aab=b.

The ability to eliminate even the same number of operations, the odd number of the same operation of the remaining one can be, a operation sequence of less than or equal to 4, each operation not more than once.

This processes a sequence of operations, first merging all the same operations together. Get the sequence of the AA...AB....BC....CD....D, and then eliminate the even number of the same operation, the odd number of the same operation of the remaining one can be, to obtain a sequence of operations less than or equal to 4, each operation not more than once.

So it is possible to limit the number of operations under 4, 4^4=256, a common 256 kinds of finally state, the mob search can be. Taking the 4-tier search as an example, the color-finding status in each layer search is based on the result state of the upper search. To store all levels of results with a linear array, it is necessary to establish the corresponding method of the lower result and the upper result in the storage location. Because each search will expand the number of results by four times times, so the corresponding relationship is I = (j-1)/4.

#include <iostream> #include <algorithm> #include <string> #include <stdlib.h>using namespace        Std;int n;int c;int a[101];int _open[101];int _close[101];string ss[300];void do1 () {for (int i=1;i<=100;++i) A[i]^=1;} void Do2 () {for (int i=2;i<=n;i+=2) a[i]^=1;} void Do3 () {for (int i=1;i<=n;i+=2) a[i]^=1;} void Do4 () {for (int i=1;i<=n;i+=3) a[i]^=1;}        int Cnt=0;int Check () {for (int i=1;i<=n;++i) {if (_open[i]==1&&a[i]==0) return 1;    if (_close[i]==1&&a[i]==1) return 1; } return 0;}            void Dfs (int c) {if (c==c) {if (check () ==0) {ss[cnt]= "";            for (int i=0; i<n; i++) ss[cnt]+= (a[i+1]+ ' 0 ');        cnt++;    } return;            } for (int i=1;i<=4;++i) {switch (i) {case 1:do1 ();//Switch once DFS (C+1);        Do1 ();//Switch back again to restore the break;          Case 2:do2 ();  DFS (C+1);            Do2 ();        Break            Case 3:do3 ();            DFS (C+1);            Do3 ();        Break            Case 4:do4 ();            DFS (C+1);            Do4 ();        Break    }}}int Main (int argc, char *argv[]) {//Freopen ("1176.in", "R", stdin);    scanf ("%d", &n);    scanf ("%d", &c);    memset (_open,0,sizeof (_open));    memset (_close,0,sizeof (_close));    for (int i=1;i<=n;++i) a[i]=1;    int tmp;    while (scanf ("%d", &tmp) ==1&&tmp!=-1) _open[tmp]=1;    while (scanf ("%d", &tmp) ==1&&tmp!=-1) _close[tmp]=1;        if (c>4) {c%=2;        if (c==1) {c=3;        } else {c=4;    }} dfs (0);    Sort (ss,ss+cnt);    cout<<ss[0]<<endl;    int i,j;            for (i=0,j=1;j<cnt;j++) if (Ss[i]!=ss[j]) {i=j;        cout<<ss[i]<<endl; } return 0;}


Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

POJ 1176 Party lamps (DFS)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.