Hdoj 1026 Ignatius and the Princess I "BFS"

Source: Internet
Author: User

Ignatius and the Princess ITime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others) Total Submission (s): 12750 Accepted Submission (s): 4034Special Judge
Problem DescriptionThe Princess have been abducted by the Beelzebub feng5166, our hero Ignatius have to rescue our pretty Pr Incess. Now he gets into feng5166 ' s castle. The castle is a large labyrinth. To make the problem simply, we assume the labyrinth are a n*m two-dimensional array which left-top corner is (0,0) and Righ T-bottom Corner is (n-1,m-1). Ignatius enters at (0,0), and the door to Feng5166 's are at (n-1,m-1), that's our target. There is some monsters in the castle, if Ignatius meet them, he had to kill them. Here is some rules:

1.Ignatius can only moves in four directions, 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 was 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. Assume that the start position and the target position would never be a trap, and there would never be a monster at The start position.

Inputthe input contains several test cases. Each test case starts with a line contains the 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.

Outputfor Each test case, you should output "God's help to our poor hero." If Ignatius can ' t reach the target position, Or you should output ' It takes n seconds to reach the target position and let me show you the ' the '. (n is the minimum seconds), and the hero the whole path. Output a line contains the "FINISH" after all test case. If there is more than one path, any one was 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 seconds to reach the target position, let me show you the Way.1s: (0,0)--(1,0) 2s: (1,0)--(+) 3s: (+)-> ;(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, seconds to reach the Target position, let me show you the Way.1s: (0,0), (1,0) 2s: (1,0), (+) 3s: (+)--(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, our poor hero. FINISH

Test instructions: Give you a picture, which consists of '. ', ' X ', and numbers, starting from (0, 0) and asking if it is possible to reach (N-1, m-1) points. If you can reach, every step, it takes 1 seconds, ask the shortest time, and the path output.

This problem test instructions not difficult to understand, it is difficult to find the shortest path, with the general BFS Affirmation WA, don't ask me why, said more are tears ~ ~ ~

(The general BFs, although said to be able to determine whether to achieve, but not necessarily the shortest distance.) )

Analysis: We build a two-dimensional array to record the path and the shortest distance from (0, 0) points to that point, each time the search to a point satisfies the condition, the time of the point is compared with the time of the original arrival point, if the current path is less than the original to that point of time, update time and path, simultaneously into the queue And finally, every point is the best solution.

Experience: Good question!!! Let the nest understand a method of how to find the optimal path

In other words, this is my first AC special judge!!!

Code:

#include <stdio.h> #include <string.h> #include <iostream> #include <stack> #include <queue >const int M = 105;const int dx[] = {0, 0, 1, -1};const int dy[] = {1,-1, 0, 0};using namespace std;struct node{in  T x, y, Prex, prey; Prex,prey is the coordinate int cost of the previous point;}    S[m][m];char map[m][m];int N, m;void init () {int i,j;    for (i = 0; i < M; i + +) {for (j = 0; J < m; j + +) S[i][j].cost = 1; }}int limit (int x, int y) {return (X>=0&&x<n&&y>=0&&y<m&&map[x][y]! = ' x ') ;} void out () {///last output with stack to save path printf ("It takes%d seconds to reach the target position, let me show you the way.\n", s [N-1]    [M-1].cost];    Stack<node > S;    Node A = s[n-1][m-1];    S.push (a);        while (1) {if (a.x = = 0&&a.y = = 0) break;        A = S[a.prex][a.prey];    S.push (a); } A = S.top ();    S.pop ();    int t = 1; while (! S.empty ()) {Node B = s.top ();        S.pop (); printf ("%ds: (%d,%d)-(%d,%d) \ n ", t++, a.x, A.y, b.x, B.Y);        int temp = b.cost-a.cost-1;        while (temp--) {printf ("%ds:fight at (%d,%d) \ n", t++, b.x, B.Y);    } a = B;    }}void BFS () {int i;    Node St;    St.x = St.y = St.prex = St.prey = St.cost = 0;    S[0][0] = st;    Queue<node >q;    Q.push (ST);        while (!q.empty ()) {Node temp = Q.front ();        Q.pop ();            for (i = 0; i < 4; i + +) {node cur = temp;            Cur.x + = Dx[i];            Cur.y + = Dy[i];            if (!limit (cur.x, cur.y)) continue;            if (map[cur.x][cur.y] = = '. ') ++cur.cost;            else Cur.cost + = (map[cur.x][cur.y]-' 0 ' + 1); if (Cur.cost < s[cur.x][cur.y].cost| |                S[cur.x][cur.y].cost = =-1) {s[cur.x][cur.y] = cur; S[cur.x][cur.y].prex = temp.x;                S[cur.x][cur.y].prey = TEMP.Y;            Q.push (cur); }}} if (s[n-1][m-1].cost = =-1) {printf ("God's help to our poor hero.\n"); Return } else out ();}        int main () {while (scanf ("%d%d", &n, &m) = = 2) {init ();        for (int i = 0; i < n; i + +) {scanf ("%s", Map[i]);        } BFS ();    printf ("finish\n"); } return 0;}



Hdoj 1026 Ignatius and the Princess I "BFS"

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.