HDU-1242-Rescue

题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1242

 

程序分析:

  题意是说一个公主被困在监狱里了,她的一些朋友想要去救她,(注意是一些,也就是会有好几个朋友),在途中他们可能会遇到士兵守卫(守卫驻守在一个格子里),但是她的朋友很强大,可以保证把守卫都干掉,不过这得耗一个单位的时间,当然经过守卫这个格子同样也需要一个单位的时间(经过没有守卫的格子是需要一个单位的时间的);那么监狱并不是敞开的,总会有那么一些墙堵住,好,这些墙是过不去的,最后就是让你计算最快要多少个单位的时间可以跟公主碰面(默认碰面即是救到公主了);

解决方法:

  广搜可以找到到达目标的最短时间,但是要考虑到经过同样多的格子数,需要的时间可能不同,因为有守卫存在,需要耗多一点时间,这样优先出队列的就不能按以往那样随意了,必须有个标准,那么,这个标准就是实际需要的时间,然后使用优先队列存储,每次都是时间最小的出队列,这样就可以保证是同样的格子数一定是时间少的先走。这样才能达到目的;需要注意的是优先队列默认的是大根堆,也就是大的优先出队列,但我们可以自定义这个优先级。下面这个就是大的优先,只要我们把,n1.priority < n2.priority 改成n1.priority > n2.priority 即可实现小根堆,这样就能实现时间少的先出队列。其他的就是除了 表示 朋友的符号进队列时不能更改为 墙 符号,其他的都一律标记为 墙,即不能走。

struct node
{
    
friend bool operator< (node n1, node n2)
    {
        
return n1.priority < n2.priority;
    }
    
int priority;
    
int value;
};

 

 

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

 

 

posted @ 2012-08-29 10:13  另Ⅰ中Feel▂  阅读(175)  评论(0编辑  收藏  举报