HDU ACM 1242 Rescue(广搜BFS + 优先队列priority_queue)

http://acm.hdu.edu.cn/showproblem.php?pid=1242

题意:给出一幅图,由'#','.','a','x','r' 组成,

  '#'表示墙  '.'表示路  'a'表示终点  'r'表示起点  'x'表示守卫

  若要经过x你必须先用1时间打倒守卫,再用1事件走过1格

  题目中说存在多个'r', 但我只找了一个却AC了

 

这题是赤裸裸的广搜题,直接用广搜做都是WA

改了一下方向向量的顺序就AC了.

View Code
 1 #include <iostream>
 2 #include <queue>
 3 using namespace std;
 4 const int MAX = 200 + 10;
 5 const int INF = 0x3ffffff;
 6 char map[MAX][MAX];
 7 int point[4][2]={0,1,0,-1,-1,0,1,0};  //  这里改成 int point[4][2]={0,1,0,-1,1,0,-1,0};  就AC了
 8 struct Node
 9 {
10     int x;
11     int y;
12     int step;
13 };
14 int n,m;
15 int mark;
16 int BFS(Node a,Node b)
17 {
18     queue <Node> q;
19     a.step = 0;
20     b.step = INF;
21     q.push(a);
22     map[a.x][a.y] = '#';
23     while(!q.empty())
24     {
25         Node mid;
26         mid = q.front();
27         
28         
29         q.pop();
30         if(mid.x == b.x && mid.y == b.y )
31         {
32             return mid.step;
33         }
34         int i;
35         for(i=0;i<4;i++)
36         {
37             a.x = mid.x + point[i][0];
38             a.y = mid.y + point[i][1];
39             a.step = mid.step + 1;
40             if( a.x >= 1 && a.y >= 1 && a.x <=n && a.y <= m && map[a.x][a.y] != '#')
41             {
42                 if(map[a.x][a.y] != '.' && map[a.x][a.y] != 'r' &&  map[a.x][a.y] != 'a')
43                 {
44                     a.step++;
45                     q.push(a);
46                     map[a.x][a.y] = '#';
47                 }
48                 else
49                 {
50                     
51                     q.push(a);
52                     map[a.x][a.y] = '#';
53                 }
54             }
55         }
56     }
57     return -1;
58 }
59 int main()
60 {
61     while(cin>>n>>m)
62     {
63         int i,j;
64         Node s,e;
65         memset(map,0,sizeof(map));
66         for(i=1;i<=n;i++)
67         {
68             for(j=1;j<=m;j++)
69             {
70                 cin>>map[i][j];
71                 if(map[i][j] == 'r')
72                 {
73                     s.x = i;
74                     s.y = j;
75                 }
76                 if(map[i][j] == 'a')
77                 {
78                     e.x = i;
79                     e.y = j;
80                 }
81             }
82         }
83         mark = 1;
84         int ans = BFS(s,e);
85         
86         if(ans == -1)
87         {
88             cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
89         }
90         else
91         {
92             cout<<ans<<endl;
93         }
94     }
95 }

 

这明显是坑爹的,于是把代码改成优先队列了.

View Code
 1 #include <iostream>
 2 #include <queue>
 3 using namespace std;
 4 const int MAX = 200 + 10;
 5 const int INF = 0x3ffffff;
 6 char map[MAX][MAX];
 7 int point[4][2]={0,1,0,-1,-1,0,1,0};
 8 struct Node
 9 {
10     int x;
11     int y;
12     int step;
13     friend bool operator <(const Node a,const Node b)
14     {
15         return a.step > b.step;
16     }
17 };
18 int n,m;
19 int mark;
20 void BFS(Node a,Node b)
21 {
22     priority_queue <Node> q;
23     a.step = 0;
24     b.step = INF;
25     q.push(a);
26     map[a.x][a.y] = '#';
27     while(!q.empty())
28     {
29         Node mid;
30         mid = q.top();
31         if(mid.x == b.x && mid.y == b.y )
32         {
33                 cout<<mid.step<<endl;
34                 mark = 0;
35                 return ;
36         }
37         
38         q.pop();
39         int i;
40         for(i=0;i<4;i++)
41         {
42             a.x = mid.x + point[i][0];
43             a.y = mid.y + point[i][1];
44             a.step = mid.step + 1;
45             if( a.x >= 1 && a.y >= 1 && a.x <=n && a.y <= m && map[a.x][a.y] != '#')
46             {
47                 if(map[a.x][a.y] == '.' || map[a.x][a.y] == 'r')
48                 {
49                     
50                     q.push(a);
51                     map[a.x][a.y] = '#';
52                 }
53                 else
54                 {
55                     a.step++;
56                     q.push(a);
57                     map[a.x][a.y] = '#';
58                 }
59             }
60         }
61     }
62 }
63 int main()
64 {
65     while(cin>>n>>m)
66     {
67         int i,j;
68         Node s,e;
69         memset(map,0,sizeof(map));
70         for(i=1;i<=n;i++)
71         {
72             for(j=1;j<=m;j++)
73             {
74                 cin>>map[i][j];
75                 if(map[i][j] == 'a')
76                 {
77                     s.x = i;
78                     s.y = j;
79                 }
80                 if(map[i][j] == 'r')
81                 {
82                     e.x = i;
83                     e.y = j;
84                 }
85             }
86         }
87         mark = 1;
88         BFS(s,e);
89         if(mark)
90         {
91             cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
92         }
93     }
94 }

 

posted @ 2012-08-29 09:54  zx雄  阅读(497)  评论(0编辑  收藏  举报