HDU 1242 rescue

Source: Internet
Author: User

It should be known to the Chinese.

Train of Thought: This question is quite easy and wrong without careful consideration. Like me, I didn't dare to use the priority queue after I finished learning STL, so I still suffered a loss!

That is, you need to find a from R.

If you simply use a queue

3 3

R ..

# X.

# When a is used, it is easy to make a mistake, and may output 5 or 4. At this time, it is wrong, so we can only use the priority queue for optimization, so that the number of steps is less than the priority queue, if you are not familiar with it, you can check STL.

Code:

#include<cstdio>#include<cstring>#include<queue>using namespace std;char str[205][205];int vis[205][205];int dir[4][2]={{-1,0},{0,-1},{1,0},{0,1}};int n,m;struct node{int x,y;int step;friend bool operator<(const node a,const node b){return a.step>b.step;}};int bfs(int x,int y){int i;node st,ed;priority_queue<node>q;memset(vis,0,sizeof(vis));    st.x=x;st.y=y;st.step=0;vis[st.x][st.y]=1;q.push(st);while(!q.empty()){st=q.top();q.pop();if(str[st.x][st.y]=='r')            return st.step;        for(i=0;i<4;i++){ed.x=st.x+dir[i][0];ed.y=st.y+dir[i][1];if((0<=ed.x&&ed.x<n&&0<=ed.y&&ed.y<m&&str[ed.x][ed.y]!='#')&&!vis[ed.x][ed.y]){vis[ed.x][ed.y]=1;if(str[ed.x][ed.y]=='x')                    ed.step=st.step+2;else                    ed.step=st.step+1;q.push(ed);}}}return -1;}int main(){int i,j;int x,y,sum;while(scanf("%d%d",&n,&m)!=EOF){for(i=0;i<n;i++)            scanf("%s",str[i]);for(i=0;i<n;i++)            for(j=0;j<m;j++)                if(str[i][j]=='a'){x=i;y=j;break;}sum=bfs(x,y);if(sum==-1)            printf("Poor ANGEL has to stay in the prison all his life.\n");else            printf("%d\n",sum);}return 0;}
I saw another code from the Internet, which can be used for reference:

#include<iostream>#include<cstring>#include<queue>using namespace std;#define INF 99999char maze[205][205];long len[205][205];int mov[4][2]={{1,0},{-1,0},{0,1},{0,-1}};void BFS(int xa,int ya){    int x0,y0,j,p,q;    queue<int> x;    queue<int> y;    x.push(xa);    y.push(ya);    while(!x.empty()&&!y.empty())    {        x0=x.front();y0=y.front();        x.pop();y.pop();        for(j=0;j<4;j++)        {            p=x0+mov[j][0];q=y0+mov[j][1];            if(maze[p][q]=='.'||maze[p][q]=='r'||maze[p][q]=='x')            {                if(maze[p][q]=='x')                {                    if(len[x0][y0]+2<len[p][q]||len[p][q]==0)                    {                        len[p][q]=len[x0][y0]+2;                        x.push(p);y.push(q);                    }                }                else if(len[x0][y0]+1<len[p][q]||len[p][q]==0)                {                    len[p][q]=len[x0][y0]+1;                    x.push(p);y.push(q);                }            }        }    }}int main(){    int m,n,i,j,a,b;    long ans;    while(cin>>m>>n)    {        ans=INF;        memset(maze,'*',sizeof(maze));        memset(len,0,sizeof(len));        for(i=1;i<=m;i++)            for(j=1;j<=n;j++)            {                cin>>maze[i][j];                if(maze[i][j]=='a')                {                    a=i;b=j;                }            }        BFS(a,b);        for(i=1;i<=m;i++)            for(j=1;j<=n;j++)                if(maze[i][j]=='r'&&ans>len[i][j]&&len[i][j])                    ans=len[i][j];        if(ans==INF)            cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;        else            cout<<ans<<endl;    }    return 0;}



HDU 1242 rescue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.