HDU 4949 light (plug DP, bit operation)

Source: Internet
Author: User

I didn't see the question during the competition. After the competition, I felt that I could be able to answer the question. After the competition, I found that the question was actually a problem ....

Because after writing, there is no limit to tle...

It wasn't until I used bitwise operations to replace the decode, encode, and shift functions commonly used by DP and changed the hash value... 7703 Ms

 

Question: to give a 01 matrix of N * m, you can select a grid for two operations each time. ① flip the neighbor grid ② flip the neighbor grid and yourself. The smallest total number of output operations makes the matrix all 0.

 

Obviously, each grid has four operations (1. No operation; 2. ①; 3. ①; 4. ② ).

At the beginning, two bits are used to indicate a plug. One bits are used to indicate that the current grid is flipped, And the other bits indicate that the source of the plug needs to be flipped. Then 2*3*(4 ^ 10) is not scientific.

Later we found that, in fact, we can classify it like this: ① don't operate (cost 0); ② flip ourselves (cost 2); ③ flip neighbors (cost 1 ); this is 2*3*(3 ^ 10)

③ Includes two cases. In fact, if you perform the ③ operation on a grid A, the neighbor grid bcde of the grid can turn off the light.

In addition, the answer must be less than twice the number of 1 equal to the first matrix. You can use this method for a certain degree of pruning.

Then there are various bitwise operations .... I am dizzy...

 

In addition, in fact, this plug-in DP consumes a lot of extra costs (clearing the hash table or something ), therefore, the speed is slower than that of direct DP [I] [J] [k ?).

  1 #include <cstdio>  2 #include <cstring>  3 #include <iostream>  4 using namespace std;  5   6   7 #define HASH 100007  8 #define STATE 500010  9 #define maxd 15 10  11 int maze[maxd][maxd]; 12 int code[maxd]; 13 int n,m; 14 struct HASHMAP{ 15     int head[HASH]; 16     int state[STATE],nxt[STATE]; 17     int f[STATE]; 18     int sz; 19     void clear(){sz=0;memset(head,-1,sizeof(head));} 20     void push(int st,int ans){ 21         int h=st%HASH; 22         for(int i=head[h];i!=-1;i=nxt[i]){ 23             if(st==state[i]){ 24                 f[i] = f[i]<ans?f[i]:ans; 25                 return ; 26             } 27         } 28         state[sz]=st,nxt[sz]=head[h],f[sz]=ans; 29         head[h]=sz++; 30     } 31 }hm[2]; 32 void decode(int st){ 33     for(int i=m;i>=0;--i) code[i]=st&3,st>>=2; 34 } 35 int encode(){ 36     int ret=0; 37     for(int i=0;i<=m;++i) ret=ret<<2|code[i]; 38     return ret; 39 } 40 void shift(){ 41     for(int i=m;i;--i) code[i]=code[i-1]; 42     code[0]=0; 43 } 44 int ans; 45 int zo,oz,oo; 46 void dpblank(int i,int j,int cur){ 47     int mv = j==m?2:0; 48     int all = (1<<(2*(m+1)-mv) ) -1; 49     for(int k=0;kView code

 

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.