POJ 1753 Flip Game BFS+位压缩状态

http://acm.pku.edu.cn/JudgeOnline/problem?id=1753

Flip Game
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2565 Accepted: 1016

Description

Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the other one is black and each piece is lying either it's black or white side up. Each round you flip 3 to 5 pieces, thus changing the color of their upper side from black to white and vice versa. The pieces to be flipped are chosen every round according to the following rules:
  1. Choose any one of the 16 pieces.
  2. Flip the chosen piece and also all adjacent pieces to the left, to the right, to the top, and to the bottom of the chosen piece (if there are any).

POJ 1753 Flip Game BFS+位压缩状态_第1张图片Consider the following position as an example:

bwbw
wwww
bbwb
bwwb
Here "b" denotes pieces lying their black side up and "w" denotes pieces lying their white side up. If we choose to flip the 1st piece from the 3rd row (this choice is shown at the picture), then the field will become:

bwbw
bwww
wwwb
wwwb
The goal of the game is to flip either all pieces white side up or all pieces black side up. You are to write a program that will search for the minimum number of rounds needed to achieve this goal.

Input

The input consists of 4 lines with 4 characters "w" or "b" each that denote game field position.

Output

Write to the output file a single integer number - the minimum number of rounds needed to achieve the goal of the game from the given position. If the goal is initially achieved, then write 0. If it's impossible to achieve the goal, then write the word "Impossible" (without quotes).

Sample Input

bwwb
bbwb
bwwb
bwww

Sample Output

4

Source

Northeastern Europe 2000
 
题目的意思很明白,就是可以同时改变最多5个的颜色(既,自身,上,下,左,右)一开始赤裸裸的BFS,很显然是MLE..
这里不讨论暴力解的情况了....-______________________-暴力是好东西可是偶不会,苍天也.......用暴力的大牛们勿鄙
考虑到状态的特殊性,一个节点要么白色要么黑色,很容易想到用2进制表示,16个节点,最多用16个表示就OK了.
选取的时候自然是去int(32位),因为需要移位
第一次写也不知道这是不是传说中的hash.................不过居然错在了没把自身的状态改变-____________-囧一下~
首先把白的状态看成0
(例如,图上的状态在int中是0000 0000 0000 0000 1010 0000 1101 1001)
1.全白/全黑就是0/65535,很容易理解
2.这题的BFS应该没什么问题,问题集中在了状态改变上,位运算解决..
具体见代码
  1. #include <iostream>
  2. #include <queue>
  3. using namespace std;
  4. struct sta
  5. {
  6.     int state;
  7.     int step;
  8. }p,temp,s;
  9. void move(sta *a,int id)
  10. {
  11.     a->state<<=id;
  12.     a->state^=32768;
  13.     a->state>>=id;
  14.     if(id<=11)
  15.         {
  16.             a->state<<=(id+4);
  17.             a->state^=32768;
  18.             a->state>>=(id+4);
  19.         }
  20.     if(id>=4)
  21.     {
  22.         a->state<<=(id-4);
  23.         a->state^=32768;
  24.         a->state>>=(id-4);
  25.     }
  26.     if(id==0||id==4||id==8||id==12)
  27.         {
  28.             a->state<<=(1+id);
  29.             a->state^=32768;
  30.             a->state>>=(id+1);
  31.         }
  32.     else
  33.     if(id==3||id==7||id==11||id==15)
  34.         {
  35.             a->state<<=(id-1);
  36.             a->state^=32768;
  37.             a->state>>=(id-1);
  38.         }
  39.     else
  40.         {
  41.             a->state<<=(id+1);
  42.             a->state^=32768;
  43.             a->state>>=2;
  44.             a->state^=32768;
  45.             a->state>>=(id-1);
  46.         }
  47. }
  48. void pri(int num)
  49. {
  50.     int i,j=0,l=0,n[16]={0};
  51.     while(num)
  52.         n[l++]=num%2,num/=2;
  53.     for(i=15;i>=0;i--)
  54.     {   
  55.         cout<<n[i];
  56.         j++;
  57.         if(j==4)
  58.             {
  59.                 cout<<endl;
  60.                 j=0;
  61.             }
  62.     }
  63.     cout<<endl;
  64. }
  65. queue<sta> q;
  66. bool hash[65537];
  67. int main()
  68. {
  69.     int w,i,j;
  70.     char map[4][5];
  71.     bool flag;
  72.     while(cin>>map[0])
  73.     {
  74.     memset(hash,false,sizeof(hash));
  75.     flag=false;
  76.     getchar();
  77.     for(i=1;i<4;i++)
  78.         cin>>map[i],getchar();
  79.     s.step=0;s.state=0;
  80.     for(i=0;i<4;i++)
  81.         for(j=0;j<4;j++)
  82.             if(map[i][j]=='b')
  83.                 s.state++,s.state<<=1;
  84.             else
  85.                 s.state<<=1;
  86.     s.state>>=1;
  87.     if(s.state==0||s.state==65535)
  88.         {
  89.             cout<<0<<endl;
  90.             continue;
  91.         }
  92.     q.push(s);
  93.     hash[s.state]=true;
  94.     w=0;
  95.     while(!q.empty())
  96.         {
  97.             while(q.front().step==w)
  98.             {
  99.                 p=q.front();
  100.                 p.step++;
  101.                 for(i=0;i<=15;i++)
  102.                 {
  103.                     temp=p;
  104.                     move(&temp,i);
  105.                  if(temp.state==0||temp.state==65535)
  106.                     {
  107.                         
  108.                         flag=true;
  109.                         break;
  110.                     }
  111.                 if(!hash[temp.state])
  112.                     {
  113.                         hash[temp.state]=true;
  114.                         q.push(temp);
  115.                     }
  116.                 }
  117.             q.pop();
  118.             if(flag) break
  119.             }
  120.         w++;
  121.         if(flag) break;
  122.         }
  123.     if(flag)
  124.         cout<<temp.step<<endl;
  125.     else
  126.         cout<<"Impossible"<<endl;
  127.     }
  128.     return 0;
  129. }

你可能感兴趣的:(Integer,search,input,UP,each,output)