Poj1753-bit compression BFS

Source: Internet
Author: User

In many cases, we can use one or more Integers to compress the variable state, which is conducive to space optimization. Although there is little benefit to the amount of code, however, when there is a large amount of data, it can still have a certain effect.

Taking poj1753 as an example, this is a simple question in the recommendation question. Generally, you can search for it without having to search...

My method is to compress the BFS status. Since each card surface is multiplied by 4, that is, 16 grids, each grid has only two States: black and white, the entire state can be loaded with an int number.

From top to bottom, the card is numbered from left to right 0 to 15. In this way, the I-bits starting from the low position of the int type are marked as 0 or 1, indicating white and black respectively, status Transfer Using bitwise operations.

Let's take a look at my struct:

Struct node <br/>{< br/> int code; <br/> int table; <br/> int depth; <br/> int last; <br/>} start;

 

Code indicates whether the grids in this status have been overturned. If the corresponding bit is 1, it indicates that the grids have been pressed. This kind of question has a notable feature, that is, one lattice is doubled once, otherwise, it would be an effect if we did not record the grids, so that we can know which grids have been turned over by us, and those that do not.

Table indicates the status card. If the I-th digit is 1, the card is black; otherwise, the card is white.

Depth indicates the number of steps from the initial status to this status.

Last indicates the number of the grids that are loaded and moved to this status. In this way, each time we start to search for the grids after the last flipped lattice, we will not repeat the search.

 

In addition, after bit compression, write a State Transfer Function Hash () to transfer the state.

Int Hash (INT table, int OP) <br/>{< br/> int st; </P> <p> ST = (1 <OP ); <br/> If (OP % 4> 0) <br/> st | = (1 <(op-1); <br/> If (OP % 4 <3) <br/> st | = (1 <(OP + 1); <br/> If (OP/4> 0) <br/> st | = (1 <(op-4); <br/> If (OP/4 <3) <br/> st | = (1 <(OP + 4); </P> <p> return (Table ^ st); <br/>}

The table parameter is the card plane to be transformed. Op represents the number of the flipped grid, and the St variable in the function represents the bit to be flipped. If the I-th bit needs to be flipped, this is 1, otherwise, it is 0. First, the op position must be flipped. Then, four if statements are used to determine the position of the Grid. When the grid is on the leftmost, OP % 4 equals 0, top, OP/4 is equal to 0, and so on, when the grid is not the leftmost, to the left (op-1) Position of the grid flip, therefore, the update st variable second (op-1) bit is 1, and others are similar.

 

In the BFS function, the last state is 15 0 or 16 0 (1 <16)-1), so you can directly determine whether the integer is equal.

 

The complete code of the main program is as follows:

# Include <iostream> <br/> # include <cstring> <br/> # include <cstdlib> <br/> # include <cstdio> <br/> # include <queue> </P> <p> using namespace STD; </P> <p> struct node <br/>{< br/> int code; <br/> int table; <br/> int depth; <br/> int last; <br/>} start; </P> <p> int Hash (INT table, int OP) <br/>{< br/> int st; </P> <p> ST = (1 <OP); <br/> If (OP % 4> 0) <br/> st | = (1 <(op-1); <br/> If (OP % 4 <3) <br/> st | = (1 <(OP + 1); <br/> If (OP/4> 0) <Br/> st | = (1 <(op-4); <br/> If (OP/4 <3) <br/> st | = (1 <(OP + 4); </P> <p> return (Table ^ st ); <br/>}</P> <p> int BFS () <br/> {<br/> node, next; <br/> queue <node> q; </P> <p> node. code = 0; <br/> node. table = start. table; <br/> node. depth = 0; <br/> node. last =-1; <br/> If (node. table = 0 | node. table = (1 <16)-1) <br/> return 0; <br/> q. push (node); <br/> while (! Q. empty () <br/>{< br/> node = Q. front (); <br/> q. pop (); <br/> for (INT I = node. last + 1; I <16; I ++) <br/>{< br/> If (node. code & (1 <I) = 0) <br/>{< br/> next. code = (node. code | (1 <I); <br/> next. table = hash (node. table, I); <br/> next. depth = node. depth + 1; <br/> next. last = I; <br/> If (next. table = 0 | next. table = (1 <16)-1) <br/> return next. depth; <br/> q. push (next); <br/>}</P> <p> return-1; <br/>}</P> <p> int main () <br/>{< br/> char STR [10]; </P> <p> Start. table = 0; <br/> for (INT I = 0; I <4; I ++) <br/> {<br/> gets (STR ); <br/> for (Int J = 0; j <4; j ++) <br/> {<br/> If (STR [J] = 'B ') <br/>{< br/> Start. table | = (1 <(I * 4 + j )); <br/>}</P> <p> int ret = BFS (); <br/> If (ret =-1) <br/> printf ("Impossible/N "); <br/> else <br/> printf ("% d/N", RET); </P> <p> return 0; <br/>}< br/>

 

Reflection: BFS bit compression can also be optimized. Code and last play the same role, because each time the search starts from the last bit of the last flip, you do not need to judge the code variable. You can consider removing another struct variable.

 

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.