• hdu 1026 Ignatius and the Princess I (bfs+记录路径)(priority_queue)


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

    Problem 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
     
    笔记:1.以前写bfs时都是直接使用的queue,其队顶元素为front(),使用优先队列一般需要写明以什么优先,即friend bool operator < (node a,node b)···且队顶元素为top()
       2.需要记录路径时,需要记录其前驱位置,以便输出。
       3.在输出时,直接使用递归输出就好,即从出口一直递归到入口,即只有一条路径!
     
      1 #include<iostream>
      2 #include<queue>
      3 #include<cstdio>
      4 
      5 using namespace std;
      6 
      7 struct node{
      8     int x,y;
      9     int time;
     10     friend bool operator < (node a,node b){//优先队列!!
     11         return a.time > b.time;
     12     }
     13 };
     14 
     15 struct M{
     16     char data;
     17     int prex,prey;
     18     int time;
     19 }map[102][102];
     20 
     21 int n,m;
     22 int dir[4][2]={1,0,-1,0,0,1,0,-1};
     23 
     24 bool isInMap(int x,int y){
     25     return x>=0&&x<n&&y>=0&&y<m;
     26 }
     27 
     28 int bfs(){
     29     node cur,next;
     30     priority_queue<node> q;
     31     cur.x = 0;
     32     cur.y = 0;
     33     cur.time = 0;
     34     map[0][0].data = 'X';
     35     q.push(cur);
     36     while(!q.empty()){
     37         cur = q.top();
     38         q.pop();
     39         if(cur.x == n-1 && cur.y == m-1)
     40             return cur.time;
     41         for(int i=0;i<4;i++){
     42             int xx = cur.x + dir[i][0];
     43             int yy = cur.y + dir[i][1];
     44             if(isInMap(xx,yy) && map[xx][yy].data == '.'){
     45                 next.x = xx;
     46                 next.y = yy;
     47                 next.time = cur.time+1;
     48                 map[xx][yy].prex = cur.x;
     49                 map[xx][yy].prey = cur.y;
     50                 map[xx][yy].time = 0;
     51                 map[xx][yy].data = 'X';
     52                 q.push(next);
     53             }
     54             else if(isInMap(xx,yy) && map[xx][yy].data!='X'){
     55                 next.x = xx;
     56                 next.y = yy;
     57                 next.time = cur.time + (map[xx][yy].data-'0') + 1;//记得加上通过时间
     58                 map[xx][yy].prex = cur.x;
     59                 map[xx][yy].prey = cur.y;
     60                 map[xx][yy].time = map[xx][yy].data-'0';
     61                 map[xx][yy].data = 'X';
     62                 q.push(next);
     63             }
     64         }
     65     }
     66     return -1;
     67 }
     68 
     69 void printPath(int x,int y,int time){
     70     if(time>0){
     71         if(map[x][y].time--){
     72             printPath(x,y,time-1);
     73             printf("%ds:FIGHT AT (%d,%d)
    ",time--,x,y);
     74         }
     75         else{
     76             printPath(map[x][y].prex,map[x][y].prey,time-1);
     77             printf("%ds:(%d,%d)->(%d,%d)
    ",time--,map[x][y].prex,map[x][y].prey,x,y);
     78         }
     79     }
     80     return ;
     81 }
     82 
     83 int main(){
     84     while(cin>>n>>m){
     85         for(int i=0;i<n;i++){
     86             getchar();
     87             for(int j=0;j<m;j++){
     88                 scanf("%c",&map[i][j].data);
     89             }
     90         }
     91         int time = bfs();
     92         if(time>=0){
     93         printf("It takes %d seconds to reach the target position, let me show you the way.
    ",time);
     94         printPath(n-1,m-1,time);
     95         }
     96         else{
     97             printf("God please help our poor hero.
    ");
     98         }
     99         printf("FINISH
    ");
    100     }
    101     return 0;
    102 }
  • 相关阅读:
    AJAX异步传输——以php文件传输为例
    js控制json生成菜单——自制菜单(一)
    vs2010中关于HTML控件与服务器控件分别和js函数混合使用的问题
    SQL数据库连接到服务器出错——无法连接到XXX
    PHP错误:Namespace declaration statement has to be the very first statement in the script
    【LeetCode】19. Remove Nth Node From End of List
    【LeetCode】14. Longest Common Prefix
    【LeetCode】38. Count and Say
    【LeetCode】242. Valid Anagram
    【LeetCode】387. First Unique Character in a String
  • 原文地址:https://www.cnblogs.com/pngcui/p/4362380.html
Copyright © 2020-2023  润新知