码迷,mamicode.com
首页 > 其他好文 > 详细

[hdu1242]优先队列

时间:2015-04-12 06:44:08      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

题意:给一个地图,‘x‘走一步代价为2,‘.‘走一步代价为1,求从s到t的最小代价。裸优先队列。

技术分享
  1 #pragma comment(linker, "/STACK:10240000,10240000")
  2 
  3 #include <iostream>
  4 #include <cstdio>
  5 #include <algorithm>
  6 #include <cstdlib>
  7 #include <cstring>
  8 #include <map>
  9 #include <queue>
 10 #include <deque>
 11 #include <cmath>
 12 #include <vector>
 13 #include <ctime>
 14 #include <cctype>
 15 #include <set>
 16 
 17 using namespace std;
 18 
 19 #define mem0(a) memset(a, 0, sizeof(a))
 20 #define lson l, m, rt << 1
 21 #define rson m + 1, r, rt << 1 | 1
 22 #define define_m int m = (l + r) >> 1
 23 #define Rep(a, b) for(int a = 0; a < b; a++)
 24 #define lowbit(x) ((x) & (-(x)))
 25 #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
 26 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
 27 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
 28 
 29 typedef double db;
 30 typedef long long LL;
 31 typedef pair<int, int> pii;
 32 typedef multiset<int> msi;
 33 typedef multiset<int>::iterator msii;
 34 typedef set<int> si;
 35 typedef set<int>::iterator sii;
 36 typedef vector<int> vi;
 37 
 38 const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
 39 const int dy[8] = {0, -1, 0, 1, -1, 1, 1, -1};
 40 const int maxn = 1e5 + 7;
 41 const int maxm = 1e5 + 7;
 42 const int maxv = 1e7 + 7;
 43 const int MD = 1e9 +7;
 44 const int INF = 1e9 + 7;
 45 const double PI = acos(-1.0);
 46 const double eps = 1e-10;
 47 
 48 struct Node {
 49     int x, y, cost;
 50     bool operator < (const Node &a) const {
 51         return cost > a.cost;
 52     }
 53     constructInt3(Node, x, y, cost);
 54 };
 55 
 56 priority_queue<Node> Q;
 57 
 58 int n, m;
 59 char s[220][220];
 60 
 61 void BFS(int x1, int y1, int x2, int y2) {
 62     while (!Q.empty()) Q.pop();
 63     Q.push(Node(x1, y1, 0));
 64     s[x1][y1] = *;
 65     while (!Q.empty()) {
 66         Node top = Q.top(); Q.pop();
 67         if (top.x == x2 && top.y == y2) {
 68             cout << top.cost << endl;
 69             return ;
 70         }
 71         for (int i = 0; i < 4; i++) {
 72             int x = top.x + dx[i], y = top.y + dy[i];
 73             if (x >= 0 && x < n && y >= 0 && y < m && (s[x][y] == . || s[x][y] == x)) {
 74                 Q.push(Node(x, y, top.cost + (s[x][y] == .? 1 : 2)));
 75                 s[x][y] = *;
 76             }
 77         }
 78     }
 79     puts("Poor ANGEL has to stay in the prison all his life.");
 80 }
 81 
 82 int main() {
 83     //freopen("in.txt", "r", stdin);
 84     while (cin >> n >> m) {
 85         int x1, x2, y1, y2;
 86         for (int i = 0; i < n; i++) {
 87             scanf("%s", s + i);
 88             for (int j = 0; j < m; j++) {
 89                 if (s[i][j] == r) {
 90                     x1 = i;
 91                     y1 = j;
 92                     s[i][j] = .;
 93                 }
 94                 if (s[i][j] == a) {
 95                     x2 = i;
 96                     y2 = j;
 97                     s[i][j] = .;
 98                 }
 99             }
100         }
101         BFS(x1, y1, x2, y2);
102     }
103     return 0;
104 }
View Code

 

[hdu1242]优先队列

标签:

原文地址:http://www.cnblogs.com/jklongint/p/4418992.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!