PKU 1753 Flip Game(Bfs+ 位壓縮狀態)

來源:互聯網
上載者:User

        一個不錯的Bfs,它的價值在於狀態的儲存,做這道題就是為了學習如何用位儲存狀態。雖然看了網上的思路,也算入了門吧!

       題目的意思:   給定一個4*4格的棋盤,每一個格子是黑的或者白的,現在有一個規則:每當把一個格子變成它相反的顏色,那麼相鄰的格子(上下左右)的顏色也將變成它與原來相反的顏色。那麼,給出一種演算法,對於一個給定的棋盤,把它的格子全部變成白色或黑色。

       直接BFs就可以,其中關鍵是狀態的儲存,可以用1表示黑色,0表示白色。則一個狀態可以用16位2進位表示,當然如何獲得下一個狀態呢,可以用位用演算法符,^(異或)取反,  <<( 左移)來產生新的狀態。 

 

Source Code

Problem: 1753  User: zhouxc
Memory: 396K  Time: 141MS
Language: C++  Result: Accepted

Source Code
#include "iostream"
#include "queue"

using namespace std;

typedef struct State{
       
        int   step;
        int   state;
};
State s,temp,t;
char map[4][4];
bool hash[70000];

void Move(int r)
{       

      t.state^=(1<<(15-r));
      if(r>=4)
            t.state^=(1<<(15-r+4));
      if(r<=11)
            t.state^=(1<<(15-r-4));   
      if((r%4))
            t.state^=(1<<(15-r+1));
      if(((r+1)%4))
            t.state^=(1<<(15-r-1));
   
}                               

void Bfs()
{
       
         queue<State> q;
         bool flag=false;
         hash[s.state]=true;
         q.push(s);
         int cunt=0;
         while(!q.empty())
         {
              temp=q.front();
              q.pop();              
              for(int i=0;i<16;i++)
              {        
                          t=temp;
                          Move(i);
                          if(t.state==0||t.state==65535)
                          {
                                    flag=true;
                                    cout<<t.step+1<<endl;
                                    break;
                          }                             
                          if(!hash[t.state])
                          {
                               hash[t.state]=true;
                               t.step=temp.step+1;
                               q.push(t);
                          }
                                                      
              }
              if(flag)
                    break;
         }     
         if(!flag)
              cout<<"Impossible"<<endl;
}

int main()
{
       
       
          s.state=0;
          for(int i=0;i<4;i++)
           for(int j=0;j<4;j++)
           {
                 cin>>map[i][j];
                 if(map[i][j]=='b')
                      s.state+=1;
                 s.state<<=1;         
           }
           s.state>>=1;
           memset(hash,false,sizeof(hash));
           s.step=0;
           if(s.state==0||s.state==65535)
                  cout<<"0"<<endl;
           else      
                  Bfs();
           return 0;         
         }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.