首页 > 代码库 > HDU 1242 Rescue

HDU 1242 Rescue

题意:中国人应该都知道了

思路:这道题,如果不经过仔细的思考还是蛮容易错的,像我,刚学完STL   不怎么敢用优先队列做,所以还是吃亏了!

就是要你从r出发找到a。

如果简单地用队列做,当输入

3 3

r..

#x.

##a的时候就很容易错有可能输出输出5或者4;这时就错了,所以只能用优先队列来优化了,让步数走得少的优先出队,不熟悉的可以去看看STL

代码:

#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;
}
在网上看到了一个大牛些的另一个代码,大家可以借鉴:

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
#define INF 99999
char 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