zoukankan      html  css  js  c++  java
  • hdu 1026 Ignatius and the Princess I

    题目连接

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

    Ignatius and the Princess I

    Description

    The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has to rescue our pretty Princess. Now he gets into feng5166's castle. The castle is a large labyrinth. To make the problem simply, we assume the labyrinth is a N*M two-dimensional array which left-top corner is (0,0) and right-bottom corner is (N-1,M-1). Ignatius enters at (0,0), and the door to feng5166's room is at (N-1,M-1), that is our target. There are some monsters in the castle, if Ignatius meet them, he has to kill them. Here is some rules:

    1.Ignatius can only move in four directions(up, down, left, right), one step per second. A step is defined as follow: if current position is (x,y), after a step, Ignatius can only stand on (x-1,y), (x+1,y), (x,y-1) or (x,y+1).
    2.The array is marked with some characters and numbers. We define them like this:
    . : The place where Ignatius can walk on.
    X : The place is a trap, Ignatius should not walk on it.
    n : Here is a monster with n HP(1<=n<=9), if Ignatius walk on it, it takes him n seconds to kill the monster.

    Your task is to give out the path which costs minimum seconds for Ignatius to reach target position. You may assume that the start position and the target position will never be a trap, and there will never be a monster at the start position.

    Input

    The input contains several test cases. Each test case starts with a line contains two numbers N and M(2<=N<=100,2<=M<=100) which indicate the size of the labyrinth. Then a N*M two-dimensional array follows, which describe the whole labyrinth. The input is terminated by the end of file. More details in the Sample Input.

    Output

    For each test case, you should output "God please help our poor hero." if Ignatius can't reach the target position, or you should output "It takes n seconds to reach the target position, let me show you the way."(n is the minimum seconds), and tell our hero the whole path. Output a line contains "FINISH" after each test case. If there are more than one path, any one is OK in this problem. More details in the Sample Output.

    Sample Input

    5 6
    .XX.1.
    ..X.2.
    2...X.
    ...XX.
    XXXXX.
    5 6
    .XX.1.
    ..X.2.
    2...X.
    ...XX.
    XXXXX1
    5 6
    .XX...
    ..XX1.
    2...X.
    ...XX.
    XXXXX.

    Sample Output

    It takes 13 seconds to reach the target position, let me show you the way.
    1s:(0, 0)->(1, 0)
    2s:(1, 0)->(1, 1)
    3s:(1, 1)->(2, 1)
    4s:(2, 1)->(2, 2)
    5s:(2, 2)->(2, 3)
    6s:(2, 3)->(1, 3)
    7s:(1, 3)->(1, 4)
    8s:FIGHT AT (1,4)
    9s:FIGHT AT (1,4)
    10s:(1, 4)->(1, 5)
    11s:(1, 5)->(2, 5)
    12s:(2, 5)->(3, 5)
    13s:(3, 5)->(4, 5)
    FINISH
    It takes 14 seconds to reach the target position, let me show you the way.
    1s:(0, 0)->(1, 0)
    2s:(1, 0)->(1, 1)
    3s:(1, 1)->(2, 1)
    4s:(2, 1)->(2, 2)
    5s:(2, 2)->(2, 3)
    6s:(2, 3)->(1, 3)
    7s:(1, 3)->(1, 4)
    8s:FIGHT AT (1,4)
    9s:FIGHT AT (1,4)
    10s:(1, 4)->(1, 5)
    11s:(1, 5)->(2, 5)
    12s:(2, 5)->(3, 5)
    13s:(3, 5)->(4, 5)
    14s:FIGHT AT (4,5)
    FINISH
    God please help our poor hero.
    FINISH

    bfs+路径记录。。

     1 #include<algorithm>
     2 #include<iostream>
     3 #include<cstdlib>
     4 #include<cstring>
     5 #include<cstdio>
     6 #include<vector>
     7 #include<queue>
     8 #include<map>
     9 using std::cin;
    10 using std::cout;
    11 using std::endl;
    12 using std::find;
    13 using std::sort;
    14 using std::pair;
    15 using std::vector;
    16 using std::multimap;
    17 using std::priority_queue;
    18 #define pb(e) push_back(e)
    19 #define sz(c) (int)(c).size()
    20 #define mp(a, b) make_pair(a, b)
    21 #define all(c) (c).begin(), (c).end()
    22 #define iter(c) decltype((c).begin())
    23 #define cls(arr,val) memset(arr,val,sizeof(arr))
    24 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
    25 #define rep(i, n) for (int i = 0; i < (int)(n); i++)
    26 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
    27 const int Max_N = 110;
    28 const int Mod = 110;
    29 typedef unsigned long long ull;
    30 const int dx[] = { 0, 0, -1, 1 }, dy[] = { -1, 1, 0, 0 };
    31 bool vis[Max_N][Max_N];
    32 char maze[Max_N][Max_N];
    33 int N, M, res, fa[Max_N][Max_N], dir[Max_N][Max_N];
    34 struct Node {
    35     int x, y, s;
    36     Node(int i = 0, int j = 0, int k = 0) :x(i), y(j), s(k) {}
    37     inline bool operator<(const Node &a) const {
    38         return s > a.s;
    39     }
    40 };
    41 void bfs() {
    42     cls(vis, false), cls(fa, 0), cls(dir, 0);
    43     priority_queue<Node> q;
    44     q.push(Node());
    45     while (!q.empty()) {
    46         Node t = q.top(); q.pop();
    47         if (t.x == N - 1 && t.y == M - 1) { res = t.s; return; }
    48         rep(i, 4) {
    49             int nx = t.x + dx[i], ny = t.y + dy[i];
    50             char &ch = maze[nx][ny];
    51             if (nx < 0 || nx >= N || ny < 0 || ny >= M) continue;
    52             if (ch == 'X' || vis[nx][ny]) continue;
    53             if ('0' <= ch && ch <= '9') q.push(Node(nx, ny, t.s + 1 + ch - '0'));
    54             else q.push(Node(nx, ny, t.s + 1));
    55             vis[nx][ny] = true;
    56             fa[nx][ny] = t.x * Mod + t.y;
    57             dir[nx][ny] = i;
    58         }
    59     }
    60     res = -1;
    61 }
    62 void show_path(int x, int y) {
    63     if (-1 == res) { printf("God please help our poor hero.
    FINISH
    "); return; }
    64     int fx, fy;
    65     vector<int> path;
    66     for (;;) {
    67         fx = fa[x][y] / Mod;
    68         fy = fa[x][y] % Mod;
    69         if (!x && !y) break;
    70         path.push_back(dir[x][y]);
    71         x = fx, y = fy;
    72     }
    73     printf("It takes %d seconds to reach the target position, let me show you the way.
    ", res);
    74     fx = fy = 0;
    75     for (int i = sz(path) - 1, j = 1; ~i && j <= res; i--) {
    76         x = fx + dx[path[i]], y = fy + dy[path[i]];
    77         char &ch = maze[x][y];
    78         if ('0' <= ch && ch <= '9') {
    79             int t = ch - '0';
    80             printf("%ds:(%d, %d)->(%d, %d)
    ", j++, fx, fy, x, y);
    81             while (t--) printf("%ds:FIGHT AT (%d,%d)
    ", j++, x, y);
    82         }
    83         else printf("%ds:(%d, %d)->(%d, %d)
    ", j++, fx, fy, x, y);
    84         fx = x, fy = y;
    85     }
    86     puts("FINISH");
    87 }
    88 int main() {
    89 #ifdef LOCAL
    90     freopen("in.txt", "r", stdin);
    91     freopen("out.txt", "w+", stdout);
    92 #endif
    93     while (~scanf("%d %d", &N, &M)) {
    94         rep(i, N) scanf("%s", maze[i]);
    95         bfs();
    96         show_path(N - 1, M - 1);
    97     }
    98     return 0;
    99 }
    View Code
    By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
  • 相关阅读:
    Python any()
    从 SQL Server 到 MySQL (一):异构数据库迁移
    sql server作业实现数据同步
    分布式异构系统的数据一致性架构实现
    实战:sqlserver 数据实时同步到mysql
    基于MySQL的高可用准实时的数据同步方案
    SQL Server数据同步的研究(单向/双向)
    YY 数据库平台化建设实践
    两台SqlServer数据同步解决方案
    热迁移、异构数据库迁移、传输性能 这些上云的难题阿里云都帮你解决了
  • 原文地址:https://www.cnblogs.com/GadyPu/p/4612847.html
Copyright © 2011-2022 走看看