概述
每日一看
Ignatius and the Princess I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18981 Accepted Submission(s): 6148
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.
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
用的普通的队列
由于一些点打怪物要占据一定的时间,要注意进队条件,(最基本的广搜只是层次遍历,每次取队头,然后其相邻的点进队,同时已经进队的做标记,是不可取的)如果某一点能提供一条更短的路则进队,所以某些点不止搜索了一次
如果对bfs队列不太熟悉 [点击这里(http://blog.csdn.net/qq_33913037/article/details/72391618 点击这里)
c++实现
#include <iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#define MAX 105
#define INF 0x3f3f3f3f
char map[MAX][MAX];
int n,m,t[MAX][MAX],fa[MAX][MAX];
int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
typedef struct
{
int x,y;
}Node;
void bfs()
{
queue<Node>q;
Node node={0,0};
q.push(node);
while(!q.empty())
{
node=q.front();
q.pop();
int x=node.x;
int y=node.y;
for(int i=0;i<4;i++)
{
int tx=x+dx[i];
int ty=y+dy[i];
if(tx>=0&&tx<n&&ty>=0&&ty<m&&map[tx][ty]=='.')
{
if(t[tx][ty]>t[x][y]+1)
{
t[tx][ty]=t[x][y]+1;
fa[tx][ty]=i;//记录如何由上一点找到该点的
node.x=tx;
node.y=ty;
q.push(node);
}
}
if(tx>=0&&tx<n&&ty>=0&&ty<m&&map[tx][ty]>='1'&&map[tx][ty]<='9')
{
int k=map[tx][ty]-'0';
if(t[tx][ty]>t[x][y]+k+1)
{
t[tx][ty]=t[x][y]+k+1;
fa[tx][ty]=i;
node.x=tx;
node.y=ty;
q.push(node);
}
}
}
}
}
int main()
{
int i;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=0;i<n;i++)
{
scanf("%s",map[i]);//图
}
memset(t,INF,sizeof(t));//初始化时间
t[0][0]=0;
bfs();
/*for(i=0;i<n;i++)
{
for(int j=0;j<m;j++)
cout<<t[i][j]<<" ";
cout<<endl;
}
cout<<endl;*/
if(t[n-1][m-1]==INF)//无法到终点
{
cout<<"God please help our poor hero."<<endl<<"FINISH"<<endl;
continue;
}
printf("It takes %d seconds to reach the target position, let me show you the way.n",t[n-1][m-1]);
int x=n-1,y=m-1,trace[MAX*MAX][2],c=2;
trace[1][0]=x,trace[1][1]=y;
while(!(x==0&&y==0))//由终点开始逆向存储路径,trace中最后一个是起点
{
i=fa[x][y];
int fx=x-dx[i];//父亲点
int fy=y-dy[i];
trace[c][0]=fx;
trace[c++][1]=fy;
x=fx;y=fy;
}
int k=1;
for(i=c-1;i>=1;i--)//由起点开始到终点,要循环到终点,因为终点可能有怪物,有FIGHT时间
{
x=trace[i][0],y=trace[i][1];
if(map[x][y]>='1'&&map[x][y]<='9')
{
int tt=map[x][y]-'0';
for(int j=1;j<=tt;j++)
{
printf("%ds:FIGHT AT (%d,%d)n",k++,x,y);
}
}
if(i!=1)
printf("%ds:(%d,%d)->(%d,%d)n",k++,trace[i][0],trace[i][1],trace[i-1][0],trace[i-1][1]);
}
cout<<"FINISH"<<endl;
}
return 0;
}
java实现
package 五月第三周;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class hdu1026 {
/**
* @param args
*/
static int n,m;
static int MAX=105;
static int x[]={0,1,0,-1},y[]={1,0,-1,0};
static int sx,sy,dx,dy;
static char map[][]=new char[MAX][MAX];
static int t[][]=new int[MAX][MAX];
static int fa[][]=new int[MAX][MAX];
static int trace[][]=new int[MAX*MAX][2];
class Node{
int sx,sy;
public Node(int sx,int sy){
this.sx=sx;
this.sy=sy;
}
public int getSx() {
return sx;
}
public int getSy() {
return sy;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan=new Scanner(System.in);
while(((n=scan.nextInt())!=0)&&((m=scan.nextInt())!=0)){
for(int i=0;i<MAX;i++){
for(int j=0;j<MAX;j++){
t[i][j]=Integer.MAX_VALUE;
}
}
t[0][0]=0;;
for(int i=0;i<n;i++){
String str=scan.next();
char a[]=str.toCharArray();
for(int j=0;j<m;j++){
map[i][j]=a[j];
}
}
sx=0;
sy=0;
dx=n-1;
dy=m-1;
bfs();
if(t[dx][dy]==Integer.MAX_VALUE){
System.out.println("God please help our poor hero.");
System.out.println("FINISH");
continue;
}
System.out.println("It takes "+t[dx][dy]+" seconds to reach the target position, let me show you the way.");
int rx=n-1,ry=m-1,c=2;
trace[1][0]=rx;
trace[1][1]=ry;
while(!(rx==0&&ry==0)){
int i=fa[rx][ry];
int fx=rx-x[i];
int fy=ry-y[i];
trace[c][0]=fx;
trace[c][1]=fy;
c++;
rx=fx;
ry=fy;
}
int k=1;
for(int i=c-1;i>=1;i--){
int rrx=trace[i][0],rry=trace[i][1];
if(map[rrx][rry]>='1'&&map[rrx][rry]<='9'){
int tt=map[rrx][rry]-'0';
for(int j=1;j<=tt;j++)
{
System.out.println(k+"s:FIGHT AT ("+rrx+","+rry+")");
k++;
}
}
if(i!=1){
System.out.println(k+"s:("+trace[i][0]+","+trace[i][1]+")"+"->("+trace[i-1][0]+","+trace[i-1][1]+")");
k++;
}
}
System.out.println("FINISH");
}
}
private static void bfs() {
// TODO Auto-generated method stub
Queue<Node> queue=new LinkedList<Node>();
hdu1026 hdu=new hdu1026();
Node node =hdu.new Node(sx,sy);
queue.offer(node);
while(queue.size()!=0){
Node frist=queue.poll();
int ssx=frist.getSx();
int ssy=frist.getSy();
for(int i=0;i<4;i++){
int tx=ssx+x[i];
int ty=ssy+y[i];
if(tx>=0&&tx<n&&ty>=0&&ty<m&&map[tx][ty]=='.'){
if(t[tx][ty]>t[ssx][ssy]+1){
t[tx][ty]=t[ssx][ssy]+1;
fa[tx][ty]=i;//记录如何由上一点找到该点的
Node node1=hdu.new Node(tx,ty);
queue.offer(node1);
}
}
if(tx>=0&&tx<n&&ty>=0&&ty<m&&map[tx][ty]>='1'&&map[tx][ty]<='9'){
int k=map[tx][ty]-'0';
if(t[tx][ty]>t[ssx][ssy]+k+1)
{
t[tx][ty]=t[ssx][ssy]+k+1;
fa[tx][ty]=i;
Node node1=hdu.new Node(tx,ty);
queue.offer(node1);
}
}
}
}
}
}
最后
以上就是虚拟乐曲为你收集整理的hdu1026 bfs+记录路径的全部内容,希望文章能够帮你解决hdu1026 bfs+记录路径所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复