hdu-1242-Rescue

题目链接


题意:

x代表卫兵,a代表终点,r代表起始点,.代表路,#代表墙

路花费一秒,卫兵花费两秒

问到达终点的最少时间

思路:BFS+优先队列的水题

优先队列的基本使用 :http://blog.csdn.net/forever_kirito/article/details/72909692


#include
#include
#include
using namespace std;
char a[210][210];
int to[4][2]={1,0,-1,0,0,1,0,-1};
bool vis[210][210];
int n,m;
struct node
{
    int x,y,time;
    friend bool operator <(const node &a,const node &b){
        return a.time>b.time;
    }
};
bool Isok (int x,int y)
{
    if(x>=0&&y>=0&&xQ;
    pre.x=x;pre.y=y;
    pre.time=0;
    vis[pre.x][pre.y]=true;
    Q.push(pre);
    while(!Q.empty()){
        pre=Q.top();
        Q.pop();
        if(a[pre.x][pre.y]=='r')
            return pre.time;
        for(int i=0;i<4;i++){
            next.x=pre.x+to[i][0];
            next.y=pre.y+to[i][1];
            if(Isok(next.x,next.y)&&!vis[next.x][next.y]){
                vis[next.x][next.y]=true;
                if(a[next.x][next.y]=='x'){
                    next.time=pre.time+2;
                }
                else
                    next.time=pre.time+1;
                Q.push(next);
            }

        }
    }
    return -1;
}
int main()
{
    int x=0,y=0;
   while(~scanf("%d %d",&n,&m)){
        for(int i=0;i



你可能感兴趣的:(acm_搜索)