"POJ 1753" Flip Game

Source: Internet
Author: User

"POJ 1753" Flip Game

Binary + (BFS) Brute Force enumeration

A two-character array was originally used to find traversal difficulties and flip a piece is also difficult to think of one-dimensional linear
Subscript 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
In the figure is the
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Preprocessing an array for flipping and then discovering that preprocessing is a tricky idea because only the black and white colors are available 1 0 tags see 1 0 Thought of of course is binary and made a linear just 16 bit binary all solve flip available binary operation ^ (XOR) on the code

#include <cstdio>#include <cstdlib>#include <cstring>#include <queue>#define INF 0x3f3f3f3fusing namespace STD;intMsintstep[65536];intdir[ +];BOOLvis[65536];intTpintGetnum (Char*STR)//Black 1 white 0{intI,x =0; for(i =1; I <= -; ++i) {x <<=1; x + = (Str[i] = =' B ')?1:0; }returnx;}//void printnum (int x)//{//int i = 1;//while (x)//    {//if (x&1) printf ("1");//Else printf ("0");//x >>= 1;//if (! ( i%4)) printf ("\ n");//++i;//    }//for (; I <=; ++i)//    {//printf ("0");//if (! ( i%4)) printf ("\ n");//    }//printf ("\ n");//}voidMemset (Char*STR,CharA) {intI,j; for(i =1; I <= -; ++i) Str[i] = A;}voidGetdir () {TP =0;Charstr[ the];intI,j; Memset (str,' W '); for(i = -; I >=1; I.) {str[i+1] = str[i+5] = str[i+2]=' W ';if(I-3>0) str[i-3] =' W '; Str[i] = str[i+4] =' B ';if(i%4) str[i+1] =' B ';if((I-1)%4) str[i-1] =' B ';if(I-4>0) str[i-4] =' B ';    dir[tp++] = getnum (str); }}voidBfs () {memset(Step,inf,sizeof(step));memset(Vis,0,sizeof(VIS)); STEP[MS] =0;intI,x,v; Queue <int>Q Q.push (MS); while(!q.empty ())        {x = Q.front ();        Q.pop (); VIS[X] =0; for(i =0; i < TP; ++i) {v = x^dir[i];if(Step[v] > step[x]+1) {Step[v] = step[x]+1;if(!vis[v])                    {Q.push (v); VIS[V] =1; }            }        }    }}intMain () {//freopen ("In.txt", "R", stdin);    Charstr[ -];intI,j,x; for(i =1; I < -; i + =4)    {scanf('%s ', str+i);    } ms = Getnum (str); Getdir ();//for (i = 0; i < TP; ++i)//Printnum (Dir[i]);    if(ms = =0|| ms = =65535)printf("0\n");Else{Bfs (); x = min (step[0],step[65535]);if(x = = INF)printf("impossible\n");Else printf("%d\n", x); }return 0;}

"POJ 1753" Flip Game

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.