zoukankan      html  css  js  c++  java
  • hdu1242 又又又是逃离迷宫(bfs模板题)

    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1242/

    这次的迷宫是有守卫的,杀死一个守卫需要花费1个单位的时间,所以以走的步数为深度,在每一层进行搜索,由于走一步的花费不一定是1,所以我们需要用优先队列寻找最优值。这个题目真是模板题。

    代码如下:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 typedef unsigned int ui;
     4 typedef long long ll;
     5 typedef unsigned long long ull;
     6 #define pf printf
     7 #define mem(a,b) memset(a,b,sizeof(a))
     8 #define prime1 1e9+7
     9 #define prime2 1e9+9
    10 #define pi 3.14159265
    11 #define lson l,mid,rt<<1
    12 #define rson mid+1,r,rt<<1|1
    13 #define scand(x) scanf("%llf",&x) 
    14 #define f(i,a,b) for(int i=a;i<=b;i++)
    15 #define scan(a) scanf("%d",&a)
    16 #define dbg(args) cout<<#args<<":"<<args<<endl;
    17 #define inf 0x3f3f3f3f
    18 #define maxn 205
    19 int n,m,t;
    20 int sx,sy,tx,ty;
    21 bool vis[maxn][maxn];
    22 char Map[maxn][maxn];
    23 int dir[][2]={0,1,0,-1,1,0,-1,0};
    24 struct node{
    25     int x,y,time;
    26     node(int x,int y,int s):x(x),y(y),time(s){}
    27     node(){}
    28     friend operator< (node a,node b)
    29     {
    30         return a.time>b.time;//逆定义运算符 
    31     }
    32 };
    33 int ans=0;
    34 node cur,nxt;
    35 void bfs()
    36 {
    37     priority_queue<node>q;
    38     q.push(node(sx,sy,0));
    39     vis[sx][sy]=1;
    40     while(!q.empty())
    41     {
    42         cur=q.top();
    43         q.pop();
    44         if(cur.x==tx&&cur.y==ty)
    45         {
    46             ans=cur.time;
    47             return;
    48         }
    49         f(i,0,3)
    50         {
    51             nxt=cur;
    52             nxt.x+=dir[i][0];
    53             nxt.y+=dir[i][1];
    54             nxt.time++;
    55             if(nxt.x<1||nxt.x>n||nxt.y<1||nxt.y>m||Map[nxt.x][nxt.y]=='#')continue;
    56             if(vis[nxt.x][nxt.y])continue;
    57             vis[nxt.x][nxt.y]=1;
    58             if(Map[nxt.x][nxt.y]=='x')nxt.time++;
    59             q.push(nxt);
    60         }
    61     }
    62 }
    63 int main()
    64 {
    65     //freopen("input.txt","r",stdin);
    66     //freopen("output.txt","w",stdout);
    67     std::ios::sync_with_stdio(false);
    68     while(scanf("%d%d",&n,&m)!=EOF)
    69     {
    70         ans=0;
    71         mem(vis,false);
    72         f(i,1,n)
    73             f(j,1,m)
    74             {
    75                 scanf(" %c",&Map[i][j]);
    76                 if(Map[i][j]=='r')sx=i,sy=j;
    77                 if(Map[i][j]=='a')tx=i,ty=j;
    78             }
    79         bfs();
    80         if(!ans)pf("Poor ANGEL has to stay in the prison all his life.
    ");
    81         else pf("%d
    ",ans);
    82     }
    83     
    84  } 

     

  • 相关阅读:
    MapInfo 文件格式说明
    一个经典编程面试题的“隐退”
    Polar 投影c#版本移植
    关于 tf.nn.softmax_cross_entropy_with_logits 及 tf.clip_by_value
    系列解读Dropout
    python删除所有的中文字符、非ASCII或非英文字符,检查字符串是否包含非ASCII
    Python使用split使用多个字符分隔字符串
    Convolutional Neural Networks for Visual Recognition
    【TensorFlow】tf.nn.conv2d是怎样实现卷积的?
    [TensorFlow] tf.nn.softmax_cross_entropy_with_logits的用法
  • 原文地址:https://www.cnblogs.com/randy-lo/p/12510790.html
Copyright © 2011-2022 走看看