BFS+狀態壓縮 hdu-1885-Key Task

來源:互聯網
上載者:User

題目連結:

http://acm.hdu.edu.cn/showproblem.php?pid=1885

題目意思:

給一個矩陣,給一個起點多個終點,有些點有牆不能通過,有些點的位置有門,需要拿到相應顏色的鑰匙才能開啟,問到達終點的最短步數。

解題思路:

BFS+狀態壓縮。

將每種顏色對應一個位元位,1表示已經得到該顏色的鑰匙,0表示沒有得到。

一把鑰匙可以同種顏色的多扇門。

代碼:

#include<iostream>#include<cmath>#include<cstdio>#include<cstdlib>#include<string>#include<cstring>#include<algorithm>#include<vector>#include<map>#include<set>#include<stack>#include<list>#include<queue>#define eps 1e-6#define INF 0x1f1f1f1f#define PI acos(-1.0)#define ll __int64#define lson l,m,(rt<<1)#define rson m+1,r,(rt<<1)|1//#pragma comment(linker, "/STACK:1024000000,1024000000")using namespace std;/*freopen("data.in","r",stdin);freopen("data.out","w",stdout);*/map<char,int>myp1,myp2;bool vis[20][110][110];char save[110][110];int n,m,dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};struct Point{   int x,y,step,ss;}s;bool iscan(int x,int y){   if(x<1||x>n||y<1||y>m||save[x][y]=='#')      return false;   return true;}//一把鑰匙可以開顏色相同的多種鎖void bfs(){   memset(vis,false,sizeof(vis));   s.step=0,s.ss=0;   queue<Point>myq;   myq.push(s);   vis[0][s.x][s.y]=true;   while(!myq.empty())   {      Point tmp=myq.front();      myq.pop();      int xx,yy;      for(int i=0;i<4;i++)      {         xx=tmp.x+dir[i][0],yy=tmp.y+dir[i][1];         if(!iscan(xx,yy))            continue;         if(save[xx][yy]=='X')         {            printf("Escape possible in %d steps.\n",tmp.step+1);            return ;         }         int tt=tmp.ss;         if(myp1.find(save[xx][yy])!=myp1.end()) //得到一把鑰匙         {            tt=tt|(1<<myp1[save[xx][yy]]);            if(vis[tt][xx][yy]) //該狀態之前已被訪問               continue;         }         else if(myp2.find(save[xx][yy])!=myp2.end())         {            if(!(tt&(1<<myp2[save[xx][yy]]))) //沒有鑰匙               continue;         }         if(vis[tt][xx][yy])            continue;         vis[tt][xx][yy]=true;         Point t;         t.x=xx,t.y=yy,t.ss=tt,t.step=tmp.step+1;         myq.push(t);      }   }   printf("The poor student is trapped!\n");   return ;}int main(){   myp1['b']=0,myp1['y']=1,myp1['r']=2,myp1['g']=3; //每種不同的顏色對應不同的數位   myp2['B']=0,myp2['Y']=1,myp2['R']=2,myp2['G']=3;   while(scanf("%d%d",&n,&m)&&m+n)   {      memset(vis,false,sizeof(vis));      for(int i=1;i<=n;i++)      {         scanf("%s",save[i]+1);         for(int j=1;j<=m;j++)         {            if(save[i][j]=='*')               s.x=i,s.y=j;         }      }      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.