Hdu 1026 bfs Ignatius and the Princess I

Ignatius and the Princess I

 

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 10896    Accepted Submission(s): 3330

Special Judge

 

Problem Description

The Princess has been abducted by theBEelzebub feng5166, our hero Ignatius has to rescue our pretty Princess. Now hegets into feng5166's castle. The castle is a large labyrinth. To make theproblem simply, we assume the labyrinth is a N*M two-dimensional array whichleft-top corner is (0,0) and right-bottom corner is (N-1,M-1). Ignatius entersat (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 killthem. 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: ifcurrent 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 charactersand numbers. We define them like this:

. : The place where Ignatius can walk on.

X : The place is a trap, Ignatius shouldnot walk on it.

n : Here is a monster with nHP(1<=n<=9), if Ignatius walk on it, it takes him n seconds to kill themonster.

 

Your task is to give out the path whichcosts minimum seconds for Ignatius to reach target position. You may assumethat the start position and the target position will never be a trap, and therewill never be a monster at the start position.

 

 

Input

The input contains several test cases. Eachtest case starts with a line contains two numbers N andM(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 targetposition, or you should output "It takes n seconds to reach the targetposition, let me show you the way."(n is the minimum seconds), and tellour hero the whole path. Output a line contains "FINISH" after eachtest case. If there are more than one path, any one is OK in this problem. Moredetails 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 targetposition, 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 targetposition, 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

 

 

Author

Ignatius.L

題解:

Bfs不解釋了。

源代碼:

#include <iostream>

#include <string.h>

#include <string>

#include <stdio.h>

#include <queue>

using namespace std;

 

struct node

{

   intx,y,v;

};

node used[105][105];

char mp[105][105];

int n,m;

int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};

queue<node> q;

node ans[10000];

 

bool judge(int x,int y)

{

   if(x< 0||x >= n||y < 0||y >= m)

     returnfalse;

   if(mp[x][y]== 'X')

     returnfalse;

   returntrue;

}

void bfs()

{

   node a,tt,tp;

   a.x = 0;

   a.y = 0;

   used[0][0].v = 0;

   q.push(a);

   while(!q.empty())

   {

     tp = q.front();

     q.pop();

 

     for(int i = 0;i < 4;i++)

     {

        tt.x = tp.x + dir[i][0];

        tt.y = tp.y + dir[i][1];

 

        if(!judge(tt.x,tt.y))

          continue;

        intv = 1;

        if(isalnum(mp[tt.x][tt.y]))

          v = mp[tt.x][tt.y] - '0' + 1;

 

        if(used[tt.x][tt.y].v> used[tp.x][tp.y].v + v)

        {

          q.push(tt);

          used[tt.x][tt.y].x = tp.x;

          used[tt.x][tt.y].y = tp.y;

          used[tt.x][tt.y].v =used[tp.x][tp.y].v + v;

        }

     }

   }

}

int main()

{

   while(cin>> n>>m)

   {

     for(int i = 0;i < n;i++)

        scanf("%s",mp[i]);

 

     for(int i = 0;i < n;i++)

        for(int k = 0;k < m;k++)

        {

          used[i][k].x = -1;

          used[i][k].y = -1;

          used[i][k].v = 999999;

        }

     bfs();

     if(used[n-1][m-1].v== 999999)

        printf("God please help our poor hero.\nFINISH\n");

     else

     {

        printf("It takes %d seconds to reach the target position,let me show you the way.\n",

             used[n-1][m-1].v);

        intpos = 0;

        ans[pos].x = n-1;

        ans[pos++].y = m-1;

        intx = n-1,y = m -1;

        while(x|| y)

        {

            ans[pos].x = used[x][y].x;

            ans[pos].y = used[x][y].y;

          x = ans[pos].x;

          y = ans[pos].y;

          pos++;

        }

        intcount = 1;

        for(int i = pos-2;i >= 0;i--)

        {

          intv = 0;

          if(isalnum(mp[ans[i].x][ans[i].y]))

             v =mp[ans[i].x][ans[i].y] - '0';

           printf("%ds:(%d,%d)->(%d,%d)\n",count++,ans[i+1].x,ans[i+1].y,ans[i].x,ans[i].y);

          for(int j = 0;j < v;j++)

          {

             printf("%ds:FIGHT AT (%d,%d)\n",count++,ans[i].x,ans[i].y);

          }

        }

        printf("FINISH\n");

     }

   }

   return0;

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章