zoukankan      html  css  js  c++  java
  • HDU 1242 Rescue BFS+优先队列

    题目链接:点击打开链接http://acm.hdu.edu.cn/showproblem.php?pid=1242


    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <queue>
    using namespace std;
    const int N=220;
    int n,m,stx,sty,endx,endy;
    char mp[N][N];
    int vis[N][N]; 
    int f[4][2]={{0,-1},{-1,0},{0,1},{1,0}};//keng
    struct node
    {
    	int x;
    	int y;
    	int t; 
    	friend bool operator < (const node &a,const node &b) 
    	{
    		return a.t>b.t;//大于是从小往大排 
    	}
    } ;
    bool isbound(int x,int y)
    {
    	if(x>0&&x<=n&&y>0&&y<=m&&mp[x][y]!='#')
    		return true;	
    	else
    	    return false;
    }
    void BFS(int x,int y)
    {
    	priority_queue<node>Q;
    	memset(vis,0,sizeof(vis));
    	while(!Q.empty())
    	{
    		Q.pop();
    	}
    	node p,q;	
    	p.x=x;
    	p.y=y;
    	p.t=0;
    	vis[p.x][p.y]=1;
    	Q.push(p); 
    	while(!Q.empty())
    	{
    		p=Q.top();
    		Q.pop() ;
    		if(p.x==endx&&p.y==endy)
    		{
    			printf("%d
    ",p.t);
    			return;
    		}
    		for(int i=0;i<4;i++)
    		{
    			q.x=p.x+f[i][0];
    			q.y=p.y+f[i][1];
    			if(!isbound(q.x,q.y)) continue;
    			if(vis[q.x ][q.y ]==1) continue;
    			vis[q.x ][q.y]=1;
    			if(mp[q.x ][q.y]=='x')
    				q.t =p.t + 2;
    			else
    				q.t =p.t + 1;
    			Q.push(q);
    		}
    		
    	}
    	printf("Poor ANGEL has to stay in the prison all his life.
    ");
    }
    int main()
    {
    	while(~scanf("%d%d",&n,&m)) 
    	{
    		for(int i=1;i<=n;i++)
    		{
    			for(int j=1;j<=m;j++)
    			{
    				cin>>mp[i][j];
    				if(mp[i][j]=='r')
    				{
    					stx=i;
    					sty=j;
    				}
    				else if(mp[i][j]=='a')
    				{
    					endx=i;
    					endy=j;
    				}
    			}
    		}
    		BFS(stx,sty);
    	}
    	return 0;
    }


  • 相关阅读:
    减治算法之寻找第K小元素问题
    OpenGL的版本号历史和发展
    动态注冊监听
    Thinking in Java -- 类型信息RTTI
    Unity3D
    Oracle改动字段类型
    函数定义
    foreach
    数组
    结构体
  • 原文地址:https://www.cnblogs.com/claireyuancy/p/7088935.html
Copyright © 2011-2022 走看看