首页 > 其他 > 详细

hdu1242 又又又是逃离迷宫(bfs模板题)

时间:2020-03-17 15:29:23      阅读:45      评论:0      收藏:0      [点我收藏+]

题目链接: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.\n");
81         else pf("%d\n",ans);
82     }
83     
84  } 

 

 

hdu1242 又又又是逃离迷宫(bfs模板题)

原文:https://www.cnblogs.com/randy-lo/p/12510790.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!