【BFS】HDU1026Ignatius and the Princess I

题目链接: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

代码:

#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
int n,m;
char Map[150][150];
int vis[150][150];
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
typedef struct NODE
{
    int x,y;
}NODE;
struct node
{
    int s;
    NODE now;
    NODE pre;
    friend bool operator <(node a,node b)
    {
        return a.s>b.s;
    }
}a[150][150];
void init()
{
    memset(vis,0,sizeof(vis));
}
bool check(int x,int y)
{
    return !vis[x][y]&&x>=0&&y>=0&&x<n&&y<m&&(Map[x][y]!='X');
}
void print(node st)
{
    cout<<"It takes "<<st.s<<" seconds to reach the target position, let me show you the way."<<endl;
    NODE route[10050];
    route[0]=st.now;
    int cnt=1;
    while(st.now.x!=0||st.now.y!=0){
        st=a[st.pre.x][st.pre.y];
        route[cnt++]=st.now;
    }
    int t=1;
    for(int i=cnt-1;i>0;i--){
        if(Map[route[i].x][route[i].y]!='.'){
            char tt=Map[route[i].x][route[i].y];
            while(tt!='0'){
                cout<<t++<<"s:FIGHT AT ("<<route[i].x<<','<<route[i].y<<")"<<endl;
                tt--;
            }
        }
        cout<<t++<<"s:("<<route[i].x<<','<<route[i].y<<")->("<<route[i-1].x<<','<<route[i-1].y<<')'<<endl;
    }
    if(Map[route[0].x][route[0].y]!='.'){
        char tt=Map[route[0].x][route[0].y];
        while(tt!='0'){
            cout<<t++<<"s:FIGHT AT ("<<route[0].x<<','<<route[0].y<<")"<<endl;
            tt--;
        }
    }
    return;
}
void bfs()
{
    priority_queue<node>q;
    node st;
    st.s=0;
    st.now.x=0;
    st.now.y=0;
    st.pre.x=0;
    st.pre.y=0;
    a[0][0]=st;
    vis[0][0]=1;
    q.push(st);
    while(!q.empty()){
        st=q.top();
        q.pop();
        if(st.now.x==n-1&&st.now.y==m-1){
            print(st);
            //cout<<st.s<<endl;
            return;
        }
        for(int i=0;i<4;i++){
            node nt=st;
            nt.pre.x=st.now.x;
            nt.pre.y=st.now.y;
            nt.now.x+=dir[i][0];
            nt.now.y+=dir[i][1];
            if(check(nt.now.x,nt.now.y)){
                vis[nt.now.x][nt.now.y]=1;
                a[nt.now.x][nt.now.y]=nt;
                int t=1;
                if(Map[nt.now.x][nt.now.y]!='.'){
                    t+=Map[nt.now.x][nt.now.y]-'0';
                }
                nt.s+=t;
                q.push(nt);
            }
        }
    }
    cout<<"God please help our poor hero."<<endl;
}
int main()
{
    cin.sync_with_stdio(false);
    while(cin>>n>>m){
        init();
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                cin>>Map[i][j];
            }
        }
        bfs();
        cout<<"FINISH"<<endl;
    }
    return 0;
}
时间: 2024-10-04 12:54:28

【BFS】HDU1026Ignatius and the Princess I的相关文章

【bfs】【中等难度】tyvj P1234 - bench与奔驰

P1234 - bench与奔驰 From zhangbh001    Normal (OI) 总时限:10s    内存限制:128MB    代码长度 限制:64KB P1234 - bench与奔驰 背景 Background 公园里有个人在练开奔驰 - -!,但是总是撞在bench上 (众人曰:狼来了,快跑啊!) 描述 Description 公园里的bench与奔驰都是无敌的,不会被撞坏.由于开奔驰的人比较"有特点",总是向上下左右四个方向开,而且只会在撞到椅子之后改变方向(

hdoj 1312 Red and Black 【BFS】

题意:一共有四个方向,从'@'出发,找能到达'.'的个数, #是不能通过的. 策略:广搜. 这道题属于最简单的bfs了. 代码: #include<stdio.h> #include<string.h> #include<queue> using std::queue; bool vis[25][25]; char s[25][25]; int n, m; int ans = 0; struct node{ int x, y; }; node st; const int

HDU1242 Rescue 【BFS】

Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16314    Accepted Submission(s): 5926 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is

HDU 1253 胜利大逃亡 NYOJ 523【BFS】

胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 24608    Accepted Submission(s): 9427 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的

NYOJ 284 坦克大战 【BFS】+【优先队列】

坦克大战 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are discussing is a simple edition of this game. Given a map that co

【BFS】uva10047The Monocycle

/* 本题的特殊之处,到达一个格子时,因为朝向不同,以及接触地面的颜色不同, 会处于不同的状态::::::::: 把(x, y, d, c)作为一个结点,表示所在位置(x, y),方向为d,颜色为c;;;;; ------------------------------------------------------------------------ 在方向上我们把前,左,右编号为0,1,2:::: 颜色,从蓝色开始编号为0,1,2,3:::::::::: ------------------

【BFS】uva11624Fire!

/* bfs宽度遍历 -------------------------------------------------------------------------- 对人和火同时进行bfs,,注意应该先火后人,即如果在人到达该格子前,格子已经着火 则不应该走,最后人走到边界无路可走,则IMPOSSIBLE!!!!!!!!!!!! --------------------------------------------------------------------------- 两次bfs

【bfs】hdu 1104 Remainder

[bfs]hdu 1104 Remainder 题目链接:hdu 1104 Remainder 很不错的一道搜索题目,但是有几个关键问题要注意. 最短路径,果断bfs+Queue 路径的存储问题,之前只想把每一步的计算结果存储到queue(int)Q中,后来发现路径无法记录,就选择存储节点的方式并用string保存路径,queue(node)Q,开一个临时的节点node p,每进行一次运算就更新它的路径string+'op',最终输出的一定是完整路径!! 但最关键的是取模!!!!! discus

Poj 3087 Shuffle&#39;m Up 【BFS】

Shuffle'm Up Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6556 Accepted: 3077 Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of pok