HDU 1026 Ignatius and the princess I (basic algorithm-BFS)

Source: Internet
Author: User

Ignatius and the princess iproblem descriptionthe 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-dimen1_array which left-top corner is () 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 ctions (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 shocould 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.
 
Inputthe 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-dimen1_array follows, which describe the whole labyrinth. the input is terminated by the end of file. more details in the sample input.
 
Outputfor each test case, You shoshould output "God please help our poor hero. "If Ignatius can't reach the target position, or you shoshould 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
 
Authorignatius. L
Recommendwe have carefully selected several similar problems for you: 1072 1175 1180 1016 1253


Question:

For a given graph, go to ". "It takes one step to go to the number. In addition to one step, it also takes so many steps to go to the number." # "cannot go. Ask how many steps you should take from the top left to the bottom right and output the path.


Solution:

The simple BFs, plus the first step of the record, can be obtained from the end point.


Solution code:

#include <iostream>#include <cstdio>#include <queue>#include <cstring>using namespace std;struct node{    int x,y,step;    node(int x0=0,int y0=0,int step0=0){        x=x0;y=y0;step=step0;    }};const int offx[]={1,-1,0,0};const int offy[]={0,0,-1,1};int n,m,step[110][110],pre[110][110];char a[110][110];void solve(){    memset(step,-1,sizeof(step));    memset(pre,-1,sizeof(pre));    queue <node> q;    q.push(node(0,0,0));    step[0][0]=0;    while(!q.empty()){        node s=q.front();        q.pop();        for(int i=0;i<4;i++){            int dx=s.x+offx[i],dy=s.y+offy[i],newstep;            if(dx<0 || dx>=n || dy<0 || dy>=m) continue;            if(a[dx][dy]=='X') continue;            if(a[dx][dy]=='.') newstep=s.step+1;            else newstep=s.step+1+a[dx][dy]-'0';            if(newstep<step[dx][dy] || step[dx][dy]==-1){                step[dx][dy]=newstep;                pre[dx][dy]=i;                q.push(node(dx,dy,step[dx][dy]));            }        }    }}void dfs(int dx,int dy){    if(pre[dx][dy]==-1) return;    int sx=dx-offx[pre[dx][dy]],sy=dy-offy[pre[dx][dy]];    dfs(sx,sy);    printf("%ds:(%d,%d)->(%d,%d)\n",step[sx][sy]+1,sx,sy,dx,dy);    if(a[dx][dy]!='.'){        for(int i=1;i<=a[dx][dy]-'0';i++){            printf("%ds:FIGHT AT (%d,%d)\n",step[sx][sy]+1+i,dx,dy);        }    }}void output(){    if(step[n-1][m-1]==-1){        printf("God please help our poor hero.\n");    }else{        printf("It takes %d seconds to reach the target position, let me show you the way.\n",step[n-1][m-1]);        dfs(n-1,m-1);    }    printf("FINISH\n");}int main(){    while(scanf("%d%d",&n,&m)!=EOF){        for(int i=0;i<n;i++) scanf("%s",a[i]);        solve();        output();    }    return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.