hdu 1429 狀壓bfs

來源:互聯網
上載者:User

標籤:class   blog   code   2014   string   os   


#include <iostream>#include <cstring>#include <string>#include <cstdio>#include <cmath>#include <algorithm>#include <vector>#include <queue>#include <map>#define inf 0x3f3f3f3f#define ll __int64#define mod 1000000007using namespace std;struct node{    int x,y,key,cnt;}now,tmp;int dx[]={1,-1,0,0};int dy[]={0,0,1,-1};int n,m,t,sx,sy,ex,ey,vis[25][25][1<<11];char mp[25][25];bool ok(int x,int y,int key){    if(x<0||x>=n||y<0||y>=m||vis[x][y][key]||mp[x][y]=='*') return 0;    return 1;}int bfs(){    queue<node> q;    now.x=sx;    now.y=sy;    now.cnt=0;    now.key=0;    vis[sx][sy][0]=1;//@#$%^&**!!    q.push(now);    while(!q.empty())    {        now=q.front();        q.pop();        for(int i=0;i<4;i++)        {            tmp.x=now.x+dx[i];            tmp.y=now.y+dy[i];            tmp.cnt=now.cnt+1;            tmp.key=now.key;            if(tmp.x==ex&&tmp.y==ey) return tmp.cnt;            if(mp[tmp.x][tmp.y]>='a'&&mp[tmp.x][tmp.y]<='j')            {                tmp.key|=(1<<(mp[tmp.x][tmp.y]-'a'));            }            else if(mp[tmp.x][tmp.y]>='A'&&mp[tmp.x][tmp.y]<='J')            {                if((tmp.key&(1<<(mp[tmp.x][tmp.y]-'A')))==0)                    continue;            }            if(!ok(tmp.x,tmp.y,tmp.key)) continue;            vis[tmp.x][tmp.y][tmp.key]=1;            q.push(tmp);        }    }    return -1;}int main(){    int i,j,ans;    while(~scanf("%d%d%d",&n,&m,&t))    {        for(i=0;i<n;i++)        {            scanf("%s",mp[i]);            for(j=0;j<m;j++)            {                if(mp[i][j]=='@')                {                    sx=i;                    sy=j;                }                if(mp[i][j]=='^')                {                    ex=i;                    ey=j;                }            }        }        memset(vis,0,sizeof vis);        ans=bfs();        if(ans<t) printf("%d\n",ans);        else printf("-1\n");    }    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.