zoukankan      html  css  js  c++  java
  • hdu--1026--Ignatius and the Princess I(bfs搜索+dfs(打印路径))

    Ignatius and the Princess I

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 18847    Accepted Submission(s): 6090
    Special Judge


    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 /*
     2     Name: hdu--1026--Ignatius and the Princess I
     3     Copyright: ©2017 日天大帝
     4     Author: 日天大帝
     5     Date: 21/04/17 17:30
     6     Description: bfs搜索路径,dfs打印路径 
     7 */
     8 #include<cstring>
     9 #include<queue> 
    10 #include<iostream>
    11 #include<cstdio>
    12 #include<cstdlib>
    13 using namespace std;
    14 struct node{
    15     int x,y,steps;
    16     bool operator<(const node&a)const{
    17         return steps>a.steps;
    18     }
    19 };
    20 int bfs();
    21 void dfs(int,int);
    22 const int MAX = 105;
    23 int map[MAX][MAX];
    24 int to[MAX][MAX];
    25 int cnt[MAX][MAX];
    26 int n,m,ct;
    27 int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
    28 int main(){
    29 
    30 //    freopen("in.txt","r",stdin);
    31     ios::sync_with_stdio(false);
    32     
    33     while(cin>>n>>m){
    34         memset(map,0,sizeof(map));
    35         memset(to,0,sizeof(to));
    36         memset(cnt,0,sizeof(cnt));
    37         for(int i=0; i<n; ++i){
    38             for(int j=0; j<m; ++j){
    39                 char ch;cin>>ch;
    40                 if(ch == '.'){
    41                     map[i][j] = 0;
    42                 }else if(ch == 'X')map[i][j] = -1;
    43                 else cnt[i][j] = map[i][j] = ch - '0';
    44             }
    45         }
    46         int ans = bfs();
    47         if(ans){
    48             cout<<"It takes "<<ans<<" seconds to reach the target position, let me show you the way."<<endl;
    49             ct = 1; 
    50             dfs(n-1,m-1);
    51         }else printf("God please help our poor hero.
    ");  
    52         cout<<"FINISH
    ";//G++WA无数次,hdu C++ AC了
    53     }
    54     return 0;
    55 }
    56 void dfs(int x,int y){
    57     if(!to[x][y])return ;
    58     int i,j;
    59     i = x - dir[to[x][y]-1][0];//剪枝 
    60     j = y - dir[to[x][y]-1][1];//剪枝 
    61     dfs(i,j);
    62     printf("%ds:(%d,%d)->(%d,%d)
    ",ct++,i,j,x,y);
    63     while(cnt[x][y]--) {
    64          printf("%ds:FIGHT AT (%d,%d)
    ",ct++,x,y);
    65     }
    66 }
    67 int bfs(){
    68     node start;
    69     start.x = 0;
    70     start.y = 0;
    71     start.steps = 0;
    72     map[0][0] = -1;
    73     priority_queue<node> q;
    74     q.push(start);
    75     while(!q.empty()){
    76         node a,temp = q.top();q.pop();
    77         if(temp.x == n-1 && temp.y == m-1)return temp.steps;
    78         for(int i=0; i<4; ++i){//i<4(n)
    79             a.x = temp.x + dir[i][0];
    80             a.y = temp.y + dir[i][1];
    81             if(a.x>=n || a.y>=m || a.x<0 ||a.y<0 || map[a.x][a.y] == -1)continue;
    82             a.steps = temp.steps + map[a.x][a.y] + 1;
    83             map[a.x][a.y] = -1;
    84             to[a.x][a.y] = i+1;//剪枝 
    85             q.push(a);
    86         }
    87     }
    88     return 0;
    89 }
    90  
  • 相关阅读:
    安卓渗透测试环境搭建笔记
    spring boot Thymeleaf 模板注入 测试实践
    分析activity安全检测实践
    xposed的使用实践
    android组件安全测试实践
    Apache Dubbo Provider默认反序列漏洞复现实践(CVE-2020-1948)
    java设计模式--策略模式
    spring 发送email
    简单介绍
    有意义的礼物——英语小短文
  • 原文地址:https://www.cnblogs.com/slothrbk/p/6745367.html
Copyright © 2011-2022 走看看