OpenJudge4980:拯救行動//stl有限隊列

來源:互聯網
上載者:User

標籤:決定   color   font   step   and   can   stl   方向   names   

總時間限制: 
10000ms

 

記憶體限制: 
65536kB
描述

公主被惡人抓走,被關押在牢房的某個地方。牢房用N*M (N, M <= 200)的矩陣來表示。矩陣中的每項可以代表道路(@)、牆壁(#)、和守衛(x)。 
英勇的騎士(r)決定孤身一人去拯救公主(a)。我們假設拯救成功的表示是“騎士到達了公主所在的位置”。由於在通往公主所在位置的道路中可能遇到守衛,騎士一旦遇到守衛,必須殺死守衛才能繼續前進。 
現假設騎士可以向上、下、左、右四個方向移動,每移動一個位置需要1個單位時間,殺死一個守衛需要花費額外的1個單位時間。同時假設騎士足夠強壯,有能力殺死所有的守衛。

給定牢房矩陣,公主、騎士和守衛在矩陣中的位置,請你計算拯救行動成功需要花費最短時間。

輸入
第一行為一個整數S,表示輸入的資料的組數(多組輸入)
隨後有S組資料,每組資料按如下格式輸入 
1、兩個整數代表N和M, (N, M <= 200). 
2、隨後N行,每行有M個字元。"@"代表道路,"a"代表公主,"r"代表騎士,"x"代表守衛, "#"代表牆壁。
輸出
如果拯救行動成功,輸出一個整數,表示行動的最短時間。
如果不可能成功,輸出"Impossible"
範例輸入
27 8#@#####@#@a#@@[email protected]#@@#[email protected]@@@@#@@#@##@@@##@@@#@@@@@@@@@@@@@@13 40@[email protected]@##[email protected]#[email protected]#xxxx##@#[email protected]@@#x#@#x#@@[email protected]#@xxx###[email protected]#@@##[email protected]@@#@[email protected]@#[email protected]@@#[email protected]#[email protected]@[email protected]#@x#@x#x#@@##@@x#@xx#[email protected]@x##@@@#@[email protected]@[email protected]@##[email protected]@@x#xx#@@#xxxx#@@[email protected]@#@[email protected]@@[email protected]#@#[email protected]#@#xxxxx##@@x##[email protected]@@#[email protected]####@@@x#x##@#@#xxx#@#x##[email protected]@#[email protected]@@[email protected]#@#[email protected]######[email protected]#@[email protected]@@@##@x#xx#[email protected]#xx#@#####x#@xxx##@#@x##x##x#@x#@a#[email protected]##@#@##[email protected]#@@[email protected]x#x#@[email protected]#x#@##@[email protected]#[email protected]##x##xx#@#[email protected]@#[email protected]@#@###x##[email protected]#@@#@@[email protected]@@[email protected]@@@##@@[email protected]@xx#[email protected]###@xxx#@#x#@@###@#@##@x#@[email protected]#@@#@@#@#[email protected]#x#x###@[email protected]@xxx####[email protected]##@x####xx#@x#x#@x#x######@@#[email protected]#xxxx#[email protected]@@#xx#x#####@
範例輸出
137

分析:

這道題就是一道水題。看起來好像有一些動態規劃。但是!這怎麼可能DP。所以。考慮BFS。BFS要保證最少步數的優先,這樣最先走到目標一定是最優接。

放出代碼:

#include<cstdio>#include<algorithm>#include<string.h>#include<queue>using namespace std;struct node{    int x,y,step;    friend bool operator < (node a,node b){        return a.step>b.step;    }};priority_queue<node>qu;char map_1[210][210];int visit[210][210];int left[4]={0,0,1,-1},right[4]={1,-1,0,0};int starx,stary,lastx,lasty;void BFS(){    node start;    start.step=0;    start.x=starx;start.y=stary;    visit[starx][stary]=1;    qu.push(start);    while(!qu.empty()){        node first=qu.top();        qu.pop();        if(first.x==lastx&&first.y==lasty){            printf("%d\n",first.step);            return ;        }        for(int i=0;i<4;++i)        {            if(map_1[first.x+left[i]][first.y+right[i]]==‘#‘)continue;            if(visit[first.x+left[i]][first.y+right[i]])continue;            if(map_1[first.x+left[i]][first.y+right[i]]==‘x‘){                node news;                news.x=first.x+left[i];                news.y=first.y+right[i];                news.step=first.step+2;                qu.push(news);                visit[news.x][news.y]=1;            }            else {                node news;                news.x=first.x+left[i];                news.y=first.y+right[i];                news.step=first.step+1;                qu.push(news);                visit[news.x][news.y]=1;            }        }    }    printf("Impossible\n");    return ;}int main(){    //freopen("4980.in","r",stdin);    //freopen("4980.out","w",stdout);    int T;    scanf("%d",&T);    while(T--){        memset(map_1,‘#‘,sizeof(map_1));        int x,y;        scanf("%d%d\n",&x,&y);        for(int i=1;i<=x;++i)        {            for(int j=1;j<=y;++j)            {                scanf("%c",&map_1[i][j]);                if(map_1[i][j]==‘a‘){                    lastx=i;                    lasty=j;                }                if(map_1[i][j]==‘r‘){                    starx=i;                    stary=j;                }            }            scanf("\n");        }        memset(visit,0,sizeof(visit));        while(!qu.empty())qu.pop();        BFS();    }    //fclose(stdin);    //fclose(stdout);    return 0;}

OpenJudge4980:拯救行動//stl有限隊列

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.