【搜尋】 HDU 5025 Saving Tang Monk,hdu5025

來源:互聯網
上載者:User

【搜尋】 HDU 5025 Saving Tang Monk,hdu5025

優先隊列+狀壓蛇+沒有了

#include <stdio.h>#include <string.h>#include <stdlib.h>#include <string>#include <iostream>#include <algorithm>#include <sstream>#include <math.h>using namespace std;#include <queue>#include <stack>#include <vector>#include <deque>#include <set>#include <map>#define cler(arr, val)    memset(arr, val, sizeof(arr))#define IN     freopen ("in.txt" , "r" , stdin);#define OUT  freopen ("out.txt" , "w" , stdout);typedef long long  LL;const int MAXN = 5040;//點數的最大值const int MAXM = 20006;//邊數的最大值const int INF = 0x3f3f3f3f;const int mod = 10000007;char mp[102][102];bool vis[102][102][30];int n,t;struct node{    int x,y,step;    int key;    int she;    bool operator < (const node &a) const    {       return step>a.step;//最小值優先    }};priority_queue<node>q;int xx[4]= {1,0,-1,0};int yy[4]= {0,1,0,-1};int star[2],end[2];bool judge(node p,int dd){    if(p.she&(1<<dd))        return false;    return true;}int bfs(int x,int y){    while(!q.empty())        q.pop();    memset(vis,false,sizeof(vis));    node front,rear;    front.x=x,front.y=y;    front.key=0;    front.step=0;    front.she=0;    vis[x][y][0]=true;    q.push(front);    while(!q.empty())    {        front=q.top();        q.pop();        for(int i=0; i<4; i++)        {            int dx=front.x+xx[i],dy=front.y+yy[i];            if(dx>=0&&dy>=0&&dx<n&&dy<n&&mp[dx][dy]!='#'&&!vis[dx][dy][front.key])            {                if(t==front.key&&mp[dx][dy]=='T')//到終點                {                    return front.step+1;                }                else if(mp[dx][dy]==front.key+'0'+1)//拿到鑰匙                {                    vis[dx][dy][front.key]=true;//標記                    rear.key =front.key+1;                    rear.x=dx,rear.y=dy,rear.step=front.step+1,rear.she=front.she;                    q.push(rear);                }                else if(mp[dx][dy]>=0&&mp[dx][dy]<=5&&judge(front,mp[dx][dy]))//遇到蛇                {                    rear.x=dx,rear.y=dy,rear.key=front.key,rear.step=front.step+2,rear.she=(front.she|(1<<mp[dx][dy]));                    vis[dx][dy][rear.key]=true;                    q.push(rear);                }                else                {                    rear.x=dx,rear.y=dy,rear.key=front.key,rear.step=front.step+1,rear.she=front.she;                    vis[dx][dy][rear.key]=true;                    q.push(rear);                }            }        }    }    return -1;}int main(){    //IN;    while(scanf("%d%d",&n,&t),n+t)    {        for(int i=0; i<n; i++)            scanf("%s",mp[i]);        int ss=0;        for(int i=0; i<n; i++)        {            for(int j=0; j<n; j++)            {                if(mp[i][j]=='K')                    star[0]=i,star[1]=j;                else if(mp[i][j]=='T')                    end[0]=i,end[1]=j;                else if(mp[i][j]=='S')                    mp[i][j]=ss++;            }        }        int ans=bfs(star[0],star[1]);        if(ans==-1)            printf("impossible\n");        else printf("%d\n",ans);    }    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.