hdu Rescue (bfs)

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

简单优先队列搜索,自己好久不敲,,,,,手残啊,,,,orz

代码:

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <math.h>
  4 #include <algorithm>
  5 #include <iostream>
  6 #include <ctype.h>
  7 #include <iomanip>
  8 #include <queue>
  9 #include <stdlib.h>
 10 using namespace std;
 11
 12 int N,M,ans;
 13 char mp[201][201];
 14 int vis[201][201];
 15
 16 int dx[4]={0,0,-1,1};
 17 int dy[4]={-1,1,0,0};
 18
 19 int sx,sy;
 20 int ex,ey;
 21
 22 struct Node
 23 {
 24     int x,y;
 25     int step;
 26 };
 27
 28 bool operator<(Node a,Node b)//定义结构体类型的优先队列的优先级,从小到大
 29 {
 30     return a.step>b.step;
 31 }
 32
 33 void getMap(int n,int m)
 34 {
 35     for(int i=0;i<n;i++)
 36         for(int j=0;j<m;j++)
 37         {
 38             cin>>mp[i][j];
 39             if(mp[i][j]==‘r‘)
 40             {
 41                 sx=i;
 42                 sy=j;
 43             }
 44             if(mp[i][j]==‘a‘)
 45             {
 46                 ex=i;
 47                 ey=j;
 48             }
 49         }
 50 }
 51
 52 bool pd(int x,int y)
 53 {
 54     if(x>=0&&x<N&&y>=0&&y<M&&!vis[x][y]&&mp[x][y]!=‘#‘)
 55         return true;
 56     return false;
 57 }
 58
 59 int bfs(int x,int y)
 60 {
 61     memset(vis,0,sizeof(vis));
 62     priority_queue<Node>q;
 63     Node a,b;
 64     a.x=x,a.y=y,a.step=0;
 65     q.push(a);
 66     while(!q.empty()){
 67         b=q.top();
 68         q.pop();
 69         for(int i=0; i<4; i++){
 70             int px=b.x+dx[i];
 71             int py=b.y+dy[i];
 72             if(pd(px,py)){
 73                 vis[px][py]=1;
 74                 a.x=px;
 75                 a.y=py;
 76                 if(mp[px][py]==‘x‘){
 77                     a.step=b.step+2;
 78                     q.push(a);
 79                 }
 80                 else
 81                 {
 82                     a.step=b.step+1;
 83                     q.push(a);
 84                     if(px==ex && py==ey)
 85                         return a.step;
 86                 }
 87             }
 88         }
 89     }
 90     return -1;
 91 }
 92
 93 int main()
 94 {
 95
 96     while(cin>>N>>M){
 97     getMap(N,M);
 98     int ans=bfs(sx,sy);
 99     if(ans==-1)
100         printf("Poor ANGEL has to stay in the prison all his life.\n");
101     else
102         printf("%d\n",ans);
103     }
104 }

优先队列搜索:

 1 ///优先队列是默认int从大到小priority_queue<int>q1,也可以定义为从小到大priority_queue<int,vector<int>,greater<int> >q2;
 2 也可以自定义优先级,重载<
 3
 4 #include <iostream>
 5 #include <functional>
 6 #include <queue>
 7 using namespace std;
 8
 9 ///自定义优先级,两种写法,按照优先级从大到小的顺序
10 /*
11 struct node
12 {
13     friend bool operator<(node n1,node n2)
14     {
15         return n1.priority<n2.priority;
16     }
17     int priority;
18     int value;
19 };*/
20
21 struct node
22 {
23     int priority;
24     int value;
25 };
26 bool operator<(node a,node b)
27 {
28     return a.priority<b.priority;
29 }
30
31
32 int main()
33 {
34     const int len=5;
35     int i;
36     int a[len]={3,5,9,6,2};
37     //优先队列中从大到小输出
38     priority_queue<int>q1;
39     for(i=0;i<len;i++)
40         q1.push(a[i]);
41     for(int i=0;i<len;i++)
42     {
43         cout<<q1.top();
44         q1.pop();
45     }
46     cout<<endl;
47     //优先队列中从小到大输出
48
49     /**
50     priority_queue<int,vector<int>,greater<int> >q2;
51     for(i=0;i<len;i++)
52         q2.push(a[i]);
53     for(i=0;i<len;i++)
54     {
55         cout<<q2.top();
56         q2.pop();
57     }*/
58
59     priority_queue<int,vector<int>,greater<int> >q;
60     for(int i=0;i<len;i++)
61         q.push(a[i]);
62     while(!q.empty())
63     {
64         cout<<q.top();
65         q.pop();
66     }
67     cout<<endl;
68     //按照某个优先级输出,该代码中为priority值大的先输出
69     priority_queue<node>q3;
70     node b[len];
71     b[0].priority=6;b[0].value=1;
72     b[1].priority=9;b[1].value=5;
73     b[2].priority=2;b[2].value=3;
74     b[3].priority=8;b[3].value=2;
75     b[4].priority=1;b[4].value=4;
76     for(i=0;i<len;i++)
77         q3.push(b[i]);
78     cout<<"优先级"<<‘\t‘<<"值"<<endl;
79     for(i=0;i<len;i++)
80     {
81         cout<<q3.top().priority<<‘\t‘<<q3.top().value<<endl;
82         q3.pop();
83     }
84     return 0;
85 }

可见链接:http://blog.csdn.net/sr_19930829/article/details/42706469

运行:

96532
23569

优先级  值
9       5
8       2
6       1
2       3
1       4

时间: 2024-12-23 14:40:11

hdu Rescue (bfs)的相关文章

[ACM] hdu 1242 Rescue (BFS+优先队列)

Rescue Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is:

Saving Princess claire_(hdu 4308 bfs模板题)

http://acm.hdu.edu.cn/showproblem.php?pid=4308 Saving Princess claire_ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2305    Accepted Submission(s): 822 Problem Description Princess claire_ wa

hdu 1175 bfs 转弯题

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1175 和之前的1728类似.就是判断转弯数,建立一个用于记录转弯数的数组.. 还有就是对于特殊情况要进行考虑,比如起点与终点相同的情况,对于本题来说是不可以消去的应该输出NO.还有就是起点或终点是零这也是不行的,因为0代表没有棋子... 还有在判断能不能走的时候要小心,对于判断条件一定要小心,不要图赶快写.. 错误的地方都写在注释中了.. 代码: // hdu 1175 bfs 转弯数 //1.起点

HDU 1072 bfs

Nightmare Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7083    Accepted Submission(s): 3409 Problem Description Ignatius had a nightmare last night. He found himself in a labyrinth with a tim

hdu 1252 BFS

1 /* 2 题意:给出一个m*m矩阵表示的完全图,且每个点都有自环,每条边都有一种颜色:有三个玩家ABC的三张纸片分别初始在 3 某三个位置(可以重叠),纸片可以沿着边走一步,可以走的路径的规则为:若A要走到某个点i,则A-i的颜色要和B-C的颜 4 色相同:问最少要走多少步.(题意太难懂了,看了别人的说明才弄懂了题意) 5 6 题解:BFS 7 首先初步分析题意似乎很难用图论来解决,那么就是搜索/DP/数据结构,然后从搜索方面去思考的话,就是要找状态,然 8 后初步列出所有信息,三个点得位置

zoj 1649 Rescue (bfs+队列)

Rescue Time Limit: 2 Seconds      Memory Limit: 65536 KB Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want

hdu 4707 bfs

bfs基础算法水题 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<string> #include<vector> #include<queue> using namespace std; const int Max = 1e5+50; int dist[Max]; vector<int> t

hdu 1242 Rescue(bfs+优先队列)

Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is: approach Angel. We assume

HDU 1242 Rescue(BFS)

题目链接 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is: a