HDU 1026 Ignatius and the Princess I

来源:互联网 发布:e是几g网络 编辑:程序博客网 时间:2024/06/06 02:36

Ignatius and the Princess I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12313    Accepted Submission(s): 3891
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.XXXXX15 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)FINISHIt 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)FINISHGod please help our poor hero.FINISH
解题思路:BFS预处理出每一点到达并杀死怪物(如果有的话)的最短时间,路径的话就从终点做一次DFS再回溯回去
#include<queue>#define Inf 0x7fffffffusing namespace std;char maze[105][105];int mov[4][2]={{1,0},{-1,0},{0,1},{0,-1}},m,n,sec[105][105];bool visit[105][105];struct Node{    int x;    int y;    bool f;    int t;}pos[10005];void bfs(){    int i;    queue<Node> q;    Node ini;    ini.x=ini.y=ini.t=1;    q.push(ini);    while(!q.empty())    {        ini=q.front();        q.pop();        for(i=0;i<4;i++)        {            int m=ini.x+mov[i][0];            int n=ini.y+mov[i][1];            if(maze[m][n]!='X')            {                int t;                if(maze[m][n]>'0'&&maze[m][n]<'10')                    t=ini.t+maze[m][n]-'0'+1;                else                    t=ini.t+1;                if(sec[m][n]==-1||t<sec[m][n])                {                    sec[m][n]=t;                    Node tmp;                    tmp.x=m;tmp.y=n;tmp.t=t;                    q.push(tmp);                }            }        }    }}void dfs(int x,int y){    pos[sec[x][y]].x=x-1;pos[sec[x][y]].y=y-1;    if(maze[x][y]>'0'&&maze[x][y]<'10')    {        pos[sec[x][y]-(maze[x][y]-'0')].x=x-1;        pos[sec[x][y]-(maze[x][y]-'0')].y=y-1;        pos[sec[x][y]-(maze[x][y]-'0')].f=true;    }    if(x==1&&y==1)        return ;    int tmp=Inf;    int x0,y0,p,q;    for(int i=0;i<4;i++)    {        p=x+mov[i][0];        q=y+mov[i][1];        if(sec[p][q]<tmp&&sec[p][q]!=-1)        {            x0=p;y0=q;            tmp=sec[p][q];        }    }    dfs(x0,y0);    return ;}int main(){    int i,j;    while(~scanf("%d%d",&m,&n))    {        memset(sec,-1,sizeof(sec));        memset(maze,'X',sizeof(maze));        memset(visit,false,sizeof(visit));        memset(pos,0,sizeof(pos));        getchar();        for(i=1;i<=m;i++)        {            for(j=1;j<=n;j++)                scanf("%c",&maze[i][j]);            getchar();        }        visit[m][n]=true;        pos[1].x=0;pos[1].y=0;pos[1].f=false;        sec[1][1]=1;        bfs();        if(sec[m][n]==-1)        {            printf("God please help our poor hero.\n");            printf("FINISH\n");            continue;        }        dfs(m,n);            printf("It takes %d seconds to reach the target position, let me show you the way.\n",sec[m][n]-1);            int tmp=-1;            for(i=1;i<=sec[m][n]-1;i++)            {                printf("%ds:",i);                if(pos[i].f)                {                    printf("FIGHT AT (%d,%d)\n",pos[i].x,pos[i].y);                    if(tmp==-1)                    {                        int x0=pos[i].x;int y0=pos[i].y;                        tmp=maze[pos[i].x+1][pos[i].y+1]-'0';                        for(j=1;j<=tmp;j++)                            pos[i+j].x=x0,pos[i+j].y=y0,pos[i+j].f=j==tmp?false:true;                    }                }                else                {                    printf("(%d,%d)->(%d,%d)\n",pos[i].x,pos[i].y,pos[i+1].x,pos[i+1].y);                    tmp=-1;                }            }        printf("FINISH\n");    }    return 0;}


 
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 跨境汇款被退回怎么办 汇款途径写错了怎么办 快递被菜鸟驿站退回怎么办 电脑登录账户已锁定怎么办 被外管局查到境外汇款买房怎么办 军校生复检被刷怎么办 企业私刻章拿去挂项目怎么办? 中通快递被退回怎么办 网易邮箱提示被修复怎么办 小孩屁股烫红了怎么办 8岁近视400度怎么办 部队体能差的人怎么办 上环5天同房了怎么办 肾结石有3mm了怎么办 4*3mm肾结石好痛怎么办 做完肾结石积水后迟续发烧怎么办 血糖高有肾结石反复发高烧怎么办 肾里面有小结石怎么办 大于2厘米的结石怎么办 双肾结石肾盏扩张怎么办 边防消防警卫部队改革义务兵怎么办 汽车年检尾气复检不合格怎么办 车辆年检尾气不合格复检怎么办? 在瓜子上买车复检有问题怎么办 更换车壳车架号怎么办 吸完甲醛的绿萝怎么办 如果公务员复检不合格有异议怎么办 国考公务员政审没有毕业证怎么办 打针硬块4年不消怎么办 外墙补起来难看不好卖怎么办 杠精现实中应该怎么办 发现记者报道假新闻怎么办 2018消防兵转制到期士官怎么办 小孩睡觉老想着军训怎么办 1岁宝宝太老实了怎么办 上课小孩很调皮不听话怎么办 初中学生上课爱说话调皮怎么办 8个月婴儿疝气怎么办 头部疤痕不长发怎么办呢 有纹身想去当兵怎么办 在部队干活的钱怎么办