Algorithm title: UVa 10318

Source: Internet
Author: User

Topic Link:

First of all, you can make sure that each grid can only be selected once, because any number greater than 0 is selected, equal to no effect.

Then, it can be understood as a r*c from the matrix to select some of the lattice to "light" operation, so that all the final lattice is "bright" state. Then, each lattice is either a little light operation, or no, a total complexity of 2^25, obviously must be reduced.

Let's say you start with the first row, from left to right, from top to bottom, select from the next, for the current location (x, y), it can no longer affect the number of x-2 lines, so when to X row, if the first x-2 line is not all lit, then the cut.

In addition, you can optimize to indicate the state of each row in a positive number, and the column is represented by the bits of the positive number. This allows you to determine whether the line is all lit as long as O (1).

Code:

* * UVA 10318-security Panel * violence + reduction * */#include <cstdio> #include <cstring> #include <io  
    
Stream> using namespace std;  
const int maxn= 6;  
int n, m;  
BOOL Pat[3][3];  
    
int PATH[MAXN*MAXN], MAT[MAXN], Minx;  
    inline void Read () {memset (mat,0, sizeof (MAT));  
        for (int i=0; i<3; ++i) {for (int j=0; j<3; ++j) pat[i][j] = GetChar () = = ' * '? True:false;  
    GetChar ();  
    } inline bool Checkrow (int R) {if (Mat[r]!= (1<<m)-1) return false;  
    
return true;  inline bool Checkall () {//Check only the last two lines (total rows greater than 1) if (n>1 && checkrow (n-1) && Checkrow (n-2)  
       ||  
    N==1 && checkrow (0)) return true;  
    
return false;  
        } inline void cover (int r,int c) {for (int i=r-1; i<=r+1; ++i) {if (i<0 | | i>=n) continue;  for (int j=c-1; j<=c+1; ++j) {if (j<0 | | j>=m) continue;
            if (pat[i-r+1][j-c+1]) mat[i] ^= (1<<J);  
    BOOL Dfs (int cur, int pathnum) {int r=cur/m, c=cur-r*m;  
    if (r-2>=0 &&!checkrow (r-2)) return false;  
            if (r>=n-2 | | cur==n*m) {if (Checkall ()) {minx = Pathnum;  
        return true;  
    
    } if (cur==n*m) return false;  
    } if (Dfs (cur+1, pathnum)) return true;  
    Cover (R, c);  
    Path[pathnum] = cur+1;  
    if (Dfs (cur+1, pathnum+1)) return true;  
    Cover (R, c);  
return false;  
    int main () {int cas=1;   
        while (~SCANF ("%d%d%*c", &n,&m) && n+m) {read ();  
        printf ("Case #%d\n", cas++);  
            if (Dfs (0, 0)) {printf ("%d", path[0]);  
            for (int i=1; i<minx; ++i) printf ("%d", path[i]);  
    
        Putchar (' \ n '); }else puts ("ImposSible. ");}     
return 0; }

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

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.