zoukankan      html  css  js  c++  java
  • HDU 1242 rescue

    这题要注意 题目中的 each 这个字眼,所以有多个可能 'r'存在,所以从被救的人身上展开搜索比较简单.

    一开始不知道要使用优先队列,所以一开始是WA.

    优先队列的操作和队列相似,但是队列中的front()函数要用top()函数代替,其他的操作类似.

    #include<iostream>
    #include<queue>
    #define maxn 205
    char map[maxn][maxn];
    int visit[maxn][maxn];
    using namespace std;
    int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
    int m,n;
    bool ok(int i,int j)
    {
    	return map[i][j]!='#'&& i>0 &&i<=n &&j>0 && j<=m ;
    }
    struct node
    {
    	int x,y,time;
    	friend bool operator <(const node &a,const node &b)
    	{
    		return a.time>b.time ;
    	}
    };
    int bfs(int x,int y)
    {
    	int i;  
        node st,ed;  
        priority_queue<node>que;  //定义一个优先队列  
        memset(visit,0,sizeof(visit));  
        st.x=x;  
        st.y=y;  
        st.time=0;  
        visit[st.x][st.y]=1;  
        que.push(st);  
        while(!que.empty())  
        {  
            st=que.top();  
            que.pop();  
            if(map[st.x][st.y]=='r')  
            return st.time;  
            for(i=0;i<4;i++)  
            {  
                ed.x=st.x+dir[i][0];  
                ed.y=st.y+dir[i][1];  
                if(ok(ed.x,ed.y)&&!visit[ed.x][ed.y])  
                {  
                    visit[ed.x][ed.y]=1;  
                    if(map[ed.x][ed.y]=='x')  
                    ed.time=st.time+2;  
                    else  
                    ed.time=st.time+1;  
                    que.push(ed);  
                }  
            }  
        }  
        return -1; 
    
    }
    int main()
    {
    	//freopen("1242.txt","r",stdin);
    	 int i,j;  
        int x,y,ans;  
        while(scanf("%d%d",&n,&m)!=EOF)  
        {  
            for(i=1;i<=n;i++)
    			for(j=1;j<=m;j++)
    				cin>>map[i][j];
           // scanf("%s",map[i]);  
            for(i=1;i<=n;i++)  
             for(j=1;j<=m;j++)  
            if(map[i][j]=='a')  
            {  
                x=i;  
                y=j;  
                break;  
            }  
            ans=bfs(x,y);  
            if(ans==-1)  
            printf("Poor ANGEL has to stay in the prison all his life.
    ");  
            else  
            printf("%d
    ",ans);  
        }  
        return 0;  
    	
    }
    
  • 相关阅读:
    linux利用yum下载rpm离线包
    Struts Spring Plugin注意点
    Spring 对没有实现接口的类使用aspect的时候,可以使用CGLIB
    Spring HibernateTemplate
    Spring 声明式事务管理
    Spring 配置dataSource和sessionFactory
    Spring 配置中的 ${}
    Srping AOP xml方式
    Spring AOP 面向切面编程相关注解
    Spring 常用注入注解(annotation)和其对应xml标签
  • 原文地址:https://www.cnblogs.com/xiaoniuniu/p/4004208.html
Copyright © 2011-2022 走看看