"Hdoj" 2209 Turn card Game

Source: Internet
Author: User

State compression + bi-directional wide search. Note Control time t.

/*2209*/#include<iostream>#include<string>#include<map>#include<queue>#include<Set>#include<stack>#include<vector>#include<deque>#include<algorithm>#include<cstdio>#include<cmath>#include<ctime>#include<cstring>#include<climits>#include<cctype>#include<cassert>#include<functional>using namespacestd;//#pragma COMMENT (linker, "/stack:102400000,1024000")#defineSTI set<int>#defineStpii Set<pair<int, int> >#defineMpii map<int,int>#defineVI vector<int>#definePII pair<int,int>#defineVpii vector<pair<int,int> >#defineRep (I, A, n) for (int i=a;i<n;++i)#definePer (i, a, n) for (int i=n-1;i>=a;--i)#defineCLR Clear#definePB Push_back#defineMP Make_pair#defineFIR First#defineSEC Second#defineAll (x) (x). Begin (), (x). End ()#defineSZ (x) ((int) (x). Size ())#defineLson L, Mid, rt<<1#defineRson mid+1, R, rt<<1|1Const intMAXN =1<< -;BOOLmark1[maxn+5];BOOLmark2[maxn+5];Chars[ -];intm[ -];voidinit () {m[0] =1; Rep (I,1, -) M[i]= m[i-1]<<1;}voidBFs () {Queue<int>Q1, Q2; intx =0, XX; intI, j, K; intt =0; memset (Mark1,false,sizeof(MARK1)); memset (MARK2,false,sizeof(MARK2));  for(i=0; S[i]; ++i) x= (x<<1) + s[i]-'0'; if(x = =0) {puts ("0"); return ; }        intL =i;    Q1.push (x); MARK1[X]=true; Q2.push (0); mark2[0] =true; intsz1, SZ2;  while(! Q1.empty () &&!Q2.empty ()) {        ++T; SZ1=SZ (Q1);  while(sz1--) {x=Q1.front ();            Q1.pop ();  for(i=0; i<l; ++i) {xx=x; XX^=M[i]; if(i) XX^= m[i-1]; if(i+1<l) XX^= m[i+1]; if(Mark1[xx])Continue; if(Mark2[xx]) {printf ("%d\n", t+t-1); return ; } Mark1[xx]=true;            Q1.push (XX); }} SZ2=SZ (Q2);  while(sz2--) {x=Q2.front ();            Q2.pop ();  for(i=0; i<l; ++i) {xx=x; XX^=M[i]; if(i) XX^= m[i-1]; if(i+1<l) XX^= m[i+1]; if(Mark2[xx])Continue; if(Mark1[xx]) {printf ("%d\n", t+t); return ; } Mark2[xx]=true;            Q2.push (XX); }}} puts ("NO");}intMain () {Ios::sync_with_stdio (false); #ifndef Online_judge Freopen ("data.in","R", stdin); Freopen ("Data.out","W", stdout); #endifinit ();  while(SCANF ("%s", s)! =EOF)    {BFS (); } #ifndef Online_judge printf ("Time =%d.\n", (int) clock ()); #endif        return 0;}

"Hdoj" 2209 Turn card 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.